/
row0ins.cc
3955 lines (3234 loc) · 106 KB
/
row0ins.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
Copyright (c) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2016, 2018, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
*****************************************************************************/
/**************************************************//**
@file row/row0ins.cc
Insert into a table
Created 4/20/1996 Heikki Tuuri
*******************************************************/
#include "row0ins.h"
#include "dict0dict.h"
#include "trx0rec.h"
#include "trx0undo.h"
#include "btr0btr.h"
#include "btr0cur.h"
#include "mach0data.h"
#include "ibuf0ibuf.h"
#include "que0que.h"
#include "row0upd.h"
#include "row0sel.h"
#include "row0log.h"
#include "rem0cmp.h"
#include "lock0lock.h"
#include "log0log.h"
#include "eval0eval.h"
#include "data0data.h"
#include "buf0lru.h"
#include "fts0fts.h"
#include "fts0types.h"
/*************************************************************************
IMPORTANT NOTE: Any operation that generates redo MUST check that there
is enough space in the redo log before for that operation. This is
done by calling log_free_check(). The reason for checking the
availability of the redo log space before the start of the operation is
that we MUST not hold any synchonization objects when performing the
check.
If you make a change in this module make sure that no codepath is
introduced where a call to log_free_check() is bypassed. */
/*********************************************************************//**
Creates an insert node struct.
@return own: insert node struct */
ins_node_t*
ins_node_create(
/*============*/
ulint ins_type, /*!< in: INS_VALUES, ... */
dict_table_t* table, /*!< in: table where to insert */
mem_heap_t* heap) /*!< in: mem heap where created */
{
ins_node_t* node;
node = static_cast<ins_node_t*>(
mem_heap_alloc(heap, sizeof(ins_node_t)));
node->common.type = QUE_NODE_INSERT;
node->ins_type = ins_type;
node->state = INS_NODE_SET_IX_LOCK;
node->table = table;
node->index = NULL;
node->entry = NULL;
node->select = NULL;
node->trx_id = 0;
node->duplicate = NULL;
node->entry_sys_heap = mem_heap_create(128);
node->magic_n = INS_NODE_MAGIC_N;
return(node);
}
/***********************************************************//**
Creates an entry template for each index of a table. */
static
void
ins_node_create_entry_list(
/*=======================*/
ins_node_t* node) /*!< in: row insert node */
{
dict_index_t* index;
dtuple_t* entry;
ut_ad(node->entry_sys_heap);
UT_LIST_INIT(node->entry_list, &dtuple_t::tuple_list);
/* We will include all indexes (include those corrupted
secondary indexes) in the entry list. Filteration of
these corrupted index will be done in row_ins() */
for (index = dict_table_get_first_index(node->table);
index != 0;
index = dict_table_get_next_index(index)) {
entry = row_build_index_entry_low(
node->row, NULL, index, node->entry_sys_heap,
ROW_BUILD_FOR_INSERT);
UT_LIST_ADD_LAST(node->entry_list, entry);
}
}
/*****************************************************************//**
Adds system field buffers to a row. */
static
void
row_ins_alloc_sys_fields(
/*=====================*/
ins_node_t* node) /*!< in: insert node */
{
dtuple_t* row;
dict_table_t* table;
const dict_col_t* col;
dfield_t* dfield;
row = node->row;
table = node->table;
ut_ad(dtuple_get_n_fields(row) == dict_table_get_n_cols(table));
/* allocate buffer to hold the needed system created hidden columns. */
compile_time_assert(DATA_ROW_ID_LEN
+ DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN
== sizeof node->sys_buf);
memset(node->sys_buf, 0, sizeof node->sys_buf);
/* Assign DB_ROLL_PTR to 1 << ROLL_PTR_INSERT_FLAG_POS */
node->sys_buf[DATA_ROW_ID_LEN + DATA_TRX_ID_LEN] = 0x80;
ut_ad(!memcmp(node->sys_buf + DATA_ROW_ID_LEN, reset_trx_id,
sizeof reset_trx_id));
/* 1. Populate row-id */
col = dict_table_get_sys_col(table, DATA_ROW_ID);
dfield = dtuple_get_nth_field(row, dict_col_get_no(col));
dfield_set_data(dfield, node->sys_buf, DATA_ROW_ID_LEN);
/* 2. Populate trx id */
col = dict_table_get_sys_col(table, DATA_TRX_ID);
dfield = dtuple_get_nth_field(row, dict_col_get_no(col));
dfield_set_data(dfield, &node->sys_buf[DATA_ROW_ID_LEN],
DATA_TRX_ID_LEN);
col = dict_table_get_sys_col(table, DATA_ROLL_PTR);
dfield = dtuple_get_nth_field(row, dict_col_get_no(col));
dfield_set_data(dfield, &node->sys_buf[DATA_ROW_ID_LEN
+ DATA_TRX_ID_LEN],
DATA_ROLL_PTR_LEN);
}
/*********************************************************************//**
Sets a new row to insert for an INS_DIRECT node. This function is only used
if we have constructed the row separately, which is a rare case; this
function is quite slow. */
void
ins_node_set_new_row(
/*=================*/
ins_node_t* node, /*!< in: insert node */
dtuple_t* row) /*!< in: new row (or first row) for the node */
{
node->state = INS_NODE_SET_IX_LOCK;
node->index = NULL;
node->entry = NULL;
node->duplicate = NULL;
node->row = row;
mem_heap_empty(node->entry_sys_heap);
/* Create templates for index entries */
ins_node_create_entry_list(node);
/* Allocate from entry_sys_heap buffers for sys fields */
row_ins_alloc_sys_fields(node);
/* As we allocated a new trx id buf, the trx id should be written
there again: */
node->trx_id = 0;
}
/*******************************************************************//**
Does an insert operation by updating a delete-marked existing record
in the index. This situation can occur if the delete-marked record is
kept in the index for consistent reads.
@return DB_SUCCESS or error code */
static MY_ATTRIBUTE((nonnull, warn_unused_result))
dberr_t
row_ins_sec_index_entry_by_modify(
/*==============================*/
ulint flags, /*!< in: undo logging and locking flags */
ulint mode, /*!< in: BTR_MODIFY_LEAF or BTR_MODIFY_TREE,
depending on whether mtr holds just a leaf
latch or also a tree latch */
btr_cur_t* cursor, /*!< in: B-tree cursor */
ulint** offsets,/*!< in/out: offsets on cursor->page_cur.rec */
mem_heap_t* offsets_heap,
/*!< in/out: memory heap that can be emptied */
mem_heap_t* heap, /*!< in/out: memory heap */
const dtuple_t* entry, /*!< in: index entry to insert */
que_thr_t* thr, /*!< in: query thread */
mtr_t* mtr) /*!< in: mtr; must be committed before
latching any further pages */
{
big_rec_t* dummy_big_rec;
upd_t* update;
rec_t* rec;
dberr_t err;
rec = btr_cur_get_rec(cursor);
ut_ad(!dict_index_is_clust(cursor->index));
ut_ad(rec_offs_validate(rec, cursor->index, *offsets));
ut_ad(!entry->info_bits);
/* We know that in the alphabetical ordering, entry and rec are
identified. But in their binary form there may be differences if
there are char fields in them. Therefore we have to calculate the
difference. */
update = row_upd_build_sec_rec_difference_binary(
rec, cursor->index, *offsets, entry, heap);
if (!rec_get_deleted_flag(rec, rec_offs_comp(*offsets))) {
/* We should never insert in place of a record that
has not been delete-marked. The only exception is when
online CREATE INDEX copied the changes that we already
made to the clustered index, and completed the
secondary index creation before we got here. In this
case, the change would already be there. The CREATE
INDEX should be waiting for a MySQL meta-data lock
upgrade at least until this INSERT or UPDATE
returns. After that point, set_committed(true)
would be invoked in commit_inplace_alter_table(). */
ut_a(update->n_fields == 0);
ut_a(!cursor->index->is_committed());
ut_ad(!dict_index_is_online_ddl(cursor->index));
return(DB_SUCCESS);
}
if (mode == BTR_MODIFY_LEAF) {
/* Try an optimistic updating of the record, keeping changes
within the page */
/* TODO: pass only *offsets */
err = btr_cur_optimistic_update(
flags | BTR_KEEP_SYS_FLAG, cursor,
offsets, &offsets_heap, update, 0, thr,
thr_get_trx(thr)->id, mtr);
switch (err) {
case DB_OVERFLOW:
case DB_UNDERFLOW:
case DB_ZIP_OVERFLOW:
err = DB_FAIL;
default:
break;
}
} else {
ut_a(mode == BTR_MODIFY_TREE);
if (buf_LRU_buf_pool_running_out()) {
return(DB_LOCK_TABLE_FULL);
}
err = btr_cur_pessimistic_update(
flags | BTR_KEEP_SYS_FLAG, cursor,
offsets, &offsets_heap,
heap, &dummy_big_rec, update, 0,
thr, thr_get_trx(thr)->id, mtr);
ut_ad(!dummy_big_rec);
}
return(err);
}
/*******************************************************************//**
Does an insert operation by delete unmarking and updating a delete marked
existing record in the index. This situation can occur if the delete marked
record is kept in the index for consistent reads.
@return DB_SUCCESS, DB_FAIL, or error code */
static MY_ATTRIBUTE((nonnull, warn_unused_result))
dberr_t
row_ins_clust_index_entry_by_modify(
/*================================*/
btr_pcur_t* pcur, /*!< in/out: a persistent cursor pointing
to the clust_rec that is being modified. */
ulint flags, /*!< in: undo logging and locking flags */
ulint mode, /*!< in: BTR_MODIFY_LEAF or BTR_MODIFY_TREE,
depending on whether mtr holds just a leaf
latch or also a tree latch */
ulint** offsets,/*!< out: offsets on cursor->page_cur.rec */
mem_heap_t** offsets_heap,
/*!< in/out: pointer to memory heap that can
be emptied, or NULL */
mem_heap_t* heap, /*!< in/out: memory heap */
const dtuple_t* entry, /*!< in: index entry to insert */
que_thr_t* thr, /*!< in: query thread */
mtr_t* mtr) /*!< in: mtr; must be committed before
latching any further pages */
{
const rec_t* rec;
upd_t* update;
dberr_t err;
btr_cur_t* cursor = btr_pcur_get_btr_cur(pcur);
TABLE* mysql_table = NULL;
ut_ad(dict_index_is_clust(cursor->index));
rec = btr_cur_get_rec(cursor);
ut_ad(rec_get_deleted_flag(rec,
dict_table_is_comp(cursor->index->table)));
/* In delete-marked records, DB_TRX_ID must
always refer to an existing undo log record. */
ut_ad(rec_get_trx_id(rec, cursor->index));
/* Build an update vector containing all the fields to be modified;
NOTE that this vector may NOT contain system columns trx_id or
roll_ptr */
if (thr->prebuilt != NULL) {
mysql_table = thr->prebuilt->m_mysql_table;
ut_ad(thr->prebuilt->trx == thr_get_trx(thr));
}
update = row_upd_build_difference_binary(
cursor->index, entry, rec, NULL, true,
thr_get_trx(thr), heap, mysql_table);
if (mode != BTR_MODIFY_TREE) {
ut_ad((mode & ulint(~BTR_ALREADY_S_LATCHED))
== BTR_MODIFY_LEAF);
/* Try optimistic updating of the record, keeping changes
within the page */
err = btr_cur_optimistic_update(
flags, cursor, offsets, offsets_heap, update, 0, thr,
thr_get_trx(thr)->id, mtr);
switch (err) {
case DB_OVERFLOW:
case DB_UNDERFLOW:
case DB_ZIP_OVERFLOW:
err = DB_FAIL;
default:
break;
}
} else {
if (buf_LRU_buf_pool_running_out()) {
return(DB_LOCK_TABLE_FULL);
}
big_rec_t* big_rec = NULL;
err = btr_cur_pessimistic_update(
flags | BTR_KEEP_POS_FLAG,
cursor, offsets, offsets_heap, heap,
&big_rec, update, 0, thr, thr_get_trx(thr)->id, mtr);
if (big_rec) {
ut_a(err == DB_SUCCESS);
DEBUG_SYNC_C("before_row_ins_upd_extern");
err = btr_store_big_rec_extern_fields(
pcur, *offsets, big_rec, mtr,
BTR_STORE_INSERT_UPDATE);
DEBUG_SYNC_C("after_row_ins_upd_extern");
dtuple_big_rec_free(big_rec);
}
}
return(err);
}
/*********************************************************************//**
Returns TRUE if in a cascaded update/delete an ancestor node of node
updates (not DELETE, but UPDATE) table.
@return TRUE if an ancestor updates table */
static
ibool
row_ins_cascade_ancestor_updates_table(
/*===================================*/
que_node_t* node, /*!< in: node in a query graph */
dict_table_t* table) /*!< in: table */
{
que_node_t* parent;
for (parent = que_node_get_parent(node);
que_node_get_type(parent) == QUE_NODE_UPDATE;
parent = que_node_get_parent(parent)) {
upd_node_t* upd_node;
upd_node = static_cast<upd_node_t*>(parent);
if (upd_node->table == table && !upd_node->is_delete) {
return(TRUE);
}
}
return(FALSE);
}
/*********************************************************************//**
Returns the number of ancestor UPDATE or DELETE nodes of a
cascaded update/delete node.
@return number of ancestors */
static MY_ATTRIBUTE((nonnull, warn_unused_result))
ulint
row_ins_cascade_n_ancestors(
/*========================*/
que_node_t* node) /*!< in: node in a query graph */
{
que_node_t* parent;
ulint n_ancestors = 0;
for (parent = que_node_get_parent(node);
que_node_get_type(parent) == QUE_NODE_UPDATE;
parent = que_node_get_parent(parent)) {
n_ancestors++;
}
return(n_ancestors);
}
/******************************************************************//**
Calculates the update vector node->cascade->update for a child table in
a cascaded update.
@return whether any FULLTEXT INDEX is affected */
static MY_ATTRIBUTE((nonnull, warn_unused_result))
bool
row_ins_cascade_calc_update_vec(
/*============================*/
upd_node_t* node, /*!< in: update node of the parent
table */
dict_foreign_t* foreign, /*!< in: foreign key constraint whose
type is != 0 */
mem_heap_t* heap, /*!< in: memory heap to use as
temporary storage */
trx_t* trx) /*!< in: update transaction */
{
upd_node_t* cascade = node->cascade_node;
dict_table_t* table = foreign->foreign_table;
dict_index_t* index = foreign->foreign_index;
upd_t* update;
dict_table_t* parent_table;
dict_index_t* parent_index;
upd_t* parent_update;
ulint n_fields_updated;
ulint parent_field_no;
ulint i;
ulint j;
bool doc_id_updated = false;
ulint doc_id_pos = 0;
doc_id_t new_doc_id = FTS_NULL_DOC_ID;
ulint prefix_col;
ut_a(node);
ut_a(foreign);
ut_a(cascade);
ut_a(table);
ut_a(index);
/* Calculate the appropriate update vector which will set the fields
in the child index record to the same value (possibly padded with
spaces if the column is a fixed length CHAR or FIXBINARY column) as
the referenced index record will get in the update. */
parent_table = node->table;
ut_a(parent_table == foreign->referenced_table);
parent_index = foreign->referenced_index;
parent_update = node->update;
update = cascade->update;
update->info_bits = 0;
n_fields_updated = 0;
bool affects_fulltext = false;
if (table->fts) {
doc_id_pos = dict_table_get_nth_col_pos(
table, table->fts->doc_col, &prefix_col);
}
for (i = 0; i < foreign->n_fields; i++) {
parent_field_no = dict_table_get_nth_col_pos(
parent_table,
dict_index_get_nth_col_no(parent_index, i),
&prefix_col);
for (j = 0; j < parent_update->n_fields; j++) {
const upd_field_t* parent_ufield
= &parent_update->fields[j];
if (parent_ufield->field_no == parent_field_no) {
ulint min_size;
const dict_col_t* col;
ulint ufield_len;
upd_field_t* ufield;
col = dict_index_get_nth_col(index, i);
/* A field in the parent index record is
updated. Let us make the update vector
field for the child table. */
ufield = update->fields + n_fields_updated;
ufield->field_no
= dict_table_get_nth_col_pos(
table, dict_col_get_no(col),
&prefix_col);
ufield->orig_len = 0;
ufield->exp = NULL;
ufield->new_val = parent_ufield->new_val;
dfield_get_type(&ufield->new_val)->prtype |=
col->prtype & DATA_VERSIONED;
ufield_len = dfield_get_len(&ufield->new_val);
/* Clear the "external storage" flag */
dfield_set_len(&ufield->new_val, ufield_len);
/* Do not allow a NOT NULL column to be
updated as NULL */
if (dfield_is_null(&ufield->new_val)
&& (col->prtype & DATA_NOT_NULL)) {
goto err_exit;
}
/* If the new value would not fit in the
column, do not allow the update */
if (!dfield_is_null(&ufield->new_val)
&& dtype_get_at_most_n_mbchars(
col->prtype,
col->mbminlen, col->mbmaxlen,
col->len,
ufield_len,
static_cast<char*>(
dfield_get_data(
&ufield->new_val)))
< ufield_len) {
goto err_exit;
}
/* If the parent column type has a different
length than the child column type, we may
need to pad with spaces the new value of the
child column */
min_size = dict_col_get_min_size(col);
/* Because UNIV_SQL_NULL (the marker
of SQL NULL values) exceeds all possible
values of min_size, the test below will
not hold for SQL NULL columns. */
if (min_size > ufield_len) {
byte* pad;
ulint pad_len;
byte* padded_data;
ulint mbminlen;
padded_data = static_cast<byte*>(
mem_heap_alloc(
heap, min_size));
pad = padded_data + ufield_len;
pad_len = min_size - ufield_len;
memcpy(padded_data,
dfield_get_data(&ufield
->new_val),
ufield_len);
mbminlen = dict_col_get_mbminlen(col);
ut_ad(!(ufield_len % mbminlen));
ut_ad(!(min_size % mbminlen));
if (mbminlen == 1
&& dtype_get_charset_coll(
col->prtype)
== DATA_MYSQL_BINARY_CHARSET_COLL) {
/* Do not pad BINARY columns */
goto err_exit;
}
row_mysql_pad_col(mbminlen,
pad, pad_len);
dfield_set_data(&ufield->new_val,
padded_data, min_size);
}
/* Check whether the current column has
FTS index on it */
if (table->fts
&& dict_table_is_fts_column(
table->fts->indexes,
dict_col_get_no(col),
col->is_virtual())
!= ULINT_UNDEFINED) {
affects_fulltext = true;
}
/* If Doc ID is updated, check whether the
Doc ID is valid */
if (table->fts
&& ufield->field_no == doc_id_pos) {
doc_id_t n_doc_id;
n_doc_id =
table->fts->cache->next_doc_id;
new_doc_id = fts_read_doc_id(
static_cast<const byte*>(
dfield_get_data(
&ufield->new_val)));
affects_fulltext = true;
doc_id_updated = true;
if (new_doc_id <= 0) {
ib::error() << "FTS Doc ID"
" must be larger than"
" 0";
goto err_exit;
}
if (new_doc_id < n_doc_id) {
ib::error() << "FTS Doc ID"
" must be larger than "
<< n_doc_id - 1
<< " for table "
<< table->name;
goto err_exit;
}
}
n_fields_updated++;
}
}
}
if (affects_fulltext) {
ut_ad(table->fts);
if (DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_HAS_DOC_ID)) {
doc_id_t doc_id;
doc_id_t* next_doc_id;
upd_field_t* ufield;
next_doc_id = static_cast<doc_id_t*>(mem_heap_alloc(
heap, sizeof(doc_id_t)));
ut_ad(!doc_id_updated);
ufield = update->fields + n_fields_updated;
fts_get_next_doc_id(table, next_doc_id);
doc_id = fts_update_doc_id(table, ufield, next_doc_id);
n_fields_updated++;
fts_trx_add_op(trx, table, doc_id, FTS_INSERT, NULL);
} else {
if (doc_id_updated) {
ut_ad(new_doc_id);
fts_trx_add_op(trx, table, new_doc_id,
FTS_INSERT, NULL);
} else {
ib::error() << "FTS Doc ID must be updated"
" along with FTS indexed column for"
" table " << table->name;
err_exit:
n_fields_updated = ULINT_UNDEFINED;
}
}
}
update->n_fields = n_fields_updated;
return affects_fulltext;
}
/*********************************************************************//**
Set detailed error message associated with foreign key errors for
the given transaction. */
static
void
row_ins_set_detailed(
/*=================*/
trx_t* trx, /*!< in: transaction */
dict_foreign_t* foreign) /*!< in: foreign key constraint */
{
ut_ad(!srv_read_only_mode);
mutex_enter(&srv_misc_tmpfile_mutex);
rewind(srv_misc_tmpfile);
if (os_file_set_eof(srv_misc_tmpfile)) {
ut_print_name(srv_misc_tmpfile, trx,
foreign->foreign_table_name);
std::string fk_str = dict_print_info_on_foreign_key_in_create_format(
trx, foreign, FALSE);
fputs(fk_str.c_str(), srv_misc_tmpfile);
trx_set_detailed_error_from_file(trx, srv_misc_tmpfile);
} else {
trx_set_detailed_error(trx, "temp file operation failed");
}
mutex_exit(&srv_misc_tmpfile_mutex);
}
/*********************************************************************//**
Acquires dict_foreign_err_mutex, rewinds dict_foreign_err_file
and displays information about the given transaction.
The caller must release dict_foreign_err_mutex. */
static
void
row_ins_foreign_trx_print(
/*======================*/
trx_t* trx) /*!< in: transaction */
{
ulint n_rec_locks;
ulint n_trx_locks;
ulint heap_size;
ut_ad(!srv_read_only_mode);
lock_mutex_enter();
n_rec_locks = lock_number_of_rows_locked(&trx->lock);
n_trx_locks = UT_LIST_GET_LEN(trx->lock.trx_locks);
heap_size = mem_heap_get_size(trx->lock.lock_heap);
lock_mutex_exit();
mutex_enter(&dict_foreign_err_mutex);
rewind(dict_foreign_err_file);
ut_print_timestamp(dict_foreign_err_file);
fputs(" Transaction:\n", dict_foreign_err_file);
trx_print_low(dict_foreign_err_file, trx, 600,
n_rec_locks, n_trx_locks, heap_size);
ut_ad(mutex_own(&dict_foreign_err_mutex));
}
/*********************************************************************//**
Reports a foreign key error associated with an update or a delete of a
parent table index entry. */
static
void
row_ins_foreign_report_err(
/*=======================*/
const char* errstr, /*!< in: error string from the viewpoint
of the parent table */
que_thr_t* thr, /*!< in: query thread whose run_node
is an update node */
dict_foreign_t* foreign, /*!< in: foreign key constraint */
const rec_t* rec, /*!< in: a matching index record in the
child table */
const dtuple_t* entry) /*!< in: index entry in the parent
table */
{
std::string fk_str;
if (srv_read_only_mode) {
return;
}
FILE* ef = dict_foreign_err_file;
trx_t* trx = thr_get_trx(thr);
row_ins_set_detailed(trx, foreign);
row_ins_foreign_trx_print(trx);
fputs("Foreign key constraint fails for table ", ef);
ut_print_name(ef, trx, foreign->foreign_table_name);
fputs(":\n", ef);
fk_str = dict_print_info_on_foreign_key_in_create_format(trx, foreign,
TRUE);
fputs(fk_str.c_str(), ef);
putc('\n', ef);
fputs(errstr, ef);
fprintf(ef, " in parent table, in index %s",
foreign->referenced_index->name());
if (entry) {
fputs(" tuple:\n", ef);
dtuple_print(ef, entry);
}
fputs("\nBut in child table ", ef);
ut_print_name(ef, trx, foreign->foreign_table_name);
fprintf(ef, ", in index %s", foreign->foreign_index->name());
if (rec) {
fputs(", there is a record:\n", ef);
rec_print(ef, rec, foreign->foreign_index);
} else {
fputs(", the record is not available\n", ef);
}
putc('\n', ef);
mutex_exit(&dict_foreign_err_mutex);
}
/*********************************************************************//**
Reports a foreign key error to dict_foreign_err_file when we are trying
to add an index entry to a child table. Note that the adding may be the result
of an update, too. */
static
void
row_ins_foreign_report_add_err(
/*===========================*/
trx_t* trx, /*!< in: transaction */
dict_foreign_t* foreign, /*!< in: foreign key constraint */
const rec_t* rec, /*!< in: a record in the parent table:
it does not match entry because we
have an error! */
const dtuple_t* entry) /*!< in: index entry to insert in the
child table */
{
std::string fk_str;
if (srv_read_only_mode) {
return;
}
FILE* ef = dict_foreign_err_file;
row_ins_set_detailed(trx, foreign);
row_ins_foreign_trx_print(trx);
fputs("Foreign key constraint fails for table ", ef);
ut_print_name(ef, trx, foreign->foreign_table_name);
fputs(":\n", ef);
fk_str = dict_print_info_on_foreign_key_in_create_format(trx, foreign,
TRUE);
fputs(fk_str.c_str(), ef);
fprintf(ef, " in parent table, in index %s",
foreign->foreign_index->name());
if (entry) {
fputs(" tuple:\n", ef);
/* TODO: DB_TRX_ID and DB_ROLL_PTR may be uninitialized.
It would be better to only display the user columns. */
dtuple_print(ef, entry);
}
fputs("\nBut in parent table ", ef);
ut_print_name(ef, trx, foreign->referenced_table_name);
fprintf(ef, ", in index %s,\n"
"the closest match we can find is record:\n",
foreign->referenced_index->name());
if (rec && page_rec_is_supremum(rec)) {
/* If the cursor ended on a supremum record, it is better
to report the previous record in the error message, so that
the user gets a more descriptive error message. */
rec = page_rec_get_prev_const(rec);
}
if (rec) {
rec_print(ef, rec, foreign->referenced_index);
}
putc('\n', ef);
mutex_exit(&dict_foreign_err_mutex);
}
/*********************************************************************//**
Invalidate the query cache for the given table. */
static
void
row_ins_invalidate_query_cache(
/*===========================*/
que_thr_t* thr, /*!< in: query thread whose run_node
is an update node */
const char* name) /*!< in: table name prefixed with
database name and a '/' character */
{
innobase_invalidate_query_cache(thr_get_trx(thr), name);
}
/** Fill virtual column information in cascade node for the child table.
@param[out] cascade child update node
@param[in] rec clustered rec of child table
@param[in] index clustered index of child table
@param[in] node parent update node
@param[in] foreign foreign key information
@param[out] err error code. */
static
void
row_ins_foreign_fill_virtual(
upd_node_t* cascade,
const rec_t* rec,
dict_index_t* index,
upd_node_t* node,
dict_foreign_t* foreign,
dberr_t* err)
{
THD* thd = current_thd;
row_ext_t* ext;
ulint offsets_[REC_OFFS_NORMAL_SIZE];
rec_offs_init(offsets_);
const ulint* offsets =
rec_get_offsets(rec, index, offsets_, true,
ULINT_UNDEFINED, &cascade->heap);
mem_heap_t* v_heap = NULL;
TABLE* mysql_table= NULL;
VCOL_STORAGE* vcol_storage= NULL;
byte* record;
upd_t* update = cascade->update;
ulint n_v_fld = index->table->n_v_def;
ulint n_diff;
upd_field_t* upd_field;
dict_vcol_set* v_cols = foreign->v_cols;
update->old_vrow = row_build(
ROW_COPY_POINTERS, index, rec,
offsets, index->table, NULL, NULL,
&ext, cascade->heap);
n_diff = update->n_fields;
update->n_fields += n_v_fld;
if (index->table->vc_templ == NULL) {
/** This can occur when there is a cascading
delete or update after restart. */
innobase_init_vc_templ(index->table);
}
if (innobase_allocate_row_for_vcol(thd, index, &v_heap,
&mysql_table,
&record, &vcol_storage))
{
*err = DB_OUT_OF_MEMORY;
goto func_exit;
}
for (ulint i = 0; i < n_v_fld; i++) {
dict_v_col_t* col = dict_table_get_nth_v_col(
index->table, i);
dict_vcol_set::iterator it = v_cols->find(col);
if (it == v_cols->end()) {
continue;
}
dfield_t* vfield = innobase_get_computed_value(
update->old_vrow, col, index,
&v_heap, update->heap, NULL, thd, mysql_table,
record, NULL, NULL, NULL);
if (vfield == NULL) {
*err = DB_COMPUTE_VALUE_FAILED;
goto func_exit;
}
upd_field = upd_get_nth_field(update, n_diff);
upd_field->old_v_val = static_cast<dfield_t*>(
mem_heap_alloc(cascade->heap,
sizeof *upd_field->old_v_val));
dfield_copy(upd_field->old_v_val, vfield);
upd_field_set_v_field_no(upd_field, i, index);