Commit d46c8ce6 authored by unknown's avatar unknown

Fix for:

  Bug #20662 "Infinite loop in CREATE TABLE IF NOT EXISTS ... SELECT
              with locked tables"
  Bug #20903 "Crash when using CREATE TABLE .. SELECT and triggers"
  Bug #24738 "CREATE TABLE ... SELECT is not isolated properly"
  Bug #24508 "Inconsistent results of CREATE TABLE ... SELECT when
              temporary table exists"

Deadlock occured when one tried to execute CREATE TABLE IF NOT
EXISTS ... SELECT statement under LOCK TABLES which held
read lock on target table.
Attempt to execute the same statement for already existing
target table with triggers caused server crashes.
Also concurrent execution of CREATE TABLE ... SELECT statement
and other statements involving target table suffered from
various races (some of which might've led to deadlocks).
Finally, attempt to execute CREATE TABLE ... SELECT in case
when a temporary table with same name was already present
led to the insertion of data into this temporary table and
creation of empty non-temporary table.
 
All above problems stemmed from the old implementation of CREATE
TABLE ... SELECT in which we created, opened and locked target
table without any special protection in a separate step and not
with the rest of tables used by this statement.
This underminded deadlock-avoidance approach used in server
and created window for races. It also excluded target table
from prelocking causing problems with trigger execution.

The patch solves these problems by implementing new approach to
handling of CREATE TABLE ... SELECT for base tables.
We try to open and lock table to be created at the same time as
the rest of tables used by this statement. If such table does not
exist at this moment we create and place in the table cache special
placeholder for it which prevents its creation or any other usage
by other threads.
We still use old approach for creation of temporary tables.

Note that we have separate fix for 5.0 since there we use slightly
different less intrusive approach.


mysql-test/r/create.result:
  Extended test coverage for CREATE TABLE ... SELECT. In particular added
  tests for bug #24508 "Inconsistent results of CREATE TABLE ... SELECT
  when temporary table exists" and bug #20662 "Infinite loop in CREATE
  TABLE IF NOT EXISTS ... SELECT with locked tables".
mysql-test/r/trigger.result:
  Added test case for bug #20903 "Crash when using CREATE TABLE .. SELECT
  and triggers"
mysql-test/t/create.test:
  Extended test coverage for CREATE TABLE ... SELECT. In particular added
  tests for bug #24508 "Inconsistent results of CREATE TABLE ... SELECT
  when temporary table exists" and bug #20662 "Infinite loop in CREATE
  TABLE IF NOT EXISTS ... SELECT with locked tables".
mysql-test/t/trigger.test:
  Added test case for bug #20903 "Crash when using CREATE TABLE .. SELECT
  and triggers"
sql/lock.cc:
  Now for creation of name-lock placeholder lock_table_name() uses
  auxiliary function table_cache_insert_placeholder().
sql/mysql_priv.h:
  Removed declaration of non-existing build_table_path() routine.
  The former mysql_create_table_internal() was renamed to
  mysql_create_table_no_lock() and now exposed to other modules to
  give them opportunity of creation of tables in cases when name-lock
  is already obtained.
  reopen_name_locked_table() now has 3rd argument which controls linking
  in of table being opened into THD::open_tables (this is useful in
  cases when placeholder used for name-locking is already linked into
  this list).
  Added declaration of auxiliary function table_cache_insert_placeholder()
  which is used for creation of table placeholders for name-locking.
  Added declaration of lock_table_name_if_not_cached() which can be
  used to take an exclusive name-lock on table if there are no records
  for it in table cache.
  Changed signature of unlink_open_table() function to simplify its use
  and make it useful for table placeholders and tables that are only open.
  Added auxiliary drop_open_table() routine.
  Moved declaration of refresh_version to table.h header to make it
  accessible from inline methods of TABLE class.
  MYSQL_OPEN_IGNORE_LOCKED_TABLES flag is no longer used. Instead
  MYSQL_OPEN_TEMPORARY_ONLY option was added.
sql/sql_base.cc:
  Added support for the new approach to the handling of CREATE TABLE
  ... SELECT for base tables.
  
  Now we try to open and lock table to be created at the same time as
  the rest of tables used by this statement. If such table does not
  exist at this moment we create and place in the table cache special
  placeholder for it which prevents its creation or any other usage
  by other threads.
  
  Note significant distinctions of this placeholder from the placeholder
  used for normal name-lock: 1) It is treated like open table by other
  name-locks so it does not allow name-lock taking operations like DROP
  TABLE or RENAME TABLE to proceed. 2) it is linked into THD::open_tables
  list and automatically removed during close_thread_tables() call
    
  open_tables():
    Implemented logic described above. To do this added
    auxiliary check_if_table_exists() function.
    Removed support for MYSQL_OPEN_IGNORE_LOCKED_TABLES option
    which is no longer used.
    Added MYSQL_OPEN_TEMPORARY_ONLY which is used to restrict
    search for temporary tables only.
  close_cached_tables()/close_thread_table()/reopen_tables()/
  close_old_data_files()/table_is_used()/remove_table_from_cache():
    Added support for open placeholders (note that we also use them
    when we need to re-open tables during flush).
  unlink_open_table():
    Changed function signature to simplify its use and to make
    useful for open placeholders and tables which are only
    open and not locked.
  Added auxiliary drop_open_table() routine.
  reopen_name_locked_table():
    Now has 3rd argument which controls linking in of table being
    opened into THD::open_tables (this is useful in cases when
    placeholder used for name-locking is already linked into
    this list).
  Added auxiliary table_cache_insert_placeholder() routine which
  simplifies creation of placeholders used for name-locking.
  Added lock_table_name_if_not_cached() which can be used to take
  an exclusive name-lock on table if there are no records for it
  in table cache.
