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.
parent 770219c1
......@@ -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 @@ end:
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)
......@@ -783,15 +783,22 @@ void mysql_ha_mark_tables_for_reopen(THD *thd, TABLE *table)
safe_mutex_assert_owner(&LOCK_open);
for (; table; table= table->next)
{
TABLE_LIST *hash_tables;
if ((hash_tables= (TABLE_LIST*) hash_search(&thd->handler_tables_hash,
(byte*) table->alias,
strlen(table->alias) + 1)))
/*
Some elements in open table list, for example placeholders used for
name-locking, can have alias set to 0.
*/
if (table->alias)
{
/* Mark table as ready for reopen. */
hash_tables->table= NULL;
/* End open index/table scans. */
table->file->ha_index_or_rnd_end();
TABLE_LIST *hash_tables;
if ((hash_tables= (TABLE_LIST*) hash_search(&thd->handler_tables_hash,
(byte*) table->alias,
strlen(table->alias) + 1)))
{
/* Mark table as ready for reopen. */
hash_tables->table= NULL;
/* End open index/table scans. */
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