summaryrefslogtreecommitdiff
path: root/mysql-test
diff options
context:
space:
mode:
authorDmitry Lenev <dlenev@mysql.com>2009-12-30 20:53:30 +0300
committerDmitry Lenev <dlenev@mysql.com>2009-12-30 20:53:30 +0300
commit0228c9893622b1a60a3128b74c35b06d3a63c72b (patch)
tree8db2608b4c8fb2a523f143d1b99effb2fd74b32e /mysql-test
parent1c327209e1512174d3353af0e079a45b9bb38e1e (diff)
downloadmariadb-git-0228c9893622b1a60a3128b74c35b06d3a63c72b.tar.gz
Implementation of simple deadlock detection for metadata locks.
This change is supposed to reduce number of ER_LOCK_DEADLOCK errors which occur when multi-statement transaction encounters conflicting metadata lock in cases when waiting is possible. The idea is not to fail ER_LOCK_DEADLOCK error immediately when we encounter conflicting metadata lock. Instead we release all metadata locks acquired by current statement and start to wait until conflicting lock go away. To avoid deadlocks we use simple empiric which aborts waiting with ER_LOCK_DEADLOCK error if it turns out that somebody is waiting for metadata locks owned by this transaction. This patch also fixes bug #46273 "MySQL 5.4.4 new MDL: Bug#989 is not fully fixed in case of ALTER". The bug was that concurrent execution of UPDATE or MULTI-UPDATE statement as a part of multi-statement transaction that already has used table being updated and ALTER TABLE statement might have resulted of loss of isolation between this transaction and ALTER TABLE statement, which manifested itself as changes performed by ALTER TABLE becoming visible in transaction and wrong binary log order as a consequence. This problem occurred when UPDATE or MULTI-UPDATE's wait in mysql_lock_tables() call was aborted due to metadata lock upgrade performed by concurrent ALTER TABLE. After such abort all metadata locks held by transaction were released but transaction silently continued to be executed as if nothing has happened. We solve this problem by changing our code not to release all locks in such case. Instead we release only locks which were acquired by current statement and then try to reacquire them by restarting open/lock tables process. We piggyback on simple deadlock detector implementation since this change has to be done anyway for it. mysql-test/include/handler.inc: After introduction of basic deadlock detector for metadata locks it became necessary to change parts of test for HANDLER statements which covered some of scenarios in which ER_LOCK_DEADLOCK error was detected in absence of real deadlock (with new deadlock detector this no longer happens). Also adjusted test to the fact that HANDLER READ for the table no longer will be blocked by ALTER TABLE for the same table which awaits for metadata lock upgrade (this is due to removal of mysql_lock_abort() from wait_while_table_is_used()). mysql-test/r/handler_innodb.result: After introduction of basic deadlock detector for metadata locks it became necessary to change parts of test for HANDLER statements which covered some of scenarios in which ER_LOCK_DEADLOCK error was detected in absence of real deadlock (with new deadlock detector this no longer happens). Also adjusted test to the fact that HANDLER READ for the table no longer will be blocked by ALTER TABLE for the same table which awaits for metadata lock upgrade (this is due to removal of mysql_lock_abort() from wait_while_table_is_used()). mysql-test/r/handler_myisam.result: After introduction of basic deadlock detector for metadata locks it became necessary to change parts of test for HANDLER statements which covered some of scenarios in which ER_LOCK_DEADLOCK error was detected in absence of real deadlock (with new deadlock detector this no longer happens). Also adjusted test to the fact that HANDLER READ for the table no longer will be blocked by ALTER TABLE for the same table which awaits for metadata lock upgrade (this is due to removal of mysql_lock_abort() from wait_while_table_is_used()). mysql-test/r/mdl_sync.result: Added test coverage for basic deadlock detection in metadata locking subsystem and for bug #46273 "MySQL 5.4.4 new MDL: Bug#989 is not fully fixed in case of ALTER". mysql-test/r/sp-lock.result: Adjusted test coverage for metadata locking for stored routines since after introduction of basic deadlock detector for metadata locks number of scenarios in which ER_LOCK_DEADLOCK error in absence of deadlock has decreased. mysql-test/t/mdl_sync.test: Added test coverage for basic deadlock detection in metadata locking subsystem and for bug #46273 "MySQL 5.4.4 new MDL: Bug#989 is not fully fixed in case of ALTER". mysql-test/t/sp-lock.test: Adjusted test coverage for metadata locking for stored routines since after introduction of basic deadlock detector for metadata locks number of scenarios in which ER_LOCK_DEADLOCK error in absence of deadlock has decreased. sql/log_event_old.cc: close_tables_for_reopen() now takes one more argument which specifies at which point it should stop releasing metadata locks acquired by this connection. sql/mdl.cc: Changed metadata locking subsystem to support basic deadlock detection with a help of the following simple empiric -- we assume that there is a deadlock if there is a connection which has to wait for a metadata lock which is currently acquired by some connection which is itself waiting to be able to acquire some shared metadata lock. To implement this change: - Added MDL_context::can_wait_lead_to_deadlock()/_impl() methods which allow to find out if there is someone waiting for metadata lock which is held by the connection and therefore deadlocks are possible if this connection is going to wait for some metadata lock. To do this added version of MDL_ticket::has_pending_conflicting_lock() method which assumes that its caller already owns LOCK_mdl mutex. - Changed MDL_context::wait_for_locks() to use one of the above methods to check if somebody is waiting for metadata lock owned by this context (and therefore deadlock is possible) and emit ER_LOCK_DEADLOCK error in this case. Also now we mark context of connections waiting inside of this method by setting MDL_context::m_is_waiting_in_mdl member. Thanks to this such connection could be waken up if some other connection starts waiting for one of its metadata locks and so a deadlock can occur. - Adjusted notify_shared_lock() to wake up connections which wait inside MDL_context::wait_for_locks() while holding shared metadata lock. - Changed MDL_ticket::upgrade_shared_lock_to_exclusive() to add temporary ticket for exclusive lock to MDL_lock::waiting queue, so request for metadata lock upgrade can be properly detected by our empiric. Also now this method invokes a callback which forces transactions holding shared metadata lock on the table to call MDL_context:: can_wait_lead_to_deadlock() method even if they don't need any new metadata locks. Thanks to this such transactions can detect deadlocks/ livelocks between MDL and table-level locks. Also reduced timeouts between calls to notify_shared_lock() in MDL_ticket::upgrade_shared_lock_to_exclusive() and MDL_context::acquire_exclusive_locks(). This was necessary to get rid of call to mysql_lock_abort() in wait_while_table_is_used(). (Now we instead rely on notify_shared_lock() timely calling mysql_lock_abort_for_thread() for the table on which lock is being upgraded/acquired). sql/mdl.h: - Added a version of MDL_ticket::has_pending_conflicting_lock() method to be used in situations when caller already has acquired LOCK_mdl mutex. - Added MDL_context::can_wait_lead_to_deadlock()/_impl() methods which allow to find out if there is someone waiting for metadata lock which is held by this connection and thus deadlocks are possible if this connections will start waiting for some metadata lock. - Added MDL_context::m_is_waiting_in_mdl member to mark connections waiting in MDL_context::wait_for_locks() method of metadata locking subsystem. Added getter method for this private member to make it accessible in notify_shared_lock() auxiliary so we can wake-up such connections if they hold shared metadata locks. - Finally, added mysql_abort_transactions_with_shared_lock() callback to be able force transactions which don't need any new metadata locks still call MDL_context::can_wait_lead_to_deadlock() and detect some of deadlocks between metadata locks and table-level locks. sql/mysql_priv.h: close_tables_for_reopen() now takes one more argument which specifies at which point it should stop releasing metadata locks acquired by this connection. sql/sql_base.cc: Changed approach to metadata locking for multi-statement transactions. We no longer fail ER_LOCK_DEADLOCK error immediately when we encounter conflicting metadata lock. Instead we release all metadata locks acquired by current statement and start to wait until conflicting locks to go away by calling MDL_context::wait_for_locks() method. To avoid deadlocks the latter implements simple empiric which aborts waiting with ER_LOCK_DEADLOCK error if it turns out that somebody is waiting for metadata locks owned by this transaction. To implement the change described above: - Introduced Open_table_context::m_start_of_statement_svp member to store state of metadata locks at the start of the statement. - Changed Open_table_context::request_backoff_action() not to fail with ER_LOCK_DEADLOCK immediately if back-off is requested due to conflicting metadata lock. - Added new argument for close_tables_for_reopen() procedure which allows to specify subset of metadata locks to be released. - Changed open_tables() not to release all metadata locks acquired by current transaction when metadata lock conflict is discovered. Instead we release only locks acquired by current statement. - Changed open_ltable() and open_and_lock_tables_derived() not to emit ER_LOCK_DEADLOCK error when mysql_lock_tables() is aborted in multi-statement transaction when somebody tries to acquire exclusive metadata lock on the table. Instead we release metadata locks acquired by current statement and try to wait until they can be re-acquired. - Adjusted tdc_wait_for_old_versions() to check if there is someone waiting for one of metadata locks held by this connection and run deadlock detection in order to avoid deadlocks in some situations. - Added mysql_abort_transactions_with_shared_lock() callback which allows to force transactions holding shared metadata lock on the table to call MDL_context::can_wait_lead_to_deadlock() even if they don't need any new metadata locks so they can detect potential deadlocks between metadata locking subsystem and table-level locks. - Adjusted wait_while_table_is_used() not to set TABLE::version to 0 as it is now done only when necessary by the above-mentioned callback. Also removed unnecessary call to mysql_lock_abort(). Instead we rely on code performing metadata lock upgrade aborting waits on the table-level lock for this table by calling mysql_lock_abort_for_thread() (invoked by mysql_notify_thread_having_shared_lock()). In future this should allow to reduce number of scenarios in which we produce ER_LOCK_DEADLOCK error even though no real deadlock exists. sql/sql_class.h: Introduced Open_table_context::m_start_of_statement_svp member to store state of metadata locks at the start of the statement. Replaced Open_table_context::m_can_deadlock member with m_has_locks member to reflect the fact that we no longer unconditionally emit ER_LOCK_DEADLOCK error for transaction having some metadata locks when conflicting metadata lock is discovered. sql/sql_insert.cc: close_tables_for_reopen() now takes one more argument which specifies at which point it should stop releasing metadata locks acquired by this connection. sql/sql_plist.h: Made I_P_List_iterator<T, B> usable with const lists. sql/sql_show.cc: close_tables_for_reopen() now takes one more argument which specifies at which point it should stop releasing metadata locks acquired by this connection. sql/sql_update.cc: Changed UPDATE and MULTI-UPDATE code not to release all metadata locks when calls to mysql_lock_tables() are aborted. Instead we release only locks which are acquired by this statement and then try to reacquire them by calling open_tables(). This solves bug #46273 "MySQL 5.4.4 new MDL: Bug#989 is not fully fixed in case of ALTER".
Diffstat (limited to 'mysql-test')
-rw-r--r--mysql-test/include/handler.inc32
-rw-r--r--mysql-test/r/handler_innodb.result27
-rw-r--r--mysql-test/r/handler_myisam.result27
-rw-r--r--mysql-test/r/mdl_sync.result251
-rw-r--r--mysql-test/r/sp-lock.result96
-rw-r--r--mysql-test/t/mdl_sync.test420
-rw-r--r--mysql-test/t/sp-lock.test210
7 files changed, 886 insertions, 177 deletions
diff --git a/mysql-test/include/handler.inc b/mysql-test/include/handler.inc
index a9965f97926..5562f7b2558 100644
--- a/mysql-test/include/handler.inc
+++ b/mysql-test/include/handler.inc
@@ -732,7 +732,7 @@ connection default;
--disable_warnings
drop table if exists t1;
--enable_warnings
-create table t1 (a int);
+create table t1 (a int, key a (a));
insert into t1 values (1);
handler t1 open;
connection con1;
@@ -743,7 +743,6 @@ let $wait_condition=
where state = "Waiting for table" and info = "alter table t1 engine=memory";
--source include/wait_condition.inc
connection default;
---error ER_ILLEGAL_HA
handler t1 read a next;
handler t1 close;
connection con1;
@@ -983,11 +982,12 @@ lock table t2 read;
--echo # --> connection con2
connection con2;
--echo # Sending:
---send drop table t2
+send rename table t2 to t3, t1 to t2, t3 to t1;
--echo # --> connection con1
connection con1;
---echo # Waiting for 'drop table t2' to get blocked...
-let $wait_condition=select count(*)=1 from information_schema.processlist where state='Waiting for table' and info='drop table t2';
+--echo # Waiting for 'rename table ...' to get blocked...
+let $wait_condition=select count(*)=1 from information_schema.processlist
+where state='Waiting for table' and info='rename table t2 to t3, t1 to t2, t3 to t1';
--source include/wait_condition.inc
--echo # --> connection default
connection default;
@@ -997,25 +997,26 @@ handler t2 open;
select * from t2;
handler t1 open;
commit;
---error ER_LOCK_DEADLOCK
-handler t2 open;
handler t1 close;
--echo # --> connection con1
connection con1;
unlock tables;
--echo # --> connection con2
connection con2;
---echo # Reaping 'drop table t2'...
+--echo # Reaping 'rename table ...'...
--reap
--echo # --> connection default
connection default;
handler t1 open;
handler t1 read a prev;
handler t1 close;
+drop table t2;
--echo #
---echo # Likewise, this doesn't require a multi-statement transaction.
---echo # ER_LOCK_DEADLOCK is also produced when we have an open
---echo # HANDLER and try to acquire locks for a single statement.
+--echo # Originally there was a deadlock error in this test.
+--echo # With implementation of deadlock detector
+--echo # we no longer deadlock, but block and wait on a lock.
+--echo # The HANDLER is auto-closed as soon as the connection
+--echo # sees a pending conflicting lock against it.
--echo #
create table t2 (a int, key a (a));
handler t1 open;
@@ -1033,10 +1034,12 @@ let $wait_condition=select count(*)=1 from information_schema.processlist where
--source include/wait_condition.inc
--echo # --> connection default
connection default;
---error ER_LOCK_DEADLOCK
-select * from t2;
+--echo # Sending 'select * from t2'
+send select * from t2;
--echo # --> connection con1
connection con1;
+--echo # Waiting for 'select * from t2' to get blocked...
+let $wait_condition=select count(*)=1 from information_schema.processlist where state='Waiting for table' and info='select * from t2';
unlock tables;
--echo # --> connection con2
connection con2;
@@ -1044,6 +1047,9 @@ connection con2;
--reap
--echo # --> connection default
connection default;
+--echo # Reaping 'select * from t2'
+--error ER_NO_SUCH_TABLE
+reap;
handler t1 close;
--echo #
diff --git a/mysql-test/r/handler_innodb.result b/mysql-test/r/handler_innodb.result
index df948f3d0b6..807e8becea8 100644
--- a/mysql-test/r/handler_innodb.result
+++ b/mysql-test/r/handler_innodb.result
@@ -745,12 +745,13 @@ drop table t1;
handler t1 read a next;
ERROR 42S02: Unknown table 't1' in HANDLER
drop table if exists t1;
-create table t1 (a int);
+create table t1 (a int, key a (a));
insert into t1 values (1);
handler t1 open;
alter table t1 engine=memory;
handler t1 read a next;
-ERROR HY000: Table storage engine for 't1' doesn't have this option
+a
+1
handler t1 close;
drop table t1;
USE information_schema;
@@ -1002,9 +1003,9 @@ a
lock table t2 read;
# --> connection con2
# Sending:
-drop table t2;
+rename table t2 to t3, t1 to t2, t3 to t1;
# --> connection con1
-# Waiting for 'drop table t2' to get blocked...
+# Waiting for 'rename table ...' to get blocked...
# --> connection default
handler t2 open;
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
@@ -1012,23 +1013,24 @@ select * from t2;
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
handler t1 open;
commit;
-handler t2 open;
-ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
handler t1 close;
# --> connection con1
unlock tables;
# --> connection con2
-# Reaping 'drop table t2'...
+# Reaping 'rename table ...'...
# --> connection default
handler t1 open;
handler t1 read a prev;
a
5
handler t1 close;
+drop table t2;
#
-# Likewise, this doesn't require a multi-statement transaction.
-# ER_LOCK_DEADLOCK is also produced when we have an open
-# HANDLER and try to acquire locks for a single statement.
+# Originally there was a deadlock error in this test.
+# With implementation of deadlock detector
+# we no longer deadlock, but block and wait on a lock.
+# The HANDLER is auto-closed as soon as the connection
+# sees a pending conflicting lock against it.
#
create table t2 (a int, key a (a));
handler t1 open;
@@ -1040,13 +1042,16 @@ drop table t2;
# --> connection con1
# Waiting for 'drop table t2' to get blocked...
# --> connection default
+# Sending 'select * from t2'
select * from t2;
-ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
# --> connection con1
+# Waiting for 'select * from t2' to get blocked...
unlock tables;
# --> connection con2
# Reaping 'drop table t2'...
# --> connection default
+# Reaping 'select * from t2'
+ERROR 42S02: Table 'test.t2' doesn't exist
handler t1 close;
#
# ROLLBACK TO SAVEPOINT releases transactional locks,
diff --git a/mysql-test/r/handler_myisam.result b/mysql-test/r/handler_myisam.result
index 4b287e6560b..adcbf068b97 100644
--- a/mysql-test/r/handler_myisam.result
+++ b/mysql-test/r/handler_myisam.result
@@ -743,12 +743,13 @@ drop table t1;
handler t1 read a next;
ERROR 42S02: Unknown table 't1' in HANDLER
drop table if exists t1;
-create table t1 (a int);
+create table t1 (a int, key a (a));
insert into t1 values (1);
handler t1 open;
alter table t1 engine=memory;
handler t1 read a next;
-ERROR HY000: Table storage engine for 't1' doesn't have this option
+a
+1
handler t1 close;
drop table t1;
USE information_schema;
@@ -999,9 +1000,9 @@ a
lock table t2 read;
# --> connection con2
# Sending:
-drop table t2;
+rename table t2 to t3, t1 to t2, t3 to t1;
# --> connection con1
-# Waiting for 'drop table t2' to get blocked...
+# Waiting for 'rename table ...' to get blocked...
# --> connection default
handler t2 open;
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
@@ -1009,23 +1010,24 @@ select * from t2;
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
handler t1 open;
commit;
-handler t2 open;
-ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
handler t1 close;
# --> connection con1
unlock tables;
# --> connection con2
-# Reaping 'drop table t2'...
+# Reaping 'rename table ...'...
# --> connection default
handler t1 open;
handler t1 read a prev;
a
5
handler t1 close;
+drop table t2;
#
-# Likewise, this doesn't require a multi-statement transaction.
-# ER_LOCK_DEADLOCK is also produced when we have an open
-# HANDLER and try to acquire locks for a single statement.
+# Originally there was a deadlock error in this test.
+# With implementation of deadlock detector
+# we no longer deadlock, but block and wait on a lock.
+# The HANDLER is auto-closed as soon as the connection
+# sees a pending conflicting lock against it.
#
create table t2 (a int, key a (a));
handler t1 open;
@@ -1037,13 +1039,16 @@ drop table t2;
# --> connection con1
# Waiting for 'drop table t2' to get blocked...
# --> connection default
+# Sending 'select * from t2'
select * from t2;
-ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
# --> connection con1
+# Waiting for 'select * from t2' to get blocked...
unlock tables;
# --> connection con2
# Reaping 'drop table t2'...
# --> connection default
+# Reaping 'select * from t2'
+ERROR 42S02: Table 'test.t2' doesn't exist
handler t1 close;
#
# ROLLBACK TO SAVEPOINT releases transactional locks,
diff --git a/mysql-test/r/mdl_sync.result b/mysql-test/r/mdl_sync.result
index ec02f29b008..0c9b6432e95 100644
--- a/mysql-test/r/mdl_sync.result
+++ b/mysql-test/r/mdl_sync.result
@@ -20,6 +20,220 @@ ERROR 42S02: Unknown table 't1'
drop table t3;
SET DEBUG_SYNC= 'RESET';
#
+# Test coverage for basic deadlock detection in metadata
+# locking subsystem.
+#
+drop tables if exists t1, t2, t3, t4;
+create table t1 (i int);
+create table t2 (j int);
+create table t3 (k int);
+create table t4 (k int);
+#
+# Test for the case in which no deadlock occurs.
+#
+#
+# Switching to connection 'deadlock_con1'.
+begin;
+insert into t1 values (1);
+#
+# Switching to connection 'deadlock_con2'.
+begin;
+insert into t2 values (1);
+#
+# Switching to connection 'default'.
+# Send:
+rename table t2 to t0, t3 to t2, t0 to t3;;
+#
+# Switching to connection 'deadlock_con1'.
+# Wait until the above RENAME TABLE is blocked because it has to wait
+# for 'deadlock_con2' which holds shared metadata lock on 't2'.
+# The below statement should wait for exclusive metadata lock
+# on 't2' to go away and should not produce ER_LOCK_DEADLOCK
+# as no deadlock is possible in this situation.
+# Send:
+select * from t2;;
+#
+# Switching to connection 'deadlock_con2'.
+# Wait until the above SELECT * FROM t2 is starts waiting
+# for an exclusive metadata lock to go away.
+#
+# Unblock RENAME TABLE by releasing shared metadata lock on t2.
+commit;
+#
+# Switching to connection 'default'.
+# Reap RENAME TABLE.
+#
+# Switching to connection 'deadlock_con1'.
+# Reap SELECT.
+k
+#
+# Switching to connection 'default'.
+#
+# Let us check that in the process of waiting for conflicting lock
+# on table 't2' to go away transaction in connection 'deadlock_con1'
+# has not released metadata lock on table 't1'.
+# Send:
+rename table t1 to t0, t3 to t1, t0 to t3;;
+#
+# Switching to connection 'deadlock_con1'.
+# Wait until the above RENAME TABLE is blocked because it has to wait
+# for 'deadlock_con1' which should still hold shared metadata lock on
+# table 't1'.
+# Commit transaction to unblock RENAME TABLE.
+commit;
+#
+# Switching to connection 'default'.
+# Reap RENAME TABLE.
+#
+# Test for case when deadlock occurs and should be detected immediately.
+#
+#
+# Switching to connection 'deadlock_con1'.
+begin;
+insert into t1 values (2);
+#
+# Switching to connection 'default'.
+# Send:
+rename table t2 to t0, t1 to t2, t0 to t1;;
+#
+# Switching to connection 'deadlock_con1'.
+# Wait until the above RENAME TABLE is blocked because it has to wait
+# for 'deadlock_con1' which holds shared metadata lock on 't1'.
+#
+# The below statement should not wait as doing so will cause deadlock.
+# Instead it should fail and emit ER_LOCK_DEADLOCK statement.
+select * from t2;
+ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
+#
+# Let us check that failure of the above statement has not released
+# metadata lock on table 't1', i.e. that RENAME TABLE is still blocked.
+# Commit transaction to unblock RENAME TABLE.
+commit;
+#
+# Switching to connection 'default'.
+# Reap RENAME TABLE.
+#
+# Test for the case in which deadlock also occurs but not immediately.
+#
+#
+# Switching to connection 'deadlock_con1'.
+begin;
+insert into t1 values (1);
+#
+# Switching to connection 'deadlock_con2'.
+begin;
+insert into t3 values (1);
+#
+# Switching to connection 'default'.
+# Send:
+rename table t2 to t0, t3 to t2, t0 to t3;;
+#
+# Switching to connection 'deadlock_con1'.
+# Wait until the above RENAME TABLE is blocked because it has to wait
+# for 'deadlock_con2' which holds shared metadata lock on 't3'.
+# The below SELECT statement should wait for metadata lock
+# on table 't2' and should not produce ER_LOCK_DEADLOCK
+# immediately as no deadlock is possible at the moment.
+select * from t2;;
+#
+# Switching to connection 'deadlock_con3'.
+# Wait until the above SELECT * FROM t2 is starts waiting
+# for an exclusive metadata lock to go away.
+# Send RENAME TABLE statement that will deadlock with the
+# SELECT statement and thus should abort the latter.
+rename table t1 to t0, t2 to t1, t0 to t2;;
+#
+# Switching to connection 'deadlock_con1'.
+# Since the latest RENAME TABLE entered in deadlock with SELECT
+# statement the latter should be aborted and emit ER_LOCK_DEADLOCK
+# error.
+# Reap SELECT * FROM t2.
+ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
+#
+# Again let us check that failure of the SELECT statement has not
+# released metadata lock on table 't1', i.e. that the latest RENAME
+# is blocked.
+# Commit transaction to unblock this RENAME TABLE.
+commit;
+#
+# Switching to connection 'deadlock_con3'.
+# Reap RENAME TABLE t1 TO t0 ... .
+#
+# Switching to connection 'deadlock_con2'.
+# Commit transaction to unblock the first RENAME TABLE.
+commit;
+#
+# Switching to connection 'default'.
+# Reap RENAME TABLE t2 TO t0 ... .
+drop tables t1, t2, t3, t4;
+#
+# Now, test case which shows that deadlock detection empiric
+# also takes into account requests for metadata lock upgrade.
+#
+create table t1 (i int);
+#
+# Switching to connection 'deadlock_con1'.
+begin;
+insert into t1 values (1);
+#
+# Switching to connection 'default'.
+# Send:
+alter table t1 add column j int, rename to t2;;
+#
+# Switching to connection 'deadlock_con1'.
+# Wait until the above ALTER TABLE ... RENAME acquires exclusive
+# metadata lock on 't2' and starts waiting for connection
+# 'deadlock_con1' which holds shared lock on 't1'.
+# The below statement should not wait as it will cause deadlock.
+# An appropriate error should be reported instead.
+select * from t2;
+ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
+# Again let us check that failure of the above statement has not
+# released all metadata locks in connection 'deadlock_con1' and
+# so ALTER TABLE ... RENAME is still blocked.
+# Commit transaction to unblock ALTER TABLE ... RENAME.
+commit;
+#
+# Switching to connection 'default'.
+# Reap ALTER TABLE ... RENAME.
+drop table t2;
+#
+# Finally, test case in which deadlock (or potentially livelock) occurs
+# between metadata locking subsystem and table definition cache/table
+# locks, but which should still be detected by our empiric.
+#
+create table t1 (i int);
+#
+# Switching to connection 'deadlock_con1'.
+begin;
+insert into t1 values (1);
+#
+# Switching to connection 'default'.
+lock tables t1 write;
+#
+# Switching to connection 'deadlock_con1'.
+# Send:
+insert into t1 values (2);;
+#
+# Switching to connection 'default'.
+# Wait until INSERT in connection 'deadlock_con1' is blocked on
+# table-level lock.
+# Send:
+alter table t1 add column j int;;
+#
+# Switching to connection 'deadlock_con1'.
+# The above ALTER TABLE statement should cause INSERT statement in
+# this connection to be aborted and emit ER_LOCK_DEADLOCK error.
+# Reap INSERT
+ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
+# Commit transaction to unblock ALTER TABLE.
+commit;
+#
+# Switching to connection 'default'.
+# Reap ALTER TABLE.
+unlock tables;
+drop table t1;
+#
# Test for bug #46748 "Assertion in MDL_context::wait_for_locks()
# on INSERT + CREATE TRIGGER".
#
@@ -234,6 +448,43 @@ drop table t2;
# Clean-up.
drop table t1;
#
+# Test for bug #46273 "MySQL 5.4.4 new MDL: Bug#989 is not fully fixed
+# in case of ALTER".
+#
+drop table if exists t1;
+set debug_sync= 'RESET';
+create table t1 (c1 int primary key, c2 int, c3 int);
+insert into t1 values (1,1,0),(2,2,0),(3,3,0),(4,4,0),(5,5,0);
+begin;
+update t1 set c3=c3+1 where c2=3;
+#
+# Switching to connection 'con46273'.
+set debug_sync='after_lock_tables_takes_lock SIGNAL alter_table_locked WAIT_FOR alter_go';
+alter table t1 add column e int, rename to t2;;
+#
+# Switching to connection 'default'.
+set debug_sync='now WAIT_FOR alter_table_locked';
+set debug_sync='wait_for_lock SIGNAL alter_go';
+# The below statement should get ER_LOCK_DEADLOCK error
+# (i.e. it should not allow ALTER to proceed, and then
+# fail due to 't1' changing its name to 't2').
+update t1 set c3=c3+1 where c2=4;
+ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
+#
+# Let us check that failure of the above statement has not released
+# metadata lock on table 't1', i.e. that ALTER TABLE is still blocked.
+# Unblock ALTER TABLE by commiting transaction and thus releasing
+# metadata lock on 't1'.
+commit;
+#
+# Switching to connection 'con46273'.
+# Reap ALTER TABLE.
+#
+# Switching to connection 'default'.
+# Clean-up.
+set debug_sync= 'RESET';
+drop table t2;
+#
# Test for bug #46673 "Deadlock between FLUSH TABLES WITH READ LOCK
# and DML".
#
diff --git a/mysql-test/r/sp-lock.result b/mysql-test/r/sp-lock.result
index 65524d02d08..e9087f61807 100644
--- a/mysql-test/r/sp-lock.result
+++ b/mysql-test/r/sp-lock.result
@@ -125,16 +125,15 @@ drop temporary table t1;
#
# For that, start a transaction, use a routine. In a concurrent
# connection, try to drop or alter the routine. It should place
-# a pending or exlusive lock and block. In a concurrnet
-# connection, try to use the routine under LOCK TABLES.
-# That should yield ER_LOCK_DEADLOCK.
+# a pending or exclusive lock and block. In another concurrnet
+# connection, try to use the routine.
+# That should block on the pending exclusive lock.
#
# Establish helper connections.
#
# Test DROP PROCEDURE.
#
# --> connection default
-create table t1 (a int);
create procedure p1() begin end;
create function f1() returns int
begin
@@ -151,14 +150,17 @@ drop procedure p1;
# --> connection con2
# Waitng for 'drop procedure t1' to get blocked on MDL lock...
# Demonstrate that there is a pending exclusive lock.
-lock table t1 read;
+# Sending 'select f1()'...
select f1();
-ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
-unlock tables;
+# --> connection con3
+# Waitng for 'select f1()' to get blocked by a pending MDL lock...
# --> connection default
commit;
# --> connection con1
# Reaping 'drop procedure p1'...
+# --> connection con2
+# Reaping 'select f1()'
+ERROR 42000: PROCEDURE test.p1 does not exist
# --> connection default
#
# Test CREATE PROCEDURE.
@@ -174,17 +176,22 @@ create procedure p1() begin end;
# --> connection con2
# Waitng for 'create procedure t1' to get blocked on MDL lock...
# Demonstrate that there is a pending exclusive lock.
-lock table t1 read;
+# Sending 'select f1()'...
select f1();
-ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
-unlock tables;
+# --> connection con3
+# Waitng for 'select f1()' to get blocked by a pending MDL lock...
# --> connection default
commit;
# --> connection con1
# Reaping 'create procedure p1'...
ERROR 42000: PROCEDURE p1 already exists
+# --> connection con2
+# Reaping 'select f1()'
+f1()
+1
#
# Test ALTER PROCEDURE.
+#
begin;
select f1();
f1()
@@ -195,14 +202,18 @@ alter procedure p1 contains sql;
# --> connection con2
# Waitng for 'alter procedure t1' to get blocked on MDL lock...
# Demonstrate that there is a pending exclusive lock.
-lock table t1 read;
+# Sending 'select f1()'...
select f1();
-ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
-unlock tables;
+# --> connection con3
+# Waitng for 'select f1()' to get blocked by a pending MDL lock...
# --> connection default
commit;
# --> connection con1
# Reaping 'alter procedure p1'...
+# --> connection con2
+# Reaping 'select f1()'
+f1()
+1
# --> connection default
#
# Test DROP FUNCTION.
@@ -217,14 +228,17 @@ drop function f1;
# --> connection con2
# Waitng for 'drop function f1' to get blocked on MDL lock...
# Demonstrate that there is a pending exclusive lock.
-lock table t1 read;
+# Sending 'select f1()'...
select f1();
-ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
-unlock tables;
+# --> connection con3
+# Waitng for 'select f1()' to get blocked by a pending MDL lock...
# --> connection default
commit;
# --> connection con1
# Reaping 'drop function f1'...
+# --> connection con2
+# Reaping 'select f1()'
+ERROR 42000: FUNCTION test.f1 does not exist
# --> connection default
#
# Test CREATE FUNCTION.
@@ -240,18 +254,23 @@ create function f1() returns int return 2;
# --> connection con2
# Waitng for 'create function f1' to get blocked on MDL lock...
# Demonstrate that there is a pending exclusive lock.
-lock table t1 read;
+# Sending 'select f1()'...
select f1();
-ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
-unlock tables;
+# --> connection con3
+# Waitng for 'select f1()' to get blocked by a pending MDL lock...
# --> connection default
commit;
# --> connection con1
# Reaping 'create function f1'...
ERROR 42000: FUNCTION f1 already exists
+# --> connection con2
+# Reaping 'select f1()'
+f1()
+1
# --> connection default
#
# Test ALTER FUNCTION.
+#
begin;
select f1();
f1()
@@ -262,14 +281,18 @@ alter function f1 contains sql;
# --> connection con2
# Waitng for 'alter function f1' to get blocked on MDL lock...
# Demonstrate that there is a pending exclusive lock.
-lock table t1 read;
+# Sending 'select f1()'...
select f1();
-ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
-unlock tables;
+# --> connection con3
+# Waitng for 'select f1()' to get blocked by a pending MDL lock...
# --> connection default
commit;
# --> connection con1
# Reaping 'alter function f1'...
+# --> connection con2
+# Reaping 'select f1()'
+f1()
+1
# --> connection default
drop function f1;
drop procedure p1;
@@ -283,6 +306,7 @@ drop procedure p1;
#
create procedure p1() begin end;
create procedure p2() begin end;
+create table t1 (a int);
create procedure p3()
begin
call p1();
@@ -415,36 +439,11 @@ drop table t1, t2;
# acquisition of a shared lock fails during a transaction or
# we need to back off to flush the sp cache.
#
-# a) A back off due to a lock conflict.
-#
-create table t1 (a int);
-create function f1() returns int return 6;
-begin;
-select f1();
-f1()
-6
-# --> connection con1
-# Sending 'drop function f1'...
-drop function f1;
-# --> connection con2
-# Waitng for 'drop function f1' to get blocked on MDL lock...
-begin;
-select * from t1;
-a
-select f1();
-ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
-commit;
-# --> connection default
-commit;
-# --> connection con1
-# Reaping 'drop function f1'...
-# --> connection default
-#
-# b) A back off to flush the cache.
# Sic: now this situation does not require a back off since we
# flush the cache on the fly.
#
create function f1() returns int return 7;
+create table t1 (a int);
begin;
select * from t1;
a
@@ -691,6 +690,7 @@ drop function f1;
set @@session.max_sp_recursion_depth=default;
# --> connection con1
# --> connection con2
+# --> connection con3
# --> connection default
#
# End of 5.5 tests
diff --git a/mysql-test/t/mdl_sync.test b/mysql-test/t/mdl_sync.test
index e3aceaa05fa..c817012fb2f 100644
--- a/mysql-test/t/mdl_sync.test
+++ b/mysql-test/t/mdl_sync.test
@@ -74,6 +74,366 @@ SET DEBUG_SYNC= 'RESET';
--echo #
+--echo # Test coverage for basic deadlock detection in metadata
+--echo # locking subsystem.
+--echo #
+--disable_warnings
+drop tables if exists t1, t2, t3, t4;
+--enable_warnings
+
+connect(deadlock_con1,localhost,root,,);
+connect(deadlock_con2,localhost,root,,);
+connect(deadlock_con3,localhost,root,,);
+connection default;
+create table t1 (i int);
+create table t2 (j int);
+create table t3 (k int);
+create table t4 (k int);
+
+--echo #
+--echo # Test for the case in which no deadlock occurs.
+--echo #
+
+--echo #
+--echo # Switching to connection 'deadlock_con1'.
+connection deadlock_con1;
+begin;
+insert into t1 values (1);
+
+--echo #
+--echo # Switching to connection 'deadlock_con2'.
+connection deadlock_con2;
+begin;
+insert into t2 values (1);
+
+--echo #
+--echo # Switching to connection 'default'.
+connection default;
+--echo # Send:
+--send rename table t2 to t0, t3 to t2, t0 to t3;
+
+--echo #
+--echo # Switching to connection 'deadlock_con1'.
+connection deadlock_con1;
+--echo # Wait until the above RENAME TABLE is blocked because it has to wait
+--echo # for 'deadlock_con2' which holds shared metadata lock on 't2'.
+let $wait_condition=
+ select count(*) = 1 from information_schema.processlist
+ where state = "Waiting for table" and info = "rename table t2 to t0, t3 to t2, t0 to t3";
+--source include/wait_condition.inc
+--echo # The below statement should wait for exclusive metadata lock
+--echo # on 't2' to go away and should not produce ER_LOCK_DEADLOCK
+--echo # as no deadlock is possible in this situation.
+--echo # Send:
+--send select * from t2;
+
+--echo #
+--echo # Switching to connection 'deadlock_con2'.
+connection deadlock_con2;
+--echo # Wait until the above SELECT * FROM t2 is starts waiting
+--echo # for an exclusive metadata lock to go away.
+let $wait_condition=
+ select count(*) = 1 from information_schema.processlist
+ where state = "Waiting for table" and info = "select * from t2";
+--source include/wait_condition.inc
+--echo #
+--echo # Unblock RENAME TABLE by releasing shared metadata lock on t2.
+commit;
+
+--echo #
+--echo # Switching to connection 'default'.
+connection default;
+--echo # Reap RENAME TABLE.
+--reap
+
+--echo #
+--echo # Switching to connection 'deadlock_con1'.
+connection deadlock_con1;
+--echo # Reap SELECT.
+--reap
+
+--echo #
+--echo # Switching to connection 'default'.
+connection default;
+--echo #
+--echo # Let us check that in the process of waiting for conflicting lock
+--echo # on table 't2' to go away transaction in connection 'deadlock_con1'
+--echo # has not released metadata lock on table 't1'.
+--echo # Send:
+--send rename table t1 to t0, t3 to t1, t0 to t3;
+
+--echo #
+--echo # Switching to connection 'deadlock_con1'.
+connection deadlock_con1;
+--echo # Wait until the above RENAME TABLE is blocked because it has to wait
+--echo # for 'deadlock_con1' which should still hold shared metadata lock on
+--echo # table 't1'.
+let $wait_condition=
+ select count(*) = 1 from information_schema.processlist
+ where state = "Waiting for table" and info = "rename table t1 to t0, t3 to t1, t0 to t3";
+--source include/wait_condition.inc
+--echo # Commit transaction to unblock RENAME TABLE.
+commit;
+
+--echo #
+--echo # Switching to connection 'default'.
+connection default;
+--echo # Reap RENAME TABLE.
+--reap
+
+--echo #
+--echo # Test for case when deadlock occurs and should be detected immediately.
+--echo #
+
+--echo #
+--echo # Switching to connection 'deadlock_con1'.
+connection deadlock_con1;
+begin;
+insert into t1 values (2);
+
+--echo #
+--echo # Switching to connection 'default'.
+connection default;
+--echo # Send:
+--send rename table t2 to t0, t1 to t2, t0 to t1;
+
+--echo #
+--echo # Switching to connection 'deadlock_con1'.
+connection deadlock_con1;
+--echo # Wait until the above RENAME TABLE is blocked because it has to wait
+--echo # for 'deadlock_con1' which holds shared metadata lock on 't1'.
+let $wait_condition=
+ select count(*) = 1 from information_schema.processlist
+ where state = "Waiting for table" and info = "rename table t2 to t0, t1 to t2, t0 to t1";
+--source include/wait_condition.inc
+--echo #
+--echo # The below statement should not wait as doing so will cause deadlock.
+--echo # Instead it should fail and emit ER_LOCK_DEADLOCK statement.
+--error ER_LOCK_DEADLOCK
+select * from t2;
+
+--echo #
+--echo # Let us check that failure of the above statement has not released
+--echo # metadata lock on table 't1', i.e. that RENAME TABLE is still blocked.
+let $wait_condition=
+ select count(*) = 1 from information_schema.processlist
+ where state = "Waiting for table" and info = "rename table t2 to t0, t1 to t2, t0 to t1";
+--source include/wait_condition.inc
+--echo # Commit transaction to unblock RENAME TABLE.
+commit;
+
+--echo #
+--echo # Switching to connection 'default'.
+connection default;
+--echo # Reap RENAME TABLE.
+--reap
+
+--echo #
+--echo # Test for the case in which deadlock also occurs but not immediately.
+--echo #
+
+--echo #
+--echo # Switching to connection 'deadlock_con1'.
+connection deadlock_con1;
+begin;
+insert into t1 values (1);
+
+--echo #
+--echo # Switching to connection 'deadlock_con2'.
+connection deadlock_con2;
+begin;
+insert into t3 values (1);
+
+--echo #
+--echo # Switching to connection 'default'.
+connection default;
+--echo # Send:
+--send rename table t2 to t0, t3 to t2, t0 to t3;
+
+--echo #
+--echo # Switching to connection 'deadlock_con1'.
+connection deadlock_con1;
+--echo # Wait until the above RENAME TABLE is blocked because it has to wait
+--echo # for 'deadlock_con2' which holds shared metadata lock on 't3'.
+let $wait_condition=
+ select count(*) = 1 from information_schema.processlist
+ where state = "Waiting for table" and info = "rename table t2 to t0, t3 to t2, t0 to t3";
+--source include/wait_condition.inc
+--echo # The below SELECT statement should wait for metadata lock
+--echo # on table 't2' and should not produce ER_LOCK_DEADLOCK
+--echo # immediately as no deadlock is possible at the moment.
+--send select * from t2;
+
+--echo #
+--echo # Switching to connection 'deadlock_con3'.
+connection deadlock_con3;
+--echo # Wait until the above SELECT * FROM t2 is starts waiting
+--echo # for an exclusive metadata lock to go away.
+let $wait_condition=
+ select count(*) = 1 from information_schema.processlist
+ where state = "Waiting for table" and info = "select * from t2";
+--source include/wait_condition.inc
+
+--echo # Send RENAME TABLE statement that will deadlock with the
+--echo # SELECT statement and thus should abort the latter.
+--send rename table t1 to t0, t2 to t1, t0 to t2;
+
+--echo #
+--echo # Switching to connection 'deadlock_con1'.
+connection deadlock_con1;
+--echo # Since the latest RENAME TABLE entered in deadlock with SELECT
+--echo # statement the latter should be aborted and emit ER_LOCK_DEADLOCK
+--echo # error.
+--echo # Reap SELECT * FROM t2.
+--error ER_LOCK_DEADLOCK
+--reap
+
+--echo #
+--echo # Again let us check that failure of the SELECT statement has not
+--echo # released metadata lock on table 't1', i.e. that the latest RENAME
+--echo # is blocked.
+let $wait_condition=
+ select count(*) = 1 from information_schema.processlist
+ where state = "Waiting for table" and info = "rename table t1 to t0, t2 to t1, t0 to t2";
+--source include/wait_condition.inc
+--echo # Commit transaction to unblock this RENAME TABLE.
+commit;
+
+--echo #
+--echo # Switching to connection 'deadlock_con3'.
+connection deadlock_con3;
+--echo # Reap RENAME TABLE t1 TO t0 ... .
+--reap;
+
+--echo #
+--echo # Switching to connection 'deadlock_con2'.
+connection deadlock_con2;
+--echo # Commit transaction to unblock the first RENAME TABLE.
+commit;
+
+--echo #
+--echo # Switching to connection 'default'.
+connection default;
+--echo # Reap RENAME TABLE t2 TO t0 ... .
+--reap
+
+drop tables t1, t2, t3, t4;
+
+--echo #
+--echo # Now, test case which shows that deadlock detection empiric
+--echo # also takes into account requests for metadata lock upgrade.
+--echo #
+create table t1 (i int);
+
+--echo #
+--echo # Switching to connection 'deadlock_con1'.
+connection deadlock_con1;
+begin;
+insert into t1 values (1);
+
+--echo #
+--echo # Switching to connection 'default'.
+connection default;
+--echo # Send:
+--send alter table t1 add column j int, rename to t2;
+
+--echo #
+--echo # Switching to connection 'deadlock_con1'.
+connection deadlock_con1;
+--echo # Wait until the above ALTER TABLE ... RENAME acquires exclusive
+--echo # metadata lock on 't2' and starts waiting for connection
+--echo # 'deadlock_con1' which holds shared lock on 't1'.
+let $wait_condition=
+ select count(*) = 1 from information_schema.processlist
+ where state = "Waiting for table" and info = "alter table t1 add column j int, rename to t2";
+--source include/wait_condition.inc
+
+--echo # The below statement should not wait as it will cause deadlock.
+--echo # An appropriate error should be reported instead.
+--error ER_LOCK_DEADLOCK
+select * from t2;
+
+--echo # Again let us check that failure of the above statement has not
+--echo # released all metadata locks in connection 'deadlock_con1' and
+--echo # so ALTER TABLE ... RENAME is still blocked.
+let $wait_condition=
+ select count(*) = 1 from information_schema.processlist
+ where state = "Waiting for table" and info = "alter table t1 add column j int, rename to t2";
+--source include/wait_condition.inc
+
+--echo # Commit transaction to unblock ALTER TABLE ... RENAME.
+commit;
+
+--echo #
+--echo # Switching to connection 'default'.
+connection default;
+--echo # Reap ALTER TABLE ... RENAME.
+--reap
+
+drop table t2;
+
+--echo #
+--echo # Finally, test case in which deadlock (or potentially livelock) occurs
+--echo # between metadata locking subsystem and table definition cache/table
+--echo # locks, but which should still be detected by our empiric.
+--echo #
+create table t1 (i int);
+
+--echo #
+--echo # Switching to connection 'deadlock_con1'.
+connection deadlock_con1;
+begin;
+insert into t1 values (1);
+
+--echo #
+--echo # Switching to connection 'default'.
+connection default;
+lock tables t1 write;
+
+--echo #
+--echo # Switching to connection 'deadlock_con1'.
+connection deadlock_con1;
+--echo # Send:
+--send insert into t1 values (2);
+
+--echo #
+--echo # Switching to connection 'default'.
+connection default;
+--echo # Wait until INSERT in connection 'deadlock_con1' is blocked on
+--echo # table-level lock.
+let $wait_condition=
+ select count(*) = 1 from information_schema.processlist
+ where state = "Table lock" and info = "insert into t1 values (2)";
+--source include/wait_condition.inc
+
+--echo # Send:
+--send alter table t1 add column j int;
+
+--echo #
+--echo # Switching to connection 'deadlock_con1'.
+connection deadlock_con1;
+--echo # The above ALTER TABLE statement should cause INSERT statement in
+--echo # this connection to be aborted and emit ER_LOCK_DEADLOCK error.
+--echo # Reap INSERT
+--error ER_LOCK_DEADLOCK
+--reap
+--echo # Commit transaction to unblock ALTER TABLE.
+commit;
+
+--echo #
+--echo # Switching to connection 'default'.
+connection default;
+--echo # Reap ALTER TABLE.
+--reap
+unlock tables;
+
+drop table t1;
+disconnect deadlock_con1;
+disconnect deadlock_con2;
+disconnect deadlock_con3;
+
+
+--echo #
--echo # Test for bug #46748 "Assertion in MDL_context::wait_for_locks()
--echo # on INSERT + CREATE TRIGGER".
--echo #
@@ -440,6 +800,66 @@ drop table t1;
--echo #
+--echo # Test for bug #46273 "MySQL 5.4.4 new MDL: Bug#989 is not fully fixed
+--echo # in case of ALTER".
+--echo #
+--disable_warnings
+drop table if exists t1;
+--enable_warnings
+set debug_sync= 'RESET';
+connect (con46273,localhost,root,,test,,);
+connection default;
+create table t1 (c1 int primary key, c2 int, c3 int);
+insert into t1 values (1,1,0),(2,2,0),(3,3,0),(4,4,0),(5,5,0);
+
+begin;
+update t1 set c3=c3+1 where c2=3;
+
+--echo #
+--echo # Switching to connection 'con46273'.
+connection con46273;
+set debug_sync='after_lock_tables_takes_lock SIGNAL alter_table_locked WAIT_FOR alter_go';
+--send alter table t1 add column e int, rename to t2;
+
+--echo #
+--echo # Switching to connection 'default'.
+connection default;
+set debug_sync='now WAIT_FOR alter_table_locked';
+set debug_sync='wait_for_lock SIGNAL alter_go';
+--echo # The below statement should get ER_LOCK_DEADLOCK error
+--echo # (i.e. it should not allow ALTER to proceed, and then
+--echo # fail due to 't1' changing its name to 't2').
+--error ER_LOCK_DEADLOCK
+update t1 set c3=c3+1 where c2=4;
+
+--echo #
+--echo # Let us check that failure of the above statement has not released
+--echo # metadata lock on table 't1', i.e. that ALTER TABLE is still blocked.
+let $wait_condition=
+ select count(*) = 1 from information_schema.processlist
+ where state = "Waiting for table" and info = "alter table t1 add column e int, rename to t2";
+--source include/wait_condition.inc
+
+--echo # Unblock ALTER TABLE by commiting transaction and thus releasing
+--echo # metadata lock on 't1'.
+commit;
+
+--echo #
+--echo # Switching to connection 'con46273'.
+connection con46273;
+--echo # Reap ALTER TABLE.
+--reap
+
+--echo #
+--echo # Switching to connection 'default'.
+connection default;
+disconnect con46273;
+--echo # Clean-up.
+set debug_sync= 'RESET';
+drop table t2;
+
+
+--echo #
--echo # Test for bug #46673 "Deadlock between FLUSH TABLES WITH READ LOCK
--echo # and DML".
--echo #
diff --git a/mysql-test/t/sp-lock.test b/mysql-test/t/sp-lock.test
index a90b85a59be..0b31eabb0f1 100644
--- a/mysql-test/t/sp-lock.test
+++ b/mysql-test/t/sp-lock.test
@@ -153,20 +153,20 @@ drop temporary table t1;
--echo #
--echo # For that, start a transaction, use a routine. In a concurrent
--echo # connection, try to drop or alter the routine. It should place
---echo # a pending or exlusive lock and block. In a concurrnet
---echo # connection, try to use the routine under LOCK TABLES.
---echo # That should yield ER_LOCK_DEADLOCK.
+--echo # a pending or exclusive lock and block. In another concurrnet
+--echo # connection, try to use the routine.
+--echo # That should block on the pending exclusive lock.
--echo #
--echo # Establish helper connections.
connect(con1, localhost, root,,);
connect(con2, localhost, root,,);
+connect(con3, localhost, root,,);
--echo #
--echo # Test DROP PROCEDURE.
--echo #
--echo # --> connection default
connection default;
-create table t1 (a int);
create procedure p1() begin end;
delimiter |;
create function f1() returns int
@@ -180,7 +180,7 @@ select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'drop procedure p1'...
---send drop procedure p1
+send drop procedure p1;
--echo # --> connection con2
connection con2;
--echo # Waitng for 'drop procedure t1' to get blocked on MDL lock...
@@ -188,17 +188,25 @@ let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for table' and info='drop procedure p1';
--source include/wait_condition.inc
--echo # Demonstrate that there is a pending exclusive lock.
-lock table t1 read;
---error ER_LOCK_DEADLOCK
-select f1();
-unlock tables;
+--echo # Sending 'select f1()'...
+send select f1();
+--echo # --> connection con3
+connection con3;
+--echo # Waitng for 'select f1()' to get blocked by a pending MDL lock...
+let $wait_condition=select count(*)=1 from information_schema.processlist
+where state='Waiting for table' and info='select f1()';
--echo # --> connection default
connection default;
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'drop procedure p1'...
---reap
+reap;
+--echo # --> connection con2
+connection con2;
+--echo # Reaping 'select f1()'
+--error ER_SP_DOES_NOT_EXIST
+reap;
--echo # --> connection default
connection default;
@@ -211,7 +219,7 @@ select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'create procedure p1'...
---send create procedure p1() begin end
+send create procedure p1() begin end;
--echo # --> connection con2
connection con2;
--echo # Waitng for 'create procedure t1' to get blocked on MDL lock...
@@ -219,10 +227,13 @@ let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for table' and info='create procedure p1() begin end';
--source include/wait_condition.inc
--echo # Demonstrate that there is a pending exclusive lock.
-lock table t1 read;
---error ER_LOCK_DEADLOCK
-select f1();
-unlock tables;
+--echo # Sending 'select f1()'...
+send select f1();
+--echo # --> connection con3
+connection con3;
+--echo # Waitng for 'select f1()' to get blocked by a pending MDL lock...
+let $wait_condition=select count(*)=1 from information_schema.processlist
+where state='Waiting for table' and info='select f1()';
--echo # --> connection default
connection default;
commit;
@@ -230,16 +241,22 @@ commit;
connection con1;
--echo # Reaping 'create procedure p1'...
--error ER_SP_ALREADY_EXISTS
---reap
+reap;
+--echo # --> connection con2
+connection con2;
+--echo # Reaping 'select f1()'
+reap;
connection default;
+
--echo #
--echo # Test ALTER PROCEDURE.
+--echo #
begin;
select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'alter procedure p1'...
---send alter procedure p1 contains sql
+send alter procedure p1 contains sql;
--echo # --> connection con2
connection con2;
--echo # Waitng for 'alter procedure t1' to get blocked on MDL lock...
@@ -247,17 +264,24 @@ let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for table' and info='alter procedure p1 contains sql';
--source include/wait_condition.inc
--echo # Demonstrate that there is a pending exclusive lock.
-lock table t1 read;
---error ER_LOCK_DEADLOCK
-select f1();
-unlock tables;
+--echo # Sending 'select f1()'...
+send select f1();
+--echo # --> connection con3
+connection con3;
+--echo # Waitng for 'select f1()' to get blocked by a pending MDL lock...
+let $wait_condition=select count(*)=1 from information_schema.processlist
+where state='Waiting for table' and info='select f1()';
--echo # --> connection default
connection default;
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'alter procedure p1'...
---reap
+reap;
+--echo # --> connection con2
+connection con2;
+--echo # Reaping 'select f1()'
+reap;
--echo # --> connection default
connection default;
@@ -269,7 +293,7 @@ select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'drop function f1'...
---send drop function f1
+send drop function f1;
--echo # --> connection con2
connection con2;
--echo # Waitng for 'drop function f1' to get blocked on MDL lock...
@@ -277,17 +301,25 @@ let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for table' and info='drop function f1';
--source include/wait_condition.inc
--echo # Demonstrate that there is a pending exclusive lock.
-lock table t1 read;
---error ER_LOCK_DEADLOCK
-select f1();
-unlock tables;
+--echo # Sending 'select f1()'...
+send select f1();
+--echo # --> connection con3
+connection con3;
+--echo # Waitng for 'select f1()' to get blocked by a pending MDL lock...
+let $wait_condition=select count(*)=1 from information_schema.processlist
+where state='Waiting for table' and info='select f1()';
--echo # --> connection default
connection default;
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'drop function f1'...
---reap
+reap;
+--echo # --> connection con2
+connection con2;
+--echo # Reaping 'select f1()'
+--error ER_SP_DOES_NOT_EXIST
+reap;
--echo # --> connection default
connection default;
@@ -300,7 +332,7 @@ select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'create function f1'...
---send create function f1() returns int return 2
+send create function f1() returns int return 2;
--echo # --> connection con2
connection con2;
--echo # Waitng for 'create function f1' to get blocked on MDL lock...
@@ -308,10 +340,13 @@ let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for table' and info='create function f1() returns int return 2';
--source include/wait_condition.inc
--echo # Demonstrate that there is a pending exclusive lock.
-lock table t1 read;
---error ER_LOCK_DEADLOCK
-select f1();
-unlock tables;
+--echo # Sending 'select f1()'...
+send select f1();
+--echo # --> connection con3
+connection con3;
+--echo # Waitng for 'select f1()' to get blocked by a pending MDL lock...
+let $wait_condition=select count(*)=1 from information_schema.processlist
+where state='Waiting for table' and info='select f1()';
--echo # --> connection default
connection default;
commit;
@@ -319,17 +354,23 @@ commit;
connection con1;
--echo # Reaping 'create function f1'...
--error ER_SP_ALREADY_EXISTS
---reap
+reap;
+--echo # --> connection con2
+connection con2;
+--echo # Reaping 'select f1()'
+reap;
--echo # --> connection default
connection default;
+
--echo #
--echo # Test ALTER FUNCTION.
+--echo #
begin;
select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'alter function f1'...
---send alter function f1 contains sql
+send alter function f1 contains sql;
--echo # --> connection con2
connection con2;
--echo # Waitng for 'alter function f1' to get blocked on MDL lock...
@@ -337,17 +378,24 @@ let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for table' and info='alter function f1 contains sql';
--source include/wait_condition.inc
--echo # Demonstrate that there is a pending exclusive lock.
-lock table t1 read;
---error ER_LOCK_DEADLOCK
-select f1();
-unlock tables;
+--echo # Sending 'select f1()'...
+send select f1();
+--echo # --> connection con3
+connection con3;
+--echo # Waitng for 'select f1()' to get blocked by a pending MDL lock...
+let $wait_condition=select count(*)=1 from information_schema.processlist
+where state='Waiting for table' and info='select f1()';
--echo # --> connection default
connection default;
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'alter function f1'...
---reap
+reap;
+--echo # --> connection con2
+connection con2;
+--echo # Reaping 'select f1()'
+reap;
--echo # --> connection default
connection default;
drop function f1;
@@ -363,6 +411,7 @@ drop procedure p1;
--echo #
create procedure p1() begin end;
create procedure p2() begin end;
+create table t1 (a int);
delimiter |;
create procedure p3()
begin
@@ -419,7 +468,7 @@ insert into t1 (a) values (1);
--echo # --> connection con1
connection con1;
--echo # Sending 'drop function f1'
---send drop function f1
+send drop function f1;
--echo # --> connection con2
connection con2;
--echo # Waitng for 'drop function f1' to get blocked on MDL lock...
@@ -432,7 +481,7 @@ commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'drop function f1'...
---reap
+reap;
--echo # --> connection default
connection default;
--echo #
@@ -445,7 +494,7 @@ select * from v1;
--echo # --> connection con1
connection con1;
--echo # Sending 'drop function f1'
---send drop function f1
+send drop function f1;
--echo # --> connection con2
connection con2;
--echo # Waitng for 'drop function f1' to get blocked on MDL lock...
@@ -458,7 +507,7 @@ commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'drop function f1'...
---reap
+reap;
--echo # --> connection default
connection default;
--echo #
@@ -478,7 +527,7 @@ select * from v1;
--echo # --> connection con1
connection con1;
--echo # Sending 'drop procedure p1'
---send drop procedure p1
+send drop procedure p1;
--echo # --> connection con2
connection con2;
--echo # Waitng for 'drop procedure p1' to get blocked on MDL lock...
@@ -491,7 +540,7 @@ commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'drop procedure p1'...
---reap
+reap;
--echo # --> connection default
connection default;
@@ -509,7 +558,7 @@ insert into t1 (a) values (3);
--echo # --> connection con1
connection con1;
--echo # Sending 'drop function f2'
---send drop function f2
+send drop function f2;
--echo # --> connection con2
connection con2;
--echo # Waitng for 'drop function f2' to get blocked on MDL lock...
@@ -522,7 +571,7 @@ commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'drop function f2'...
---reap
+reap;
--echo # --> connection default
connection default;
@@ -536,42 +585,11 @@ drop table t1, t2;
--echo # acquisition of a shared lock fails during a transaction or
--echo # we need to back off to flush the sp cache.
--echo #
---echo # a) A back off due to a lock conflict.
---echo #
-create table t1 (a int);
-create function f1() returns int return 6;
-begin;
-select f1();
---echo # --> connection con1
-connection con1;
---echo # Sending 'drop function f1'...
---send drop function f1
---echo # --> connection con2
-connection con2;
---echo # Waitng for 'drop function f1' to get blocked on MDL lock...
-let $wait_condition=select count(*)=1 from information_schema.processlist
-where state='Waiting for table' and info='drop function f1';
---source include/wait_condition.inc
-begin;
-select * from t1;
---error ER_LOCK_DEADLOCK
-select f1();
-commit;
---echo # --> connection default
-connection default;
-commit;
---echo # --> connection con1
-connection con1;
---echo # Reaping 'drop function f1'...
---reap
---echo # --> connection default
-connection default;
---echo #
---echo # b) A back off to flush the cache.
--echo # Sic: now this situation does not require a back off since we
--echo # flush the cache on the fly.
--echo #
create function f1() returns int return 7;
+create table t1 (a int);
begin;
select * from t1;
# Used to have a back-off here, with optional ER_LOCK_DEADLOCK
@@ -602,7 +620,7 @@ select f2();
--echo # --> connection con1
connection con1;
--echo # Sending 'drop function f1'...
---send drop function f1
+send drop function f1;
--echo # --> connection con2
connection con2;
--echo # Waitng for 'drop function f1' to get blocked on MDL lock...
@@ -610,7 +628,7 @@ let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for table' and info='drop function f1';
--source include/wait_condition.inc
--echo # Sending 'drop function f2'...
---send drop function f2
+send drop function f2;
--echo # --> connection default
connection default;
--echo # Waitng for 'drop function f2' to get blocked on MDL lock...
@@ -621,14 +639,14 @@ rollback to savepoint sv;
--echo # --> connection con2
connection con2;
--echo # Reaping 'drop function f2'...
---reap
+reap;
--echo # --> connection default
connection default;
unlock tables;
--echo # --> connection con1
connection con1;
--echo # Reaping 'drop function f1'...
---reap
+reap;
--echo # --> connection default
connection default;
--error ER_SP_DOES_NOT_EXIST
@@ -678,7 +696,7 @@ select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'alter function f1 ...'...
---send alter function f1 comment "comment"
+send alter function f1 comment "comment";
--echo # --> connection con2
connection con2;
--echo # Waitng for 'alter function f1 ...' to get blocked on MDL lock...
@@ -686,7 +704,7 @@ let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for table' and info like 'alter function f1 comment%';
--source include/wait_condition.inc
--echo # Sending 'call p1()'...
---send call p1()
+send call p1();
connection default;
--echo # Waitng for 'call p1()' to get blocked on MDL lock on f1...
let $wait_condition=select count(*)=1 from information_schema.processlist
@@ -697,11 +715,11 @@ commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'alter function f1 ...'
---reap
+reap;
--echo # --> connection con2
connection con2;
--echo # Reaping 'call p1()'...
---reap
+reap;
deallocate prepare stmt;
--echo # --> connection default
connection default;
@@ -725,7 +743,7 @@ select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'alter function f1 ...'...
---send alter function f1 comment "comment"
+send alter function f1 comment "comment";
--echo # --> connection con2
connection con2;
--echo # Waitng for 'alter function f1 ...' to get blocked on MDL lock...
@@ -754,7 +772,7 @@ begin
end|
delimiter ;|
--echo # Sending 'call p1()'...
---send call p1()
+send call p1();
connection default;
--echo # Waitng for 'call p1()' to get blocked on MDL lock on f1...
let $wait_condition=select count(*)=1 from information_schema.processlist
@@ -765,11 +783,11 @@ commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'alter function f1 ...'
---reap
+reap;
--echo # --> connection con2
connection con2;
--echo # Reaping 'call p1()'...
---reap
+reap;
--echo # --> connection default
connection default;
drop function f1;
@@ -804,7 +822,7 @@ select get_lock("30977", 0);
--echo # --> connection default
connection default;
--echo # Sending 'select f3()'...
---send select f3()
+send select f3();
--echo # --> connection con1
connection con1;
--echo # Waitng for 'select f3()' to get blocked on the user level lock...
@@ -819,7 +837,7 @@ select release_lock("30977");
connection default;
--echo # Reaping 'select f3()'...
--echo # Routine 'f2()' should exist and get executed successfully.
---reap
+reap;
select @var;
drop function f1;
drop function f2;
@@ -869,6 +887,10 @@ disconnect con1;
connection con2;
disconnect con2;
--source include/wait_until_disconnected.inc
+--echo # --> connection con3
+connection con3;
+disconnect con3;
+--source include/wait_until_disconnected.inc
--echo # --> connection default
connection default;
--echo #