diff options
author | Thirunarayanan Balathandayuthapani <thiru@mariadb.com> | 2020-08-27 16:50:23 +0530 |
---|---|---|
committer | Thirunarayanan Balathandayuthapani <thiru@mariadb.com> | 2020-08-27 17:13:02 +0530 |
commit | 5b2b1af2233c2febbce3fd2b5793cb2de5929cac (patch) | |
tree | aca4b4d9c96cdd4f9ee63794b0cc60917bfb2f90 | |
parent | 4a90bb85c0cb1407b0f112245e47ec995dc767c7 (diff) | |
download | mariadb-git-bb-10.2-MDEV-23470.tar.gz |
MDEV-23470 InnoDB: Failing assertion: cmp < 0 in row_ins_check_foreign_constraintbb-10.2-MDEV-23470
During insertion of clustered index, InnoDB does the check for foreign key
constraints. Problem is that it uses the clustered index entry to search
indexes of referenced tables and it could lead to unexpected result
when there is no foreign index.
Solution:
========
Rebuild the tuple based on foreign column names before searching it
on reference index when there is no foreign index.
-rw-r--r-- | mysql-test/suite/innodb/r/foreign-keys.result | 16 | ||||
-rw-r--r-- | mysql-test/suite/innodb/t/foreign-keys.test | 19 | ||||
-rw-r--r-- | storage/innobase/row/row0ins.cc | 64 |
3 files changed, 94 insertions, 5 deletions
diff --git a/mysql-test/suite/innodb/r/foreign-keys.result b/mysql-test/suite/innodb/r/foreign-keys.result index 5cbbb5298de..05e68b0c3db 100644 --- a/mysql-test/suite/innodb/r/foreign-keys.result +++ b/mysql-test/suite/innodb/r/foreign-keys.result @@ -220,3 +220,19 @@ drop table t1,t2; ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails drop table t1,t2; ERROR 42S02: Unknown table 'test.t2' +# +# MDEV-23470 InnoDB: Failing assertion: cmp < 0 in +# row_ins_check_foreign_constraint +# +CREATE TABLE t1(f1 INT NOT NULL PRIMARY KEY, f2 INT NOT NULL)ENGINE=InnoDB; +CREATE TABLE t2(f1 VARCHAR(100), f2 INT NOT NULL, +INDEX(f2))ENGINE=InnoDB; +INSERT INTO t1 VALUES(99, 2); +ALTER TABLE t2 ADD FOREIGN KEY(f2) REFERENCES t1(f1); +SET FOREIGN_KEY_CHECKS=0; +DROP INDEX f2 ON t2; +SET FOREIGN_KEY_CHECKS=1; +INSERT INTO t2 VALUES('G', 3); +ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`f2`) REFERENCES `t1` (`f1`)) +DROP TABLE t2, t1; +SET FOREIGN_KEY_CHECKS=DEFAULT; diff --git a/mysql-test/suite/innodb/t/foreign-keys.test b/mysql-test/suite/innodb/t/foreign-keys.test index 58a71d11a6a..53fbe8d00dc 100644 --- a/mysql-test/suite/innodb/t/foreign-keys.test +++ b/mysql-test/suite/innodb/t/foreign-keys.test @@ -250,3 +250,22 @@ show create table t2; drop table t1,t2; --error ER_BAD_TABLE_ERROR drop table t1,t2; + +--echo # +--echo # MDEV-23470 InnoDB: Failing assertion: cmp < 0 in +--echo # row_ins_check_foreign_constraint +--echo # +CREATE TABLE t1(f1 INT NOT NULL PRIMARY KEY, f2 INT NOT NULL)ENGINE=InnoDB; +CREATE TABLE t2(f1 VARCHAR(100), f2 INT NOT NULL, + INDEX(f2))ENGINE=InnoDB; + +INSERT INTO t1 VALUES(99, 2); +ALTER TABLE t2 ADD FOREIGN KEY(f2) REFERENCES t1(f1); + +SET FOREIGN_KEY_CHECKS=0; +DROP INDEX f2 ON t2; +SET FOREIGN_KEY_CHECKS=1; +--error ER_NO_REFERENCED_ROW_2 +INSERT INTO t2 VALUES('G', 3); +DROP TABLE t2, t1; +SET FOREIGN_KEY_CHECKS=DEFAULT; diff --git a/storage/innobase/row/row0ins.cc b/storage/innobase/row/row0ins.cc index 50394193a15..d0c6c092d7f 100644 --- a/storage/innobase/row/row0ins.cc +++ b/storage/innobase/row/row0ins.cc @@ -1920,6 +1920,34 @@ exit_func: DBUG_RETURN(err); } +/** Sets the values of the dtuple fields in ref_entry from the values of +foreign columns in entry. +@param[in] foreign foreign key constraint +@param[in] index clustered index +@param[in] entry tuple of clustered index +@param[in] ref_entry tuple of foreign columns */ +static void +row_ins_foreign_index_entry(dict_foreign_t *foreign, + const dict_index_t *index, + const dtuple_t *entry, + dtuple_t *ref_entry) +{ + for (ulint i= 0; i < foreign->n_fields; i++) + { + for (ulint j= 0; j < index->n_fields; j++) + { + const dict_col_t *col= dict_field_get_col( + dict_index_get_nth_field(index, j)); + const char *col_name= dict_table_get_col_name(index->table, col->ind); + if (0 == innobase_strcasecmp(col_name, foreign->foreign_col_names[i])) + { + dfield_copy(&ref_entry->fields[i], &entry->fields[j]); + break; + } + } + } +} + /***************************************************************//** Checks if foreign key constraints fail for an index entry. If index is not mentioned in any constraint, this function does nothing, @@ -1938,9 +1966,11 @@ row_ins_check_foreign_constraints( que_thr_t* thr) /*!< in: query thread */ { dict_foreign_t* foreign; - dberr_t err; + dberr_t err = DB_SUCCESS; trx_t* trx; ibool got_s_lock = FALSE; + mem_heap_t* heap = NULL; + dtuple_t* ref_tuple; DBUG_ASSERT(index->is_primary() == pk); @@ -1957,6 +1987,23 @@ row_ins_check_foreign_constraints( if (foreign->foreign_index == index || (pk && !foreign->foreign_index)) { + + ref_tuple= entry; + + if (!foreign->foreign_index) { + /* Change primary key entry to + foreign key index entry */ + if (!heap) { + heap = mem_heap_create(1000); + } + ref_tuple = dtuple_create( + heap, foreign->n_fields); + dtuple_set_n_fields_cmp( + ref_tuple, foreign->n_fields); + row_ins_foreign_index_entry( + foreign, index, entry, ref_tuple); + } + dict_table_t* ref_table = NULL; dict_table_t* referenced_table = foreign->referenced_table; @@ -1986,7 +2033,7 @@ row_ins_check_foreign_constraints( table from being dropped while the check is running. */ err = row_ins_check_foreign_constraint( - TRUE, foreign, table, entry, thr); + TRUE, foreign, table, ref_tuple, thr); if (referenced_table) { my_atomic_addlint( @@ -2002,14 +2049,21 @@ row_ins_check_foreign_constraints( dict_table_close(ref_table, FALSE, FALSE); } - if (err != DB_SUCCESS) { + if (heap) { + mem_heap_empty(heap); + } - return(err); + if (err != DB_SUCCESS) { + goto func_exit; } } } - return(DB_SUCCESS); +func_exit: + if (heap) { + mem_heap_free(heap); + } + return err; } /***************************************************************//** |