sql/sql_handler.cc:
  Adjusted mysql_ha_mark_tables_for_reopen() routine to properly
  handle placeholders which now can be linked into open tables
  list.
sql/sql_insert.cc:
  Introduced new approach to handling of base tables in CREATE TABLE
  ... SELECT statement.
  
  Now we try to open and lock table to be created at the same time as
  the rest of tables used by this statement. If such table does not
  exist at this moment we create and place in the table cache special
  placeholder for it which prevents its creation or any other usage
  by other threads. By doing this we avoid races which existed with
  previous approach in which we created, opened and locked target in
  separate step without any special protection.
  This also allows properly calculate prelocking set in cases when
  target table already exists and has some on insert triggers.
  
  Note that we don't employ the same approach for temporary tables
  (this is okay as such tables are unaffected by other threads).
  
  Changed create_table_from_items() and methods of select_create
  class to implement this approach.
sql/sql_parse.cc:
  The new approach to handling of CREATE TABLE ... SELECT for
  base tables assumes that all tables (including table to be
  created) are opened and (or) locked at the same time.
  So in cases when we create base table we have to pass to
  open_and_lock_tables() table list which includes target table.
sql/sql_prepare.cc:
  The new approach to handling of CREATE TABLE ... SELECT for
  base tables assumes that all tables (including table to be
  created) are opened and (or) locked at the same time.
  So in cases when we create base table we have to pass to
  open_and_lock_tables() table list which includes target table.
sql/sql_table.cc:
  Changed mysql_create_table(), mysql_create_like_table() and
  mysql_alter_table() (in rename case) to obtain exclusive name-lock
  on the non-temporary table which is going to be created (to which
  we going to rename). This ensures that not only destination table
  doesn't exist on disk but also that there are no placeholder in 
  table cache for it (i.e. there is no CREATE TABLE ... SELECT operation
  in progress for it). Note that to avoid deadlocks while taking these
  name-locks this code assumes that existence of any record for table in
  table cache (even name-lock) means that table exists. Altough such
  check can lead to false positives these should occur only in case of
  highly concurrent DDL operations on the table and should not break
  binary logging.
  
  Renamed mysql_create_table_internal() to mysql_create_table_no_lock()
  and made it accessible from other files to give them ability to create
  table in situation when name-lock is already obtained or not relevant.
  
  Adjusted calls to reopen_name_locked_table(), which now takes
  extra argument, which controls linking of open table into
  THD::open_tables list.
  
  Removed redundant setting of table's 'version' field before calls
  to close_cached_table(). This function will set it to 0 itself
  anyway.
sql/sql_trigger.cc:
  reopen_name_locked_tables() now has one more argument which controls
  linking of opened table into the THD::open_tables list.
sql/sql_yacc.yy:
  The new approach to handling of CREATE TABLE ... SELECT statement
  for base tables assumes that all tables including table to be
  created are open and (or) locked at the same time. Therefore
  we need to set correct lock for target table.
sql/table.h:
  Moved declaration of refresh_version variable from mysql_priv.h
  to make it accessible from inline methods of TABLE class.
  Renamed TABLE::locked_by_flush member to open_placeholder since
  now it is also used for taking exclusive name-lock and not only
  by flush. 
  Introduced TABLE::is_name_opened() helper method which can be used
  to distinguish TABLE instances corresponding to open tables or
  placeholders for them from closed instances (e.g. due to their old
  version). Also introduced TABLE::needs_reopen_or_name_lock() helper
  which allows to check if TABLE instance corresponds to outdated
  version of table or to name-lock placeholder.
  Introduced TABLE_LIST::create member which marks elements of
  table list corresponds to the table to be created.
  Adjusted TABLE_LIST::placeholder() method to take into account 
  name-lock placeholders for tables to be created (this, for example,
  allows to properly handle such placeholders in lock_tables()).
  Finally, moved currently unused TABLE::open_next/open_prev
  members under ifdef NOT_YET.
mysql-test/r/create_select-big.result:
  New BitKeeper file ``mysql-test/r/create_select-big.result''
mysql-test/t/create_select-big.test:
  New BitKeeper file ``mysql-test/t/create_select-big.test''
