diff --git a/mysql-test/suite/innodb/r/foreign-keys.result b/mysql-test/suite/innodb/r/foreign-keys.result index 4402c5ca2fe..447013d408d 100644 --- a/mysql-test/suite/innodb/r/foreign-keys.result +++ b/mysql-test/suite/innodb/r/foreign-keys.result @@ -87,6 +87,19 @@ drop table t3; drop table t2; drop table t1; set debug_sync='reset'; +# +# MDEV-17595 - Server crashes in copy_data_between_tables or +# Assertion `thd->transaction.stmt.is_empty() || +# (thd->state_flags & Open_tables_state::BACKUPS_AVAIL)' +# fails in close_tables_for_reopen upon concurrent +# ALTER TABLE and FLUSH +# +CREATE TABLE t1 (a INT, KEY(a)) ENGINE=InnoDB; +INSERT INTO t1 VALUES(1),(2); +CREATE TABLE t2 (b INT, KEY(b)) ENGINE=InnoDB; +INSERT INTO t2 VALUES(2); +ALTER TABLE t2 ADD FOREIGN KEY(b) REFERENCES t1(a), LOCK=EXCLUSIVE; +DROP TABLE t2, t1; create table t1 (a int primary key, b int) engine=innodb; create table t2 (c int primary key, d int, foreign key (d) references t1 (a) on update cascade) engine=innodb; diff --git a/mysql-test/suite/innodb/t/foreign-keys.test b/mysql-test/suite/innodb/t/foreign-keys.test index 35da5a5d075..442467b7dbe 100644 --- a/mysql-test/suite/innodb/t/foreign-keys.test +++ b/mysql-test/suite/innodb/t/foreign-keys.test @@ -112,6 +112,21 @@ drop table t2; drop table t1; set debug_sync='reset'; + +--echo # +--echo # MDEV-17595 - Server crashes in copy_data_between_tables or +--echo # Assertion `thd->transaction.stmt.is_empty() || +--echo # (thd->state_flags & Open_tables_state::BACKUPS_AVAIL)' +--echo # fails in close_tables_for_reopen upon concurrent +--echo # ALTER TABLE and FLUSH +--echo # +CREATE TABLE t1 (a INT, KEY(a)) ENGINE=InnoDB; +INSERT INTO t1 VALUES(1),(2); +CREATE TABLE t2 (b INT, KEY(b)) ENGINE=InnoDB; +INSERT INTO t2 VALUES(2); +ALTER TABLE t2 ADD FOREIGN KEY(b) REFERENCES t1(a), LOCK=EXCLUSIVE; +DROP TABLE t2, t1; + # # FK and prelocking: # child table accesses (reads and writes) wait for locks. diff --git a/sql/sql_table.cc b/sql/sql_table.cc index af40ae23a6c..b559e36315b 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -9547,6 +9547,8 @@ do_continue:; new_table->file->get_foreign_key_list(thd, &fk_list); while ((fk= fk_list_it++)) { + MDL_request mdl_request; + if (lower_case_table_names) { char buf[NAME_LEN]; @@ -9558,20 +9560,14 @@ do_continue:; len = my_casedn_str(files_charset_info, buf); thd->make_lex_string(fk->referenced_table, buf, len); } - if (table_already_fk_prelocked(table_list, fk->referenced_db, - fk->referenced_table, TL_READ_NO_INSERT)) - continue; - TABLE_LIST *tl= (TABLE_LIST *) thd->alloc(sizeof(TABLE_LIST)); - tl->init_one_table_for_prelocking(fk->referenced_db->str, fk->referenced_db->length, - fk->referenced_table->str, fk->referenced_table->length, - NULL, TL_READ_NO_INSERT, false, NULL, 0, - &thd->lex->query_tables_last); + mdl_request.init(MDL_key::TABLE, + fk->referenced_db->str, fk->referenced_table->str, + MDL_SHARED_NO_WRITE, MDL_TRANSACTION); + if (thd->mdl_context.acquire_lock(&mdl_request, + thd->variables.lock_wait_timeout)) + goto err_new_table_cleanup; } - - if (open_tables(thd, &table_list->next_global, &tables_opened, 0, - &alter_prelocking_strategy)) - goto err_new_table_cleanup; } } diff --git a/storage/innobase/row/row0ftsort.cc b/storage/innobase/row/row0ftsort.cc index c4be5ad69b3..de622527766 100644 --- a/storage/innobase/row/row0ftsort.cc +++ b/storage/innobase/row/row0ftsort.cc @@ -98,8 +98,8 @@ row_merge_create_fts_sort_index( field = dict_index_get_nth_field(new_index, 0); field->name = NULL; field->prefix_len = 0; - field->col = new(mem_heap_zalloc(new_index->heap, sizeof(dict_col_t))) - dict_col_t(); + field->col = static_cast( + mem_heap_zalloc(new_index->heap, sizeof(dict_col_t))); field->col->prtype = idx_field->col->prtype | DATA_NOT_NULL; field->col->mtype = charset == &my_charset_latin1 ? DATA_VARCHAR : DATA_VARMYSQL; @@ -113,8 +113,8 @@ row_merge_create_fts_sort_index( field = dict_index_get_nth_field(new_index, 1); field->name = NULL; field->prefix_len = 0; - field->col = new(mem_heap_zalloc(new_index->heap, sizeof(dict_col_t))) - dict_col_t(); + field->col = static_cast( + mem_heap_zalloc(new_index->heap, sizeof(dict_col_t))); field->col->mtype = DATA_INT; *opt_doc_id_size = FALSE; @@ -152,8 +152,8 @@ row_merge_create_fts_sort_index( field = dict_index_get_nth_field(new_index, 2); field->name = NULL; field->prefix_len = 0; - field->col = new(mem_heap_zalloc(new_index->heap, sizeof(dict_col_t))) - dict_col_t(); + field->col = static_cast( + mem_heap_zalloc(new_index->heap, sizeof(dict_col_t))); field->col->mtype = DATA_INT; field->col->len = 4 ; field->fixed_len = 4; diff --git a/storage/xtradb/row/row0ftsort.cc b/storage/xtradb/row/row0ftsort.cc index 1798f997033..fb30f867f87 100644 --- a/storage/xtradb/row/row0ftsort.cc +++ b/storage/xtradb/row/row0ftsort.cc @@ -101,8 +101,8 @@ row_merge_create_fts_sort_index( field = dict_index_get_nth_field(new_index, 0); field->name = NULL; field->prefix_len = 0; - field->col = new(mem_heap_zalloc(new_index->heap, sizeof(dict_col_t))) - dict_col_t(); + field->col = static_cast( + mem_heap_zalloc(new_index->heap, sizeof(dict_col_t))); field->col->prtype = idx_field->col->prtype | DATA_NOT_NULL; field->col->mtype = charset == &my_charset_latin1 ? DATA_VARCHAR : DATA_VARMYSQL; @@ -116,8 +116,8 @@ row_merge_create_fts_sort_index( field = dict_index_get_nth_field(new_index, 1); field->name = NULL; field->prefix_len = 0; - field->col = new(mem_heap_zalloc(new_index->heap, sizeof(dict_col_t))) - dict_col_t(); + field->col = static_cast( + mem_heap_zalloc(new_index->heap, sizeof(dict_col_t))); field->col->mtype = DATA_INT; *opt_doc_id_size = FALSE; @@ -155,8 +155,8 @@ row_merge_create_fts_sort_index( field = dict_index_get_nth_field(new_index, 2); field->name = NULL; field->prefix_len = 0; - field->col = new(mem_heap_zalloc(new_index->heap, sizeof(dict_col_t))) - dict_col_t(); + field->col = static_cast( + mem_heap_zalloc(new_index->heap, sizeof(dict_col_t))); field->col->mtype = DATA_INT; field->col->len = 4 ; field->fixed_len = 4;