Commit 6e390a62 authored by Marko Mäkelä's avatar Marko Mäkelä

MDEV-26772 InnoDB DDL fails with DUPLICATE KEY error

ha_innobase::delete_table(): When the table that is being dropped
has a name starting with #sql, temporarily set
innodb_lock_wait_timeout=0 while attempting to lock the
persistent statistics tables. If the statistics tables cannot be locked,
pretend that statistics did not exist and carry on with dropping
the table. The SQL layer is not really prepared for failures of
this operation. This is what fixes the test case.

ha_innobase::rename_table(): When renaming a table from a name
that starts with #sql, try to lock the statistics tables with an
immediate timeout, and ignore the statistics if the locks were
not available. In fact, during any rename from a #sql name,
dict_stats_rename_table() should have no effect, because already
when an earlier rename to a #sql name took place we should have
deleted the statistics for the table using the non-#sql name.
This change is just analogous to the ha_innobase::delete_table().
parent f7684f0c
...@@ -107,3 +107,29 @@ ALTER TABLE t RENAME INDEX i2 to x, ALGORITHM=INPLACE; ...@@ -107,3 +107,29 @@ ALTER TABLE t RENAME INDEX i2 to x, ALGORITHM=INPLACE;
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
SET DEBUG_DBUG = @saved_debug_dbug; SET DEBUG_DBUG = @saved_debug_dbug;
DROP TABLE t; DROP TABLE t;
#
# MDEV-26772 InnoDB DDL fails with DUPLICATE KEY error
#
create table t1(f1 int not null primary key,
f2 int not null, index idx(f2))engine=innodb;
insert into t1 values(1, 1);
connect con1,localhost,root,,,;
SET DEBUG_SYNC='before_delete_table_stats SIGNAL blocked WAIT_FOR go';
SET innodb_lock_wait_timeout=0;
ALTER TABLE t1 FORCE, ALGORITHM=COPY;
connection default;
SET DEBUG_SYNC='now WAIT_FOR blocked';
BEGIN;
SELECT * FROM mysql.innodb_table_stats FOR UPDATE;
database_name table_name last_update n_rows clustered_index_size sum_of_other_index_sizes
SET DEBUG_SYNC='now SIGNAL go';
connection con1;
connection default;
COMMIT;
SET DEBUG_SYNC=RESET;
connection con1;
ALTER TABLE t1 RENAME KEY idx TO idx1, ALGORITHM=COPY;
disconnect con1;
connection default;
DROP TABLE t1;
# End of 10.6 tests
...@@ -142,5 +142,42 @@ SET DEBUG_DBUG = @saved_debug_dbug; ...@@ -142,5 +142,42 @@ SET DEBUG_DBUG = @saved_debug_dbug;
DROP TABLE t; DROP TABLE t;
--echo #
--echo # MDEV-26772 InnoDB DDL fails with DUPLICATE KEY error
--echo #
create table t1(f1 int not null primary key,
f2 int not null, index idx(f2))engine=innodb;
insert into t1 values(1, 1);
connect(con1,localhost,root,,,);
SET DEBUG_SYNC='before_delete_table_stats SIGNAL blocked WAIT_FOR go';
SET innodb_lock_wait_timeout=0;
send ALTER TABLE t1 FORCE, ALGORITHM=COPY;
connection default;
SET DEBUG_SYNC='now WAIT_FOR blocked';
BEGIN;
SELECT * FROM mysql.innodb_table_stats FOR UPDATE;
SET DEBUG_SYNC='now SIGNAL go';
connection con1;
reap;
connection default;
COMMIT;
SET DEBUG_SYNC=RESET;
connection con1;
ALTER TABLE t1 RENAME KEY idx TO idx1, ALGORITHM=COPY;
disconnect con1;
connection default;
DROP TABLE t1;
--echo # End of 10.6 tests
# Wait till all disconnects are completed # Wait till all disconnects are completed
--source include/wait_until_count_sessions.inc --source include/wait_until_count_sessions.inc
...@@ -13473,6 +13473,8 @@ int ha_innobase::delete_table(const char *name) ...@@ -13473,6 +13473,8 @@ int ha_innobase::delete_table(const char *name)
} }
#endif #endif
DEBUG_SYNC(thd, "before_delete_table_stats");
if (err == DB_SUCCESS && dict_stats_is_persistent_enabled(table) && if (err == DB_SUCCESS && dict_stats_is_persistent_enabled(table) &&
!table->is_stats_table()) !table->is_stats_table())
{ {
...@@ -13496,11 +13498,29 @@ int ha_innobase::delete_table(const char *name) ...@@ -13496,11 +13498,29 @@ int ha_innobase::delete_table(const char *name)
dict_sys.unfreeze(); dict_sys.unfreeze();
} }
auto &timeout= THDVAR(thd, lock_wait_timeout);
const auto save_timeout= timeout;
if (table->name.is_temporary())
timeout= 0;
if (table_stats && index_stats && if (table_stats && index_stats &&
!strcmp(table_stats->name.m_name, TABLE_STATS_NAME) && !strcmp(table_stats->name.m_name, TABLE_STATS_NAME) &&
!strcmp(index_stats->name.m_name, INDEX_STATS_NAME) && !strcmp(index_stats->name.m_name, INDEX_STATS_NAME) &&
!(err= lock_table_for_trx(table_stats, trx, LOCK_X))) !(err= lock_table_for_trx(table_stats, trx, LOCK_X)))
err= lock_table_for_trx(index_stats, trx, LOCK_X); err= lock_table_for_trx(index_stats, trx, LOCK_X);
if (err != DB_SUCCESS && !timeout)
{
/* We may skip deleting statistics if we cannot lock the tables,
when the table carries a temporary name. */
err= DB_SUCCESS;
dict_table_close(table_stats, false, thd, mdl_table);
dict_table_close(index_stats, false, thd, mdl_index);
table_stats= nullptr;
index_stats= nullptr;
}
timeout= save_timeout;
} }
if (err == DB_SUCCESS) if (err == DB_SUCCESS)
...@@ -13959,8 +13979,9 @@ ha_innobase::rename_table( ...@@ -13959,8 +13979,9 @@ ha_innobase::rename_table(
normalize_table_name(norm_to, to); normalize_table_name(norm_to, to);
dberr_t error = DB_SUCCESS; dberr_t error = DB_SUCCESS;
const bool from_temp = dict_table_t::is_temporary_name(norm_from);
if (dict_table_t::is_temporary_name(norm_from)) { if (from_temp) {
/* There is no need to lock any FOREIGN KEY child tables. */ /* There is no need to lock any FOREIGN KEY child tables. */
} else if (dict_table_t *table = dict_table_open_on_name( } else if (dict_table_t *table = dict_table_open_on_name(
norm_from, false, DICT_ERR_IGNORE_FK_NOKEY)) { norm_from, false, DICT_ERR_IGNORE_FK_NOKEY)) {
...@@ -14003,9 +14024,31 @@ ha_innobase::rename_table( ...@@ -14003,9 +14024,31 @@ ha_innobase::rename_table(
if (error == DB_SUCCESS && table_stats && index_stats if (error == DB_SUCCESS && table_stats && index_stats
&& !strcmp(table_stats->name.m_name, TABLE_STATS_NAME) && !strcmp(table_stats->name.m_name, TABLE_STATS_NAME)
&& !strcmp(index_stats->name.m_name, INDEX_STATS_NAME) && && !strcmp(index_stats->name.m_name, INDEX_STATS_NAME)) {
!(error = lock_table_for_trx(table_stats, trx, LOCK_X))) { auto &timeout = THDVAR(thd, lock_wait_timeout);
error = lock_table_for_trx(index_stats, trx, LOCK_X); const auto save_timeout = timeout;
if (from_temp) {
timeout = 0;
}
error = lock_table_for_trx(table_stats, trx, LOCK_X);
if (error == DB_SUCCESS) {
error = lock_table_for_trx(index_stats, trx,
LOCK_X);
}
if (error != DB_SUCCESS && from_temp) {
error = DB_SUCCESS;
/* We may skip renaming statistics if
we cannot lock the tables, when the
table is being renamed from from a
temporary name. */
dict_table_close(table_stats, false, thd,
mdl_table);
dict_table_close(index_stats, false, thd,
mdl_index);
table_stats = nullptr;
index_stats = nullptr;
}
timeout = save_timeout;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment