Backport of revno ## 2617.31.1, 2617.31.3, 2617.31.4, 2617.31.5,
2617.31.12, 2617.31.15, 2617.31.15, 2617.31.16, 2617.43.1
- initial changeset that introduced the fix for
Bug#989 and follow up fixes for all test suite failures
introduced in the initial changeset.
------------------------------------------------------------
revno: 2617.31.1
committer: Davi Arnaut <Davi.Arnaut@Sun.COM>
branch nick: 4284-6.0
timestamp: Fri 2009-03-06 19:17:00 -0300
message:
Bug#989: If DROP TABLE while there's an active transaction, wrong binlog order
WL#4284: Transactional DDL locking
Currently the MySQL server does not keep metadata locks on
schema objects for the duration of a transaction, thus failing
to guarantee the integrity of the schema objects being used
during the transaction and to protect then from concurrent
DDL operations. This also poses a problem for replication as
a DDL operation might be replicated even thought there are
active transactions using the object being modified.
The solution is to defer the release of metadata locks until
a active transaction is either committed or rolled back. This
prevents other statements from modifying the table for the
entire duration of the transaction. This provides commitment
ordering for guaranteeing serializability across multiple
transactions.
- Incompatible change:
If MySQL's metadata locking system encounters a lock conflict,
the usual schema is to use the try and back-off technique to
avoid deadlocks -- this schema consists in releasing all locks
and trying to acquire them all in one go.
But in a transactional context this algorithm can't be utilized
as its not possible to release locks acquired during the course
of the transaction without breaking the transaction commitments.
To avoid deadlocks in this case, the ER_LOCK_DEADLOCK will be
returned if a lock conflict is encountered during a transaction.
Let's consider an example:
A transaction has two statements that modify table t1, then table
t2, and then commits. The first statement of the transaction will
acquire a shared metadata lock on table t1, and it will be kept
utill COMMIT to ensure serializability.
At the moment when the second statement attempts to acquire a
shared metadata lock on t2, a concurrent ALTER or DROP statement
might have locked t2 exclusively. The prescription of the current
locking protocol is that the acquirer of the shared lock backs off
-- gives up all his current locks and retries. This implies that
the entire multi-statement transaction has to be rolled back.
- Incompatible change:
FLUSH commands such as FLUSH PRIVILEGES and FLUSH TABLES WITH READ
LOCK won't cause locked tables to be implicitly unlocked anymore.
2009-12-05 02:02:48 +03:00
|
|
|
drop table if exists t1,t2,t3;
|
2003-12-10 04:31:42 +00:00
|
|
|
CREATE TABLE t1 ( `id` int(11) NOT NULL default '0', `id2` int(11) NOT NULL default '0', `id3` int(11) NOT NULL default '0', `dummy1` char(30) default NULL, PRIMARY KEY (`id`,`id2`), KEY `index_id3` (`id3`)) ENGINE=MyISAM;
|
2001-09-27 23:05:54 -06:00
|
|
|
insert into t1 (id,id2) values (1,1),(1,2),(1,3);
|
|
|
|
LOCK TABLE t1 WRITE;
|
|
|
|
select dummy1,count(distinct id) from t1 group by dummy1;
|
2001-03-27 13:05:48 +03:00
|
|
|
dummy1 count(distinct id)
|
|
|
|
NULL 1
|
2001-09-27 23:05:54 -06:00
|
|
|
update t1 set id=-1 where id=1;
|
|
|
|
LOCK TABLE t1 READ;
|
|
|
|
update t1 set id=1 where id=1;
|
2003-06-04 18:28:51 +03:00
|
|
|
ERROR HY000: Table 't1' was locked with a READ lock and can't be updated
|
2001-09-27 23:05:54 -06:00
|
|
|
create table t2 SELECT * from t1;
|
2003-06-04 18:28:51 +03:00
|
|
|
ERROR HY000: Table 't2' was not locked with LOCK TABLES
|
2001-09-27 23:05:54 -06:00
|
|
|
create temporary table t2 SELECT * from t1;
|
|
|
|
drop table if exists t2;
|
|
|
|
unlock tables;
|
|
|
|
create table t2 SELECT * from t1;
|
|
|
|
LOCK TABLE t1 WRITE,t2 write;
|
|
|
|
insert into t2 SELECT * from t1;
|
|
|
|
update t1 set id=1 where id=-1;
|
|
|
|
drop table t1,t2;
|
|
|
|
CREATE TABLE t1 (
|
|
|
|
index1 smallint(6) default NULL,
|
|
|
|
nr smallint(6) default NULL,
|
|
|
|
KEY index1(index1)
|
2003-12-10 04:31:42 +00:00
|
|
|
) ENGINE=MyISAM;
|
2001-09-27 23:05:54 -06:00
|
|
|
CREATE TABLE t2 (
|
|
|
|
nr smallint(6) default NULL,
|
|
|
|
name varchar(20) default NULL
|
2003-12-10 04:31:42 +00:00
|
|
|
) ENGINE=MyISAM;
|
2001-09-27 23:05:54 -06:00
|
|
|
INSERT INTO t2 VALUES (1,'item1');
|
|
|
|
INSERT INTO t2 VALUES (2,'item2');
|
|
|
|
lock tables t1 write, t2 read;
|
|
|
|
insert into t1 select 1,nr from t2 where name='item1';
|
|
|
|
insert into t1 select 2,nr from t2 where name='item2';
|
|
|
|
unlock tables;
|
|
|
|
check table t1;
|
2001-04-18 23:47:11 +03:00
|
|
|
Table Op Msg_type Msg_text
|
|
|
|
test.t1 check status OK
|
2001-09-27 23:05:54 -06:00
|
|
|
lock tables t1 write;
|
|
|
|
check table t2;
|
2001-04-18 23:47:11 +03:00
|
|
|
Table Op Msg_type Msg_text
|
2007-06-06 04:42:41 +05:00
|
|
|
test.t2 check Error Table 't2' was not locked with LOCK TABLES
|
2009-12-09 14:03:37 +01:00
|
|
|
test.t2 check status Operation failed
|
2005-02-08 20:52:50 +01:00
|
|
|
insert into t1 select index1,nr from t1;
|
2005-03-04 16:35:28 +03:00
|
|
|
ERROR HY000: Table 't1' was not locked with LOCK TABLES
|
2001-09-27 23:05:54 -06:00
|
|
|
unlock tables;
|
2004-02-03 09:46:48 +01:00
|
|
|
lock tables t1 write, t1 as t1_alias read;
|
|
|
|
insert into t1 select index1,nr from t1 as t1_alias;
|
2001-09-27 23:05:54 -06:00
|
|
|
drop table t1,t2;
|
2009-11-20 22:51:12 +03:00
|
|
|
ERROR HY000: Table 't2' was not locked with LOCK TABLES
|
|
|
|
unlock tables;
|
|
|
|
drop table t1,t2;
|
2006-01-23 19:12:29 +01:00
|
|
|
create table t1 (c1 int);
|
|
|
|
create table t2 (c1 int);
|
|
|
|
create table t3 (c1 int);
|
|
|
|
lock tables t1 write, t2 write, t3 write;
|
|
|
|
drop table t2, t3, t1;
|
|
|
|
create table t1 (c1 int);
|
|
|
|
create table t2 (c1 int);
|
|
|
|
create table t3 (c1 int);
|
|
|
|
lock tables t1 write, t2 write, t3 write, t1 as t4 read;
|
|
|
|
alter table t2 add column c2 int;
|
|
|
|
drop table t1, t2, t3;
|
2005-05-24 11:44:34 +01:00
|
|
|
create table t1 ( a int(11) not null auto_increment, primary key(a));
|
|
|
|
create table t2 ( a int(11) not null auto_increment, primary key(a));
|
|
|
|
lock tables t1 write, t2 read;
|
|
|
|
delete from t1 using t1,t2 where t1.a=t2.a;
|
|
|
|
delete t1 from t1,t2 where t1.a=t2.a;
|
|
|
|
delete from t2 using t1,t2 where t1.a=t2.a;
|
|
|
|
ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
|
|
|
|
delete t2 from t1,t2 where t1.a=t2.a;
|
|
|
|
ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
|
|
|
|
drop table t1,t2;
|
2009-11-20 22:51:12 +03:00
|
|
|
ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
|
|
|
|
unlock tables;
|
|
|
|
drop table t2,t1;
|
BUG#9953: CONVERT_TZ requires mysql.time_zone_name to be locked
The problem was that some facilities (like CONVERT_TZ() function or
server HELP statement) may require implicit access to some tables in
'mysql' database. This access was done by ordinary means of adding
such tables to the list of tables the query is going to open.
However, if we issued LOCK TABLES before that, we would get "table
was not locked" error trying to open such implicit tables.
The solution is to treat certain tables as MySQL system tables, like
we already do for mysql.proc. Such tables may be opened for reading
at any moment regardless of any locks in effect. The cost of this is
that system table may be locked for writing only together with other
system tables, it is disallowed to lock system tables for writing and
have any other lock on any other table.
After this patch the following tables are treated as MySQL system
tables:
mysql.help_category
mysql.help_keyword
mysql.help_relation
mysql.help_topic
mysql.proc (it already was)
mysql.time_zone
mysql.time_zone_leap_second
mysql.time_zone_name
mysql.time_zone_transition
mysql.time_zone_transition_type
These tables are now opened with open_system_tables_for_read() and
closed with close_system_tables(), or one table may be opened with
open_system_table_for_update() and closed with close_thread_tables()
(the latter is used for mysql.proc table, which is updated as part of
normal MySQL server operation). These functions may be used when
some tables were opened and locked already.
NOTE: online update of time zone tables is not possible during
replication, because there's no time zone cache flush neither on LOCK
TABLES, nor on FLUSH TABLES, so the master may serve stale time zone
data from cache, while on slave updated data will be loaded from the
time zone tables.
2007-03-09 13:12:31 +03:00
|
|
|
End of 4.1 tests.
|
2006-07-08 00:26:18 +04:00
|
|
|
drop table if exists t1;
|
|
|
|
create table t1 (a int);
|
|
|
|
lock table t1 write;
|
|
|
|
flush tables with read lock;
|
|
|
|
ERROR HY000: Can't execute the given command because you have active locked tables or an active transaction
|
|
|
|
unlock tables;
|
|
|
|
drop table t1;
|
BUG#9953: CONVERT_TZ requires mysql.time_zone_name to be locked
The problem was that some facilities (like CONVERT_TZ() function or
server HELP statement) may require implicit access to some tables in
'mysql' database. This access was done by ordinary means of adding
such tables to the list of tables the query is going to open.
However, if we issued LOCK TABLES before that, we would get "table
was not locked" error trying to open such implicit tables.
The solution is to treat certain tables as MySQL system tables, like
we already do for mysql.proc. Such tables may be opened for reading
at any moment regardless of any locks in effect. The cost of this is
that system table may be locked for writing only together with other
system tables, it is disallowed to lock system tables for writing and
have any other lock on any other table.
After this patch the following tables are treated as MySQL system
tables:
mysql.help_category
mysql.help_keyword
mysql.help_relation
mysql.help_topic
mysql.proc (it already was)
mysql.time_zone
mysql.time_zone_leap_second
mysql.time_zone_name
mysql.time_zone_transition
mysql.time_zone_transition_type
These tables are now opened with open_system_tables_for_read() and
closed with close_system_tables(), or one table may be opened with
open_system_table_for_update() and closed with close_thread_tables()
(the latter is used for mysql.proc table, which is updated as part of
normal MySQL server operation). These functions may be used when
some tables were opened and locked already.
NOTE: online update of time zone tables is not possible during
replication, because there's no time zone cache flush neither on LOCK
TABLES, nor on FLUSH TABLES, so the master may serve stale time zone
data from cache, while on slave updated data will be loaded from the
time zone tables.
2007-03-09 13:12:31 +03:00
|
|
|
DROP TABLE IF EXISTS t1;
|
|
|
|
CREATE TABLE t1 (i INT);
|
|
|
|
LOCK TABLES mysql.time_zone READ, mysql.proc READ, t1 READ;
|
|
|
|
UNLOCK TABLES;
|
|
|
|
LOCK TABLES mysql.time_zone READ, mysql.proc READ, t1 WRITE;
|
|
|
|
UNLOCK TABLES;
|
|
|
|
LOCK TABLES mysql.time_zone READ, mysql.proc READ;
|
|
|
|
UNLOCK TABLES;
|
|
|
|
LOCK TABLES mysql.time_zone WRITE, mysql.proc WRITE;
|
|
|
|
UNLOCK TABLES;
|
|
|
|
LOCK TABLES mysql.time_zone READ, mysql.proc WRITE, t1 READ;
|
|
|
|
ERROR HY000: You can't combine write-locking of system tables with other tables or lock types
|
|
|
|
LOCK TABLES mysql.time_zone WRITE, mysql.proc WRITE, t1 READ;
|
|
|
|
ERROR HY000: You can't combine write-locking of system tables with other tables or lock types
|
|
|
|
LOCK TABLES mysql.time_zone WRITE, mysql.proc WRITE, t1 WRITE;
|
|
|
|
ERROR HY000: You can't combine write-locking of system tables with other tables or lock types
|
|
|
|
LOCK TABLES mysql.time_zone READ, mysql.proc WRITE;
|
|
|
|
ERROR HY000: You can't combine write-locking of system tables with other tables or lock types
|
|
|
|
DROP TABLE t1;
|
2007-08-02 13:59:02 +04:00
|
|
|
|
|
|
|
Bug#5719 impossible to lock VIEW
|
|
|
|
|
|
|
|
Just covering existing behaviour with tests.
|
|
|
|
Consistency has not been found here.
|
|
|
|
|
|
|
|
drop view if exists v_bug5719;
|
|
|
|
drop table if exists t1, t2, t3;
|
|
|
|
create table t1 (a int);
|
|
|
|
create temporary table t2 (a int);
|
|
|
|
create table t3 (a int);
|
|
|
|
create view v_bug5719 as select 1;
|
|
|
|
lock table v_bug5719 write;
|
|
|
|
select * from t1;
|
|
|
|
ERROR HY000: Table 't1' was not locked with LOCK TABLES
|
|
|
|
|
|
|
|
Allowed to select from a temporary talbe under LOCK TABLES
|
|
|
|
|
|
|
|
select * from t2;
|
|
|
|
a
|
|
|
|
select * from t3;
|
|
|
|
ERROR HY000: Table 't3' was not locked with LOCK TABLES
|
|
|
|
select * from v_bug5719;
|
|
|
|
1
|
|
|
|
1
|
|
|
|
drop view v_bug5719;
|
2009-11-30 18:55:03 +03:00
|
|
|
ERROR HY000: Can't execute the given command because you have active locked tables or an active transaction
|
2007-08-02 13:59:02 +04:00
|
|
|
|
|
|
|
sic: did not left LOCK TABLES mode automatically
|
|
|
|
|
|
|
|
select * from t1;
|
|
|
|
ERROR HY000: Table 't1' was not locked with LOCK TABLES
|
|
|
|
unlock tables;
|
2009-11-30 18:55:03 +03:00
|
|
|
create or replace view v_bug5719 as select * from t1;
|
2007-08-02 13:59:02 +04:00
|
|
|
lock tables v_bug5719 write;
|
|
|
|
select * from v_bug5719;
|
|
|
|
a
|
|
|
|
|
|
|
|
Allowed to use an underlying table under LOCK TABLES <view>
|
|
|
|
|
|
|
|
select * from t1;
|
|
|
|
a
|
|
|
|
|
|
|
|
Allowed to select from a temporary table under LOCK TABLES
|
|
|
|
|
|
|
|
select * from t2;
|
|
|
|
a
|
|
|
|
select * from t3;
|
|
|
|
ERROR HY000: Table 't3' was not locked with LOCK TABLES
|
Implement new type-of-operation-aware metadata locks.
Add a wait-for graph based deadlock detector to the
MDL subsystem.
Fixes bug #46272 "MySQL 5.4.4, new MDL: unnecessary deadlock" and
bug #37346 "innodb does not detect deadlock between update and
alter table".
The first bug manifested itself as an unwarranted abort of a
transaction with ER_LOCK_DEADLOCK error by a concurrent ALTER
statement, when this transaction tried to repeat use of a
table, which it has already used in a similar fashion before
ALTER started.
The second bug showed up as a deadlock between table-level
locks and InnoDB row locks, which was "detected" only after
innodb_lock_wait_timeout timeout.
A transaction would start using the table and modify a few
rows.
Then ALTER TABLE would come in, and start copying rows
into a temporary table. Eventually it would stumble on
the modified records and get blocked on a row lock.
The first transaction would try to do more updates, and get
blocked on thr_lock.c lock.
This situation of circular wait would only get resolved
by a timeout.
Both these bugs stemmed from inadequate solutions to the
problem of deadlocks occurring between different
locking subsystems.
In the first case we tried to avoid deadlocks between metadata
locking and table-level locking subsystems, when upgrading shared
metadata lock to exclusive one.
Transactions holding the shared lock on the table and waiting for
some table-level lock used to be aborted too aggressively.
We also allowed ALTER TABLE to start in presence of transactions
that modify the subject table. ALTER TABLE acquires
TL_WRITE_ALLOW_READ lock at start, and that block all writes
against the table (naturally, we don't want any writes to be lost
when switching the old and the new table). TL_WRITE_ALLOW_READ
lock, in turn, would block the started transaction on thr_lock.c
lock, should they do more updates. This, again, lead to the need
to abort such transactions.
The second bug occurred simply because we didn't have any
mechanism to detect deadlocks between the table-level locks
in thr_lock.c and row-level locks in InnoDB, other than
innodb_lock_wait_timeout.
This patch solves both these problems by moving lock conflicts
which are causing these deadlocks into the metadata locking
subsystem, thus making it possible to avoid or detect such
deadlocks inside MDL.
To do this we introduce new type-of-operation-aware metadata
locks, which allow MDL subsystem to know not only the fact that
transaction has used or is going to use some object but also what
kind of operation it has carried out or going to carry out on the
object.
This, along with the addition of a special kind of upgradable
metadata lock, allows ALTER TABLE to wait until all
transactions which has updated the table to go away.
This solves the second issue.
Another special type of upgradable metadata lock is acquired
by LOCK TABLE WRITE. This second lock type allows to solve the
first issue, since abortion of table-level locks in event of
DDL under LOCK TABLES becomes also unnecessary.
Below follows the list of incompatible changes introduced by
this patch:
- From now on, ALTER TABLE and CREATE/DROP TRIGGER SQL (i.e. those
statements that acquire TL_WRITE_ALLOW_READ lock)
wait for all transactions which has *updated* the table to
complete.
- From now on, LOCK TABLES ... WRITE, REPAIR/OPTIMIZE TABLE
(i.e. all statements which acquire TL_WRITE table-level lock) wait
for all transaction which *updated or read* from the table
to complete.
As a consequence, innodb_table_locks=0 option no longer applies
to LOCK TABLES ... WRITE.
- DROP DATABASE, DROP TABLE, RENAME TABLE no longer abort
statements or transactions which use tables being dropped or
renamed, and instead wait for these transactions to complete.
- Since LOCK TABLES WRITE now takes a special metadata lock,
not compatible with with reads or writes against the subject table
and transaction-wide, thr_lock.c deadlock avoidance algorithm
that used to ensure absence of deadlocks between LOCK TABLES
WRITE and other statements is no longer sufficient, even for
MyISAM. The wait-for graph based deadlock detector of MDL
subsystem may sometimes be necessary and is involved. This may
lead to ER_LOCK_DEADLOCK error produced for multi-statement
transactions even if these only use MyISAM:
session 1: session 2:
begin;
update t1 ... lock table t2 write, t1 write;
-- gets a lock on t2, blocks on t1
update t2 ...
(ER_LOCK_DEADLOCK)
- Finally, support of LOW_PRIORITY option for LOCK TABLES ... WRITE
was abandoned.
LOCK TABLE ... LOW_PRIORITY WRITE from now on has the same
priority as the usual LOCK TABLE ... WRITE.
SELECT HIGH PRIORITY no longer trumps LOCK TABLE ... WRITE in
the wait queue.
- We do not take upgradable metadata locks on implicitly
locked tables. So if one has, say, a view v1 that uses
table t1, and issues:
LOCK TABLE v1 WRITE;
FLUSH TABLE t1; -- (or just 'FLUSH TABLES'),
an error is produced.
In order to be able to perform DDL on a table under LOCK TABLES,
the table must be locked explicitly in the LOCK TABLES list.
2010-02-01 14:43:06 +03:00
|
|
|
Dropping of implicitly locked table is disallowed.
|
|
|
|
drop table t1;
|
|
|
|
ERROR HY000: Table 't1' was locked with a READ lock and can't be updated
|
|
|
|
unlock tables;
|
|
|
|
Now let us also lock table explicitly and drop it.
|
|
|
|
lock tables t1 write, v_bug5719 write;
|
2007-08-02 13:59:02 +04:00
|
|
|
drop table t1;
|
|
|
|
|
|
|
|
sic: left LOCK TABLES mode
|
|
|
|
|
|
|
|
select * from t3;
|
|
|
|
a
|
|
|
|
select * from v_bug5719;
|
|
|
|
ERROR HY000: View 'test.v_bug5719' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
|
|
|
|
unlock tables;
|
|
|
|
drop view v_bug5719;
|
|
|
|
|
|
|
|
When limitation to use temporary tables in views is removed, please
|
|
|
|
add a test that shows what happens under LOCK TABLES when a view
|
|
|
|
references a temporary table, is locked, and the underlying table
|
|
|
|
is dropped.
|
|
|
|
|
|
|
|
create view v_bug5719 as select * from t2;
|
|
|
|
ERROR HY000: View's SELECT refers to a temporary table 't2'
|
|
|
|
|
|
|
|
Cleanup.
|
|
|
|
|
|
|
|
drop table t2, t3;
|
2009-03-05 15:22:33 +01:00
|
|
|
#
|
|
|
|
# Bug#39843 DELETE requires write access to table in subquery in where clause
|
|
|
|
#
|
|
|
|
DROP TABLE IF EXISTS t1,t2;
|
|
|
|
CREATE TABLE t1 (
|
|
|
|
table1_rowid SMALLINT NOT NULL
|
|
|
|
);
|
|
|
|
CREATE TABLE t2 (
|
|
|
|
table2_rowid SMALLINT NOT NULL
|
|
|
|
);
|
|
|
|
INSERT INTO t1 VALUES (1);
|
|
|
|
INSERT INTO t2 VALUES (1);
|
|
|
|
LOCK TABLES t1 WRITE, t2 READ;
|
|
|
|
# Sub-select should not try to aquire a write lock.
|
|
|
|
DELETE FROM t1
|
|
|
|
WHERE EXISTS
|
|
|
|
(
|
|
|
|
SELECT 'x'
|
|
|
|
FROM t2
|
|
|
|
WHERE t1.table1_rowid = t2.table2_rowid
|
|
|
|
) ;
|
|
|
|
# While implementing the patch we didn't break old behavior;
|
|
|
|
# The following sub-select should still requires a write lock:
|
|
|
|
SELECT * FROM t1 WHERE 1 IN (SELECT * FROM t2 FOR UPDATE);
|
|
|
|
ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
|
|
|
|
UNLOCK TABLES;
|
|
|
|
DROP TABLE t1,t2;
|
BUG#9953: CONVERT_TZ requires mysql.time_zone_name to be locked
The problem was that some facilities (like CONVERT_TZ() function or
server HELP statement) may require implicit access to some tables in
'mysql' database. This access was done by ordinary means of adding
such tables to the list of tables the query is going to open.
However, if we issued LOCK TABLES before that, we would get "table
was not locked" error trying to open such implicit tables.
The solution is to treat certain tables as MySQL system tables, like
we already do for mysql.proc. Such tables may be opened for reading
at any moment regardless of any locks in effect. The cost of this is
that system table may be locked for writing only together with other
system tables, it is disallowed to lock system tables for writing and
have any other lock on any other table.
After this patch the following tables are treated as MySQL system
tables:
mysql.help_category
mysql.help_keyword
mysql.help_relation
mysql.help_topic
mysql.proc (it already was)
mysql.time_zone
mysql.time_zone_leap_second
mysql.time_zone_name
mysql.time_zone_transition
mysql.time_zone_transition_type
These tables are now opened with open_system_tables_for_read() and
closed with close_system_tables(), or one table may be opened with
open_system_table_for_update() and closed with close_thread_tables()
(the latter is used for mysql.proc table, which is updated as part of
normal MySQL server operation). These functions may be used when
some tables were opened and locked already.
NOTE: online update of time zone tables is not possible during
replication, because there's no time zone cache flush neither on LOCK
TABLES, nor on FLUSH TABLES, so the master may serve stale time zone
data from cache, while on slave updated data will be loaded from the
time zone tables.
2007-03-09 13:12:31 +03:00
|
|
|
End of 5.1 tests.
|
2009-12-02 18:22:15 +03:00
|
|
|
#
|
|
|
|
# Ensure that FLUSH TABLES doesn't substitute a base locked table
|
|
|
|
# with a temporary one.
|
|
|
|
#
|
|
|
|
drop table if exists t1, t2;
|
|
|
|
create table t1 (a int);
|
|
|
|
create table t2 (a int);
|
|
|
|
lock table t1 write, t2 write;
|
|
|
|
create temporary table t1 (a int);
|
|
|
|
flush table t1;
|
|
|
|
drop temporary table t1;
|
|
|
|
select * from t1;
|
|
|
|
a
|
|
|
|
unlock tables;
|
|
|
|
drop table t1, t2;
|
|
|
|
#
|
|
|
|
# Ensure that REPAIR .. USE_FRM works under LOCK TABLES.
|
|
|
|
#
|
|
|
|
drop table if exists t1, t2;
|
|
|
|
create table t1 (a int);
|
|
|
|
create table t2 (a int);
|
|
|
|
lock table t1 write, t2 write;
|
|
|
|
repair table t1 use_frm;
|
|
|
|
Table Op Msg_type Msg_text
|
|
|
|
test.t1 repair status OK
|
|
|
|
repair table t1 use_frm;
|
|
|
|
Table Op Msg_type Msg_text
|
|
|
|
test.t1 repair status OK
|
|
|
|
select * from t1;
|
|
|
|
a
|
|
|
|
select * from t2;
|
|
|
|
a
|
|
|
|
repair table t2 use_frm;
|
|
|
|
Table Op Msg_type Msg_text
|
|
|
|
test.t2 repair status OK
|
|
|
|
repair table t2 use_frm;
|
|
|
|
Table Op Msg_type Msg_text
|
|
|
|
test.t2 repair status OK
|
|
|
|
select * from t1;
|
|
|
|
a
|
|
|
|
unlock tables;
|
|
|
|
drop table t1, t2;
|
|
|
|
#
|
2009-12-02 23:47:23 +03:00
|
|
|
# Ensure that mi_copy_status is called for two instances
|
|
|
|
# of the same table when it is reopened after a flush.
|
|
|
|
#
|
|
|
|
drop table if exists t1;
|
|
|
|
drop view if exists v1;
|
|
|
|
create table t1 (c1 int);
|
|
|
|
create view v1 as select * from t1;
|
|
|
|
lock tables t1 write, v1 write;
|
|
|
|
flush table t1;
|
|
|
|
insert into t1 values (33);
|
|
|
|
flush table t1;
|
|
|
|
select * from t1;
|
|
|
|
c1
|
|
|
|
33
|
|
|
|
unlock tables;
|
|
|
|
drop table t1;
|
|
|
|
drop view v1;
|
|
|
|
#
|
Backport of revno ## 2617.31.1, 2617.31.3, 2617.31.4, 2617.31.5,
2617.31.12, 2617.31.15, 2617.31.15, 2617.31.16, 2617.43.1
- initial changeset that introduced the fix for
Bug#989 and follow up fixes for all test suite failures
introduced in the initial changeset.
------------------------------------------------------------
revno: 2617.31.1
committer: Davi Arnaut <Davi.Arnaut@Sun.COM>
branch nick: 4284-6.0
timestamp: Fri 2009-03-06 19:17:00 -0300
message:
Bug#989: If DROP TABLE while there's an active transaction, wrong binlog order
WL#4284: Transactional DDL locking
Currently the MySQL server does not keep metadata locks on
schema objects for the duration of a transaction, thus failing
to guarantee the integrity of the schema objects being used
during the transaction and to protect then from concurrent
DDL operations. This also poses a problem for replication as
a DDL operation might be replicated even thought there are
active transactions using the object being modified.
The solution is to defer the release of metadata locks until
a active transaction is either committed or rolled back. This
prevents other statements from modifying the table for the
entire duration of the transaction. This provides commitment
ordering for guaranteeing serializability across multiple
transactions.
- Incompatible change:
If MySQL's metadata locking system encounters a lock conflict,
the usual schema is to use the try and back-off technique to
avoid deadlocks -- this schema consists in releasing all locks
and trying to acquire them all in one go.
But in a transactional context this algorithm can't be utilized
as its not possible to release locks acquired during the course
of the transaction without breaking the transaction commitments.
To avoid deadlocks in this case, the ER_LOCK_DEADLOCK will be
returned if a lock conflict is encountered during a transaction.
Let's consider an example:
A transaction has two statements that modify table t1, then table
t2, and then commits. The first statement of the transaction will
acquire a shared metadata lock on table t1, and it will be kept
utill COMMIT to ensure serializability.
At the moment when the second statement attempts to acquire a
shared metadata lock on t2, a concurrent ALTER or DROP statement
might have locked t2 exclusively. The prescription of the current
locking protocol is that the acquirer of the shared lock backs off
-- gives up all his current locks and retries. This implies that
the entire multi-statement transaction has to be rolled back.
- Incompatible change:
FLUSH commands such as FLUSH PRIVILEGES and FLUSH TABLES WITH READ
LOCK won't cause locked tables to be implicitly unlocked anymore.
2009-12-05 02:02:48 +03:00
|
|
|
# WL#4284: Transactional DDL locking
|
|
|
|
#
|
|
|
|
drop table if exists t1;
|
|
|
|
create table t1 (a int);
|
|
|
|
set autocommit= 0;
|
|
|
|
insert into t1 values (1);
|
|
|
|
lock table t1 write;
|
|
|
|
# Disconnect
|
|
|
|
# Ensure that metadata locks will be released if there is an open
|
|
|
|
# transaction (autocommit=off) in conjunction with lock tables.
|
|
|
|
drop table t1;
|
|
|
|
# Same problem but now for BEGIN
|
|
|
|
drop table if exists t1;
|
|
|
|
create table t1 (a int);
|
|
|
|
begin;
|
|
|
|
insert into t1 values (1);
|
|
|
|
# Disconnect
|
|
|
|
# Ensure that metadata locks held by the transaction are released.
|
|
|
|
drop table t1;
|
|
|
|
#
|
Implement new type-of-operation-aware metadata locks.
Add a wait-for graph based deadlock detector to the
MDL subsystem.
Fixes bug #46272 "MySQL 5.4.4, new MDL: unnecessary deadlock" and
bug #37346 "innodb does not detect deadlock between update and
alter table".
The first bug manifested itself as an unwarranted abort of a
transaction with ER_LOCK_DEADLOCK error by a concurrent ALTER
statement, when this transaction tried to repeat use of a
table, which it has already used in a similar fashion before
ALTER started.
The second bug showed up as a deadlock between table-level
locks and InnoDB row locks, which was "detected" only after
innodb_lock_wait_timeout timeout.
A transaction would start using the table and modify a few
rows.
Then ALTER TABLE would come in, and start copying rows
into a temporary table. Eventually it would stumble on
the modified records and get blocked on a row lock.
The first transaction would try to do more updates, and get
blocked on thr_lock.c lock.
This situation of circular wait would only get resolved
by a timeout.
Both these bugs stemmed from inadequate solutions to the
problem of deadlocks occurring between different
locking subsystems.
In the first case we tried to avoid deadlocks between metadata
locking and table-level locking subsystems, when upgrading shared
metadata lock to exclusive one.
Transactions holding the shared lock on the table and waiting for
some table-level lock used to be aborted too aggressively.
We also allowed ALTER TABLE to start in presence of transactions
that modify the subject table. ALTER TABLE acquires
TL_WRITE_ALLOW_READ lock at start, and that block all writes
against the table (naturally, we don't want any writes to be lost
when switching the old and the new table). TL_WRITE_ALLOW_READ
lock, in turn, would block the started transaction on thr_lock.c
lock, should they do more updates. This, again, lead to the need
to abort such transactions.
The second bug occurred simply because we didn't have any
mechanism to detect deadlocks between the table-level locks
in thr_lock.c and row-level locks in InnoDB, other than
innodb_lock_wait_timeout.
This patch solves both these problems by moving lock conflicts
which are causing these deadlocks into the metadata locking
subsystem, thus making it possible to avoid or detect such
deadlocks inside MDL.
To do this we introduce new type-of-operation-aware metadata
locks, which allow MDL subsystem to know not only the fact that
transaction has used or is going to use some object but also what
kind of operation it has carried out or going to carry out on the
object.
This, along with the addition of a special kind of upgradable
metadata lock, allows ALTER TABLE to wait until all
transactions which has updated the table to go away.
This solves the second issue.
Another special type of upgradable metadata lock is acquired
by LOCK TABLE WRITE. This second lock type allows to solve the
first issue, since abortion of table-level locks in event of
DDL under LOCK TABLES becomes also unnecessary.
Below follows the list of incompatible changes introduced by
this patch:
- From now on, ALTER TABLE and CREATE/DROP TRIGGER SQL (i.e. those
statements that acquire TL_WRITE_ALLOW_READ lock)
wait for all transactions which has *updated* the table to
complete.
- From now on, LOCK TABLES ... WRITE, REPAIR/OPTIMIZE TABLE
(i.e. all statements which acquire TL_WRITE table-level lock) wait
for all transaction which *updated or read* from the table
to complete.
As a consequence, innodb_table_locks=0 option no longer applies
to LOCK TABLES ... WRITE.
- DROP DATABASE, DROP TABLE, RENAME TABLE no longer abort
statements or transactions which use tables being dropped or
renamed, and instead wait for these transactions to complete.
- Since LOCK TABLES WRITE now takes a special metadata lock,
not compatible with with reads or writes against the subject table
and transaction-wide, thr_lock.c deadlock avoidance algorithm
that used to ensure absence of deadlocks between LOCK TABLES
WRITE and other statements is no longer sufficient, even for
MyISAM. The wait-for graph based deadlock detector of MDL
subsystem may sometimes be necessary and is involved. This may
lead to ER_LOCK_DEADLOCK error produced for multi-statement
transactions even if these only use MyISAM:
session 1: session 2:
begin;
update t1 ... lock table t2 write, t1 write;
-- gets a lock on t2, blocks on t1
update t2 ...
(ER_LOCK_DEADLOCK)
- Finally, support of LOW_PRIORITY option for LOCK TABLES ... WRITE
was abandoned.
LOCK TABLE ... LOW_PRIORITY WRITE from now on has the same
priority as the usual LOCK TABLE ... WRITE.
SELECT HIGH PRIORITY no longer trumps LOCK TABLE ... WRITE in
the wait queue.
- We do not take upgradable metadata locks on implicitly
locked tables. So if one has, say, a view v1 that uses
table t1, and issues:
LOCK TABLE v1 WRITE;
FLUSH TABLE t1; -- (or just 'FLUSH TABLES'),
an error is produced.
In order to be able to perform DDL on a table under LOCK TABLES,
the table must be locked explicitly in the LOCK TABLES list.
2010-02-01 14:43:06 +03:00
|
|
|
# Coverage for situations when we try to execute DDL on tables
|
|
|
|
# which are locked by LOCK TABLES only implicitly.
|
|
|
|
#
|
|
|
|
drop tables if exists t1, t2;
|
|
|
|
drop view if exists v1;
|
|
|
|
drop function if exists f1;
|
|
|
|
create table t1 (i int);
|
|
|
|
create table t2 (j int);
|
|
|
|
#
|
|
|
|
# Try to perform DDL on table which is locked through view.
|
|
|
|
create view v1 as select * from t2;
|
|
|
|
lock tables t1 write, v1 write;
|
|
|
|
flush table t2;
|
|
|
|
ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
|
|
|
|
drop table t2;
|
|
|
|
ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
|
|
|
|
alter table t2 add column k int;
|
|
|
|
ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
|
|
|
|
create trigger t2_bi before insert on t2 for each row set @a:=1;
|
|
|
|
ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
|
|
|
|
# Repair produces error as part of its result set.
|
|
|
|
repair table t2;
|
|
|
|
Table Op Msg_type Msg_text
|
|
|
|
test.t2 repair Error Table 't2' was locked with a READ lock and can't be updated
|
|
|
|
test.t2 repair status Operation failed
|
|
|
|
unlock tables;
|
|
|
|
drop view v1;
|
|
|
|
#
|
|
|
|
# Now, try DDL on table which is locked through routine.
|
|
|
|
create function f1 () returns int
|
|
|
|
begin
|
|
|
|
insert into t2 values (1);
|
|
|
|
return 0;
|
|
|
|
end|
|
|
|
|
create view v1 as select f1() from t1;
|
|
|
|
lock tables v1 read;
|
|
|
|
flush table t2;
|
|
|
|
ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
|
|
|
|
drop table t2;
|
|
|
|
ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
|
|
|
|
alter table t2 add column k int;
|
|
|
|
ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
|
|
|
|
create trigger t2_bi before insert on t2 for each row set @a:=1;
|
|
|
|
ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
|
|
|
|
# Repair produces error as part of its result set.
|
|
|
|
repair table t2;
|
|
|
|
Table Op Msg_type Msg_text
|
|
|
|
test.t2 repair Error Table 't2' was locked with a READ lock and can't be updated
|
|
|
|
test.t2 repair status Operation failed
|
|
|
|
unlock tables;
|
|
|
|
drop view v1;
|
|
|
|
drop function f1;
|
|
|
|
#
|
|
|
|
# Finally, try DDL on table which is locked thanks to trigger.
|
|
|
|
create trigger t1_ai after insert on t1 for each row insert into t2 values (1);
|
|
|
|
lock tables t1 write;
|
|
|
|
flush table t2;
|
|
|
|
ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
|
|
|
|
drop table t2;
|
|
|
|
ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
|
|
|
|
alter table t2 add column k int;
|
|
|
|
ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
|
|
|
|
create trigger t2_bi before insert on t2 for each row set @a:=1;
|
|
|
|
ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
|
|
|
|
# Repair produces error as part of its result set.
|
|
|
|
repair table t2;
|
|
|
|
Table Op Msg_type Msg_text
|
|
|
|
test.t2 repair Error Table 't2' was locked with a READ lock and can't be updated
|
|
|
|
test.t2 repair status Operation failed
|
|
|
|
unlock tables;
|
|
|
|
drop trigger t1_ai;
|
|
|
|
drop tables t1, t2;
|
|
|
|
#
|
Backport of:
----------------------------------------------------------
revno: 2617.69.2
committer: Konstantin Osipov <kostja@sun.com>
branch nick: 5.4-azalea-bugfixing
timestamp: Mon 2009-08-03 19:26:04 +0400
message:
A fix and a test case for Bug#45035 "Altering table under LOCK TABLES
results in "Error 1213 Deadlock found...".
If a user had a table locked with LOCK TABLES
for READ and for WRITE in the same connection, ALTER TABLE
could fail.
Root cause analysis:
If a connection issues
LOCK TABLE t1 write, t1 a read, t1 b read;
the new LOCK TABLES code in 6.0 (part of WL 3726) will create
the following list of TABLE_LIST objects
(thd->locked_tables_list->m_locked_tables):
{"t1" "b" tl_read_no_insert}, {"t1" "a" tl_read_no_insert},
{"t1" "t1" tl_write }
Later on, when we try to ALTER table t1, mysql_alter_table()
closes all TABLE instances and releases its thr_lock locks,
keeping only an exclusive metadata lock on t1.
But when ALTER is finished, Locked_table_list::reopen_tables()
tries to restore the original list of open and locked tables.
Before this patch, it used to do so one by one:
Open t1 b, get TL_READ_NO_INSERT lock,
Open t1 a, get TL_READ_NO_INSERT lock
Open t1, try to get TL_WRITE lock, deadlock.
The cause of the deadlock is that thr_lock.c doesn't
resolve the situation when the read list only consists
of locks taken by the same thread, followed by this very
thread trying to take a WRITE lock. Indeed, since
thr_lock_multi always gets a sorted list of locks,
WRITE locks always precede READ locks in the list
to lock.
Don't try to fix thr_lock.c deficiency, keep this
code simple.
Instead, try to take all thr_lock locks at once
in ::reopen_tables().
2009-12-08 11:38:45 +03:00
|
|
|
# Bug#45035 " Altering table under LOCK TABLES results in
|
|
|
|
# "Error 1213 Deadlock found..."
|
|
|
|
#
|
|
|
|
# When reopening tables under LOCK TABLES after ALTER TABLE,
|
|
|
|
# 6.0 used to be taking thr_lock locks one by one, and
|
|
|
|
# that would lead to a lock conflict.
|
|
|
|
# Check that taking all locks at once works.
|
|
|
|
#
|
|
|
|
drop table if exists t1;
|
|
|
|
create table t1 (i int);
|
|
|
|
lock tables t1 write, t1 as a read, t1 as b read;
|
|
|
|
alter table t1 add column j int;
|
|
|
|
unlock tables;
|
|
|
|
drop table t1;
|
|
|
|
create temporary table t1 (i int);
|
|
|
|
#
|
|
|
|
# This is just for test coverage purposes,
|
|
|
|
# when this is allowed, remove the --error.
|
|
|
|
#
|
|
|
|
lock tables t1 write, t1 as a read, t1 as b read;
|
|
|
|
ERROR HY000: Can't reopen table: 't1'
|
|
|
|
alter table t1 add column j int;
|
|
|
|
unlock tables;
|
|
|
|
drop table t1;
|
|
|
|
#
|
|
|
|
# Separate case for partitioned tables is important
|
|
|
|
# because each partition has an own thr_lock object.
|
|
|
|
#
|
|
|
|
create table t1 (i int) partition by list (i)
|
|
|
|
(partition p0 values in (1),
|
|
|
|
partition p1 values in (2,3),
|
|
|
|
partition p2 values in (4,5));
|
|
|
|
lock tables t1 write, t1 as a read, t1 as b read;
|
|
|
|
alter table t1 add column j int;
|
|
|
|
unlock tables;
|
|
|
|
drop table t1;
|
|
|
|
#
|
2009-12-08 15:56:06 +01:00
|
|
|
# Bug #43272 HANDLER SQL command does not work under LOCK TABLES
|
|
|
|
#
|
|
|
|
DROP TABLE IF EXISTS t1;
|
|
|
|
CREATE TABLE t1 (a INT);
|
|
|
|
LOCK TABLE t1 WRITE;
|
|
|
|
# HANDLER commands are not allowed in LOCK TABLES mode
|
|
|
|
HANDLER t1 OPEN;
|
|
|
|
ERROR HY000: Can't execute the given command because you have active locked tables or an active transaction
|
|
|
|
HANDLER t1 READ FIRST;
|
|
|
|
ERROR HY000: Can't execute the given command because you have active locked tables or an active transaction
|
|
|
|
HANDLER t1 CLOSE;
|
|
|
|
ERROR HY000: Can't execute the given command because you have active locked tables or an active transaction
|
|
|
|
UNLOCK TABLES;
|
|
|
|
DROP TABLE t1;
|
|
|
|
#
|
2009-12-08 14:14:05 +01:00
|
|
|
# Bug#45066 FLUSH TABLES WITH READ LOCK deadlocks against
|
|
|
|
# LOCK TABLE
|
|
|
|
#
|
|
|
|
DROP TABLE IF EXISTS t1;
|
|
|
|
CREATE TABLE t1(a INT);
|
|
|
|
LOCK TABLE t1 READ;
|
|
|
|
FLUSH TABLES;
|
|
|
|
ERROR HY000: Table 't1' was locked with a READ lock and can't be updated
|
|
|
|
LOCK TABLE t1 WRITE;
|
|
|
|
FLUSH TABLES;
|
|
|
|
#
|
|
|
|
# If you allow the next combination, you reintroduce bug Bug#45066
|
|
|
|
#
|
|
|
|
LOCK TABLE t1 READ;
|
|
|
|
FLUSH TABLES WITH READ LOCK;
|
|
|
|
ERROR HY000: Can't execute the given command because you have active locked tables or an active transaction
|
|
|
|
LOCK TABLE t1 WRITE;
|
|
|
|
FLUSH TABLES WITH READ LOCK;
|
|
|
|
ERROR HY000: Can't execute the given command because you have active locked tables or an active transaction
|
|
|
|
UNLOCK TABLES;
|
|
|
|
DROP TABLE t1;
|
|
|
|
#
|
2010-01-08 11:26:32 +01:00
|
|
|
# Simplified test for bug #48538 "Assertion in thr_lock() on LOAD DATA
|
|
|
|
# CONCURRENT INFILE".
|
|
|
|
#
|
|
|
|
DROP TABLE IF EXISTS t1;
|
|
|
|
CREATE TABLE t1 (f1 INT, f2 INT) ENGINE = MEMORY;
|
|
|
|
CREATE TRIGGER t1_ai AFTER INSERT ON t1 FOR EACH ROW
|
|
|
|
UPDATE LOW_PRIORITY t1 SET f2 = 7;
|
|
|
|
# Statement below should fail with ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG
|
|
|
|
# error instead of failing on assertion in table-level locking subsystem.
|
|
|
|
INSERT INTO t1(f1) VALUES(0);
|
|
|
|
ERROR HY000: Can't update table 't1' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
|
|
|
|
DROP TABLE t1;
|
|
|
|
#
|
2010-01-15 12:47:22 +01:00
|
|
|
# Bug#43685 Lock table affects other non-related tables
|
|
|
|
#
|
|
|
|
DROP TABLE IF EXISTS t1, t2;
|
|
|
|
CREATE TABLE t1 (id INT);
|
|
|
|
CREATE TABLE t2 (id INT);
|
|
|
|
# Connection default
|
|
|
|
LOCK TABLE t1 WRITE;
|
|
|
|
ANALYZE TABLE t1;
|
|
|
|
Table Op Msg_type Msg_text
|
|
|
|
test.t1 analyze status Table is already up to date
|
|
|
|
# Connection con2
|
|
|
|
LOCK TABLE t2 WRITE;
|
|
|
|
# This used to hang until the first connection
|
|
|
|
# unlocked t1.
|
|
|
|
FLUSH TABLE t2;
|
|
|
|
UNLOCK TABLES;
|
|
|
|
# Connection default
|
|
|
|
UNLOCK TABLES;
|
|
|
|
DROP TABLE t1, t2;
|
|
|
|
#
|
2009-12-02 18:22:15 +03:00
|
|
|
# End of 6.0 tests.
|
|
|
|
#
|