1
/******************************************************
6
Created 4/20/1996 Heikki Tuuri
7
*******************************************************/
15
#include "dict0dict.h"
16
#include "dict0boot.h"
20
#include "mach0data.h"
26
#include "lock0lock.h"
28
#include "eval0eval.h"
29
#include "data0data.h"
33
#define ROW_INS_PREV 1
34
#define ROW_INS_NEXT 2
37
/*********************************************************************
38
This prototype is copied from /mysql/sql/ha_innodb.cc.
39
Invalidates the MySQL query cache for the table.
40
NOTE that the exact prototype of this function has to be in
41
/innobase/row/row0ins.c! */
44
innobase_invalidate_query_cache(
45
/*============================*/
46
trx_t* trx, /* in: transaction which modifies the table */
47
char* full_name, /* in: concatenation of database name, null
48
char '\0', table name, null char'\0';
49
NOTE that in Windows this is always
51
ulint full_name_len); /* in: full name length where also the null
54
/*************************************************************************
55
Creates an insert node struct. */
60
/* out, own: insert node struct */
61
ulint ins_type, /* in: INS_VALUES, ... */
62
dict_table_t* table, /* in: table where to insert */
63
mem_heap_t* heap) /* in: mem heap where created */
67
node = mem_heap_alloc(heap, sizeof(ins_node_t));
69
node->common.type = QUE_NODE_INSERT;
71
node->ins_type = ins_type;
73
node->state = INS_NODE_SET_IX_LOCK;
80
node->trx_id = ut_dulint_zero;
82
node->entry_sys_heap = mem_heap_create(128);
84
node->magic_n = INS_NODE_MAGIC_N;
89
/***************************************************************
90
Creates an entry template for each index of a table. */
93
ins_node_create_entry_list(
94
/*=======================*/
95
ins_node_t* node) /* in: row insert node */
100
ut_ad(node->entry_sys_heap);
102
UT_LIST_INIT(node->entry_list);
104
index = dict_table_get_first_index(node->table);
106
while (index != NULL) {
107
entry = row_build_index_entry(node->row, index,
108
node->entry_sys_heap);
109
UT_LIST_ADD_LAST(tuple_list, node->entry_list, entry);
111
index = dict_table_get_next_index(index);
115
/*********************************************************************
116
Adds system field buffers to a row. */
119
row_ins_alloc_sys_fields(
120
/*=====================*/
121
ins_node_t* node) /* in: insert node */
126
const dict_col_t* col;
132
heap = node->entry_sys_heap;
134
ut_ad(row && table && heap);
135
ut_ad(dtuple_get_n_fields(row) == dict_table_get_n_cols(table));
137
/* 1. Allocate buffer for row id */
139
col = dict_table_get_sys_col(table, DATA_ROW_ID);
141
dfield = dtuple_get_nth_field(row, dict_col_get_no(col));
143
ptr = mem_heap_alloc(heap, DATA_ROW_ID_LEN);
145
dfield_set_data(dfield, ptr, DATA_ROW_ID_LEN);
147
node->row_id_buf = ptr;
149
/* 3. Allocate buffer for trx id */
151
col = dict_table_get_sys_col(table, DATA_TRX_ID);
153
dfield = dtuple_get_nth_field(row, dict_col_get_no(col));
154
ptr = mem_heap_alloc(heap, DATA_TRX_ID_LEN);
156
dfield_set_data(dfield, ptr, DATA_TRX_ID_LEN);
158
node->trx_id_buf = ptr;
160
/* 4. Allocate buffer for roll ptr */
162
col = dict_table_get_sys_col(table, DATA_ROLL_PTR);
164
dfield = dtuple_get_nth_field(row, dict_col_get_no(col));
165
ptr = mem_heap_alloc(heap, DATA_ROLL_PTR_LEN);
167
dfield_set_data(dfield, ptr, DATA_ROLL_PTR_LEN);
170
/*************************************************************************
171
Sets a new row to insert for an INS_DIRECT node. This function is only used
172
if we have constructed the row separately, which is a rare case; this
173
function is quite slow. */
176
ins_node_set_new_row(
177
/*=================*/
178
ins_node_t* node, /* in: insert node */
179
dtuple_t* row) /* in: new row (or first row) for the node */
181
node->state = INS_NODE_SET_IX_LOCK;
187
mem_heap_empty(node->entry_sys_heap);
189
/* Create templates for index entries */
191
ins_node_create_entry_list(node);
193
/* Allocate from entry_sys_heap buffers for sys fields */
195
row_ins_alloc_sys_fields(node);
197
/* As we allocated a new trx id buf, the trx id should be written
200
node->trx_id = ut_dulint_zero;
203
/***********************************************************************
204
Does an insert operation by updating a delete-marked existing record
205
in the index. This situation can occur if the delete-marked record is
206
kept in the index for consistent reads. */
209
row_ins_sec_index_entry_by_modify(
210
/*==============================*/
211
/* out: DB_SUCCESS or error code */
212
ulint mode, /* in: BTR_MODIFY_LEAF or BTR_MODIFY_TREE,
213
depending on whether mtr holds just a leaf
214
latch or also a tree latch */
215
btr_cur_t* cursor, /* in: B-tree cursor */
216
dtuple_t* entry, /* in: index entry to insert */
217
que_thr_t* thr, /* in: query thread */
218
mtr_t* mtr) /* in: mtr */
220
big_rec_t* dummy_big_rec;
226
rec = btr_cur_get_rec(cursor);
228
ut_ad((cursor->index->type & DICT_CLUSTERED) == 0);
229
ut_ad(rec_get_deleted_flag(rec,
230
dict_table_is_comp(cursor->index->table)));
232
/* We know that in the alphabetical ordering, entry and rec are
233
identified. But in their binary form there may be differences if
234
there are char fields in them. Therefore we have to calculate the
237
heap = mem_heap_create(1024);
239
update = row_upd_build_sec_rec_difference_binary(
240
cursor->index, entry, rec, thr_get_trx(thr), heap);
241
if (mode == BTR_MODIFY_LEAF) {
242
/* Try an optimistic updating of the record, keeping changes
245
err = btr_cur_optimistic_update(BTR_KEEP_SYS_FLAG, cursor,
246
update, 0, thr, mtr);
247
if (err == DB_OVERFLOW || err == DB_UNDERFLOW) {
251
ut_a(mode == BTR_MODIFY_TREE);
252
if (buf_LRU_buf_pool_running_out()) {
254
err = DB_LOCK_TABLE_FULL;
259
err = btr_cur_pessimistic_update(BTR_KEEP_SYS_FLAG, cursor,
260
&dummy_big_rec, update,
269
/***********************************************************************
270
Does an insert operation by delete unmarking and updating a delete marked
271
existing record in the index. This situation can occur if the delete marked
272
record is kept in the index for consistent reads. */
275
row_ins_clust_index_entry_by_modify(
276
/*================================*/
277
/* out: DB_SUCCESS, DB_FAIL, or error code */
278
ulint mode, /* in: BTR_MODIFY_LEAF or BTR_MODIFY_TREE,
279
depending on whether mtr holds just a leaf
280
latch or also a tree latch */
281
btr_cur_t* cursor, /* in: B-tree cursor */
282
big_rec_t** big_rec,/* out: possible big rec vector of fields
283
which have to be stored externally by the
285
dtuple_t* entry, /* in: index entry to insert */
286
ulint* ext_vec,/* in: array containing field numbers of
287
externally stored fields in entry, or NULL */
288
ulint n_ext_vec,/* in: number of fields in ext_vec */
289
que_thr_t* thr, /* in: query thread */
290
mtr_t* mtr) /* in: mtr */
297
ut_ad(cursor->index->type & DICT_CLUSTERED);
301
rec = btr_cur_get_rec(cursor);
303
ut_ad(rec_get_deleted_flag(rec,
304
dict_table_is_comp(cursor->index->table)));
306
heap = mem_heap_create(1024);
308
/* Build an update vector containing all the fields to be modified;
309
NOTE that this vector may NOT contain system columns trx_id or
312
update = row_upd_build_difference_binary(cursor->index, entry, ext_vec,
314
thr_get_trx(thr), heap);
315
if (mode == BTR_MODIFY_LEAF) {
316
/* Try optimistic updating of the record, keeping changes
319
err = btr_cur_optimistic_update(0, cursor, update, 0, thr,
321
if (err == DB_OVERFLOW || err == DB_UNDERFLOW) {
325
ut_a(mode == BTR_MODIFY_TREE);
326
if (buf_LRU_buf_pool_running_out()) {
328
err = DB_LOCK_TABLE_FULL;
332
err = btr_cur_pessimistic_update(0, cursor, big_rec, update,
341
/*************************************************************************
342
Returns TRUE if in a cascaded update/delete an ancestor node of node
343
updates (not DELETE, but UPDATE) table. */
346
row_ins_cascade_ancestor_updates_table(
347
/*===================================*/
348
/* out: TRUE if an ancestor updates table */
349
que_node_t* node, /* in: node in a query graph */
350
dict_table_t* table) /* in: table */
353
upd_node_t* upd_node;
355
parent = que_node_get_parent(node);
357
while (que_node_get_type(parent) == QUE_NODE_UPDATE) {
361
if (upd_node->table == table && upd_node->is_delete == FALSE) {
366
parent = que_node_get_parent(parent);
374
/*************************************************************************
375
Returns the number of ancestor UPDATE or DELETE nodes of a
376
cascaded update/delete node. */
379
row_ins_cascade_n_ancestors(
380
/*========================*/
381
/* out: number of ancestors */
382
que_node_t* node) /* in: node in a query graph */
385
ulint n_ancestors = 0;
387
parent = que_node_get_parent(node);
389
while (que_node_get_type(parent) == QUE_NODE_UPDATE) {
392
parent = que_node_get_parent(parent);
400
/**********************************************************************
401
Calculates the update vector node->cascade->update for a child table in
402
a cascaded update. */
405
row_ins_cascade_calc_update_vec(
406
/*============================*/
407
/* out: number of fields in the
408
calculated update vector; the value
409
can also be 0 if no foreign key
410
fields changed; the returned value
411
is ULINT_UNDEFINED if the column
412
type in the child table is too short
413
to fit the new value in the parent
414
table: that means the update fails */
415
upd_node_t* node, /* in: update node of the parent
417
dict_foreign_t* foreign, /* in: foreign key constraint whose
419
mem_heap_t* heap) /* in: memory heap to use as
422
upd_node_t* cascade = node->cascade_node;
423
dict_table_t* table = foreign->foreign_table;
424
dict_index_t* index = foreign->foreign_index;
427
dict_table_t* parent_table;
428
dict_index_t* parent_index;
429
upd_t* parent_update;
430
upd_field_t* parent_ufield;
431
ulint n_fields_updated;
432
ulint parent_field_no;
442
/* Calculate the appropriate update vector which will set the fields
443
in the child index record to the same value (possibly padded with
444
spaces if the column is a fixed length CHAR or FIXBINARY column) as
445
the referenced index record will get in the update. */
447
parent_table = node->table;
448
ut_a(parent_table == foreign->referenced_table);
449
parent_index = foreign->referenced_index;
450
parent_update = node->update;
452
update = cascade->update;
454
update->info_bits = 0;
455
update->n_fields = foreign->n_fields;
457
n_fields_updated = 0;
459
for (i = 0; i < foreign->n_fields; i++) {
461
parent_field_no = dict_table_get_nth_col_pos(
463
dict_index_get_nth_col_no(parent_index, i));
465
for (j = 0; j < parent_update->n_fields; j++) {
466
parent_ufield = parent_update->fields + j;
468
if (parent_ufield->field_no == parent_field_no) {
471
const dict_col_t* col;
473
col = dict_index_get_nth_col(index, i);
475
/* A field in the parent index record is
476
updated. Let us make the update vector
477
field for the child table. */
479
ufield = update->fields + n_fields_updated;
482
= dict_table_get_nth_col_pos(
483
table, dict_col_get_no(col));
486
ufield->new_val = parent_ufield->new_val;
488
/* Do not allow a NOT NULL column to be
491
if (ufield->new_val.len == UNIV_SQL_NULL
492
&& (col->prtype & DATA_NOT_NULL)) {
494
return(ULINT_UNDEFINED);
497
/* If the new value would not fit in the
498
column, do not allow the update */
500
if (ufield->new_val.len != UNIV_SQL_NULL
501
&& dtype_get_at_most_n_mbchars(
503
col->mbminlen, col->mbmaxlen,
506
ufield->new_val.data)
507
< ufield->new_val.len) {
509
return(ULINT_UNDEFINED);
512
/* If the parent column type has a different
513
length than the child column type, we may
514
need to pad with spaces the new value of the
517
min_size = dict_col_get_min_size(col);
520
&& ufield->new_val.len != UNIV_SQL_NULL
521
&& ufield->new_val.len < min_size) {
525
ufield->new_val.data = mem_heap_alloc(
527
pad_start = ((char*) ufield
529
+ ufield->new_val.len;
530
pad_end = ((char*) ufield
533
ufield->new_val.len = min_size;
534
ut_memcpy(ufield->new_val.data,
535
parent_ufield->new_val.data,
536
parent_ufield->new_val.len);
538
switch (UNIV_EXPECT(col->mbminlen,1)) {
543
(dtype_get_charset_coll(
545
== DATA_MYSQL_BINARY_CHARSET_COLL)) {
548
return(ULINT_UNDEFINED);
552
memset(pad_start, 0x20,
553
pad_end - pad_start);
557
ut_a(!(ufield->new_val.len
559
ut_a(!(min_size % 2));
563
} while (pad_start < pad_end);
568
ufield->extern_storage = FALSE;
575
update->n_fields = n_fields_updated;
577
return(n_fields_updated);
580
/*************************************************************************
581
Set detailed error message associated with foreign key errors for
582
the given transaction. */
585
row_ins_set_detailed(
586
/*=================*/
587
trx_t* trx, /* in: transaction */
588
dict_foreign_t* foreign) /* in: foreign key constraint */
590
mutex_enter(&srv_misc_tmpfile_mutex);
591
rewind(srv_misc_tmpfile);
593
if (os_file_set_eof(srv_misc_tmpfile)) {
594
ut_print_name(srv_misc_tmpfile, trx, TRUE,
595
foreign->foreign_table_name);
596
dict_print_info_on_foreign_key_in_create_format(
597
srv_misc_tmpfile, trx, foreign, FALSE);
598
trx_set_detailed_error_from_file(trx, srv_misc_tmpfile);
600
trx_set_detailed_error(trx, "temp file operation failed");
603
mutex_exit(&srv_misc_tmpfile_mutex);
606
/*************************************************************************
607
Reports a foreign key error associated with an update or a delete of a
608
parent table index entry. */
611
row_ins_foreign_report_err(
612
/*=======================*/
613
const char* errstr, /* in: error string from the viewpoint
614
of the parent table */
615
que_thr_t* thr, /* in: query thread whose run_node
617
dict_foreign_t* foreign, /* in: foreign key constraint */
618
rec_t* rec, /* in: a matching index record in the
620
dtuple_t* entry) /* in: index entry in the parent
623
FILE* ef = dict_foreign_err_file;
624
trx_t* trx = thr_get_trx(thr);
626
row_ins_set_detailed(trx, foreign);
628
mutex_enter(&dict_foreign_err_mutex);
630
ut_print_timestamp(ef);
631
fputs(" Transaction:\n", ef);
632
trx_print(ef, trx, 600);
634
fputs("Foreign key constraint fails for table ", ef);
635
ut_print_name(ef, trx, TRUE, foreign->foreign_table_name);
637
dict_print_info_on_foreign_key_in_create_format(ef, trx, foreign,
641
fputs(" in parent table, in index ", ef);
642
ut_print_name(ef, trx, FALSE, foreign->referenced_index->name);
644
fputs(" tuple:\n", ef);
645
dtuple_print(ef, entry);
647
fputs("\nBut in child table ", ef);
648
ut_print_name(ef, trx, TRUE, foreign->foreign_table_name);
649
fputs(", in index ", ef);
650
ut_print_name(ef, trx, FALSE, foreign->foreign_index->name);
652
fputs(", there is a record:\n", ef);
653
rec_print(ef, rec, foreign->foreign_index);
655
fputs(", the record is not available\n", ef);
659
mutex_exit(&dict_foreign_err_mutex);
662
/*************************************************************************
663
Reports a foreign key error to dict_foreign_err_file when we are trying
664
to add an index entry to a child table. Note that the adding may be the result
665
of an update, too. */
668
row_ins_foreign_report_add_err(
669
/*===========================*/
670
trx_t* trx, /* in: transaction */
671
dict_foreign_t* foreign, /* in: foreign key constraint */
672
rec_t* rec, /* in: a record in the parent table:
673
it does not match entry because we
675
dtuple_t* entry) /* in: index entry to insert in the
678
FILE* ef = dict_foreign_err_file;
680
row_ins_set_detailed(trx, foreign);
682
mutex_enter(&dict_foreign_err_mutex);
684
ut_print_timestamp(ef);
685
fputs(" Transaction:\n", ef);
686
trx_print(ef, trx, 600);
687
fputs("Foreign key constraint fails for table ", ef);
688
ut_print_name(ef, trx, TRUE, foreign->foreign_table_name);
690
dict_print_info_on_foreign_key_in_create_format(ef, trx, foreign,
692
fputs("\nTrying to add in child table, in index ", ef);
693
ut_print_name(ef, trx, FALSE, foreign->foreign_index->name);
695
fputs(" tuple:\n", ef);
696
dtuple_print(ef, entry);
698
fputs("\nBut in parent table ", ef);
699
ut_print_name(ef, trx, TRUE, foreign->referenced_table_name);
700
fputs(", in index ", ef);
701
ut_print_name(ef, trx, FALSE, foreign->referenced_index->name);
702
fputs(",\nthe closest match we can find is record:\n", ef);
703
if (rec && page_rec_is_supremum(rec)) {
704
/* If the cursor ended on a supremum record, it is better
705
to report the previous record in the error message, so that
706
the user gets a more descriptive error message. */
707
rec = page_rec_get_prev(rec);
711
rec_print(ef, rec, foreign->referenced_index);
715
mutex_exit(&dict_foreign_err_mutex);
718
/*************************************************************************
719
Invalidate the query cache for the given table. */
722
row_ins_invalidate_query_cache(
723
/*===========================*/
724
que_thr_t* thr, /* in: query thread whose run_node
726
const char* name) /* in: table name prefixed with
727
database name and a '/' character */
731
ulint len = strlen(name) + 1;
733
buf = mem_strdupl(name, len);
735
ptr = strchr(buf, '/');
739
/* We call a function in ha_innodb.cc */
740
#ifndef UNIV_HOTBACKUP
741
innobase_invalidate_query_cache(thr_get_trx(thr), buf, len);
746
/*************************************************************************
747
Perform referential actions or checks when a parent row is deleted or updated
748
and the constraint had an ON DELETE or ON UPDATE condition which was not
752
row_ins_foreign_check_on_constraint(
753
/*================================*/
754
/* out: DB_SUCCESS, DB_LOCK_WAIT,
756
que_thr_t* thr, /* in: query thread whose run_node
758
dict_foreign_t* foreign, /* in: foreign key constraint whose
760
btr_pcur_t* pcur, /* in: cursor placed on a matching
761
index record in the child table */
762
dtuple_t* entry, /* in: index entry in the parent
764
mtr_t* mtr) /* in: mtr holding the latch of pcur
769
dict_table_t* table = foreign->foreign_table;
771
dict_index_t* clust_index;
773
mem_heap_t* upd_vec_heap = NULL;
781
mem_heap_t* tmp_heap = NULL;
788
trx = thr_get_trx(thr);
790
/* Since we are going to delete or update a row, we have to invalidate
791
the MySQL query cache for table. A deadlock of threads is not possible
792
here because the caller of this function does not hold any latches with
793
the sync0sync.h rank above the kernel mutex. The query cache mutex has
794
a rank just above the kernel mutex. */
796
row_ins_invalidate_query_cache(thr, table->name);
798
node = thr->run_node;
800
if (node->is_delete && 0 == (foreign->type
801
& (DICT_FOREIGN_ON_DELETE_CASCADE
802
| DICT_FOREIGN_ON_DELETE_SET_NULL))) {
804
row_ins_foreign_report_err("Trying to delete",
806
btr_pcur_get_rec(pcur), entry);
808
return(DB_ROW_IS_REFERENCED);
811
if (!node->is_delete && 0 == (foreign->type
812
& (DICT_FOREIGN_ON_UPDATE_CASCADE
813
| DICT_FOREIGN_ON_UPDATE_SET_NULL))) {
815
/* This is an UPDATE */
817
row_ins_foreign_report_err("Trying to update",
819
btr_pcur_get_rec(pcur), entry);
821
return(DB_ROW_IS_REFERENCED);
824
if (node->cascade_node == NULL) {
825
/* Extend our query graph by creating a child to current
826
update node. The child is used in the cascade or set null
829
node->cascade_heap = mem_heap_create(128);
830
node->cascade_node = row_create_update_node_for_mysql(
831
table, node->cascade_heap);
832
que_node_set_parent(node->cascade_node, node);
835
/* Initialize cascade_node to do the operation we want. Note that we
836
use the SAME cascade node to do all foreign key operations of the
837
SQL DELETE: the table of the cascade node may change if there are
838
several child tables to the table where the delete is done! */
840
cascade = node->cascade_node;
842
cascade->table = table;
844
cascade->foreign = foreign;
847
&& (foreign->type & DICT_FOREIGN_ON_DELETE_CASCADE)) {
848
cascade->is_delete = TRUE;
850
cascade->is_delete = FALSE;
852
if (foreign->n_fields > cascade->update_n_fields) {
853
/* We have to make the update vector longer */
855
cascade->update = upd_create(foreign->n_fields,
857
cascade->update_n_fields = foreign->n_fields;
861
/* We do not allow cyclic cascaded updating (DELETE is allowed,
862
but not UPDATE) of the same table, as this can lead to an infinite
863
cycle. Check that we are not updating the same table which is
864
already being modified in this cascade chain. We have to check
865
this also because the modification of the indexes of a 'parent'
866
table may still be incomplete, and we must avoid seeing the indexes
867
of the parent table in an inconsistent state! */
869
if (!cascade->is_delete
870
&& row_ins_cascade_ancestor_updates_table(cascade, table)) {
872
/* We do not know if this would break foreign key
873
constraints, but play safe and return an error */
875
err = DB_ROW_IS_REFERENCED;
877
row_ins_foreign_report_err(
878
"Trying an update, possibly causing a cyclic"
880
"in the child table,", thr, foreign,
881
btr_pcur_get_rec(pcur), entry);
883
goto nonstandard_exit_func;
886
if (row_ins_cascade_n_ancestors(cascade) >= 15) {
887
err = DB_ROW_IS_REFERENCED;
889
row_ins_foreign_report_err(
890
"Trying a too deep cascaded delete or update\n",
891
thr, foreign, btr_pcur_get_rec(pcur), entry);
893
goto nonstandard_exit_func;
896
index = btr_pcur_get_btr_cur(pcur)->index;
898
ut_a(index == foreign->foreign_index);
900
rec = btr_pcur_get_rec(pcur);
902
if (index->type & DICT_CLUSTERED) {
903
/* pcur is already positioned in the clustered index of
909
/* We have to look for the record in the clustered index
910
in the child table */
912
clust_index = dict_table_get_first_index(table);
914
tmp_heap = mem_heap_create(256);
916
ref = row_build_row_ref(ROW_COPY_POINTERS, index, rec,
918
btr_pcur_open_with_no_init(clust_index, ref,
919
PAGE_CUR_LE, BTR_SEARCH_LEAF,
920
cascade->pcur, 0, mtr);
922
clust_rec = btr_pcur_get_rec(cascade->pcur);
924
if (!page_rec_is_user_rec(clust_rec)
925
|| btr_pcur_get_low_match(cascade->pcur)
926
< dict_index_get_n_unique(clust_index)) {
928
fputs("InnoDB: error in cascade of a foreign key op\n"
930
dict_index_name_print(stderr, trx, index);
933
"InnoDB: record ", stderr);
934
rec_print(stderr, rec, index);
936
"InnoDB: clustered record ", stderr);
937
rec_print(stderr, clust_rec, clust_index);
939
"InnoDB: Submit a detailed bug report to"
940
" http://bugs.mysql.com\n", stderr);
944
goto nonstandard_exit_func;
948
/* Set an X-lock on the row to delete or update in the child table */
950
err = lock_table(0, table, LOCK_IX, thr);
952
if (err == DB_SUCCESS) {
953
/* Here it suffices to use a LOCK_REC_NOT_GAP type lock;
954
we already have a normal shared lock on the appropriate
955
gap if the search criterion was not unique */
957
err = lock_clust_rec_read_check_and_lock_alt(
958
0, clust_rec, clust_index, LOCK_X, LOCK_REC_NOT_GAP,
962
if (err != DB_SUCCESS) {
964
goto nonstandard_exit_func;
967
if (rec_get_deleted_flag(clust_rec, dict_table_is_comp(table))) {
968
/* This can happen if there is a circular reference of
969
rows such that cascading delete comes to delete a row
970
already in the process of being delete marked */
973
goto nonstandard_exit_func;
977
&& (foreign->type & DICT_FOREIGN_ON_DELETE_SET_NULL))
979
&& (foreign->type & DICT_FOREIGN_ON_UPDATE_SET_NULL))) {
981
/* Build the appropriate update vector which sets
982
foreign->n_fields first fields in rec to SQL NULL */
984
update = cascade->update;
986
update->info_bits = 0;
987
update->n_fields = foreign->n_fields;
989
for (i = 0; i < foreign->n_fields; i++) {
990
(update->fields + i)->field_no
991
= dict_table_get_nth_col_pos(
993
dict_index_get_nth_col_no(index, i));
994
(update->fields + i)->exp = NULL;
995
(update->fields + i)->new_val.len = UNIV_SQL_NULL;
996
(update->fields + i)->new_val.data = NULL;
997
(update->fields + i)->extern_storage = FALSE;
1001
if (!node->is_delete
1002
&& (foreign->type & DICT_FOREIGN_ON_UPDATE_CASCADE)) {
1004
/* Build the appropriate update vector which sets changing
1005
foreign->n_fields first fields in rec to new values */
1007
upd_vec_heap = mem_heap_create(256);
1009
n_to_update = row_ins_cascade_calc_update_vec(node, foreign,
1011
if (n_to_update == ULINT_UNDEFINED) {
1012
err = DB_ROW_IS_REFERENCED;
1014
row_ins_foreign_report_err(
1015
"Trying a cascaded update where the"
1016
" updated value in the child\n"
1017
"table would not fit in the length"
1018
" of the column, or the value would\n"
1019
"be NULL and the column is"
1020
" declared as not NULL in the child table,",
1021
thr, foreign, btr_pcur_get_rec(pcur), entry);
1023
goto nonstandard_exit_func;
1026
if (cascade->update->n_fields == 0) {
1028
/* The update does not change any columns referred
1029
to in this foreign key constraint: no need to do
1034
goto nonstandard_exit_func;
1038
/* Store pcur position and initialize or store the cascade node
1039
pcur stored position */
1041
btr_pcur_store_position(pcur, mtr);
1043
if (index == clust_index) {
1044
btr_pcur_copy_stored_position(cascade->pcur, pcur);
1046
btr_pcur_store_position(cascade->pcur, mtr);
1051
ut_a(cascade->pcur->rel_pos == BTR_PCUR_ON);
1053
cascade->state = UPD_NODE_UPDATE_CLUSTERED;
1055
err = row_update_cascade_for_mysql(thr, cascade,
1056
foreign->foreign_table);
1058
if (foreign->foreign_table->n_foreign_key_checks_running == 0) {
1060
"InnoDB: error: table %s has the counter 0"
1061
" though there is\n"
1062
"InnoDB: a FOREIGN KEY check running on it.\n",
1063
foreign->foreign_table->name);
1066
/* Release the data dictionary latch for a while, so that we do not
1067
starve other threads from doing CREATE TABLE etc. if we have a huge
1068
cascaded operation running. The counter n_foreign_key_checks_running
1069
will prevent other users from dropping or ALTERing the table when we
1070
release the latch. */
1072
row_mysql_unfreeze_data_dictionary(thr_get_trx(thr));
1073
row_mysql_freeze_data_dictionary(thr_get_trx(thr));
1077
/* Restore pcur position */
1079
btr_pcur_restore_position(BTR_SEARCH_LEAF, pcur, mtr);
1082
mem_heap_free(tmp_heap);
1086
mem_heap_free(upd_vec_heap);
1091
nonstandard_exit_func:
1093
mem_heap_free(tmp_heap);
1097
mem_heap_free(upd_vec_heap);
1100
btr_pcur_store_position(pcur, mtr);
1105
btr_pcur_restore_position(BTR_SEARCH_LEAF, pcur, mtr);
1110
/*************************************************************************
1111
Sets a shared lock on a record. Used in locking possible duplicate key
1112
records and also in checking foreign key constraints. */
1115
row_ins_set_shared_rec_lock(
1116
/*========================*/
1117
/* out: DB_SUCCESS or error code */
1118
ulint type, /* in: LOCK_ORDINARY, LOCK_GAP, or
1119
LOCK_REC_NOT_GAP type lock */
1120
rec_t* rec, /* in: record */
1121
dict_index_t* index, /* in: index */
1122
const ulint* offsets,/* in: rec_get_offsets(rec, index) */
1123
que_thr_t* thr) /* in: query thread */
1127
ut_ad(rec_offs_validate(rec, index, offsets));
1129
if (index->type & DICT_CLUSTERED) {
1130
err = lock_clust_rec_read_check_and_lock(
1131
0, rec, index, offsets, LOCK_S, type, thr);
1133
err = lock_sec_rec_read_check_and_lock(
1134
0, rec, index, offsets, LOCK_S, type, thr);
1140
#ifndef UNIV_HOTBACKUP
1141
/*************************************************************************
1142
Sets a exclusive lock on a record. Used in locking possible duplicate key
1146
row_ins_set_exclusive_rec_lock(
1147
/*===========================*/
1148
/* out: DB_SUCCESS or error code */
1149
ulint type, /* in: LOCK_ORDINARY, LOCK_GAP, or
1150
LOCK_REC_NOT_GAP type lock */
1151
rec_t* rec, /* in: record */
1152
dict_index_t* index, /* in: index */
1153
const ulint* offsets,/* in: rec_get_offsets(rec, index) */
1154
que_thr_t* thr) /* in: query thread */
1158
ut_ad(rec_offs_validate(rec, index, offsets));
1160
if (index->type & DICT_CLUSTERED) {
1161
err = lock_clust_rec_read_check_and_lock(
1162
0, rec, index, offsets, LOCK_X, type, thr);
1164
err = lock_sec_rec_read_check_and_lock(
1165
0, rec, index, offsets, LOCK_X, type, thr);
1170
#endif /* !UNIV_HOTBACKUP */
1172
/*******************************************************************
1173
Checks if foreign key constraint fails for an index entry. Sets shared locks
1174
which lock either the success or the failure of the constraint. NOTE that
1175
the caller must have a shared latch on dict_operation_lock. */
1178
row_ins_check_foreign_constraint(
1179
/*=============================*/
1181
DB_NO_REFERENCED_ROW,
1182
or DB_ROW_IS_REFERENCED */
1183
ibool check_ref,/* in: TRUE if we want to check that
1184
the referenced table is ok, FALSE if we
1185
want to to check the foreign key table */
1186
dict_foreign_t* foreign,/* in: foreign constraint; NOTE that the
1187
tables mentioned in it must be in the
1188
dictionary cache if they exist at all */
1189
dict_table_t* table, /* in: if check_ref is TRUE, then the foreign
1190
table, else the referenced table */
1191
dtuple_t* entry, /* in: index entry for index */
1192
que_thr_t* thr) /* in: query thread */
1194
upd_node_t* upd_node;
1195
dict_table_t* check_table;
1196
dict_index_t* check_index;
1205
trx_t* trx = thr_get_trx(thr);
1206
mem_heap_t* heap = NULL;
1207
ulint offsets_[REC_OFFS_NORMAL_SIZE];
1208
ulint* offsets = offsets_;
1209
*offsets_ = (sizeof offsets_) / sizeof *offsets_;
1212
#ifdef UNIV_SYNC_DEBUG
1213
ut_ad(rw_lock_own(&dict_operation_lock, RW_LOCK_SHARED));
1214
#endif /* UNIV_SYNC_DEBUG */
1218
if (trx->check_foreigns == FALSE) {
1219
/* The user has suppressed foreign key checks currently for
1224
/* If any of the foreign key fields in entry is SQL NULL, we
1225
suppress the foreign key check: this is compatible with Oracle,
1228
for (i = 0; i < foreign->n_fields; i++) {
1229
if (UNIV_SQL_NULL == dfield_get_len(
1230
dtuple_get_nth_field(entry, i))) {
1236
if (que_node_get_type(thr->run_node) == QUE_NODE_UPDATE) {
1237
upd_node = thr->run_node;
1239
if (!(upd_node->is_delete) && upd_node->foreign == foreign) {
1240
/* If a cascaded update is done as defined by a
1241
foreign key constraint, do not check that
1242
constraint for the child row. In ON UPDATE CASCADE
1243
the update of the parent row is only half done when
1244
we come here: if we would check the constraint here
1245
for the child row it would fail.
1247
A QUESTION remains: if in the child table there are
1248
several constraints which refer to the same parent
1249
table, we should merge all updates to the child as
1250
one update? And the updates can be contradictory!
1251
Currently we just perform the update associated
1252
with each foreign key constraint, one after
1253
another, and the user has problems predicting in
1254
which order they are performed. */
1261
check_table = foreign->referenced_table;
1262
check_index = foreign->referenced_index;
1264
check_table = foreign->foreign_table;
1265
check_index = foreign->foreign_index;
1268
if (check_table == NULL || check_table->ibd_file_missing) {
1270
FILE* ef = dict_foreign_err_file;
1272
row_ins_set_detailed(trx, foreign);
1274
mutex_enter(&dict_foreign_err_mutex);
1276
ut_print_timestamp(ef);
1277
fputs(" Transaction:\n", ef);
1278
trx_print(ef, trx, 600);
1279
fputs("Foreign key constraint fails for table ", ef);
1280
ut_print_name(ef, trx, TRUE,
1281
foreign->foreign_table_name);
1283
dict_print_info_on_foreign_key_in_create_format(
1284
ef, trx, foreign, TRUE);
1285
fputs("\nTrying to add to index ", ef);
1286
ut_print_name(ef, trx, FALSE,
1287
foreign->foreign_index->name);
1288
fputs(" tuple:\n", ef);
1289
dtuple_print(ef, entry);
1290
fputs("\nBut the parent table ", ef);
1291
ut_print_name(ef, trx, TRUE,
1292
foreign->referenced_table_name);
1293
fputs("\nor its .ibd file does"
1294
" not currently exist!\n", ef);
1295
mutex_exit(&dict_foreign_err_mutex);
1297
err = DB_NO_REFERENCED_ROW;
1306
if (check_table != table) {
1307
/* We already have a LOCK_IX on table, but not necessarily
1310
err = lock_table(0, check_table, LOCK_IS, thr);
1312
if (err != DB_SUCCESS) {
1314
goto do_possible_lock_wait;
1320
/* Store old value on n_fields_cmp */
1322
n_fields_cmp = dtuple_get_n_fields_cmp(entry);
1324
dtuple_set_n_fields_cmp(entry, foreign->n_fields);
1326
btr_pcur_open(check_index, entry, PAGE_CUR_GE,
1327
BTR_SEARCH_LEAF, &pcur, &mtr);
1329
/* Scan index records and check if there is a matching record */
1332
rec = btr_pcur_get_rec(&pcur);
1334
if (page_rec_is_infimum(rec)) {
1339
offsets = rec_get_offsets(rec, check_index,
1340
offsets, ULINT_UNDEFINED, &heap);
1342
if (page_rec_is_supremum(rec)) {
1344
err = row_ins_set_shared_rec_lock(
1345
LOCK_ORDINARY, rec, check_index, offsets, thr);
1346
if (err != DB_SUCCESS) {
1354
cmp = cmp_dtuple_rec(entry, rec, offsets);
1357
if (rec_get_deleted_flag(rec,
1358
rec_offs_comp(offsets))) {
1359
err = row_ins_set_shared_rec_lock(
1360
LOCK_ORDINARY, rec, check_index,
1362
if (err != DB_SUCCESS) {
1367
/* Found a matching record. Lock only
1368
a record because we can allow inserts
1371
err = row_ins_set_shared_rec_lock(
1372
LOCK_REC_NOT_GAP, rec, check_index,
1375
if (err != DB_SUCCESS) {
1384
} else if (foreign->type != 0) {
1385
/* There is an ON UPDATE or ON DELETE
1386
condition: check them in a separate
1389
err = row_ins_foreign_check_on_constraint(
1390
thr, foreign, &pcur, entry,
1392
if (err != DB_SUCCESS) {
1393
/* Since reporting a plain
1394
"duplicate key" error
1395
message to the user in
1396
cases where a long CASCADE
1397
operation would lead to a
1398
duplicate key in some
1400
confusing, map duplicate
1401
key errors resulting from
1403
separate error code. */
1405
if (err == DB_DUPLICATE_KEY) {
1406
err = DB_FOREIGN_DUPLICATE_KEY;
1412
row_ins_foreign_report_err(
1413
"Trying to delete or update",
1414
thr, foreign, rec, entry);
1416
err = DB_ROW_IS_REFERENCED;
1423
err = row_ins_set_shared_rec_lock(
1424
LOCK_GAP, rec, check_index, offsets, thr);
1425
if (err != DB_SUCCESS) {
1431
err = DB_NO_REFERENCED_ROW;
1432
row_ins_foreign_report_add_err(
1433
trx, foreign, rec, entry);
1443
moved = btr_pcur_move_to_next(&pcur, &mtr);
1447
rec = btr_pcur_get_rec(&pcur);
1448
row_ins_foreign_report_add_err(
1449
trx, foreign, rec, entry);
1450
err = DB_NO_REFERENCED_ROW;
1459
btr_pcur_close(&pcur);
1463
/* Restore old value */
1464
dtuple_set_n_fields_cmp(entry, n_fields_cmp);
1466
do_possible_lock_wait:
1467
if (err == DB_LOCK_WAIT) {
1468
trx->error_state = err;
1470
que_thr_stop_for_mysql(thr);
1472
srv_suspend_mysql_thread(thr);
1474
if (trx->error_state == DB_SUCCESS) {
1479
err = trx->error_state;
1483
if (UNIV_LIKELY_NULL(heap)) {
1484
mem_heap_free(heap);
1489
/*******************************************************************
1490
Checks if foreign key constraints fail for an index entry. If index
1491
is not mentioned in any constraint, this function does nothing,
1492
Otherwise does searches to the indexes of referenced tables and
1493
sets shared locks which lock either the success or the failure of
1497
row_ins_check_foreign_constraints(
1498
/*==============================*/
1499
/* out: DB_SUCCESS or error code */
1500
dict_table_t* table, /* in: table */
1501
dict_index_t* index, /* in: index */
1502
dtuple_t* entry, /* in: index entry for index */
1503
que_thr_t* thr) /* in: query thread */
1505
dict_foreign_t* foreign;
1508
ibool got_s_lock = FALSE;
1510
trx = thr_get_trx(thr);
1512
foreign = UT_LIST_GET_FIRST(table->foreign_list);
1515
if (foreign->foreign_index == index) {
1517
if (foreign->referenced_table == NULL) {
1518
dict_table_get(foreign->referenced_table_name,
1522
if (0 == trx->dict_operation_lock_mode) {
1525
row_mysql_freeze_data_dictionary(trx);
1528
if (foreign->referenced_table) {
1529
mutex_enter(&(dict_sys->mutex));
1531
(foreign->referenced_table
1532
->n_foreign_key_checks_running)++;
1534
mutex_exit(&(dict_sys->mutex));
1537
/* NOTE that if the thread ends up waiting for a lock
1538
we will release dict_operation_lock temporarily!
1539
But the counter on the table protects the referenced
1540
table from being dropped while the check is running. */
1542
err = row_ins_check_foreign_constraint(
1543
TRUE, foreign, table, entry, thr);
1545
if (foreign->referenced_table) {
1546
mutex_enter(&(dict_sys->mutex));
1548
ut_a(foreign->referenced_table
1549
->n_foreign_key_checks_running > 0);
1550
(foreign->referenced_table
1551
->n_foreign_key_checks_running)--;
1553
mutex_exit(&(dict_sys->mutex));
1557
row_mysql_unfreeze_data_dictionary(trx);
1560
if (err != DB_SUCCESS) {
1565
foreign = UT_LIST_GET_NEXT(foreign_list, foreign);
1571
#ifndef UNIV_HOTBACKUP
1572
/*******************************************************************
1573
Checks if a unique key violation to rec would occur at the index entry
1577
row_ins_dupl_error_with_rec(
1578
/*========================*/
1579
/* out: TRUE if error */
1580
rec_t* rec, /* in: user record; NOTE that we assume
1581
that the caller already has a record lock on
1583
dtuple_t* entry, /* in: entry to insert */
1584
dict_index_t* index, /* in: index */
1585
const ulint* offsets)/* in: rec_get_offsets(rec, index) */
1587
ulint matched_fields;
1588
ulint matched_bytes;
1592
ut_ad(rec_offs_validate(rec, index, offsets));
1594
n_unique = dict_index_get_n_unique(index);
1599
cmp_dtuple_rec_with_match(entry, rec, offsets,
1600
&matched_fields, &matched_bytes);
1602
if (matched_fields < n_unique) {
1607
/* In a unique secondary index we allow equal key values if they
1608
contain SQL NULLs */
1610
if (!(index->type & DICT_CLUSTERED)) {
1612
for (i = 0; i < n_unique; i++) {
1613
if (UNIV_SQL_NULL == dfield_get_len(
1614
dtuple_get_nth_field(entry, i))) {
1621
return(!rec_get_deleted_flag(rec, rec_offs_comp(offsets)));
1623
#endif /* !UNIV_HOTBACKUP */
1625
/*******************************************************************
1626
Scans a unique non-clustered index at a given index entry to determine
1627
whether a uniqueness violation has occurred for the key value of the entry.
1628
Set shared locks on possible duplicate records. */
1631
row_ins_scan_sec_index_for_duplicate(
1632
/*=================================*/
1633
/* out: DB_SUCCESS, DB_DUPLICATE_KEY, or
1635
dict_index_t* index, /* in: non-clustered unique index */
1636
dtuple_t* entry, /* in: index entry */
1637
que_thr_t* thr) /* in: query thread */
1639
#ifndef UNIV_HOTBACKUP
1646
ulint err = DB_SUCCESS;
1648
unsigned allow_duplicates;
1650
mem_heap_t* heap = NULL;
1651
ulint offsets_[REC_OFFS_NORMAL_SIZE];
1652
ulint* offsets = offsets_;
1653
*offsets_ = (sizeof offsets_) / sizeof *offsets_;
1655
n_unique = dict_index_get_n_unique(index);
1657
/* If the secondary index is unique, but one of the fields in the
1658
n_unique first fields is NULL, a unique key violation cannot occur,
1659
since we define NULL != NULL in this case */
1661
for (i = 0; i < n_unique; i++) {
1662
if (UNIV_SQL_NULL == dfield_get_len(
1663
dtuple_get_nth_field(entry, i))) {
1671
/* Store old value on n_fields_cmp */
1673
n_fields_cmp = dtuple_get_n_fields_cmp(entry);
1675
dtuple_set_n_fields_cmp(entry, dict_index_get_n_unique(index));
1677
btr_pcur_open(index, entry, PAGE_CUR_GE, BTR_SEARCH_LEAF, &pcur, &mtr);
1679
allow_duplicates = thr_get_trx(thr)->duplicates & TRX_DUP_IGNORE;
1681
/* Scan index records and check if there is a duplicate */
1684
rec = btr_pcur_get_rec(&pcur);
1686
if (page_rec_is_infimum(rec)) {
1691
offsets = rec_get_offsets(rec, index, offsets,
1692
ULINT_UNDEFINED, &heap);
1694
if (allow_duplicates) {
1696
/* If the SQL-query will update or replace
1697
duplicate key we will take X-lock for
1698
duplicates ( REPLACE, LOAD DATAFILE REPLACE,
1699
INSERT ON DUPLICATE KEY UPDATE). */
1701
err = row_ins_set_exclusive_rec_lock(
1702
LOCK_ORDINARY, rec, index, offsets, thr);
1705
err = row_ins_set_shared_rec_lock(
1706
LOCK_ORDINARY, rec, index, offsets, thr);
1709
if (err != DB_SUCCESS) {
1714
if (page_rec_is_supremum(rec)) {
1719
cmp = cmp_dtuple_rec(entry, rec, offsets);
1722
if (row_ins_dupl_error_with_rec(rec, entry,
1724
err = DB_DUPLICATE_KEY;
1726
thr_get_trx(thr)->error_info = index;
1738
moved = btr_pcur_move_to_next(&pcur, &mtr);
1745
if (UNIV_LIKELY_NULL(heap)) {
1746
mem_heap_free(heap);
1750
/* Restore old value */
1751
dtuple_set_n_fields_cmp(entry, n_fields_cmp);
1754
#else /* UNIV_HOTBACKUP */
1755
/* This function depends on MySQL code that is not included in
1756
InnoDB Hot Backup builds. Besides, this function should never
1757
be called in InnoDB Hot Backup. */
1760
#endif /* UNIV_HOTBACKUP */
1763
/*******************************************************************
1764
Checks if a unique key violation error would occur at an index entry
1765
insert. Sets shared locks on possible duplicate records. Works only
1766
for a clustered index! */
1769
row_ins_duplicate_error_in_clust(
1770
/*=============================*/
1771
/* out: DB_SUCCESS if no error,
1772
DB_DUPLICATE_KEY if error, DB_LOCK_WAIT if we
1773
have to wait for a lock on a possible
1775
btr_cur_t* cursor, /* in: B-tree cursor */
1776
dtuple_t* entry, /* in: entry to insert */
1777
que_thr_t* thr, /* in: query thread */
1778
mtr_t* mtr) /* in: mtr */
1780
#ifndef UNIV_HOTBACKUP
1784
trx_t* trx = thr_get_trx(thr);
1785
mem_heap_t*heap = NULL;
1786
ulint offsets_[REC_OFFS_NORMAL_SIZE];
1787
ulint* offsets = offsets_;
1788
*offsets_ = (sizeof offsets_) / sizeof *offsets_;
1792
ut_a(cursor->index->type & DICT_CLUSTERED);
1793
ut_ad(cursor->index->type & DICT_UNIQUE);
1795
/* NOTE: For unique non-clustered indexes there may be any number
1796
of delete marked records with the same value for the non-clustered
1797
index key (remember multiversioning), and which differ only in
1798
the row refererence part of the index record, containing the
1799
clustered index key fields. For such a secondary index record,
1800
to avoid race condition, we must FIRST do the insertion and after
1801
that check that the uniqueness condition is not breached! */
1803
/* NOTE: A problem is that in the B-tree node pointers on an
1804
upper level may match more to the entry than the actual existing
1805
user records on the leaf level. So, even if low_match would suggest
1806
that a duplicate key violation may occur, this may not be the case. */
1808
n_unique = dict_index_get_n_unique(cursor->index);
1810
if (cursor->low_match >= n_unique) {
1812
rec = btr_cur_get_rec(cursor);
1814
if (!page_rec_is_infimum(rec)) {
1815
offsets = rec_get_offsets(rec, cursor->index, offsets,
1816
ULINT_UNDEFINED, &heap);
1818
/* We set a lock on the possible duplicate: this
1819
is needed in logical logging of MySQL to make
1820
sure that in roll-forward we get the same duplicate
1821
errors as in original execution */
1823
if (trx->duplicates & TRX_DUP_IGNORE) {
1825
/* If the SQL-query will update or replace
1826
duplicate key we will take X-lock for
1827
duplicates ( REPLACE, LOAD DATAFILE REPLACE,
1828
INSERT ON DUPLICATE KEY UPDATE). */
1830
err = row_ins_set_exclusive_rec_lock(
1831
LOCK_REC_NOT_GAP, rec,
1832
cursor->index, offsets, thr);
1835
err = row_ins_set_shared_rec_lock(
1836
LOCK_REC_NOT_GAP, rec,
1837
cursor->index, offsets, thr);
1840
if (err != DB_SUCCESS) {
1844
if (row_ins_dupl_error_with_rec(
1845
rec, entry, cursor->index, offsets)) {
1846
trx->error_info = cursor->index;
1847
err = DB_DUPLICATE_KEY;
1853
if (cursor->up_match >= n_unique) {
1855
rec = page_rec_get_next(btr_cur_get_rec(cursor));
1857
if (!page_rec_is_supremum(rec)) {
1858
offsets = rec_get_offsets(rec, cursor->index, offsets,
1859
ULINT_UNDEFINED, &heap);
1861
if (trx->duplicates & TRX_DUP_IGNORE) {
1863
/* If the SQL-query will update or replace
1864
duplicate key we will take X-lock for
1865
duplicates ( REPLACE, LOAD DATAFILE REPLACE,
1866
INSERT ON DUPLICATE KEY UPDATE). */
1868
err = row_ins_set_exclusive_rec_lock(
1869
LOCK_REC_NOT_GAP, rec,
1870
cursor->index, offsets, thr);
1873
err = row_ins_set_shared_rec_lock(
1874
LOCK_REC_NOT_GAP, rec,
1875
cursor->index, offsets, thr);
1878
if (err != DB_SUCCESS) {
1882
if (row_ins_dupl_error_with_rec(
1883
rec, entry, cursor->index, offsets)) {
1884
trx->error_info = cursor->index;
1885
err = DB_DUPLICATE_KEY;
1890
ut_a(!(cursor->index->type & DICT_CLUSTERED));
1891
/* This should never happen */
1896
if (UNIV_LIKELY_NULL(heap)) {
1897
mem_heap_free(heap);
1900
#else /* UNIV_HOTBACKUP */
1901
/* This function depends on MySQL code that is not included in
1902
InnoDB Hot Backup builds. Besides, this function should never
1903
be called in InnoDB Hot Backup. */
1906
#endif /* UNIV_HOTBACKUP */
1909
/*******************************************************************
1910
Checks if an index entry has long enough common prefix with an existing
1911
record so that the intended insert of the entry must be changed to a modify of
1912
the existing record. In the case of a clustered index, the prefix must be
1913
n_unique fields long, and in the case of a secondary index, all fields must be
1917
row_ins_must_modify(
1918
/*================*/
1919
/* out: 0 if no update, ROW_INS_PREV if
1920
previous should be updated; currently we
1921
do the search so that only the low_match
1922
record can match enough to the search tuple,
1923
not the next record */
1924
btr_cur_t* cursor) /* in: B-tree cursor */
1929
/* NOTE: (compare to the note in row_ins_duplicate_error) Because node
1930
pointers on upper levels of the B-tree may match more to entry than
1931
to actual user records on the leaf level, we have to check if the
1932
candidate record is actually a user record. In a clustered index
1933
node pointers contain index->n_unique first fields, and in the case
1934
of a secondary index, all fields of the index. */
1936
enough_match = dict_index_get_n_unique_in_tree(cursor->index);
1938
if (cursor->low_match >= enough_match) {
1940
rec = btr_cur_get_rec(cursor);
1942
if (!page_rec_is_infimum(rec)) {
1944
return(ROW_INS_PREV);
1951
/*******************************************************************
1952
Tries to insert an index entry to an index. If the index is clustered
1953
and a record with the same unique key is found, the other record is
1954
necessarily marked deleted by a committed transaction, or a unique key
1955
violation error occurs. The delete marked record is then updated to an
1956
existing record, and we must write an undo log record on the delete
1957
marked record. If the index is secondary, and a record with exactly the
1958
same fields is found, the other record is necessarily marked deleted.
1959
It is then unmarked. Otherwise, the entry is just inserted to the index. */
1962
row_ins_index_entry_low(
1963
/*====================*/
1964
/* out: DB_SUCCESS, DB_LOCK_WAIT, DB_FAIL
1965
if pessimistic retry needed, or error code */
1966
ulint mode, /* in: BTR_MODIFY_LEAF or BTR_MODIFY_TREE,
1967
depending on whether we wish optimistic or
1968
pessimistic descent down the index tree */
1969
dict_index_t* index, /* in: index */
1970
dtuple_t* entry, /* in: index entry to insert */
1971
ulint* ext_vec,/* in: array containing field numbers of
1972
externally stored fields in entry, or NULL */
1973
ulint n_ext_vec,/* in: number of fields in ext_vec */
1974
que_thr_t* thr) /* in: query thread */
1977
ulint ignore_sec_unique = 0;
1978
ulint modify = 0; /* remove warning */
1983
big_rec_t* big_rec = NULL;
1985
mem_heap_t* heap = NULL;
1986
ulint offsets_[REC_OFFS_NORMAL_SIZE];
1987
ulint* offsets = offsets_;
1988
*offsets_ = (sizeof offsets_) / sizeof *offsets_;
1996
/* Note that we use PAGE_CUR_LE as the search mode, because then
1997
the function will return in both low_match and up_match of the
1998
cursor sensible values */
2000
if (!(thr_get_trx(thr)->check_unique_secondary)) {
2001
ignore_sec_unique = BTR_IGNORE_SEC_UNIQUE;
2004
btr_cur_search_to_nth_level(index, 0, entry, PAGE_CUR_LE,
2005
mode | BTR_INSERT | ignore_sec_unique,
2008
if (cursor.flag == BTR_CUR_INSERT_TO_IBUF) {
2009
/* The insertion was made to the insert buffer already during
2010
the search: we are done */
2019
page_t* page = btr_cur_get_page(&cursor);
2020
rec_t* first_rec = page_rec_get_next(
2021
page_get_infimum_rec(page));
2023
if (UNIV_LIKELY(first_rec != page_get_supremum_rec(page))) {
2024
ut_a(rec_get_n_fields(first_rec, index)
2025
== dtuple_get_n_fields(entry));
2030
n_unique = dict_index_get_n_unique(index);
2032
if (index->type & DICT_UNIQUE && (cursor.up_match >= n_unique
2033
|| cursor.low_match >= n_unique)) {
2035
if (index->type & DICT_CLUSTERED) {
2036
/* Note that the following may return also
2039
err = row_ins_duplicate_error_in_clust(
2040
&cursor, entry, thr, &mtr);
2041
if (err != DB_SUCCESS) {
2047
err = row_ins_scan_sec_index_for_duplicate(
2051
if (err != DB_SUCCESS) {
2056
/* We did not find a duplicate and we have now
2057
locked with s-locks the necessary records to
2058
prevent any insertion of a duplicate by another
2059
transaction. Let us now reposition the cursor and
2060
continue the insertion. */
2062
btr_cur_search_to_nth_level(index, 0, entry,
2069
modify = row_ins_must_modify(&cursor);
2072
/* There is already an index entry with a long enough common
2073
prefix, we must convert the insert into a modify of an
2076
if (modify == ROW_INS_NEXT) {
2077
rec = page_rec_get_next(btr_cur_get_rec(&cursor));
2079
btr_cur_position(index, rec, &cursor);
2082
if (index->type & DICT_CLUSTERED) {
2083
err = row_ins_clust_index_entry_by_modify(
2084
mode, &cursor, &big_rec, entry,
2085
ext_vec, n_ext_vec, thr, &mtr);
2087
err = row_ins_sec_index_entry_by_modify(
2088
mode, &cursor, entry, thr, &mtr);
2092
if (mode == BTR_MODIFY_LEAF) {
2093
err = btr_cur_optimistic_insert(
2094
0, &cursor, entry, &insert_rec, &big_rec,
2097
ut_a(mode == BTR_MODIFY_TREE);
2098
if (buf_LRU_buf_pool_running_out()) {
2100
err = DB_LOCK_TABLE_FULL;
2104
err = btr_cur_pessimistic_insert(
2105
0, &cursor, entry, &insert_rec, &big_rec,
2109
if (err == DB_SUCCESS) {
2111
rec_set_field_extern_bits(insert_rec, index,
2125
btr_cur_search_to_nth_level(index, 0, entry, PAGE_CUR_LE,
2126
BTR_MODIFY_TREE, &cursor, 0, &mtr);
2127
rec = btr_cur_get_rec(&cursor);
2128
offsets = rec_get_offsets(rec, index, offsets,
2129
ULINT_UNDEFINED, &heap);
2131
err = btr_store_big_rec_extern_fields(index, rec,
2132
offsets, big_rec, &mtr);
2135
dtuple_big_rec_free(big_rec);
2137
dtuple_convert_back_big_rec(index, entry, big_rec);
2143
if (UNIV_LIKELY_NULL(heap)) {
2144
mem_heap_free(heap);
2149
/*******************************************************************
2150
Inserts an index entry to index. Tries first optimistic, then pessimistic
2151
descent down the tree. If the entry matches enough to a delete marked record,
2152
performs the insert by updating or delete unmarking the delete marked
2156
row_ins_index_entry(
2157
/*================*/
2158
/* out: DB_SUCCESS, DB_LOCK_WAIT,
2159
DB_DUPLICATE_KEY, or some other error code */
2160
dict_index_t* index, /* in: index */
2161
dtuple_t* entry, /* in: index entry to insert */
2162
ulint* ext_vec,/* in: array containing field numbers of
2163
externally stored fields in entry, or NULL */
2164
ulint n_ext_vec,/* in: number of fields in ext_vec */
2165
que_thr_t* thr) /* in: query thread */
2169
if (UT_LIST_GET_FIRST(index->table->foreign_list)) {
2170
err = row_ins_check_foreign_constraints(index->table, index,
2172
if (err != DB_SUCCESS) {
2178
/* Try first optimistic descent to the B-tree */
2180
err = row_ins_index_entry_low(BTR_MODIFY_LEAF, index, entry,
2181
ext_vec, n_ext_vec, thr);
2182
if (err != DB_FAIL) {
2187
/* Try then pessimistic descent to the B-tree */
2189
err = row_ins_index_entry_low(BTR_MODIFY_TREE, index, entry,
2190
ext_vec, n_ext_vec, thr);
2194
/***************************************************************
2195
Sets the values of the dtuple fields in entry from the values of appropriate
2199
row_ins_index_entry_set_vals(
2200
/*=========================*/
2201
dict_index_t* index, /* in: index */
2202
dtuple_t* entry, /* in: index entry to make */
2203
dtuple_t* row) /* in: row */
2205
dict_field_t* ind_field;
2207
dfield_t* row_field;
2211
ut_ad(entry && row);
2213
n_fields = dtuple_get_n_fields(entry);
2215
for (i = 0; i < n_fields; i++) {
2216
field = dtuple_get_nth_field(entry, i);
2217
ind_field = dict_index_get_nth_field(index, i);
2219
row_field = dtuple_get_nth_field(row, ind_field->col->ind);
2221
/* Check column prefix indexes */
2222
if (ind_field->prefix_len > 0
2223
&& dfield_get_len(row_field) != UNIV_SQL_NULL) {
2225
const dict_col_t* col
2226
= dict_field_get_col(ind_field);
2228
field->len = dtype_get_at_most_n_mbchars(
2229
col->prtype, col->mbminlen, col->mbmaxlen,
2230
ind_field->prefix_len,
2231
row_field->len, row_field->data);
2233
field->len = row_field->len;
2236
field->data = row_field->data;
2240
/***************************************************************
2241
Inserts a single index entry to the table. */
2244
row_ins_index_entry_step(
2245
/*=====================*/
2246
/* out: DB_SUCCESS if operation successfully
2247
completed, else error code or DB_LOCK_WAIT */
2248
ins_node_t* node, /* in: row insert node */
2249
que_thr_t* thr) /* in: query thread */
2253
ut_ad(dtuple_check_typed(node->row));
2255
row_ins_index_entry_set_vals(node->index, node->entry, node->row);
2257
ut_ad(dtuple_check_typed(node->entry));
2259
err = row_ins_index_entry(node->index, node->entry, NULL, 0, thr);
2264
/***************************************************************
2265
Allocates a row id for row and inits the node->index field. */
2268
row_ins_alloc_row_id_step(
2269
/*======================*/
2270
ins_node_t* node) /* in: row insert node */
2274
ut_ad(node->state == INS_NODE_ALLOC_ROW_ID);
2276
if (dict_table_get_first_index(node->table)->type & DICT_UNIQUE) {
2278
/* No row id is stored if the clustered index is unique */
2283
/* Fill in row id value to row */
2285
row_id = dict_sys_get_new_row_id();
2287
dict_sys_write_row_id(node->row_id_buf, row_id);
2290
/***************************************************************
2291
Gets a row to insert from the values list. */
2294
row_ins_get_row_from_values(
2295
/*========================*/
2296
ins_node_t* node) /* in: row insert node */
2298
que_node_t* list_node;
2303
/* The field values are copied in the buffers of the select node and
2304
it is safe to use them until we fetch from select again: therefore
2305
we can just copy the pointers */
2310
list_node = node->values_list;
2313
eval_exp(list_node);
2315
dfield = dtuple_get_nth_field(row, i);
2316
dfield_copy_data(dfield, que_node_get_val(list_node));
2319
list_node = que_node_get_next(list_node);
2323
/***************************************************************
2324
Gets a row to insert from the select list. */
2327
row_ins_get_row_from_select(
2328
/*========================*/
2329
ins_node_t* node) /* in: row insert node */
2331
que_node_t* list_node;
2336
/* The field values are copied in the buffers of the select node and
2337
it is safe to use them until we fetch from select again: therefore
2338
we can just copy the pointers */
2343
list_node = node->select->select_list;
2346
dfield = dtuple_get_nth_field(row, i);
2347
dfield_copy_data(dfield, que_node_get_val(list_node));
2350
list_node = que_node_get_next(list_node);
2354
/***************************************************************
2355
Inserts a row to a table. */
2360
/* out: DB_SUCCESS if operation successfully
2361
completed, else error code or DB_LOCK_WAIT */
2362
ins_node_t* node, /* in: row insert node */
2363
que_thr_t* thr) /* in: query thread */
2369
if (node->state == INS_NODE_ALLOC_ROW_ID) {
2371
row_ins_alloc_row_id_step(node);
2373
node->index = dict_table_get_first_index(node->table);
2374
node->entry = UT_LIST_GET_FIRST(node->entry_list);
2376
if (node->ins_type == INS_SEARCHED) {
2378
row_ins_get_row_from_select(node);
2380
} else if (node->ins_type == INS_VALUES) {
2382
row_ins_get_row_from_values(node);
2385
node->state = INS_NODE_INSERT_ENTRIES;
2388
ut_ad(node->state == INS_NODE_INSERT_ENTRIES);
2390
while (node->index != NULL) {
2391
err = row_ins_index_entry_step(node, thr);
2393
if (err != DB_SUCCESS) {
2398
node->index = dict_table_get_next_index(node->index);
2399
node->entry = UT_LIST_GET_NEXT(tuple_list, node->entry);
2402
ut_ad(node->entry == NULL);
2404
node->state = INS_NODE_ALLOC_ROW_ID;
2409
/***************************************************************
2410
Inserts a row to a table. This is a high-level function used in SQL execution
2416
/* out: query thread to run next or NULL */
2417
que_thr_t* thr) /* in: query thread */
2421
sel_node_t* sel_node;
2427
trx = thr_get_trx(thr);
2429
trx_start_if_not_started(trx);
2431
node = thr->run_node;
2433
ut_ad(que_node_get_type(node) == QUE_NODE_INSERT);
2435
parent = que_node_get_parent(node);
2436
sel_node = node->select;
2438
if (thr->prev_node == parent) {
2439
node->state = INS_NODE_SET_IX_LOCK;
2442
/* If this is the first time this node is executed (or when
2443
execution resumes after wait for the table IX lock), set an
2444
IX lock on the table and reset the possible select node. MySQL's
2445
partitioned table code may also call an insert within the same
2446
SQL statement AFTER it has used this table handle to do a search.
2447
This happens, for example, when a row update moves it to another
2448
partition. In that case, we have already set the IX lock on the
2449
table during the search operation, and there is no need to set
2450
it again here. But we must write trx->id to node->trx_id_buf. */
2452
trx_write_trx_id(node->trx_id_buf, trx->id);
2454
if (node->state == INS_NODE_SET_IX_LOCK) {
2456
/* It may be that the current session has not yet started
2457
its transaction, or it has been committed: */
2459
if (UT_DULINT_EQ(trx->id, node->trx_id)) {
2460
/* No need to do IX-locking */
2465
err = lock_table(0, node->table, LOCK_IX, thr);
2467
if (err != DB_SUCCESS) {
2469
goto error_handling;
2472
node->trx_id = trx->id;
2474
node->state = INS_NODE_ALLOC_ROW_ID;
2476
if (node->ins_type == INS_SEARCHED) {
2477
/* Reset the cursor */
2478
sel_node->state = SEL_NODE_OPEN;
2480
/* Fetch a row to insert */
2482
thr->run_node = sel_node;
2488
if ((node->ins_type == INS_SEARCHED)
2489
&& (sel_node->state != SEL_NODE_FETCH)) {
2491
ut_ad(sel_node->state == SEL_NODE_NO_MORE_ROWS);
2493
/* No more rows to insert */
2494
thr->run_node = parent;
2499
/* DO THE CHECKS OF THE CONSISTENCY CONSTRAINTS HERE */
2501
err = row_ins(node, thr);
2504
trx->error_state = err;
2506
if (err != DB_SUCCESS) {
2507
/* err == DB_LOCK_WAIT or SQL error detected */
2511
/* DO THE TRIGGER ACTIONS HERE */
2513
if (node->ins_type == INS_SEARCHED) {
2514
/* Fetch a row to insert */
2516
thr->run_node = sel_node;
2518
thr->run_node = que_node_get_parent(node);