parent 0adfd75e
......@@ -782,6 +782,100 @@ t1 CREATE TABLE `t1` (
`i` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 MAX_ROWS=4294967295
drop table t1;
create table t1 select * from t2;
ERROR 42S02: Table 'test.t2' doesn't exist
create table t1 select * from t1;
ERROR HY000: You can't specify target table 't1' for update in FROM clause
create table t1 select coalesce('a' collate latin1_swedish_ci,'b' collate latin1_bin);
ERROR HY000: Illegal mix of collations (latin1_swedish_ci,EXPLICIT) and (latin1_bin,EXPLICIT) for operation 'coalesce'
create table t1 (primary key(a)) select "b" as b;
ERROR 42000: Key column 'a' doesn't exist in table
create table t1 (a int);
create table if not exists t1 select 1 as a, 2 as b;
ERROR 21S01: Column count doesn't match value count at row 1
drop table t1;
create table t1 (primary key (a)) (select 1 as a) union all (select 1 as a);
ERROR 23000: Duplicate entry '1' for key 'PRIMARY'
create table t1 (i int);
create table t1 select 1 as i;
ERROR 42S01: Table 't1' already exists
create table if not exists t1 select 1 as i;
Warnings:
Note 1050 Table 't1' already exists
select * from t1;
i
1
create table t1 select coalesce('a' collate latin1_swedish_ci,'b' collate latin1_bin);
ERROR HY000: Illegal mix of collations (latin1_swedish_ci,EXPLICIT) and (latin1_bin,EXPLICIT) for operation 'coalesce'
select * from t1;
i
1
alter table t1 add primary key (i);
create table if not exists t1 (select 2 as i) union all (select 2 as i);
ERROR 23000: Duplicate entry '2' for key 'PRIMARY'
select * from t1;
i
1
2
drop table t1;
create temporary table t1 (j int);
create table if not exists t1 select 1;
Warnings:
Note 1050 Table 't1' already exists
select * from t1;
j
1
drop temporary table t1;
select * from t1;
ERROR 42S02: Table 'test.t1' doesn't exist
drop table t1;
ERROR 42S02: Unknown table 't1'
create table t1 (i int);
insert into t1 values (1), (2);
lock tables t1 read;
create table t2 select * from t1;
ERROR HY000: Table 't2' was not locked with LOCK TABLES
create table if not exists t2 select * from t1;
ERROR HY000: Table 't2' was not locked with LOCK TABLES
unlock tables;
create table t2 (j int);
lock tables t1 read;
create table t2 select * from t1;
ERROR HY000: Table 't2' was not locked with LOCK TABLES
create table if not exists t2 select * from t1;
ERROR HY000: Table 't2' was not locked with LOCK TABLES
unlock tables;
lock table t1 read, t2 read;
create table t2 select * from t1;
ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
create table if not exists t2 select * from t1;
ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
unlock tables;
lock table t1 read, t2 write;
create table t2 select * from t1;
ERROR 42S01: Table 't2' already exists
create table if not exists t2 select * from t1;
Warnings:
Note 1050 Table 't2' already exists
select * from t1;
i
1
2
unlock tables;
drop table t2;
lock tables t1 read;
create temporary table t2 select * from t1;
create temporary table if not exists t2 select * from t1;
Warnings:
Note 1050 Table 't2' already exists
select * from t2;
i
1
2
1
2
unlock tables;
drop table t1, t2;
create table t1 (upgrade int);
drop table t1;
End of 5.0 tests
......
drop table if exists t1,t2,t3,t4,t5;
set session debug="+d,sleep_create_select_before_create";
create table t1 select 1 as i;;
create table t1 (j char(5));
ERROR 42S01: Table 't1' already exists
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`i` int(1) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
create table t1 select 1 as i;;
create table t1 select "Test" as j;
ERROR 42S01: Table 't1' already exists
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`i` int(1) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
create table t3 (j char(5));
create table t1 select 1 as i;;
create table t1 like t3;
ERROR 42S01: Table 't1' already exists
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`i` int(1) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
create table t1 select 1 as i;;
rename table t3 to t1;
ERROR 42S01: Table 't1' already exists
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`i` int(1) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
create table t1 select 1 as i;;
alter table t3 rename to t1;
ERROR 42S01: Table 't1' already exists
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`i` int(1) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
create table t1 select 1 as i;;
alter table t3 rename to t1, add k int;
ERROR 42S01: Table 't1' already exists
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`i` int(1) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1, t3;
set session debug="-d,sleep_create_select_before_create:+d,sleep_create_select_before_open";
create table t1 select 1 as i;;
drop table t1;
create table t1 select 1 as i;;
rename table t1 to t2;
drop table t2;
create table t1 select 1 as i;;
select * from t1;
i
1
drop table t1;
create table t1 select 1 as i;;
insert into t1 values (2);
select * from t1;
i
1
2
drop table t1;
set @a:=0;
create table t1 select 1 as i;;
create trigger t1_bi before insert on t1 for each row set @a:=1;
select @a;
@a
0
drop table t1;
set session debug="-d,sleep_create_select_before_open:+d,sleep_create_select_before_lock";
create table t1 select 1 as i;;
drop table t1;
create table t1 select 1 as i;;
rename table t1 to t2;
drop table t2;
create table t1 select 1 as i;;
select * from t1;
i
1
drop table t1;
create table t1 select 1 as i;;
insert into t1 values (2);
select * from t1;
i
1
2
drop table t1;
set @a:=0;
create table t1 select 1 as i;;
create trigger t1_bi before insert on t1 for each row set @a:=1;
select @a;
@a
0
drop table t1;
set session debug="-d,sleep_create_select_before_lock:+d,sleep_create_select_before_check_if_exists";
create table t1 (i int);
create table if not exists t1 select 1 as i;;
drop table t1;
Warnings:
Note 1050 Table 't1' already exists
create table t1 (i int);
set @a:=0;
create table if not exists t1 select 1 as i;;
create trigger t1_bi before insert on t1 for each row set @a:=1;
Warnings:
Note 1050 Table 't1' already exists
select @a;
@a
0
select * from t1;
i
1
drop table t1;
set session debug="-d,sleep_create_select_before_check_if_exists";
create table t2 (a int);
create table t4 (b int);
lock table t4 write;
select 1;
1
1
create table t3 as select * from t4;;
create table t1 select * from t2, t3;;
unlock tables;
select * from t1;
a b
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1, t3;
lock table t4 read;
select 1;
1
1
rename table t4 to t3;;
create table if not exists t1 select 1 as i from t2, t3;;
create table t5 (j int);
rename table t5 to t1;
unlock tables;
Warnings:
Note 1050 Table 't1' already exists
select * from t1;
j
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`j` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1, t2, t3;
......@@ -1414,4 +1414,39 @@ id val
DROP TRIGGER trg27006_a_insert;
DROP TRIGGER trg27006_a_update;
drop table t1,t2;
drop table if exists t1, t2, t3;
create table t1 (i int);
create trigger t1_bi before insert on t1 for each row set new.i = 7;
create trigger t1_ai after insert on t1 for each row set @a := 7;
create table t2 (j int);
insert into t2 values (1), (2);
set @a:="";
create table if not exists t1 select * from t2;
Warnings:
Note 1050 Table 't1' already exists
select * from t1;
i
7
7
select @a;
@a
7
drop trigger t1_bi;
drop trigger t1_ai;
create table t3 (isave int);
create trigger t1_bi before insert on t1 for each row insert into t3 values (new.i);
create table if not exists t1 select * from t2;
Warnings:
Note 1050 Table 't1' already exists
select * from t1;
i
7
7
1
2
select * from t3;
isave
1
2
drop table t1, t2, t3;
End of 5.0 tests
......@@ -673,6 +673,117 @@ alter table t1 max_rows=100000000000;
show create table t1;
drop table t1;
#
# Tests for errors happening at various stages of CREATE TABLES ... SELECT
#
# (Also checks that it behaves atomically in the sense that in case
# of error it is automatically dropped if it has not existed before.)
#
# Error during open_and_lock_tables() of tables
--error ER_NO_SUCH_TABLE
create table t1 select * from t2;
# Rather special error which also caught during open tables pahse
--error ER_UPDATE_TABLE_USED
create table t1 select * from t1;
# Error which happens before select_create::prepare()
--error ER_CANT_AGGREGATE_2COLLATIONS
create table t1 select coalesce('a' collate latin1_swedish_ci,'b' collate latin1_bin);
# Error during table creation
--error ER_KEY_COLUMN_DOES_NOT_EXITS
create table t1 (primary key(a)) select "b" as b;
# Error in select_create::prepare() which is not related to table creation
create table t1 (a int);
--error ER_WRONG_VALUE_COUNT_ON_ROW
create table if not exists t1 select 1 as a, 2 as b;
drop table t1;
# Finally error which happens during insert
--error ER_DUP_ENTRY_WITH_KEY_NAME
create table t1 (primary key (a)) (select 1 as a) union all (select 1 as a);
# What happens if table already exists ?
create table t1 (i int);
--error ER_TABLE_EXISTS_ERROR
create table t1 select 1 as i;
create table if not exists t1 select 1 as i;
select * from t1;
# Error before select_create::prepare()
--error ER_CANT_AGGREGATE_2COLLATIONS
create table t1 select coalesce('a' collate latin1_swedish_ci,'b' collate latin1_bin);
select * from t1;
# Error which happens during insertion of rows
alter table t1 add primary key (i);
--error ER_DUP_ENTRY_WITH_KEY_NAME
create table if not exists t1 (select 2 as i) union all (select 2 as i);
select * from t1;
drop table t1;
# Base vs temporary tables dillema (a.k.a. bug#24508 "Inconsistent
# results of CREATE TABLE ... SELECT when temporary table exists").
# In this situation we either have to create non-temporary table and
# insert data in it or insert data in temporary table without creation
# of permanent table. Since currently temporary tables always shadow
# permanent tables we adopt second approach.
create temporary table t1 (j int);
create table if not exists t1 select 1;
select * from t1;
drop temporary table t1;
--error ER_NO_SUCH_TABLE
select * from t1;
--error ER_BAD_TABLE_ERROR
drop table t1;
#
# CREATE TABLE ... SELECT and LOCK TABLES
#
# There is little sense in using CREATE TABLE ... SELECT under
# LOCK TABLES as it mostly does not work. At least we check that
# the server doesn't crash, hang and produces sensible errors.
# Includes test for bug #20662 "Infinite loop in CREATE TABLE
# IF NOT EXISTS ... SELECT with locked tables".
create table t1 (i int);
insert into t1 values (1), (2);
lock tables t1 read;
--error ER_TABLE_NOT_LOCKED
create table t2 select * from t1;
--error ER_TABLE_NOT_LOCKED
create table if not exists t2 select * from t1;
unlock tables;
create table t2 (j int);
lock tables t1 read;
--error ER_TABLE_NOT_LOCKED
create table t2 select * from t1;
# This should not be ever allowed as it will undermine
# lock-all-at-once approach
--error ER_TABLE_NOT_LOCKED
create table if not exists t2 select * from t1;
unlock tables;
lock table t1 read, t2 read;
--error ER_TABLE_NOT_LOCKED_FOR_WRITE
create table t2 select * from t1;
--error ER_TABLE_NOT_LOCKED_FOR_WRITE
create table if not exists t2 select * from t1;
unlock tables;
lock table t1 read, t2 write;
--error ER_TABLE_EXISTS_ERROR
create table t2 select * from t1;
# This is the only case which really works.
create table if not exists t2 select * from t1;
select * from t1;
unlock tables;
drop table t2;
# OTOH CREATE TEMPORARY TABLE ... SELECT should work
# well under LOCK TABLES.
lock tables t1 read;
create temporary table t2 select * from t1;
create temporary table if not exists t2 select * from t1;
select * from t2;
unlock tables;
drop table t1, t2;
#
# Bug#21772: can not name a column 'upgrade' when create a table
#
......@@ -715,6 +826,7 @@ INSERT INTO t2 select * from t1;
SELECT * from t2;
drop table t1,t2;
#
# Test incorrect database names
#
......
# Tests for various aspects of CREATE TABLE ... SELECT implementation
#
# Note that we don't test general CREATE TABLE ... SELECT functionality here as
# it is already covered by create.test. We are more interested in extreme cases.
#
# This test takes rather long time so let us run it only in --big-test mode
--source include/big_test.inc
# We are using some debug-only features in this test
--source include/have_debug.inc
# Create auxilliary connections
connect (addconroot1, localhost, root,,);
connect (addconroot2, localhost, root,,);
connect (addconroot3, localhost, root,,);
connection default;
--disable_warnings
drop table if exists t1,t2,t3,t4,t5;
--enable_warnings
#
# Tests for concurrency problems.
#
# We introduce delays between various stages of table creation
# and check that other statements dealing with this table cannot
# interfere during those delays.
#
# What happens in situation when other statement messes with
# table to be created before it is created ?
# Concurrent CREATE TABLE
set session debug="+d,sleep_create_select_before_create";
--send create table t1 select 1 as i;
connection addconroot1;
--sleep 2
--error ER_TABLE_EXISTS_ERROR
create table t1 (j char(5));
connection default;
--reap
show create table t1;
drop table t1;
# Concurrent CREATE TABLE ... SELECT
--send create table t1 select 1 as i;
connection addconroot1;
--sleep 2
--error ER_TABLE_EXISTS_ERROR
create table t1 select "Test" as j;
connection default;
--reap
show create table t1;
drop table t1;
# Concurrent CREATE TABLE LIKE
create table t3 (j char(5));
--send create table t1 select 1 as i;
connection addconroot1;
--sleep 2
--error ER_TABLE_EXISTS_ERROR
create table t1 like t3;
connection default;
--reap
show create table t1;
drop table t1;
# Concurrent RENAME TABLE
--send create table t1 select 1 as i;
connection addconroot1;
--sleep 2
--error ER_TABLE_EXISTS_ERROR
rename table t3 to t1;
connection default;
--reap
show create table t1;
drop table t1;
# Concurrent ALTER TABLE RENAME
--send create table t1 select 1 as i;
connection addconroot1;
--sleep 2
--error ER_TABLE_EXISTS_ERROR
alter table t3 rename to t1;
connection default;
--reap
show create table t1;
drop table t1;
# Concurrent ALTER TABLE RENAME which also adds column
--send create table t1 select 1 as i;
connection addconroot1;
--sleep 2
--error ER_TABLE_EXISTS_ERROR
alter table t3 rename to t1, add k int;
connection default;
--reap
show create table t1;
drop table t1, t3;
# What happens if other statement sneaks in after the table
# creation but before its opening ?
set session debug="-d,sleep_create_select_before_create:+d,sleep_create_select_before_open";
# Concurrent DROP TABLE
--send create table t1 select 1 as i;
connection addconroot1;
--sleep 2
drop table t1;
connection default;
--reap
# Concurrent RENAME TABLE
--send create table t1 select 1 as i;
connection addconroot1;
--sleep 2
rename table t1 to t2;
connection default;
--reap
drop table t2;
# Concurrent SELECT
--send create table t1 select 1 as i;
connection addconroot1;
--sleep 2
select * from t1;
connection default;
--reap
drop table t1;
# Concurrent INSERT
--send create table t1 select 1 as i;
connection addconroot1;
--sleep 2
insert into t1 values (2);
connection default;
--reap
select * from t1;
drop table t1;
# Concurrent CREATE TRIGGER
set @a:=0;
--send create table t1 select 1 as i;
connection addconroot1;
--sleep 2
create trigger t1_bi before insert on t1 for each row set @a:=1;
connection default;
--reap
select @a;
drop table t1;
# Okay, now the same tests for the potential gap between open and lock
set session debug="-d,sleep_create_select_before_open:+d,sleep_create_select_before_lock";
# Concurrent DROP TABLE
--send create table t1 select 1 as i;
connection addconroot1;
--sleep 2
drop table t1;
connection default;
--reap
# Concurrent RENAME TABLE
--send create table t1 select 1 as i;
connection addconroot1;
--sleep 2
rename table t1 to t2;
connection default;
--reap
drop table t2;
# Concurrent SELECT
--send create table t1 select 1 as i;
connection addconroot1;
--sleep 2
select * from t1;
connection default;
--reap
drop table t1;
# Concurrent INSERT
--send create table t1 select 1 as i;
connection addconroot1;
--sleep 2
insert into t1 values (2);
connection default;
--reap
select * from t1;
drop table t1;
# Concurrent CREATE TRIGGER
set @a:=0;
--send create table t1 select 1 as i;
connection addconroot1;
--sleep 2
create trigger t1_bi before insert on t1 for each row set @a:=1;
connection default;
--reap
select @a;
drop table t1;
# Some tests for case with existing table
set session debug="-d,sleep_create_select_before_lock:+d,sleep_create_select_before_check_if_exists";
create table t1 (i int);
# Concurrent DROP TABLE
--send create table if not exists t1 select 1 as i;
connection addconroot1;
--sleep 2
drop table t1;
connection default;
--reap
# Concurrent CREATE TRIGGER
create table t1 (i int);
set @a:=0;
--send create table if not exists t1 select 1 as i;
connection addconroot1;
--sleep 2
create trigger t1_bi before insert on t1 for each row set @a:=1;
connection default;
--reap
select @a;
select * from t1;
drop table t1;
set session debug="-d,sleep_create_select_before_check_if_exists";
# Test for some details of CREATE TABLE ... SELECT implementation.
#
# We check that create placeholder is handled properly if we have
# to reopen tables in open_tables().
# This test heavily relies on current implementation of name-locking/
# table cache so it may stop working if it changes. OTOH it such problem
# will serve as warning that such changes should not be done lightly.
create table t2 (a int);
create table t4 (b int);
connection addconroot2;
lock table t4 write;
select 1;
connection addconroot1;
# Create placeholder/name-lock for t3
--send create table t3 as select * from t4;
--sleep 2
connection default;
# This statement creates placeholder for t1, then opens t2,
# then meets name-lock for t3 and then reopens all tables
--send create table t1 select * from t2, t3;
--sleep 2
connection addconroot2;
unlock tables;
connection addconroot1;
--reap
connection default;
--reap
select * from t1;
show create table t1;
drop table t1, t3;
# Now similar test which proves that we really temporarily
# remove placeholder when we reopen tables.
connection addconroot2;
lock table t4 read;
select 1;
connection addconroot1;
# Create name-lock for t3
--send rename table t4 to t3;
--sleep 2
connection default;
# This statement creates placeholder for t1, then opens t2,
# then meets name-lock for t3 and then reopens all tables
--send create table if not exists t1 select 1 as i from t2, t3;
--sleep 2
connection addconroot3;
# We should be able to take name-lock on table t1 as we should not have
# open placeholder for it at this point (otherwise it is possible to
# come-up with situation which will lead to deadlock, e.g. think of
# concurrent CREATE TABLE t1 SELECT * FROM t2 and RENAME TABLE t2 TO t1)
create table t5 (j int);
# This statement takes name-lock on t1 and therefore proves
# that there is no active open placeholder for it.
rename table t5 to t1;
connection addconroot2;
unlock tables;
connection addconroot1;
--reap
connection default;
--reap
select * from t1;
show create table t1;
drop table t1, t2, t3;
......@@ -1737,4 +1737,30 @@ DROP TRIGGER trg27006_a_insert;
DROP TRIGGER trg27006_a_update;
drop table t1,t2;
#
# Bug #20903 "Crash when using CREATE TABLE .. SELECT and triggers"
#
--disable_warnings
drop table if exists t1, t2, t3;
--enable_warnings
create table t1 (i int);
create trigger t1_bi before insert on t1 for each row set new.i = 7;
create trigger t1_ai after insert on t1 for each row set @a := 7;
create table t2 (j int);
insert into t2 values (1), (2);
set @a:="";
create table if not exists t1 select * from t2;
select * from t1;
select @a;
# Let us check that trigger that involves table also works ok.
drop trigger t1_bi;
drop trigger t1_ai;
create table t3 (isave int);
create trigger t1_bi before insert on t1 for each row insert into t3 values (new.i);
create table if not exists t1 select * from t2;
select * from t1;
select * from t3;
drop table t1, t2, t3;
--echo End of 5.0 tests
......@@ -892,8 +892,6 @@ int lock_and_wait_for_table_name(THD *thd, TABLE_LIST *table_list)
int lock_table_name(THD *thd, TABLE_LIST *table_list, bool check_in_use)
{
TABLE *table;
TABLE_SHARE *share;
char *key_buff;
char key[MAX_DBKEY_LENGTH];
char *db= table_list->db;
uint key_length;
......@@ -921,29 +919,11 @@ int lock_table_name(THD *thd, TABLE_LIST *table_list, bool check_in_use)
}
}
}
/*
Create a table entry with the right key and with an old refresh version
Note that we must use my_multi_malloc() here as this is freed by the
table cache
*/
if (!my_multi_malloc(MYF(MY_WME | MY_ZEROFILL),
&table, sizeof(*table),
&share, sizeof(*share),
&key_buff, key_length,
NULL))
DBUG_RETURN(-1);
table->s= share;
share->set_table_cache_key(key_buff, key, key_length);
share->tmp_table= INTERNAL_TMP_TABLE; // for intern_close_table
table->in_use= thd;
table->locked_by_name=1;
table_list->table=table;
if (my_hash_insert(&open_cache, (byte*) table))
{
my_free((gptr) table,MYF(0));
if (!(table= table_cache_insert_placeholder(thd, key, key_length)))
DBUG_RETURN(-1);
}
table_list->table=table;
/* Return 1 if table is in use */
DBUG_RETURN(test(remove_table_from_cache(thd, db, table_list->table_name,
......
......@@ -794,8 +794,6 @@ check_and_unset_inject_value(int value)
#endif
uint build_table_path(char *buff, size_t bufflen, const char *db,
const char *table, const char *ext);
void write_bin_log(THD *thd, bool clear_error,
char const *query, ulong query_length);
......@@ -970,6 +968,12 @@ bool mysql_create_table(THD *thd,const char *db, const char *table_name,
List<create_field> &fields, List<Key> &keys,
bool tmp_table, uint select_field_count,
bool use_copy_create_info);
bool mysql_create_table_no_lock(THD *thd, const char *db,
const char *table_name,
HA_CREATE_INFO *create_info,
List<create_field> &fields, List<Key> &keys,
bool tmp_table, uint select_field_count,
bool use_copy_create_info);
bool mysql_alter_table(THD *thd, char *new_db, char *new_name,
HA_CREATE_INFO *create_info,
......@@ -1027,7 +1031,11 @@ TABLE_SHARE *get_cached_table_share(const char *db, const char *table_name);
TABLE *open_ltable(THD *thd, TABLE_LIST *table_list, thr_lock_type update);
TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT* mem,
bool *refresh, uint flags);
bool reopen_name_locked_table(THD* thd, TABLE_LIST* table);
bool reopen_name_locked_table(THD* thd, TABLE_LIST* table_list, bool link_in);
TABLE *table_cache_insert_placeholder(THD *thd, const char *key,
uint key_length);
bool lock_table_name_if_not_cached(THD *thd, const char *db,
const char *table_name, TABLE **table);
TABLE *find_locked_table(THD *thd, const char *db,const char *table_name);
bool reopen_tables(THD *thd,bool get_locks,bool in_refresh);
bool close_data_tables(THD *thd,const char *db, const char *table_name);
......@@ -1195,7 +1203,9 @@ void add_join_on(TABLE_LIST *b,Item *expr);
void add_join_natural(TABLE_LIST *a,TABLE_LIST *b,List<String> *using_fields,
SELECT_LEX *lex);
bool add_proc_to_list(THD *thd, Item *item);
TABLE *unlink_open_table(THD *thd,TABLE *list,TABLE *find);
void unlink_open_table(THD *thd, TABLE *find, bool unlock);
void drop_open_table(THD *thd, TABLE *table, const char *db_name,
const char *table_name);
void update_non_unique_table_error(TABLE_LIST *update,
const char *operation,
TABLE_LIST *duplicate);
......@@ -1630,7 +1640,7 @@ extern double log_01[32];
extern ulonglong log_10_int[20];
extern ulonglong keybuff_size;
extern ulonglong thd_startup_options;
extern ulong refresh_version, thread_id;
extern ulong thread_id;
extern ulong binlog_cache_use, binlog_cache_disk_use;
extern ulong aborted_threads,aborted_connects;
extern ulong delayed_insert_timeout;
......@@ -1773,7 +1783,7 @@ MYSQL_LOCK *mysql_lock_tables(THD *thd, TABLE **table, uint count,
#define MYSQL_LOCK_IGNORE_GLOBAL_READ_LOCK 0x0001
#define MYSQL_LOCK_IGNORE_FLUSH 0x0002
#define MYSQL_LOCK_NOTIFY_IF_NEED_REOPEN 0x0004
#define MYSQL_OPEN_IGNORE_LOCKED_TABLES 0x0008
#define MYSQL_OPEN_TEMPORARY_ONLY 0x0008
void mysql_unlock_tables(THD *thd, MYSQL_LOCK *sql_lock);
void mysql_unlock_read_tables(THD *thd, MYSQL_LOCK *sql_lock);
......
This diff is collapsed.
......@@ -682,7 +682,7 @@ int mysql_ha_flush(THD *thd, TABLE_LIST *tables, uint mode_flags,
while (*table_ptr)
{
if ((mode_flags & MYSQL_HA_FLUSH_ALL) ||
((*table_ptr)->s->version != refresh_version))
(*table_ptr)->needs_reopen_or_name_lock())
{
/* The first time it is required, lock for close_thread_table(). */
if (! did_lock && ! is_locked)
......@@ -782,6 +782,12 @@ void mysql_ha_mark_tables_for_reopen(THD *thd, TABLE *table)
safe_mutex_assert_owner(&LOCK_open);
for (; table; table= table->next)
{
/*
Some elements in open table list, for example placeholders used for
name-locking, can have alias set to 0.
*/
if (table->alias)
{
TABLE_LIST *hash_tables;
if ((hash_tables= (TABLE_LIST*) hash_search(&thd->handler_tables_hash,
......@@ -794,5 +800,6 @@ void mysql_ha_mark_tables_for_reopen(THD *thd, TABLE *table)
table->file->ha_index_or_rnd_end();
}
}
}
DBUG_VOID_RETURN;
}
This diff is collapsed.
......@@ -2164,7 +2164,13 @@ mysql_execute_command(THD *thd)
select_lex->options|= SELECT_NO_UNLOCK;
unit->set_limit(select_lex);
if (!(res= open_and_lock_tables(thd, select_tables)))
if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
{
lex->link_first_table_back(create_table, link_to_local);
create_table->create= TRUE;
}
if (!(res= open_and_lock_tables(thd, lex->query_tables)))
{
/*
Is table which we are changing used somewhere in other parts
......@@ -2173,6 +2179,7 @@ mysql_execute_command(THD *thd)
if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
{
TABLE_LIST *duplicate;
create_table= lex->unlink_first_table(&link_to_local);
if ((duplicate= unique_table(thd, create_table, select_tables, 0)))
{
update_non_unique_table_error(create_table, "CREATE", duplicate);
......@@ -2198,6 +2205,12 @@ mysql_execute_command(THD *thd)
}
}
/*
FIXME Temporary hack which will go away once Kostja pushes
his uber-fix for ALTER/CREATE TABLE.
*/
lex->create_info.table_existed= 0;
if ((result= new select_create(create_table,
&lex->create_info,
lex->create_list,
......@@ -2217,6 +2230,9 @@ mysql_execute_command(THD *thd)
lex->create_list.empty();
lex->key_list.empty();
}
else if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
create_table= lex->unlink_first_table(&link_to_local);
}
else
{
......
......@@ -1492,8 +1492,21 @@ static bool mysql_test_create_table(Prepared_statement *stmt)
if (select_lex->item_list.elements)
{
if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
{
lex->link_first_table_back(create_table, link_to_local);
create_table->create= TRUE;
}
if (open_normal_and_derived_tables(stmt->thd, lex->query_tables, 0))
DBUG_RETURN(TRUE);
if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
create_table= lex->unlink_first_table(&link_to_local);
select_lex->context.resolve_in_select_list= TRUE;
res= select_like_stmt_test_with_open(stmt, tables, 0, 0);
res= select_like_stmt_test(stmt, 0, 0);
}
/* put tables back for PS rexecuting */
......
This diff is collapsed.
......@@ -279,7 +279,7 @@ bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create)
/* We also don't allow creation of triggers on views. */
tables->required_type= FRMTYPE_TABLE;
if (reopen_name_locked_table(thd, tables))
if (reopen_name_locked_table(thd, tables, TRUE))
{
unlock_table_name(thd, tables);
goto end;
......
......@@ -1582,9 +1582,7 @@ create:
lex->sql_command= SQLCOM_CREATE_TABLE;
if (!lex->select_lex.add_table_to_list(thd, $5, NULL,
TL_OPTION_UPDATING,
(using_update_log ?
TL_READ_NO_INSERT:
TL_READ)))
TL_WRITE))
MYSQL_YYABORT;
lex->create_list.empty();
lex->key_list.empty();
......
......@@ -299,6 +299,8 @@ typedef struct st_table_share
} TABLE_SHARE;
extern ulong refresh_version;
/* Information for one open table */
enum index_hint_type
{
......@@ -314,8 +316,8 @@ struct st_table {
handler *file;
#ifdef NOT_YET
struct st_table *used_next, **used_prev; /* Link to used tables */
#endif
struct st_table *open_next, **open_prev; /* Link to open tables */
#endif
struct st_table *next, *prev;
THD *in_use; /* Which thread uses this */
......@@ -431,7 +433,24 @@ struct st_table {
my_bool force_index;
my_bool distinct,const_table,no_rows;
my_bool key_read, no_keyread;
my_bool locked_by_flush;
/*
Placeholder for an open table which prevents other connections
from taking name-locks on this table. Typically used with
TABLE_SHARE::version member to take an exclusive name-lock on
this table name -- a name lock that not only prevents other
threads from opening the table, but also blocks other name
locks. This is achieved by:
- setting open_placeholder to 1 - this will block other name
locks, as wait_for_locked_table_name will be forced to wait,
see table_is_used for details.
- setting version to 0 - this will force other threads to close
the instance of this table and wait (this is the same approach
as used for usual name locks).
An exclusively name-locked table currently can have no handler
object associated with it (db_stat is always 0), but please do
not rely on that.
*/
my_bool open_placeholder;
my_bool locked_by_logger;
my_bool no_replicate;
my_bool locked_by_name;
......@@ -492,7 +511,13 @@ struct st_table {
read_set= &def_read_set;
write_set= &def_write_set;
}
/* Is table open or should be treated as such by name-locking? */
inline bool is_name_opened() { return db_stat || open_placeholder; }
/*
Is this instance of the table should be reopen or represents a name-lock?
*/
inline bool needs_reopen_or_name_lock()
{ return s->version != refresh_version; }
};
enum enum_schema_table_state
......@@ -888,6 +913,12 @@ typedef struct st_table_list
used for implicit LOCK TABLES only and won't be used in real statement.
*/
bool prelocking_placeholder;
/*
This TABLE_LIST object corresponds to the table to be created
so it is possible that it does not exist (used in CREATE TABLE
... SELECT implementation).
*/
bool create;
enum enum_schema_table_state schema_table_state;
void calc_md5(char *buffer);
......@@ -895,7 +926,11 @@ typedef struct st_table_list
int view_check_option(THD *thd, bool ignore_failure);
bool setup_underlying(THD *thd);
void cleanup_items();
bool placeholder() {return derived || view || schema_table || !table; }
bool placeholder()
{
return derived || view || schema_table || create && !table->db_stat ||
!table;
}
void print(THD *thd, String *str);
bool check_single_table(st_table_list **table, table_map map,
st_table_list *view);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment