Commit d5dbdd98 authored by dlenev@mockturtle.local's avatar dlenev@mockturtle.local

Merge bk-internal.mysql.com:/home/bk/mysql-5.0-runtime

into  mockturtle.local:/home/dlenev/src/mysql-5.0-cts-3
parents ad609d6e 8b93e52e
...@@ -769,6 +769,100 @@ t1 CREATE TABLE `t1` ( ...@@ -769,6 +769,100 @@ t1 CREATE TABLE `t1` (
`i` int(11) default NULL `i` int(11) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 MAX_ROWS=4294967295 ) ENGINE=MyISAM DEFAULT CHARSET=latin1 MAX_ROWS=4294967295
drop table t1; 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 1
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 1
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); create table t1 (upgrade int);
drop table t1; drop table t1;
End of 5.0 tests End of 5.0 tests
...@@ -1414,4 +1414,39 @@ id val ...@@ -1414,4 +1414,39 @@ id val
DROP TRIGGER trg27006_a_insert; DROP TRIGGER trg27006_a_insert;
DROP TRIGGER trg27006_a_update; DROP TRIGGER trg27006_a_update;
drop table t1,t2; 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 End of 5.0 tests
...@@ -669,6 +669,117 @@ alter table t1 max_rows=100000000000; ...@@ -669,6 +669,117 @@ alter table t1 max_rows=100000000000;
show create table t1; show create table t1;
drop 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
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
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 # Bug#21772: can not name a column 'upgrade' when create a table
# #
......
...@@ -1737,4 +1737,30 @@ DROP TRIGGER trg27006_a_insert; ...@@ -1737,4 +1737,30 @@ DROP TRIGGER trg27006_a_insert;
DROP TRIGGER trg27006_a_update; DROP TRIGGER trg27006_a_update;
drop table t1,t2; 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 --echo End of 5.0 tests
...@@ -853,7 +853,6 @@ int lock_table_name(THD *thd, TABLE_LIST *table_list) ...@@ -853,7 +853,6 @@ int lock_table_name(THD *thd, TABLE_LIST *table_list)
TABLE *table; TABLE *table;
char key[MAX_DBKEY_LENGTH]; char key[MAX_DBKEY_LENGTH];
char *db= table_list->db; char *db= table_list->db;
int table_in_key_offset;
uint key_length; uint key_length;
HASH_SEARCH_STATE state; HASH_SEARCH_STATE state;
DBUG_ENTER("lock_table_name"); DBUG_ENTER("lock_table_name");
...@@ -861,10 +860,8 @@ int lock_table_name(THD *thd, TABLE_LIST *table_list) ...@@ -861,10 +860,8 @@ int lock_table_name(THD *thd, TABLE_LIST *table_list)
safe_mutex_assert_owner(&LOCK_open); safe_mutex_assert_owner(&LOCK_open);
table_in_key_offset= strmov(key, db) - key + 1; key_length= (uint)(strmov(strmov(key, db) + 1, table_list->table_name) -
key_length= (uint)(strmov(key + table_in_key_offset, table_list->table_name) key) + 1;
- key) + 1;
/* Only insert the table if we haven't insert it already */ /* Only insert the table if we haven't insert it already */
for (table=(TABLE*) hash_first(&open_cache, (byte*)key, key_length, &state); for (table=(TABLE*) hash_first(&open_cache, (byte*)key, key_length, &state);
...@@ -873,28 +870,10 @@ int lock_table_name(THD *thd, TABLE_LIST *table_list) ...@@ -873,28 +870,10 @@ int lock_table_name(THD *thd, TABLE_LIST *table_list)
if (table->in_use == thd) if (table->in_use == thd)
DBUG_RETURN(0); DBUG_RETURN(0);
/* if (!(table= table_cache_insert_placeholder(thd, key, key_length)))
Create a table entry with the right key and with an old refresh version
Note that we must use my_malloc() here as this is freed by the table
cache
*/
if (!(table= (TABLE*) my_malloc(sizeof(*table)+key_length,
MYF(MY_WME | MY_ZEROFILL))))
DBUG_RETURN(-1); DBUG_RETURN(-1);
table->s= &table->share_not_to_be_used;
memcpy((table->s->table_cache_key= (char*) (table+1)), key, key_length); table_list->table= table;
table->s->db= table->s->table_cache_key;
table->s->table_name= table->s->table_cache_key + table_in_key_offset;
table->s->key_length=key_length;
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));
DBUG_RETURN(-1);
}
/* Return 1 if table is in use */ /* Return 1 if table is in use */
DBUG_RETURN(test(remove_table_from_cache(thd, db, table_list->table_name, DBUG_RETURN(test(remove_table_from_cache(thd, db, table_list->table_name,
......
...@@ -676,6 +676,8 @@ struct Query_cache_query_flags ...@@ -676,6 +676,8 @@ struct Query_cache_query_flags
#define query_cache_invalidate_by_MyISAM_filename_ref NULL #define query_cache_invalidate_by_MyISAM_filename_ref NULL
#endif /*HAVE_QUERY_CACHE*/ #endif /*HAVE_QUERY_CACHE*/
uint build_table_path(char *buff, size_t bufflen, const char *db,
const char *table, const char *ext);
bool mysql_create_db(THD *thd, char *db, HA_CREATE_INFO *create, bool silent); bool mysql_create_db(THD *thd, char *db, HA_CREATE_INFO *create, bool silent);
bool mysql_alter_db(THD *thd, const char *db, HA_CREATE_INFO *create); bool mysql_alter_db(THD *thd, const char *db, HA_CREATE_INFO *create);
bool mysql_rm_db(THD *thd,char *db,bool if_exists, bool silent); bool mysql_rm_db(THD *thd,char *db,bool if_exists, bool silent);
...@@ -858,12 +860,14 @@ bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create); ...@@ -858,12 +860,14 @@ bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create);
TABLE *open_ltable(THD *thd, TABLE_LIST *table_list, thr_lock_type update); 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, TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT* mem,
bool *refresh, uint flags); 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 table_cache_has_open_placeholder(THD *thd, const char *db,
const char *table_name);
TABLE *find_locked_table(THD *thd, const char *db,const char *table_name); TABLE *find_locked_table(THD *thd, const char *db,const char *table_name);
bool reopen_table(TABLE *table,bool locked); bool reopen_table(TABLE *table,bool locked);
bool reopen_tables(THD *thd,bool get_locks,bool in_refresh); bool reopen_tables(THD *thd,bool get_locks,bool in_refresh);
void close_old_data_files(THD *thd, TABLE *table, bool abort_locks,
bool send_refresh);
bool close_data_tables(THD *thd,const char *db, const char *table_name); bool close_data_tables(THD *thd,const char *db, const char *table_name);
bool wait_for_tables(THD *thd); bool wait_for_tables(THD *thd);
bool table_is_used(TABLE *table, bool wait_for_name_lock); bool table_is_used(TABLE *table, bool wait_for_name_lock);
...@@ -1019,6 +1023,8 @@ void add_join_natural(TABLE_LIST *a,TABLE_LIST *b,List<String> *using_fields, ...@@ -1019,6 +1023,8 @@ void add_join_natural(TABLE_LIST *a,TABLE_LIST *b,List<String> *using_fields,
SELECT_LEX *lex); SELECT_LEX *lex);
bool add_proc_to_list(THD *thd, Item *item); bool add_proc_to_list(THD *thd, Item *item);
TABLE *unlink_open_table(THD *thd,TABLE *list,TABLE *find); TABLE *unlink_open_table(THD *thd,TABLE *list,TABLE *find);
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, void update_non_unique_table_error(TABLE_LIST *update,
const char *operation, const char *operation,
TABLE_LIST *duplicate); TABLE_LIST *duplicate);
...@@ -1271,7 +1277,7 @@ extern double log_01[32]; ...@@ -1271,7 +1277,7 @@ extern double log_01[32];
extern ulonglong log_10_int[20]; extern ulonglong log_10_int[20];
extern ulonglong keybuff_size; extern ulonglong keybuff_size;
extern ulonglong thd_startup_options; extern ulonglong thd_startup_options;
extern ulong refresh_version,flush_version, thread_id; extern ulong flush_version, thread_id;
extern ulong binlog_cache_use, binlog_cache_disk_use; extern ulong binlog_cache_use, binlog_cache_disk_use;
extern ulong aborted_threads,aborted_connects; extern ulong aborted_threads,aborted_connects;
extern ulong delayed_insert_timeout; extern ulong delayed_insert_timeout;
...@@ -1445,7 +1451,7 @@ MYSQL_LOCK *mysql_lock_tables(THD *thd, TABLE **table, uint count, ...@@ -1445,7 +1451,7 @@ MYSQL_LOCK *mysql_lock_tables(THD *thd, TABLE **table, uint count,
#define MYSQL_LOCK_IGNORE_GLOBAL_READ_LOCK 0x0001 #define MYSQL_LOCK_IGNORE_GLOBAL_READ_LOCK 0x0001
#define MYSQL_LOCK_IGNORE_FLUSH 0x0002 #define MYSQL_LOCK_IGNORE_FLUSH 0x0002
#define MYSQL_LOCK_NOTIFY_IF_NEED_REOPEN 0x0004 #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_tables(THD *thd, MYSQL_LOCK *sql_lock);
void mysql_unlock_read_tables(THD *thd, MYSQL_LOCK *sql_lock); void mysql_unlock_read_tables(THD *thd, MYSQL_LOCK *sql_lock);
......
This diff is collapsed.
...@@ -670,7 +670,7 @@ int mysql_ha_flush(THD *thd, TABLE_LIST *tables, uint mode_flags, ...@@ -670,7 +670,7 @@ int mysql_ha_flush(THD *thd, TABLE_LIST *tables, uint mode_flags,
while (*table_ptr) while (*table_ptr)
{ {
if ((mode_flags & MYSQL_HA_FLUSH_ALL) || 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(). */ /* The first time it is required, lock for close_thread_table(). */
if (! did_lock && ! is_locked) if (! did_lock && ! is_locked)
...@@ -770,6 +770,12 @@ void mysql_ha_mark_tables_for_reopen(THD *thd, TABLE *table) ...@@ -770,6 +770,12 @@ void mysql_ha_mark_tables_for_reopen(THD *thd, TABLE *table)
safe_mutex_assert_owner(&LOCK_open); safe_mutex_assert_owner(&LOCK_open);
for (; table; table= table->next) 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; TABLE_LIST *hash_tables;
if ((hash_tables= (TABLE_LIST*) hash_search(&thd->handler_tables_hash, if ((hash_tables= (TABLE_LIST*) hash_search(&thd->handler_tables_hash,
...@@ -782,5 +788,6 @@ void mysql_ha_mark_tables_for_reopen(THD *thd, TABLE *table) ...@@ -782,5 +788,6 @@ void mysql_ha_mark_tables_for_reopen(THD *thd, TABLE *table)
table->file->ha_index_or_rnd_end(); table->file->ha_index_or_rnd_end();
} }
} }
}
DBUG_VOID_RETURN; DBUG_VOID_RETURN;
} }
This diff is collapsed.
...@@ -3014,7 +3014,13 @@ mysql_execute_command(THD *thd) ...@@ -3014,7 +3014,13 @@ mysql_execute_command(THD *thd)
select_lex->options|= SELECT_NO_UNLOCK; select_lex->options|= SELECT_NO_UNLOCK;
unit->set_limit(select_lex); 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 Is table which we are changing used somewhere in other parts
...@@ -3023,6 +3029,7 @@ mysql_execute_command(THD *thd) ...@@ -3023,6 +3029,7 @@ mysql_execute_command(THD *thd)
if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE)) if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
{ {
TABLE_LIST *duplicate; TABLE_LIST *duplicate;
create_table= lex->unlink_first_table(&link_to_local);
if ((duplicate= unique_table(thd, create_table, select_tables, 0))) if ((duplicate= unique_table(thd, create_table, select_tables, 0)))
{ {
update_non_unique_table_error(create_table, "CREATE", duplicate); update_non_unique_table_error(create_table, "CREATE", duplicate);
...@@ -3066,6 +3073,9 @@ mysql_execute_command(THD *thd) ...@@ -3066,6 +3073,9 @@ mysql_execute_command(THD *thd)
delete sel_result; delete sel_result;
} }
} }
else if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
create_table= lex->unlink_first_table(&link_to_local);
} }
else else
{ {
......
...@@ -1491,8 +1491,21 @@ static bool mysql_test_create_table(Prepared_statement *stmt) ...@@ -1491,8 +1491,21 @@ static bool mysql_test_create_table(Prepared_statement *stmt)
if (select_lex->item_list.elements) 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_and_lock_tables(stmt->thd, lex->query_tables))
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; select_lex->context.resolve_in_select_list= TRUE;
res= select_like_stmt_test_with_open_n_lock(stmt, tables, 0, 0);
res= select_like_stmt_test(stmt, 0, 0);
} }
/* put tables back for PS rexecuting */ /* put tables back for PS rexecuting */
......
...@@ -62,7 +62,7 @@ static void set_tmp_file_path(char *buf, size_t bufsize, THD *thd); ...@@ -62,7 +62,7 @@ static void set_tmp_file_path(char *buf, size_t bufsize, THD *thd);
# Size of path # Size of path
*/ */
static uint build_table_path(char *buff, size_t bufflen, const char *db, uint build_table_path(char *buff, size_t bufflen, const char *db,
const char *table, const char *ext) const char *table, const char *ext)
{ {
strxnmov(buff, bufflen-1, mysql_data_home, "/", db, "/", table, ext, strxnmov(buff, bufflen-1, mysql_data_home, "/", db, "/", table, ext,
...@@ -1722,7 +1722,14 @@ bool mysql_create_table(THD *thd,const char *db, const char *table_name, ...@@ -1722,7 +1722,14 @@ bool mysql_create_table(THD *thd,const char *db, const char *table_name,
VOID(pthread_mutex_lock(&LOCK_open)); VOID(pthread_mutex_lock(&LOCK_open));
if (!internal_tmp_table && !(create_info->options & HA_LEX_CREATE_TMP_TABLE)) if (!internal_tmp_table && !(create_info->options & HA_LEX_CREATE_TMP_TABLE))
{ {
if (!access(path,F_OK)) /*
Inspecting table cache for placeholders created by concurrent
CREATE TABLE ... SELECT statements to avoid interfering with them
is 5.0-only solution. Starting from 5.1 we solve this problem by
obtaining name-lock on the table to be created first.
*/
if (table_cache_has_open_placeholder(thd, db, table_name) ||
!access(path, F_OK))
{ {
if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS) if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
goto warn; goto warn;
...@@ -2051,7 +2058,7 @@ static int prepare_for_restore(THD* thd, TABLE_LIST* table, ...@@ -2051,7 +2058,7 @@ static int prepare_for_restore(THD* thd, TABLE_LIST* table,
to finish the restore in the handler later on to finish the restore in the handler later on
*/ */
pthread_mutex_lock(&LOCK_open); pthread_mutex_lock(&LOCK_open);
if (reopen_name_locked_table(thd, table)) if (reopen_name_locked_table(thd, table, TRUE))
{ {
unlock_table_name(thd, table); unlock_table_name(thd, table);
pthread_mutex_unlock(&LOCK_open); pthread_mutex_unlock(&LOCK_open);
...@@ -2158,7 +2165,7 @@ static int prepare_for_repair(THD* thd, TABLE_LIST *table_list, ...@@ -2158,7 +2165,7 @@ static int prepare_for_repair(THD* thd, TABLE_LIST *table_list,
to finish the repair in the handler later on. to finish the repair in the handler later on.
*/ */
pthread_mutex_lock(&LOCK_open); pthread_mutex_lock(&LOCK_open);
if (reopen_name_locked_table(thd, table_list)) if (reopen_name_locked_table(thd, table_list, TRUE))
{ {
unlock_table_name(thd, table_list); unlock_table_name(thd, table_list);
pthread_mutex_unlock(&LOCK_open); pthread_mutex_unlock(&LOCK_open);
...@@ -2803,10 +2810,24 @@ bool mysql_create_like_table(THD* thd, TABLE_LIST* table, ...@@ -2803,10 +2810,24 @@ bool mysql_create_like_table(THD* thd, TABLE_LIST* table,
} }
else else
{ {
bool exists;
strxmov(dst_path, mysql_data_home, "/", db, "/", table_name, strxmov(dst_path, mysql_data_home, "/", db, "/", table_name,
reg_ext, NullS); reg_ext, NullS);
fn_format(dst_path, dst_path, "", "", MYF(MY_UNPACK_FILENAME)); fn_format(dst_path, dst_path, "", "", MYF(MY_UNPACK_FILENAME));
if (!access(dst_path, F_OK))
/*
Note that this critical section should actually cover most
of mysql_create_like_table() function. See bugs #18950 and
#23667 for more information.
Also note that starting from 5.1 we obtain name-lock on
target table instead of inspecting table cache for presence
of open placeholders (see comment in mysql_create_table()).
*/
pthread_mutex_lock(&LOCK_open);
exists= (table_cache_has_open_placeholder(thd, db, table_name) ||
!access(dst_path, F_OK));
pthread_mutex_unlock(&LOCK_open);
if (exists)
goto table_exists; goto table_exists;
} }
...@@ -3160,9 +3181,14 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name, ...@@ -3160,9 +3181,14 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
else else
{ {
char dir_buff[FN_REFLEN]; char dir_buff[FN_REFLEN];
bool exists;
strxnmov(dir_buff, FN_REFLEN, mysql_real_data_home, new_db, NullS); strxnmov(dir_buff, FN_REFLEN, mysql_real_data_home, new_db, NullS);
if (!access(fn_format(new_name_buff,new_name_buff,dir_buff,reg_ext,0), VOID(pthread_mutex_lock(&LOCK_open));
F_OK)) exists= (table_cache_has_open_placeholder(thd, new_db, new_name) ||
!access(fn_format(new_name_buff, new_name_buff, dir_buff,
reg_ext, 0), F_OK));
VOID(pthread_mutex_unlock(&LOCK_open));
if (exists)
{ {
/* Table will be closed in do_command() */ /* Table will be closed in do_command() */
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias); my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
...@@ -3247,8 +3273,22 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name, ...@@ -3247,8 +3273,22 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
if (!error && (new_name != table_name || new_db != db)) if (!error && (new_name != table_name || new_db != db))
{ {
thd->proc_info="rename"; thd->proc_info="rename";
/* Then do a 'simple' rename of the table */ /*
if (!access(new_name_buff,F_OK)) Then do a 'simple' rename of the table. First we need to close all
instances of 'source' table.
*/
close_cached_table(thd, table);
/*
Then, we want check once again that target table does not exist.
Note that we can't fully rely on results of previous check since
no lock was taken on target table during it. We also can't do this
before calling close_cached_table() as the latter temporarily
releases LOCK_open mutex.
Also note that starting from 5.1 we use approach with obtaining
of name-lock on target table.
*/
if (table_cache_has_open_placeholder(thd, new_db, new_name) ||
!access(new_name_buff,F_OK))
{ {
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_name); my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_name);
error= -1; error= -1;
...@@ -3256,7 +3296,6 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name, ...@@ -3256,7 +3296,6 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
else else
{ {
*fn_ext(new_name)=0; *fn_ext(new_name)=0;
close_cached_table(thd, table);
if (mysql_rename_table(old_db_type,db,table_name,new_db,new_alias)) if (mysql_rename_table(old_db_type,db,table_name,new_db,new_alias))
error= -1; error= -1;
else if (Table_triggers_list::change_table_name(thd, db, table_name, else if (Table_triggers_list::change_table_name(thd, db, table_name,
...@@ -3806,17 +3845,6 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name, ...@@ -3806,17 +3845,6 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
current_pid, thd->thread_id); current_pid, thd->thread_id);
if (lower_case_table_names) if (lower_case_table_names)
my_casedn_str(files_charset_info, old_name); my_casedn_str(files_charset_info, old_name);
if (new_name != table_name || new_db != db)
{
if (!access(new_name_buff,F_OK))
{
error=1;
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_name_buff);
VOID(quick_rm_table(new_db_type,new_db,tmp_name));
VOID(pthread_mutex_unlock(&LOCK_open));
goto err;
}
}
#if (!defined( __WIN__) && !defined( __EMX__) && !defined( OS2)) #if (!defined( __WIN__) && !defined( __EMX__) && !defined( OS2))
if (table->file->has_transactions()) if (table->file->has_transactions())
...@@ -3835,6 +3863,22 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name, ...@@ -3835,6 +3863,22 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
table->file->extra(HA_EXTRA_FORCE_REOPEN); // Don't use this file anymore table->file->extra(HA_EXTRA_FORCE_REOPEN); // Don't use this file anymore
#endif #endif
if (new_name != table_name || new_db != db)
{
/*
Check that there is no table with target name. See the
comment describing code for 'simple' ALTER TABLE ... RENAME.
*/
if (table_cache_has_open_placeholder(thd, new_db, new_name) ||
!access(new_name_buff,F_OK))
{
error=1;
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_name_buff);
VOID(quick_rm_table(new_db_type,new_db,tmp_name));
VOID(pthread_mutex_unlock(&LOCK_open));
goto err;
}
}
error=0; error=0;
if (!need_copy_table) if (!need_copy_table)
......
...@@ -271,7 +271,7 @@ bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create) ...@@ -271,7 +271,7 @@ bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create)
/* We also don't allow creation of triggers on views. */ /* We also don't allow creation of triggers on views. */
tables->required_type= FRMTYPE_TABLE; 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); unlock_table_name(thd, tables);
goto end; goto end;
......
...@@ -1474,9 +1474,7 @@ create: ...@@ -1474,9 +1474,7 @@ create:
lex->sql_command= SQLCOM_CREATE_TABLE; lex->sql_command= SQLCOM_CREATE_TABLE;
if (!lex->select_lex.add_table_to_list(thd, $5, NULL, if (!lex->select_lex.add_table_to_list(thd, $5, NULL,
TL_OPTION_UPDATING, TL_OPTION_UPDATING,
(using_update_log ? TL_WRITE))
TL_READ_NO_INSERT:
TL_READ)))
MYSQL_YYABORT; MYSQL_YYABORT;
lex->alter_info.reset(); lex->alter_info.reset();
lex->col_list.empty(); lex->col_list.empty();
......
...@@ -188,6 +188,8 @@ typedef struct st_table_share ...@@ -188,6 +188,8 @@ typedef struct st_table_share
} TABLE_SHARE; } TABLE_SHARE;
extern ulong refresh_version;
/* Information for one open table */ /* Information for one open table */
struct st_table { struct st_table {
...@@ -268,7 +270,24 @@ struct st_table { ...@@ -268,7 +270,24 @@ struct st_table {
my_bool force_index; my_bool force_index;
my_bool distinct,const_table,no_rows; my_bool distinct,const_table,no_rows;
my_bool key_read, no_keyread; 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_name; my_bool locked_by_name;
my_bool fulltext_searched; my_bool fulltext_searched;
my_bool no_cache; my_bool no_cache;
...@@ -291,6 +310,13 @@ struct st_table { ...@@ -291,6 +310,13 @@ struct st_table {
bool fill_item_list(List<Item> *item_list) const; bool fill_item_list(List<Item> *item_list) const;
void reset_item_list(List<Item> *item_list) const; void reset_item_list(List<Item> *item_list) const;
/* 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 enum enum_schema_table_state
...@@ -648,6 +674,12 @@ typedef struct st_table_list ...@@ -648,6 +674,12 @@ typedef struct st_table_list
used for implicit LOCK TABLES only and won't be used in real statement. used for implicit LOCK TABLES only and won't be used in real statement.
*/ */
bool prelocking_placeholder; 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; enum enum_schema_table_state schema_table_state;
void calc_md5(char *buffer); void calc_md5(char *buffer);
...@@ -655,7 +687,11 @@ typedef struct st_table_list ...@@ -655,7 +687,11 @@ typedef struct st_table_list
int view_check_option(THD *thd, bool ignore_failure); int view_check_option(THD *thd, bool ignore_failure);
bool setup_underlying(THD *thd); bool setup_underlying(THD *thd);
void cleanup_items(); 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); void print(THD *thd, String *str);
bool check_single_table(st_table_list **table, table_map map, bool check_single_table(st_table_list **table, table_map map,
st_table_list *view); 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