Skip to content

Commit

Permalink
Follow-up fix to MDEV-14585 Automatically remove #sql- tables in Inno…
Browse files Browse the repository at this point in the history
…DB dictionary during recovery

If InnoDB is killed while ALTER TABLE...ALGORITHM=COPY is in progress,
after recovery there could be undo log records some records that were
inserted into an intermediate copy of the table. Due to these undo log
records, InnoDB would resurrect locks at recovery, and the intermediate
table would be locked while we are trying to drop it. This would cause
a call to row_rename_table_for_mysql(), either from
row_mysql_drop_garbage_tables() or from the rollback of a RENAME
operation that was part of the ALTER TABLE.

row_rename_table_for_mysql(): Do not attempt to parse FOREIGN KEY
constraints when renaming from #sql-something to #sql-something-else,
because it does not make any sense.

row_drop_table_for_mysql(): When deferring DROP TABLE due to locks,
do not rename the table if its name already starts with the #sql-
prefix, which is what row_mysql_drop_garbage_tables() uses.
Previously, the too strict prefix #sql-ib was used, and some
tables were renamed unnecessarily.
  • Loading branch information
dr-m committed Jan 17, 2018
1 parent 04eef79 commit 656f66d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
31 changes: 27 additions & 4 deletions mysql-test/suite/innodb/r/rename_table_debug.result
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
CREATE TABLE t1 (a INT UNSIGNED PRIMARY KEY) ENGINE=InnoDB;
INSERT INTO t1 VALUES(42);
CREATE TABLE t1 (a SERIAL, b INT, c INT, d INT) ENGINE=InnoDB;
INSERT INTO t1 () VALUES ();
connect con1,localhost,root,,test;
SET DEBUG_SYNC='before_rename_table_commit SIGNAL renamed WAIT_FOR ever';
RENAME TABLE t1 TO t2;
connection default;
SET DEBUG_SYNC='now WAIT_FOR renamed';
disconnect con1;
SELECT * FROM t1;
a
42
a b c d
1 NULL NULL NULL
BEGIN;
COMMIT;
UPDATE t1 SET b=a%7, c=a%11, d=a%13;
SET DEBUG_DBUG='+d,crash_commit_before';
ALTER TABLE t1
ADD INDEX(b,c,d,a),ADD INDEX(b,c,a,d),ADD INDEX(b,a,c,d),ADD INDEX(b,a,d,c),
ADD INDEX(b,d,a,c),ADD INDEX(b,d,c,a),ADD INDEX(a,b,c,d),ADD INDEX(a,b,d,c),
ADD INDEX(a,c,b,d),ADD INDEX(a,c,d,b),ADD INDEX(a,d,b,c),ADD INDEX(a,d,c,b),
ADD INDEX(c,a,b,d),ADD INDEX(c,a,d,b),ADD INDEX(c,b,a,d),ADD INDEX(c,b,d,a),
ADD INDEX(c,d,a,b),ADD INDEX(c,d,b,a),ADD INDEX(d,a,b,c),ADD INDEX(d,a,c,b),
ADD INDEX(d,b,a,c),ADD INDEX(d,b,c,a),ADD INDEX(d,c,a,b),ADD INDEX(d,c,b,a),
ADD INDEX(a,b,c), ADD INDEX(a,c,b), ADD INDEX(a,c,d), ADD INDEX(a,d,c),
ADD INDEX(a,b,d), ADD INDEX(a,d,b), ADD INDEX(b,c,d), ADD INDEX(b,d,c),
ALGORITHM=COPY;
ERROR HY000: Lost connection to MySQL server during query
CHECK TABLE t1;
Table Op Msg_type Msg_text
test.t1 check status OK
SELECT COUNT(*) FROM t1;
COUNT(*)
1000
DROP TABLE t1;
SET GLOBAL innodb_background_drop_list_empty=
@@GLOBAL.innodb_background_drop_list_empty;
39 changes: 37 additions & 2 deletions mysql-test/suite/innodb/t/rename_table_debug.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
--source include/have_debug_sync.inc
--source include/not_embedded.inc

