mirror of
https://github.com/MariaDB/server.git
synced 2025-01-16 20:12:31 +01:00
7ffc9da093
Using CREATE OR REPLACE TABLE is be identical to DROP TABLE IF EXISTS table_name; CREATE TABLE ...; Except that: * CREATE OR REPLACE is be atomic (now one can create the same table between drop and create). * Temporary tables will not shadow the table name for the DROP as the CREATE TABLE tells us already if we are using a temporary table or not. * If the table was locked with LOCK TABLES, the new table will be locked with the same lock after it's created. Implementation details: - We don't anymore open the to-be-created table during CREATE TABLE, which the original code did. - There is no need to open a table we are planning to create. It's enough to check if the table exists or not. - Removed some of duplicated code for CREATE IF NOT EXISTS. - Give an error when using CREATE OR REPLACE with IF NOT EXISTS (conflicting options). - As a side effect of the code changes, we don't anymore have to internally re-prepare prepared statements with CREATE TABLE if the table exists. - Made one code path for all testing if log table are in use. - Better error message if one tries to create/drop/alter a log table in use - Added back disabled rpl_row_create_table test as it now seams to work and includes a lot of interesting tests. - Added HA_LEX_CREATE_REPLACE to mark if we are using CREATE OR REPLACE - Aligned CREATE OR REPLACE parsing code in sql_yacc.yy for TABLE and VIEW - Changed interface for drop_temporary_table() to make it more reusable - Changed Locked_tables_list::init_locked_tables() to work on the table object instead of the table list object. Before this it used a mix of both, which was not good. - Locked_tables_list::unlock_locked_tables(THD *thd) now requires a valid thd argument. Old usage of calling this with 0 i changed to instead call Locked_tables_list::reset() - Added functions Locked_tables_list:restore_lock() and Locked_tables_list::add_back_last_deleted_lock() to be able to easily add back a locked table to the lock list. - Added restart_trans_for_tables() to be able to restart a transaction. - DROP_ACL is required if one uses CREATE TABLE OR REPLACE. - Added drop of normal and temporary tables in create_table_imp() if CREATE OR REPLACE was used. - Added reacquiring of table locks in mysql_create_table() and mysql_create_like_table() mysql-test/include/commit.inc: With new code we get fewer status increments mysql-test/r/commit_1innodb.result: With new code we get fewer status increments mysql-test/r/create.result: Added testing of create or replace with timeout mysql-test/r/create_or_replace.result: Basic testing of CREATE OR REPLACE TABLE mysql-test/r/partition_exchange.result: New error message mysql-test/r/ps_ddl.result: Fewer reprepares with new code mysql-test/suite/archive/discover.result: Don't rediscover archive tables if the .frm file exists (Sergei will look at this if there is a better way...) mysql-test/suite/archive/discover.test: Don't rediscover archive tables if the .frm file exists (Sergei will look at this if there is a better way...) mysql-test/suite/funcs_1/r/innodb_views.result: New error message mysql-test/suite/funcs_1/r/memory_views.result: New error message mysql-test/suite/rpl/disabled.def: rpl_row_create_table should now be safe to use mysql-test/suite/rpl/r/rpl_row_create_table.result: Updated results after adding back disabled test mysql-test/suite/rpl/t/rpl_create_if_not_exists.test: Added comment mysql-test/suite/rpl/t/rpl_row_create_table.test: Added CREATE OR REPLACE TABLE test mysql-test/t/create.test: Added CREATE OR REPLACE TABLE test mysql-test/t/create_or_replace-master.opt: Create logs mysql-test/t/create_or_replace.test: Basic testing of CREATE OR REPLACE TABLE mysql-test/t/partition_exchange.test: Error number changed as we are now using same code for all log table change issues mysql-test/t/ps_ddl.test: Fewer reprepares with new code sql/handler.h: Moved things around a bit in a structure to get better alignment. Added HA_LEX_CREATE_REPLACE to mark if we are using CREATE OR REPLACE Added 3 elements to end of HA_CREATE_INFO to be able to store state to add backs locks in case of LOCK TABLES. sql/log.cc: Reimplemented check_if_log_table(): - Simpler and faster usage - Can give error messages This gives us one code path for allmost all error messages if log tables are in use sql/log.h: New interface for check_if_log_table() sql/slave.cc: More logging sql/sql_alter.cc: New interface for check_if_log_table() sql/sql_base.cc: More documentation Changed interface for drop_temporary_table() to make it more reusable Changed Locked_tables_list::init_locked_tables() to work on the table object instead of the table list object. Before this it used a mix of both, which was not good. Locked_tables_list::unlock_locked_tables(THD *thd) now requires a valid thd argument. Old usage of calling this with 0 i changed to instead call Locked_tables_list::reset() Added functions Locked_tables_list:restore_lock() and Locked_tables_list::add_back_last_deleted_lock() to be able to easily add back a locked table to the lock list. Check for command number instead of open_strategy of CREATE TABLE was used. Added restart_trans_for_tables() to be able to restart a transaction. This was needed in "create or replace ... select" between the drop table and the select. sql/sql_base.h: Added and updated function prototypes sql/sql_class.h: Added new prototypes to Locked_tables_list class Added extra argument to select_create to avoid double call to eof() or send_error() - I needed this in some edge case where the table was not created against expections. sql/sql_db.cc: New interface for check_if_log_table() sql/sql_insert.cc: Remember position to lock information so that we can reaquire table lock for LOCK TABLES + CREATE OR REPLACE TABLE SELECT. Later add back the lock by calling restore_lock(). Removed one not needed indentation level in create_table_from_items() Ensure we don't call send_eof() or abort_result_set() twice. sql/sql_lex.h: Removed variable that I temporarly added in an earlier changeset sql/sql_parse.cc: Removed old test code (marked with QQ) Ensure that we have open_strategy set as TABLE_LIST::OPEN_STUB in CREATE TABLE Removed some IF NOT EXISTS code as this is now handled in create_table_table_impl(). Set OPTION_KEEP_LOGS later. This code had to be moved as the test for IF EXISTS has changed place. DROP_ACL is required if one uses CREATE TABLE OR REPLACE. sql/sql_partition_admin.cc: New interface for check_if_log_table() sql/sql_rename.cc: New interface for check_if_log_table() sql/sql_table.cc: New interface for check_if_log_table() Moved some code in mysql_rm_table() under a common test. - Safe as temporary tables doesn't have statistics. - !is_temporary_table(table) test was moved out from drop_temporary_table() and merged with upper level code. - Added drop of normal and temporary tables in create_table_imp() if CREATE OR REPLACE was used. - Added reacquiring of table locks in mysql_create_table() and mysql_create_like_table() - In mysql_create_like_table(), restore table->open_strategy() if it was changed. - Re-test if table was a view after opening it. sql/sql_table.h: New prototype for mysql_create_table_no_lock() sql/sql_yacc.yy: Added syntax for CREATE OR REPLACE TABLE Reuse new code for CREATE OR REPLACE VIEW sql/table.h: Added name for enum type sql/table_cache.cc: More DBUG
362 lines
10 KiB
C++
362 lines
10 KiB
C++
/*
|
|
Copyright (c) 2000, 2013, Oracle and/or its affiliates.
|
|
Copyright (c) 2011, 2013, Monty Program Ab.
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; version 2 of the License.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
|
|
|
|
/*
|
|
Atomic rename of table; RENAME TABLE t1 to t2, tmp to t1 [,...]
|
|
*/
|
|
|
|
#include "sql_priv.h"
|
|
#include "unireg.h"
|
|
#include "sql_rename.h"
|
|
#include "sql_cache.h" // query_cache_*
|
|
#include "sql_table.h" // build_table_filename
|
|
#include "sql_view.h" // mysql_frm_type, mysql_rename_view
|
|
#include "sql_trigger.h"
|
|
#include "lock.h" // MYSQL_OPEN_SKIP_TEMPORARY
|
|
#include "sql_base.h" // tdc_remove_table, lock_table_names,
|
|
#include "sql_handler.h" // mysql_ha_rm_tables
|
|
#include "sql_statistics.h"
|
|
|
|
static TABLE_LIST *rename_tables(THD *thd, TABLE_LIST *table_list,
|
|
bool skip_error);
|
|
static bool do_rename(THD *thd, TABLE_LIST *ren_table, char *new_db,
|
|
char *new_table_name, char *new_table_alias,
|
|
bool skip_error);
|
|
|
|
static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list);
|
|
|
|
/*
|
|
Every two entries in the table_list form a pair of original name and
|
|
the new name.
|
|
*/
|
|
|
|
bool mysql_rename_tables(THD *thd, TABLE_LIST *table_list, bool silent)
|
|
{
|
|
bool error= 1;
|
|
bool binlog_error= 0;
|
|
TABLE_LIST *ren_table= 0;
|
|
int to_table;
|
|
char *rename_log_table[2]= {NULL, NULL};
|
|
DBUG_ENTER("mysql_rename_tables");
|
|
|
|
/*
|
|
Avoid problems with a rename on a table that we have locked or
|
|
if the user is trying to to do this in a transcation context
|
|
*/
|
|
|
|
if (thd->locked_tables_mode || thd->in_active_multi_stmt_transaction())
|
|
{
|
|
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
|
|
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
|
|
DBUG_RETURN(1);
|
|
}
|
|
|
|
mysql_ha_rm_tables(thd, table_list);
|
|
|
|
if (logger.is_log_table_enabled(QUERY_LOG_GENERAL) ||
|
|
logger.is_log_table_enabled(QUERY_LOG_SLOW))
|
|
{
|
|
|
|
/*
|
|
Rules for rename of a log table:
|
|
|
|
IF 1. Log tables are enabled
|
|
AND 2. Rename operates on the log table and nothing is being
|
|
renamed to the log table.
|
|
DO 3. Throw an error message.
|
|
ELSE 4. Perform rename.
|
|
*/
|
|
|
|
for (to_table= 0, ren_table= table_list; ren_table;
|
|
to_table= 1 - to_table, ren_table= ren_table->next_local)
|
|
{
|
|
int log_table_rename;
|
|
if ((log_table_rename= check_if_log_table(ren_table, TRUE, NullS)))
|
|
{
|
|
/*
|
|
as we use log_table_rename as an array index, we need it to start
|
|
with 0, while QUERY_LOG_SLOW == 1 and QUERY_LOG_GENERAL == 2.
|
|
So, we shift the value to start with 0;
|
|
*/
|
|
log_table_rename--;
|
|
if (rename_log_table[log_table_rename])
|
|
{
|
|
if (to_table)
|
|
rename_log_table[log_table_rename]= NULL;
|
|
else
|
|
{
|
|
/*
|
|
Two renames of "log_table TO" w/o rename "TO log_table" in
|
|
between.
|
|
*/
|
|
my_error(ER_CANT_RENAME_LOG_TABLE, MYF(0), ren_table->table_name,
|
|
ren_table->table_name);
|
|
goto err;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (to_table)
|
|
{
|
|
/*
|
|
Attempt to rename a table TO log_table w/o renaming
|
|
log_table TO some table.
|
|
*/
|
|
my_error(ER_CANT_RENAME_LOG_TABLE, MYF(0), ren_table->table_name,
|
|
ren_table->table_name);
|
|
goto err;
|
|
}
|
|
else
|
|
{
|
|
/* save the name of the log table to report an error */
|
|
rename_log_table[log_table_rename]= ren_table->table_name;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (rename_log_table[0] || rename_log_table[1])
|
|
{
|
|
if (rename_log_table[0])
|
|
my_error(ER_CANT_RENAME_LOG_TABLE, MYF(0), rename_log_table[0],
|
|
rename_log_table[0]);
|
|
else
|
|
my_error(ER_CANT_RENAME_LOG_TABLE, MYF(0), rename_log_table[1],
|
|
rename_log_table[1]);
|
|
goto err;
|
|
}
|
|
}
|
|
|
|
if (lock_table_names(thd, table_list, 0, thd->variables.lock_wait_timeout,
|
|
0))
|
|
goto err;
|
|
|
|
error=0;
|
|
/*
|
|
An exclusive lock on table names is satisfactory to ensure
|
|
no other thread accesses this table.
|
|
*/
|
|
if ((ren_table=rename_tables(thd,table_list,0)))
|
|
{
|
|
/* Rename didn't succeed; rename back the tables in reverse order */
|
|
TABLE_LIST *table;
|
|
|
|
/* Reverse the table list */
|
|
table_list= reverse_table_list(table_list);
|
|
|
|
/* Find the last renamed table */
|
|
for (table= table_list;
|
|
table->next_local != ren_table ;
|
|
table= table->next_local->next_local) ;
|
|
table= table->next_local->next_local; // Skip error table
|
|
/* Revert to old names */
|
|
rename_tables(thd, table, 1);
|
|
|
|
/* Revert the table list (for prepared statements) */
|
|
table_list= reverse_table_list(table_list);
|
|
|
|
error= 1;
|
|
}
|
|
|
|
if (!silent && !error)
|
|
{
|
|
binlog_error= write_bin_log(thd, TRUE, thd->query(), thd->query_length());
|
|
if (!binlog_error)
|
|
my_ok(thd);
|
|
}
|
|
|
|
if (!error)
|
|
query_cache_invalidate3(thd, table_list, 0);
|
|
|
|
err:
|
|
DBUG_RETURN(error || binlog_error);
|
|
}
|
|
|
|
|
|
/*
|
|
reverse table list
|
|
|
|
SYNOPSIS
|
|
reverse_table_list()
|
|
table_list pointer to table _list
|
|
|
|
RETURN
|
|
pointer to new (reversed) list
|
|
*/
|
|
static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list)
|
|
{
|
|
TABLE_LIST *prev= 0;
|
|
|
|
while (table_list)
|
|
{
|
|
TABLE_LIST *next= table_list->next_local;
|
|
table_list->next_local= prev;
|
|
prev= table_list;
|
|
table_list= next;
|
|
}
|
|
return (prev);
|
|
}
|
|
|
|
|
|
/*
|
|
Rename a single table or a view
|
|
|
|
SYNPOSIS
|
|
do_rename()
|
|
thd Thread handle
|
|
ren_table A table/view to be renamed
|
|
new_db The database to which the table to be moved to
|
|
new_table_name The new table/view name
|
|
new_table_alias The new table/view alias
|
|
skip_error Whether to skip error
|
|
|
|
DESCRIPTION
|
|
Rename a single table or a view.
|
|
|
|
RETURN
|
|
false Ok
|
|
true rename failed
|
|
*/
|
|
|
|
static bool
|
|
do_rename(THD *thd, TABLE_LIST *ren_table, char *new_db, char *new_table_name,
|
|
char *new_table_alias, bool skip_error)
|
|
{
|
|
int rc= 1;
|
|
handlerton *hton;
|
|
bool new_exists, old_exists;
|
|
const char *new_alias, *old_alias;
|
|
DBUG_ENTER("do_rename");
|
|
|
|
if (lower_case_table_names == 2)
|
|
{
|
|
old_alias= ren_table->alias;
|
|
new_alias= new_table_alias;
|
|
}
|
|
else
|
|
{
|
|
old_alias= ren_table->table_name;
|
|
new_alias= new_table_name;
|
|
}
|
|
DBUG_ASSERT(new_alias);
|
|
|
|
new_exists= ha_table_exists(thd, new_db, new_alias);
|
|
|
|
if (new_exists)
|
|
{
|
|
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
|
|
DBUG_RETURN(1); // This can't be skipped
|
|
}
|
|
|
|
old_exists= ha_table_exists(thd, ren_table->db, old_alias, &hton);
|
|
|
|
if (old_exists)
|
|
{
|
|
DBUG_ASSERT(!thd->locked_tables_mode);
|
|
tdc_remove_table(thd, TDC_RT_REMOVE_ALL,
|
|
ren_table->db, ren_table->table_name, false);
|
|
|
|
if (hton != view_pseudo_hton)
|
|
{
|
|
if (!(rc= mysql_rename_table(hton, ren_table->db, old_alias,
|
|
new_db, new_alias, 0)))
|
|
{
|
|
LEX_STRING db_name= { ren_table->db, ren_table->db_length };
|
|
LEX_STRING table_name= { ren_table->table_name,
|
|
ren_table->table_name_length };
|
|
LEX_STRING new_table= { (char *) new_alias, strlen(new_alias) };
|
|
(void) rename_table_in_stat_tables(thd, &db_name, &table_name,
|
|
&db_name, &new_table);
|
|
if ((rc= Table_triggers_list::change_table_name(thd, ren_table->db,
|
|
old_alias,
|
|
ren_table->table_name,
|
|
new_db,
|
|
new_alias)))
|
|
{
|
|
/*
|
|
We've succeeded in renaming table's .frm and in updating
|
|
corresponding handler data, but have failed to update table's
|
|
triggers appropriately. So let us revert operations on .frm
|
|
and handler's data and report about failure to rename table.
|
|
*/
|
|
(void) mysql_rename_table(hton, new_db, new_alias,
|
|
ren_table->db, old_alias, NO_FK_CHECKS);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/*
|
|
change of schema is not allowed
|
|
except of ALTER ...UPGRADE DATA DIRECTORY NAME command
|
|
because a view has valid internal db&table names in this case.
|
|
*/
|
|
if (thd->lex->sql_command != SQLCOM_ALTER_DB_UPGRADE &&
|
|
strcmp(ren_table->db, new_db))
|
|
my_error(ER_FORBID_SCHEMA_CHANGE, MYF(0), ren_table->db, new_db);
|
|
else
|
|
rc= mysql_rename_view(thd, new_db, new_alias, ren_table);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
my_error(ER_NO_SUCH_TABLE, MYF(0), ren_table->db, old_alias);
|
|
}
|
|
if (rc && !skip_error)
|
|
DBUG_RETURN(1);
|
|
|
|
DBUG_RETURN(0);
|
|
}
|
|
/*
|
|
Rename all tables in list; Return pointer to wrong entry if something goes
|
|
wrong. Note that the table_list may be empty!
|
|
*/
|
|
|
|
/*
|
|
Rename tables/views in the list
|
|
|
|
SYNPOSIS
|
|
rename_tables()
|
|
thd Thread handle
|
|
table_list List of tables to rename
|
|
skip_error Whether to skip errors
|
|
|
|
DESCRIPTION
|
|
Take a table/view name from and odd list element and rename it to a
|
|
the name taken from list element+1. Note that the table_list may be
|
|
empty.
|
|
|
|
RETURN
|
|
false Ok
|
|
true rename failed
|
|
*/
|
|
|
|
static TABLE_LIST *
|
|
rename_tables(THD *thd, TABLE_LIST *table_list, bool skip_error)
|
|
{
|
|
TABLE_LIST *ren_table, *new_table;
|
|
|
|
DBUG_ENTER("rename_tables");
|
|
|
|
for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
|
|
{
|
|
new_table= ren_table->next_local;
|
|
if (do_rename(thd, ren_table, new_table->db, new_table->table_name,
|
|
new_table->alias, skip_error))
|
|
DBUG_RETURN(ren_table);
|
|
}
|
|
DBUG_RETURN(0);
|
|
}
|