mariadb/mysql-test/t/sp-lock.test
Dmitry Lenev 0228c98936 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  "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  "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  "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  "MySQL 5.4.4 new MDL: Bug#989 is not fully fixed in
  case of ALTER".
2009-12-30 20:53:30 +03:00

898 lines
26 KiB
Text

# Copyright (C) 2009 Sun Microsystems, Inc
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Metadata lock handling for stored procedures and
# functions.
#
--echo #
--echo # Test coverage for changes performed by the fix
--echo # for Bug#30977 "Concurrent statement using stored function
--echo # and DROP FUNCTION breaks SBR.
--echo #
--echo #
--echo # 1) Verify that the preceding transaction is
--echo # (implicitly) committed before CREATE/ALTER/DROP
--echo # PROCEDURE. Note, that this is already tested
--echo # in implicit_commit.test, but here we use an alternative
--echo # approach.
--echo #
--echo # Start a transaction, create a savepoint,
--echo # then call a DDL operation on a procedure, and then check
--echo # that the savepoint is no longer present.
--disable_warnings
drop table if exists t1;
drop procedure if exists p1;
drop procedure if exists p2;
drop procedure if exists p3;
drop procedure if exists p4;
drop function if exists f1;
--enable_warnings
create table t1 (a int);
--echo #
--echo # Test 'CREATE PROCEDURE'.
--echo #
begin;
savepoint sv;
create procedure p1() begin end;
--error ER_SP_DOES_NOT_EXIST
rollback to savepoint sv;
--echo #
--echo # Test 'ALTER PROCEDURE'.
--echo #
begin;
savepoint sv;
alter procedure p1 comment 'changed comment';
--error ER_SP_DOES_NOT_EXIST
rollback to savepoint sv;
--echo #
--echo # Test 'DROP PROCEDURE'.
--echo #
begin;
savepoint sv;
drop procedure p1;
--error ER_SP_DOES_NOT_EXIST
rollback to savepoint sv;
--echo #
--echo # Test 'CREATE FUNCTION'.
--echo #
begin;
savepoint sv;
create function f1() returns int return 1;
--error ER_SP_DOES_NOT_EXIST
rollback to savepoint sv;
--echo #
--echo # Test 'ALTER FUNCTION'.
--echo #
begin;
savepoint sv;
alter function f1 comment 'new comment';
--error ER_SP_DOES_NOT_EXIST
rollback to savepoint sv;
--echo #
--echo # Test 'DROP FUNCTION'.
--echo #
begin;
savepoint sv;
drop function f1;
--error ER_SP_DOES_NOT_EXIST
rollback to savepoint sv;
--echo #
--echo # 2) Verify that procedure DDL operations fail
--echo # under lock tables.
--echo #
--echo # Auxiliary routines to test ALTER.
create procedure p1() begin end;
create function f1() returns int return 1;
lock table t1 write;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
create procedure p2() begin end;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
alter procedure p1 comment 'changed comment';
--error ER_LOCK_OR_ACTIVE_TRANSACTION
drop procedure p1;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
create function f2() returns int return 1;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
alter function f1 comment 'changed comment';
lock table t1 read;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
create procedure p2() begin end;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
alter procedure p1 comment 'changed comment';
--error ER_LOCK_OR_ACTIVE_TRANSACTION
drop procedure p1;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
create function f2() returns int return 1;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
alter function f1 comment 'changed comment';
unlock tables;
--echo #
--echo # Even if we locked a temporary table.
--echo # Todo: this is a restriction we could possibly lift.
--echo #
drop table t1;
create temporary table t1 (a int);
lock table t1 read;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
create procedure p2() begin end;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
alter procedure p1 comment 'changed comment';
--error ER_LOCK_OR_ACTIVE_TRANSACTION
drop procedure p1;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
create function f2() returns int return 1;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
alter function f1 comment 'changed comment';
unlock tables;
drop function f1;
drop procedure p1;
drop temporary table t1;
--echo #
--echo # 3) Verify that CREATE/ALTER/DROP routine grab an
--echo # exclusive lock.
--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 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 procedure p1() begin end;
delimiter |;
create function f1() returns int
begin
call p1();
return 1;
end|
delimiter ;|
begin;
select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'drop procedure p1'...
send drop procedure p1;
--echo # --> connection con2
connection con2;
--echo # Waitng for 'drop procedure t1' to get blocked on MDL lock...
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.
--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;
--echo # --> connection con2
connection con2;
--echo # Reaping 'select f1()'
--error ER_SP_DOES_NOT_EXIST
reap;
--echo # --> connection default
connection default;
--echo #
--echo # Test CREATE PROCEDURE.
--echo #
create procedure p1() begin end;
begin;
select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'create procedure p1'...
send create procedure p1() begin end;
--echo # --> connection con2
connection con2;
--echo # Waitng for 'create procedure t1' to get blocked on MDL lock...
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.
--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 'create procedure p1'...
--error ER_SP_ALREADY_EXISTS
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;
--echo # --> connection con2
connection con2;
--echo # Waitng for 'alter procedure t1' to get blocked on MDL lock...
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.
--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;
--echo # --> connection con2
connection con2;
--echo # Reaping 'select f1()'
reap;
--echo # --> connection default
connection default;
--echo #
--echo # Test DROP FUNCTION.
--echo #
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
--echo # Demonstrate that there is a pending exclusive lock.
--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;
--echo # --> connection con2
connection con2;
--echo # Reaping 'select f1()'
--error ER_SP_DOES_NOT_EXIST
reap;
--echo # --> connection default
connection default;
--echo #
--echo # Test CREATE FUNCTION.
--echo #
create function f1() returns int return 1;
begin;
select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'create function f1'...
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...
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.
--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 'create function f1'...
--error ER_SP_ALREADY_EXISTS
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;
--echo # --> connection con2
connection con2;
--echo # Waitng for 'alter 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='alter function f1 contains sql';
--source include/wait_condition.inc
--echo # Demonstrate that there is a pending exclusive lock.
--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;
--echo # --> connection con2
connection con2;
--echo # Reaping 'select f1()'
reap;
--echo # --> connection default
connection default;
drop function f1;
drop procedure p1;
--echo #
--echo # 4) MDL lock should not be taken for
--echo # unrolled CALL statements.
--echo # The primary goal of metadata locks is a consistent binary log.
--echo # When a call statement is unrolled, it doesn't get to the
--echo # binary log, instead the statements that are contained
--echo # in the procedure body do. This can nest to any level.
--echo #
create procedure p1() begin end;
create procedure p2() begin end;
create table t1 (a int);
delimiter |;
create procedure p3()
begin
call p1();
call p1();
call p2();
end|
create procedure p4()
begin
call p1();
call p1();
call p2();
call p2();
call p3();
end|
delimiter ;|
begin;
select * from t1;
savepoint sv;
call p4();
--echo # Prepared statement should not add any locks either.
prepare stmt from "call p4()";
execute stmt;
execute stmt;
--echo # --> connection con1
connection con1;
drop procedure p1;
drop procedure p2;
drop procedure p3;
drop procedure p4;
--echo # --> connection default
connection default;
--echo # This is to verify there was no implicit commit.
rollback to savepoint sv;
--error ER_SP_DOES_NOT_EXIST
call p4();
commit;
drop table t1;
--echo #
--echo # 5) Locks should be taken on routines
--echo # used indirectly by views or triggers.
--echo #
--echo #
--echo # A function is used from a trigger.
--echo #
create function f1() returns int return 1;
create table t1 (a int);
create table t2 (a int, b int);
create trigger t1_ai after insert on t1 for each row
insert into t2 (a, b) values (new.a, f1());
begin;
insert into t1 (a) values (1);
--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
--echo # --> connnection default
connection default;
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'drop function f1'...
reap;
--echo # --> connection default
connection default;
--echo #
--echo # A function is used from a view.
--echo #
create function f1() returns int return 1;
create view v1 as select f1() as a;
begin;
select * from v1;
--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
--echo # --> connnection default
connection default;
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'drop function f1'...
reap;
--echo # --> connection default
connection default;
--echo #
--echo # A procedure is used from a function.
--echo #
delimiter |;
create function f1() returns int
begin
declare v_out int;
call p1(v_out);
return v_out;
end|
delimiter ;|
create procedure p1(out v_out int) set v_out=3;
begin;
select * from v1;
--echo # --> connection con1
connection con1;
--echo # Sending 'drop procedure p1'
send drop procedure p1;
--echo # --> connection con2
connection con2;
--echo # Waitng for 'drop procedure p1' to get blocked on MDL lock...
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 # --> connnection default
connection default;
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'drop procedure p1'...
reap;
--echo # --> connection default
connection default;
--echo #
--echo # Deep nesting: a function is used from a procedure used
--echo # from a function used from a view used in a trigger.
--echo #
create function f2() returns int return 4;
create procedure p1(out v_out int) set v_out=f2();
drop trigger t1_ai;
create trigger t1_ai after insert on t1 for each row
insert into t2 (a, b) values (new.a, (select max(a) from v1));
begin;
insert into t1 (a) values (3);
--echo # --> connection con1
connection con1;
--echo # Sending 'drop function f2'
send drop function f2;
--echo # --> connection con2
connection con2;
--echo # Waitng for 'drop function f2' 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 f2';
--source include/wait_condition.inc
--echo # --> connnection default
connection default;
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'drop function f2'...
reap;
--echo # --> connection default
connection default;
drop view v1;
drop function f1;
drop procedure p1;
drop table t1, t2;
--echo #
--echo # 6) Check that ER_LOCK_DEADLOCK is reported if
--echo # acquisition of a shared lock fails during a transaction or
--echo # we need to back off to flush the sp cache.
--echo #
--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
#--error ER_LOCK_DEADLOCK
select f1();
commit;
drop table t1;
drop function f1;
--echo #
--echo # 7) Demonstrate that under LOCK TABLES we accumulate locks
--echo # on stored routines, and release metadata locks in
--echo # ROLLBACK TO SAVEPOINT. That is done only for those stored
--echo # routines that are not part of LOCK TABLES prelocking list.
--echo # Those stored routines that are part of LOCK TABLES
--echo # prelocking list are implicitly locked when entering
--echo # LOCK TABLES, and ROLLBACK TO SAVEPOINT has no effect on
--echo # them.
--echo #
create function f1() returns varchar(20) return "f1()";
create function f2() returns varchar(20) return "f2()";
create view v1 as select f1() as a;
set @@session.autocommit=0;
lock table v1 read;
select * from v1;
savepoint sv;
select f2();
--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
--echo # Sending 'drop function f2'...
send drop function f2;
--echo # --> connection default
connection default;
--echo # Waitng for 'drop function f2' 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 f2';
--source include/wait_condition.inc
rollback to savepoint sv;
--echo # --> connection con2
connection con2;
--echo # Reaping 'drop function f2'...
reap;
--echo # --> connection default
connection default;
unlock tables;
--echo # --> connection con1
connection con1;
--echo # Reaping 'drop function f1'...
reap;
--echo # --> connection default
connection default;
--error ER_SP_DOES_NOT_EXIST
drop function f1;
--error ER_SP_DOES_NOT_EXIST
drop function f2;
drop view v1;
set @@session.autocommit=default;
--echo #
--echo # 8) Check the situation when we're preparing or executing a
--echo # prepared statement, and as part of that try to flush the
--echo # session sp cache. However, one of the procedures that
--echo # needs a flush is in use. Verify that there is no infinite
--echo # reprepare loop and no crash.
--echo #
create function f1() returns int return 1;
delimiter |;
--echo #
--echo # We just mention p1() in the body of f2() to make
--echo # sure that p1() metadata is validated when validating
--echo # 'select f2()'.
--echo # Recursion is not allowed in stored functions, so
--echo # an attempt to just invoke p1() from f2() which is in turn
--echo # called from p1() would have given a run-time error.
--echo #
create function f2() returns int
begin
if @var is null then
call p1();
end if;
return 1;
end|
create procedure p1()
begin
select f1() into @var;
execute stmt;
end|
delimiter ;|
--echo # --> connection con2
connection con2;
prepare stmt from "select f2()";
--echo # --> connection default
connection default;
begin;
select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'alter function f1 ...'...
send alter function f1 comment "comment";
--echo # --> connection con2
connection con2;
--echo # Waitng for 'alter 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 like 'alter function f1 comment%';
--source include/wait_condition.inc
--echo # Sending '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
where state='Waiting for table' and info='select f1() into @var';
--source include/wait_condition.inc
--echo # Let 'alter function f1 ...' go through...
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'alter function f1 ...'
reap;
--echo # --> connection con2
connection con2;
--echo # Reaping 'call p1()'...
reap;
deallocate prepare stmt;
--echo # --> connection default
connection default;
drop function f1;
drop function f2;
drop procedure p1;
--echo #
--echo # 9) Check the situation when a stored function is invoked
--echo # from a stored procedure, and recursively invokes the
--echo # stored procedure that is in use. But for the second
--echo # invocation, a cache flush is requested. We can't
--echo # flush the procedure that's in use, and are forced
--echo # to use an old version. It is not a violation of
--echo # consistency, since we unroll top-level calls.
--echo # Just verify the code works.
--echo #
create function f1() returns int return 1;
begin;
select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'alter function f1 ...'...
send alter function f1 comment "comment";
--echo # --> connection con2
connection con2;
--echo # Waitng for 'alter 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 like 'alter function f1 comment%';
--source include/wait_condition.inc
delimiter |;
--echo #
--echo # We just mention p1() in the body of f2() to make
--echo # sure that p1() is prelocked for f2().
--echo # Recursion is not allowed in stored functions, so
--echo # an attempt to just invoke p1() from f2() which is in turn
--echo # called from p1() would have given a run-time error.
--echo #
create function f2() returns int
begin
if @var is null then
call p1();
end if;
return 1;
end|
create procedure p1()
begin
select f1() into @var;
select f2() into @var;
end|
delimiter ;|
--echo # Sending '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
where state='Waiting for table' and info='select f1() into @var';
--source include/wait_condition.inc
--echo # Let 'alter function f1 ...' go through...
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'alter function f1 ...'
reap;
--echo # --> connection con2
connection con2;
--echo # Reaping 'call p1()'...
reap;
--echo # --> connection default
connection default;
drop function f1;
drop function f2;
drop procedure p1;
--echo #
--echo # 10) A select from information_schema.routines now
--echo # flushes the stored routines caches. Test that this
--echo # does not remove from the cache a stored routine
--echo # that is already prelocked.
--echo #
create function f1() returns int return get_lock("30977", 100000);
create function f2() returns int return 2;
delimiter |;
create function f3() returns varchar(255)
begin
declare res varchar(255);
declare c cursor for select routine_name from
information_schema.routines where routine_name='f1';
select f1() into @var;
open c;
fetch c into res;
close c;
select f2() into @var;
return res;
end|
delimiter ;|
--echo # --> connection con1
connection con1;
select get_lock("30977", 0);
--echo # --> connection default
connection default;
--echo # Sending 'select f3()'...
send select f3();
--echo # --> connection con1
connection con1;
--echo # Waitng for 'select f3()' to get blocked on the user level lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='User lock' and info='select f1() into @var';
--source include/wait_condition.inc
--echo # Do something to change the cache version.
create function f4() returns int return 4;
drop function f4;
select release_lock("30977");
--echo # --> connection default
connection default;
--echo # Reaping 'select f3()'...
--echo # Routine 'f2()' should exist and get executed successfully.
reap;
select @var;
drop function f1;
drop function f2;
drop function f3;
--echo # 11) Check the situation when the connection is flushing the
--echo # SP cache which contains a procedure that is being executed.
--echo #
--echo # Function f1() calls p1(). Procedure p1() has a DROP
--echo # VIEW statement, which, we know, invalidates the routines cache.
--echo # During cache flush p1() must not be flushed since it's in
--echo # use.
--echo #
delimiter |;
create function f1() returns int
begin
call p1();
return 1;
end|
create procedure p1()
begin
create view v1 as select 1;
drop view v1;
select f1() into @var;
set @exec_count=@exec_count+1;
end|
delimiter ;|
set @exec_count=0;
--error ER_SP_RECURSION_LIMIT
call p1();
select @exec_count;
set @@session.max_sp_recursion_depth=5;
set @exec_count=0;
--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
call p1();
select @exec_count;
drop procedure p1;
drop function f1;
set @@session.max_sp_recursion_depth=default;
--echo # --> connection con1
connection con1;
disconnect con1;
--source include/wait_until_disconnected.inc
--echo # --> connection con2
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 #
--echo # End of 5.5 tests
--echo #