CREATE TABLE t1 (a INT UNSIGNED PRIMARY KEY) ENGINE=InnoDB;
INSERT INTO t1 VALUES(42);
LET $datadir= `SELECT @@datadir`;

CREATE TABLE t1 (a SERIAL, b INT, c INT, d INT) ENGINE=InnoDB;
INSERT INTO t1 () VALUES ();

--connect (con1,localhost,root,,test)
SET DEBUG_SYNC='before_rename_table_commit SIGNAL renamed WAIT_FOR ever';
Expand All @@ -16,4 +18,37 @@ SET DEBUG_SYNC='now WAIT_FOR renamed';
--source include/restart_mysqld.inc
--disconnect con1
SELECT * FROM t1;

let $c = 999;
BEGIN;
--disable_query_log
while ($c) {
INSERT INTO t1() VALUES();
dec $c;
}
--enable_query_log
COMMIT;
UPDATE t1 SET b=a%7, c=a%11, d=a%13;

--source include/expect_crash.inc
SET DEBUG_DBUG='+d,crash_commit_before';
--error 2013
ALTER TABLE t1
ADD INDEX(b,c,d,a),ADD INDEX(b,c,a,d),ADD INDEX(b,a,c,d),ADD INDEX(b,a,d,c),
ADD INDEX(b,d,a,c),ADD INDEX(b,d,c,a),ADD INDEX(a,b,c,d),ADD INDEX(a,b,d,c),
ADD INDEX(a,c,b,d),ADD INDEX(a,c,d,b),ADD INDEX(a,d,b,c),ADD INDEX(a,d,c,b),
ADD INDEX(c,a,b,d),ADD INDEX(c,a,d,b),ADD INDEX(c,b,a,d),ADD INDEX(c,b,d,a),
ADD INDEX(c,d,a,b),ADD INDEX(c,d,b,a),ADD INDEX(d,a,b,c),ADD INDEX(d,a,c,b),
ADD INDEX(d,b,a,c),ADD INDEX(d,b,c,a),ADD INDEX(d,c,a,b),ADD INDEX(d,c,b,a),
ADD INDEX(a,b,c), ADD INDEX(a,c,b), ADD INDEX(a,c,d), ADD INDEX(a,d,c),
ADD INDEX(a,b,d), ADD INDEX(a,d,b), ADD INDEX(b,c,d), ADD INDEX(b,d,c),
ALGORITHM=COPY;
--source include/start_mysqld.inc
CHECK TABLE t1;
SELECT COUNT(*) FROM t1;
DROP TABLE t1;
# MDEV-11415 TODO: remove the following
SET GLOBAL innodb_background_drop_list_empty=
@@GLOBAL.innodb_background_drop_list_empty;
# Work around missing crash recovery at the SQL layer.
--remove_files_wildcard $datadir/test #sql-*.frm
4 changes: 2 additions & 2 deletions storage/innobase/row/row0mysql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3712,7 +3712,7 @@ row_drop_table_for_mysql(

if (table->n_foreign_key_checks_running > 0) {
defer:
if (!strstr(table->name.m_name, "/" TEMP_FILE_PREFIX_INNODB)) {
if (!strstr(table->name.m_name, "/" TEMP_FILE_PREFIX)) {
heap = mem_heap_create(FN_REFLEN);
const char* tmp_name
= dict_mem_create_temporary_tablename(
Expand Down Expand Up @@ -4477,7 +4477,7 @@ row_rename_table_for_mysql(

goto funct_exit;

} else if (new_is_tmp) {
} else if (!old_is_tmp && new_is_tmp) {
/* MySQL is doing an ALTER TABLE command and it renames the
original table to a temporary table name. We want to preserve
the original foreign key constraint definitions despite the
Expand Down

0 comments on commit 656f66d

Please sign in to comment.