2014-05-07 22:36:25 +02:00
|
|
|
/* Copyright (c) 2010, 2014, Oracle and/or its affiliates.
|
2021-11-09 09:11:50 +02:00
|
|
|
Copyright (c) 2013, 2021, MariaDB Corporation.
|
2010-08-16 14:53:30 +02:00
|
|
|
|
|
|
|
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
|
2019-05-11 21:29:06 +03:00
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
|
2010-08-16 14:53:30 +02:00
|
|
|
|
|
|
|
#ifndef SQL_ALTER_TABLE_H
|
|
|
|
#define SQL_ALTER_TABLE_H
|
|
|
|
|
2013-03-26 00:03:13 +02:00
|
|
|
class Alter_drop;
|
|
|
|
class Alter_column;
|
2020-03-03 13:50:33 +03:00
|
|
|
class Alter_rename_key;
|
2021-02-25 19:59:51 +05:30
|
|
|
class Alter_index_ignorability;
|
2013-03-26 00:03:13 +02:00
|
|
|
class Key;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Data describing the table being created by CREATE TABLE or
|
|
|
|
altered by ALTER TABLE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Alter_info
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum enum_enable_or_disable { LEAVE_AS_IS, ENABLE, DISABLE };
|
|
|
|
|
2018-05-08 14:14:36 +03:00
|
|
|
bool vers_prohibited(THD *thd) const;
|
2017-11-24 19:15:10 +03:00
|
|
|
|
2013-03-26 00:03:13 +02:00
|
|
|
/**
|
|
|
|
The different values of the ALGORITHM clause.
|
|
|
|
Describes which algorithm to use when altering the table.
|
|
|
|
*/
|
|
|
|
enum enum_alter_table_algorithm
|
|
|
|
{
|
2018-05-07 14:54:58 +05:30
|
|
|
/*
|
|
|
|
Use thd->variables.alter_algorithm for alter method. If this is also
|
|
|
|
default then use the fastest possible ALTER TABLE method
|
|
|
|
(INSTANT, NOCOPY, INPLACE, COPY)
|
|
|
|
*/
|
2013-03-26 00:03:13 +02:00
|
|
|
ALTER_TABLE_ALGORITHM_DEFAULT,
|
|
|
|
|
2018-05-07 14:54:58 +05:30
|
|
|
// Copy if supported, error otherwise.
|
|
|
|
ALTER_TABLE_ALGORITHM_COPY,
|
|
|
|
|
2013-03-26 00:03:13 +02:00
|
|
|
// In-place if supported, error otherwise.
|
|
|
|
ALTER_TABLE_ALGORITHM_INPLACE,
|
|
|
|
|
2018-05-07 14:54:58 +05:30
|
|
|
// No Copy will refuse any operation which does rebuild.
|
|
|
|
ALTER_TABLE_ALGORITHM_NOCOPY,
|
|
|
|
|
|
|
|
// Instant should allow any operation that changes metadata only.
|
2020-04-25 14:27:00 +05:30
|
|
|
ALTER_TABLE_ALGORITHM_INSTANT,
|
|
|
|
|
|
|
|
// When there is no specification of algorithm during alter table.
|
|
|
|
ALTER_TABLE_ALGORITHM_NONE
|
2013-03-26 00:03:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
The different values of the LOCK clause.
|
|
|
|
Describes the level of concurrency during ALTER TABLE.
|
|
|
|
*/
|
|
|
|
enum enum_alter_table_lock
|
|
|
|
{
|
|
|
|
// Maximum supported level of concurency for the given operation.
|
|
|
|
ALTER_TABLE_LOCK_DEFAULT,
|
|
|
|
|
2018-05-07 14:54:58 +05:30
|
|
|
// Allow concurrent reads & writes. If not supported, give error.
|
2013-03-26 00:03:13 +02:00
|
|
|
ALTER_TABLE_LOCK_NONE,
|
|
|
|
|
|
|
|
// Allow concurrent reads only. If not supported, give error.
|
|
|
|
ALTER_TABLE_LOCK_SHARED,
|
|
|
|
|
|
|
|
// Block reads and writes.
|
|
|
|
ALTER_TABLE_LOCK_EXCLUSIVE
|
|
|
|
};
|
|
|
|
|
2023-04-26 15:27:01 +04:00
|
|
|
Lex_ident_db db;
|
|
|
|
Lex_ident_table table_name;
|
2013-03-26 00:03:13 +02:00
|
|
|
|
|
|
|
// Columns and keys to be dropped.
|
|
|
|
List<Alter_drop> drop_list;
|
2018-10-01 14:33:48 +03:00
|
|
|
// Columns for ALTER_CHANGE_COLUMN_DEFAULT.
|
2013-03-26 00:03:13 +02:00
|
|
|
List<Alter_column> alter_list;
|
|
|
|
// List of keys, used by both CREATE and ALTER TABLE.
|
|
|
|
List<Key> key_list;
|
2020-03-03 13:50:33 +03:00
|
|
|
// List of keys to be renamed.
|
|
|
|
List<Alter_rename_key> alter_rename_key_list;
|
2013-03-26 00:03:13 +02:00
|
|
|
// List of columns, used by both CREATE and ALTER TABLE.
|
|
|
|
List<Create_field> create_list;
|
2021-02-25 19:59:51 +05:30
|
|
|
// Indexes whose ignorability needs to be changed.
|
|
|
|
List<Alter_index_ignorability> alter_index_ignorability_list;
|
2016-06-29 21:27:34 +02:00
|
|
|
List<Virtual_column_info> check_constraint_list;
|
2013-03-26 00:03:13 +02:00
|
|
|
// Type of ALTER TABLE operation.
|
2018-02-16 10:56:03 +02:00
|
|
|
alter_table_operations flags;
|
2018-02-19 11:23:20 +02:00
|
|
|
ulong partition_flags;
|
2013-03-26 00:03:13 +02:00
|
|
|
// Enable or disable keys.
|
|
|
|
enum_enable_or_disable keys_onoff;
|
2023-08-18 18:35:02 +03:00
|
|
|
// Used only in add_stat_drop_index()
|
|
|
|
TABLE *original_table;
|
2013-03-26 00:03:13 +02:00
|
|
|
// List of partitions.
|
2017-04-23 19:39:57 +03:00
|
|
|
List<const char> partition_names;
|
2013-03-26 00:03:13 +02:00
|
|
|
// Number of partitions.
|
|
|
|
uint num_parts;
|
2023-08-18 18:35:02 +03:00
|
|
|
|
|
|
|
/* List of fields that we should delete statistics from */
|
|
|
|
List<Field> drop_stat_fields;
|
|
|
|
|
|
|
|
struct DROP_INDEX_STAT_PARAMS
|
|
|
|
{
|
|
|
|
KEY *key;
|
|
|
|
bool ext_prefixes_only;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RENAME_COLUMN_STAT_PARAMS
|
|
|
|
{
|
|
|
|
Field *field;
|
|
|
|
LEX_CSTRING *name;
|
|
|
|
uint duplicate_counter; // For temporary names
|
|
|
|
};
|
|
|
|
struct RENAME_INDEX_STAT_PARAMS
|
|
|
|
{
|
|
|
|
const KEY *key;
|
|
|
|
const LEX_CSTRING *name;
|
|
|
|
uint duplicate_counter; // For temporary names
|
|
|
|
uint usage_count; // How many rename entries
|
|
|
|
};
|
|
|
|
|
|
|
|
/* List of index that we should delete statistics from */
|
|
|
|
List<DROP_INDEX_STAT_PARAMS> drop_stat_indexes;
|
|
|
|
|
|
|
|
List<RENAME_COLUMN_STAT_PARAMS> rename_stat_fields;
|
|
|
|
|
|
|
|
List<RENAME_INDEX_STAT_PARAMS> rename_stat_indexes;
|
|
|
|
|
|
|
|
bool add_stat_drop_index(KEY *key, bool ext_prefixes_only,
|
|
|
|
MEM_ROOT *mem_root)
|
|
|
|
{
|
|
|
|
DROP_INDEX_STAT_PARAMS *param;
|
|
|
|
if (!(param= (DROP_INDEX_STAT_PARAMS*)
|
|
|
|
alloc_root(mem_root, sizeof(*param))))
|
|
|
|
return true;
|
|
|
|
param->key= key;
|
|
|
|
param->ext_prefixes_only= ext_prefixes_only;
|
|
|
|
return drop_stat_indexes.push_back(param, mem_root);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool add_stat_drop_index(THD *thd, const LEX_CSTRING *key_name);
|
|
|
|
|
|
|
|
bool add_stat_rename_index(const KEY *key, const LEX_CSTRING *name,
|
|
|
|
MEM_ROOT *mem_root)
|
|
|
|
{
|
|
|
|
RENAME_INDEX_STAT_PARAMS *param;
|
|
|
|
if (!(param= (RENAME_INDEX_STAT_PARAMS*)
|
|
|
|
alloc_root(mem_root, sizeof(*param))))
|
|
|
|
return true;
|
|
|
|
param->key= key;
|
|
|
|
param->name= name;
|
|
|
|
param->usage_count= 0;
|
|
|
|
return rename_stat_indexes.push_back(param, mem_root);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool add_stat_rename_field(Field *field, LEX_CSTRING *name,
|
|
|
|
MEM_ROOT *mem_root)
|
|
|
|
{
|
|
|
|
RENAME_COLUMN_STAT_PARAMS *param;
|
|
|
|
if (!(param= (RENAME_COLUMN_STAT_PARAMS*)
|
|
|
|
alloc_root(mem_root, sizeof(*param))))
|
|
|
|
return true;
|
|
|
|
param->field= field;
|
|
|
|
param->name= name;
|
|
|
|
param->duplicate_counter= 0;
|
|
|
|
return rename_stat_fields.push_back(param, mem_root);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool collect_renamed_fields(THD *thd);
|
|
|
|
|
|
|
|
/* Delete/update statistics in EITS tables */
|
|
|
|
void apply_statistics_deletes_renames(THD *thd, TABLE *table);
|
|
|
|
|
2020-04-25 14:27:00 +05:30
|
|
|
private:
|
2013-03-26 00:03:13 +02:00
|
|
|
// Type of ALTER TABLE algorithm.
|
|
|
|
enum_alter_table_algorithm requested_algorithm;
|
2020-04-25 14:27:00 +05:30
|
|
|
|
|
|
|
public:
|
2013-03-26 00:03:13 +02:00
|
|
|
// Type of ALTER TABLE lock.
|
|
|
|
enum_alter_table_lock requested_lock;
|
|
|
|
|
|
|
|
|
|
|
|
Alter_info() :
|
2018-02-19 11:23:20 +02:00
|
|
|
flags(0), partition_flags(0),
|
2013-03-26 00:03:13 +02:00
|
|
|
keys_onoff(LEAVE_AS_IS),
|
2023-10-14 15:46:29 +03:00
|
|
|
original_table(0),
|
2013-03-26 00:03:13 +02:00
|
|
|
num_parts(0),
|
2020-04-25 14:27:00 +05:30
|
|
|
requested_algorithm(ALTER_TABLE_ALGORITHM_NONE),
|
2013-03-26 00:03:13 +02:00
|
|
|
requested_lock(ALTER_TABLE_LOCK_DEFAULT)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
drop_list.empty();
|
|
|
|
alter_list.empty();
|
|
|
|
key_list.empty();
|
2020-03-03 13:50:33 +03:00
|
|
|
alter_rename_key_list.empty();
|
2013-03-26 00:03:13 +02:00
|
|
|
create_list.empty();
|
2021-02-25 19:59:51 +05:30
|
|
|
alter_index_ignorability_list.empty();
|
2016-06-29 21:27:34 +02:00
|
|
|
check_constraint_list.empty();
|
2023-08-18 18:35:02 +03:00
|
|
|
drop_stat_fields.empty();
|
|
|
|
drop_stat_indexes.empty();
|
|
|
|
rename_stat_fields.empty();
|
|
|
|
rename_stat_indexes.empty();
|
2013-03-26 00:03:13 +02:00
|
|
|
flags= 0;
|
2018-02-19 11:23:20 +02:00
|
|
|
partition_flags= 0;
|
2013-03-26 00:03:13 +02:00
|
|
|
keys_onoff= LEAVE_AS_IS;
|
|
|
|
num_parts= 0;
|
|
|
|
partition_names.empty();
|
2020-04-25 14:27:00 +05:30
|
|
|
requested_algorithm= ALTER_TABLE_ALGORITHM_NONE;
|
2013-03-26 00:03:13 +02:00
|
|
|
requested_lock= ALTER_TABLE_LOCK_DEFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Construct a copy of this object to be used for mysql_alter_table
|
|
|
|
and mysql_create_table.
|
|
|
|
|
|
|
|
Historically, these two functions modify their Alter_info
|
|
|
|
arguments. This behaviour breaks re-execution of prepared
|
|
|
|
statements and stored procedures and is compensated by always
|
|
|
|
supplying a copy of Alter_info to these functions.
|
|
|
|
|
|
|
|
@param rhs Alter_info to make copy of
|
|
|
|
@param mem_root Mem_root for new Alter_info
|
|
|
|
|
|
|
|
@note You need to use check the error in THD for out
|
|
|
|
of memory condition after calling this function.
|
|
|
|
*/
|
|
|
|
Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Parses the given string and sets requested_algorithm
|
|
|
|
if the string value matches a supported value.
|
|
|
|
Supported values: INPLACE, COPY, DEFAULT
|
|
|
|
|
|
|
|
@param str String containing the supplied value
|
|
|
|
@retval false Supported value found, state updated
|
|
|
|
@retval true Not supported value, no changes made
|
|
|
|
*/
|
2017-04-23 19:39:57 +03:00
|
|
|
bool set_requested_algorithm(const LEX_CSTRING *str);
|
2013-03-26 00:03:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Parses the given string and sets requested_lock
|
|
|
|
if the string value matches a supported value.
|
|
|
|
Supported values: NONE, SHARED, EXCLUSIVE, DEFAULT
|
|
|
|
|
|
|
|
@param str String containing the supplied value
|
|
|
|
@retval false Supported value found, state updated
|
|
|
|
@retval true Not supported value, no changes made
|
|
|
|
*/
|
|
|
|
|
2017-04-23 19:39:57 +03:00
|
|
|
bool set_requested_lock(const LEX_CSTRING *str);
|
2013-03-26 00:03:13 +02:00
|
|
|
|
2020-04-25 14:27:00 +05:30
|
|
|
/**
|
|
|
|
Set the requested algorithm to the given algorithm value
|
|
|
|
@param algo_value algorithm to be set
|
|
|
|
*/
|
|
|
|
void set_requested_algorithm(enum_alter_table_algorithm algo_value);
|
|
|
|
|
2018-05-07 14:54:58 +05:30
|
|
|
/**
|
|
|
|
Returns the algorithm value in the format "algorithm=value"
|
|
|
|
*/
|
2020-04-25 14:27:00 +05:30
|
|
|
const char* algorithm_clause(THD *thd) const;
|
2018-05-07 14:54:58 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the lock value in the format "lock=value"
|
|
|
|
*/
|
|
|
|
const char* lock() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Check whether the given result can be supported
|
|
|
|
with the specified user alter algorithm.
|
|
|
|
|
|
|
|
@param thd Thread handle
|
|
|
|
@param ha_alter_info Structure describing changes to be done
|
|
|
|
by ALTER TABLE and holding data during
|
|
|
|
in-place alter
|
|
|
|
@retval false Supported operation
|
|
|
|
@retval true Not supported value
|
|
|
|
*/
|
2020-07-27 15:04:04 +03:00
|
|
|
bool supports_algorithm(THD *thd,
|
2018-05-07 14:54:58 +05:30
|
|
|
const Alter_inplace_info *ha_alter_info);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Check whether the given result can be supported
|
|
|
|
with the specified user lock type.
|
|
|
|
|
|
|
|
@param ha_alter_info Structure describing changes to be done
|
|
|
|
by ALTER TABLE and holding data during
|
|
|
|
in-place alter
|
|
|
|
@retval false Supported lock type
|
|
|
|
@retval true Not supported value
|
|
|
|
*/
|
2022-05-24 20:10:48 +02:00
|
|
|
bool supports_lock(THD *thd, bool, Alter_inplace_info *ha_alter_info);
|
2018-05-07 14:54:58 +05:30
|
|
|
|
2020-04-25 14:27:00 +05:30
|
|
|
/**
|
|
|
|
Return user requested algorithm. If user does not specify
|
|
|
|
algorithm then return alter_algorithm variable value.
|
|
|
|
*/
|
|
|
|
enum_alter_table_algorithm algorithm(const THD *thd) const;
|
2023-06-28 14:37:47 +03:00
|
|
|
bool algorithm_is_nocopy(const THD *thd) const;
|
MDEV-33449 improving repair of tables
This task is to ensure we have a clear definition and rules of how to
repair or optimize a table.
The rules are:
- REPAIR should be used with tables that are crashed and are
unreadable (hardware issues with not readable blocks, blocks with
'unexpected data' etc)
- OPTIMIZE table should be used to optimize the storage layout for the
table (recover space for delete rows and optimize the index
structure.
- ALTER TABLE table_name FORCE should be used to rebuild the .frm file
(the table definition) and the table (with the original table row
format). If the table is from and older MariaDB/MySQL release with a
different storage format, it will convert the data to the new
format. ALTER TABLE ... FORCE is used as part of mariadb-upgrade
Here follows some more background:
The 3 ways to repair a table are:
1) ALTER TABLE table_name FORCE" (not other options).
As an alias we allow: "ALTER TABLE table_name ENGINE=original_engine"
2) "REPAIR TABLE" (without FORCE)
3) "OPTIMIZE TABLE"
All of the above commands will optimize row space usage (which means that
space will be needed to hold a temporary copy of the table) and
re-generate all indexes. They will also try to replicate the original
table definition as exact as possible.
For ALTER TABLE and "REPAIR TABLE without FORCE", the following will hold:
If the table is from an older MariaDB version and data conversion is
needed (for example for old type HASH columns, MySQL JSON type or new
TIMESTAMP format) "ALTER TABLE table_name FORCE, algorithm=COPY" will be
used.
The differences between the algorithms are
1) Will use the fastest algorithm the engine supports to do a full repair
of the table (except if data conversions are is needed).
2) Will use the storage engine internal REPAIR facility (MyISAM, Aria).
If the engine does not support REPAIR then
"ALTER TABLE FORCE, ALGORITHM=COPY" will be used.
If there was data incompatibilities (which means that FORCE was used)
then there will be a warning after REPAIR that ALTER TABLE FORCE is
still needed.
The reason for this is that REPAIR may be able to go around data
errors (wrong incompatible data, crashed or unreadable sectors) that
ALTER TABLE cannot do.
3) Will use the storage engine internal OPTIMIZE. If engine does not
support optimize, then "ALTER TABLE FORCE" is used.
The above will ensure that ALTER TABLE FORCE is able to
correct almost any errors in the row or index data. In case of
corrupted blocks then REPAIR possible followed by ALTER TABLE is needed.
This is important as mariadb-upgrade executes ALTER TABLE table_name
FORCE for any table that must be re-created.
Bugs fixed with InnoDB tables when using ALTER TABLE FORCE:
- No error for INNODB_DEFAULT_ROW_FORMAT=COMPACT even if row length
would be too wide. (Independent of innodb_strict_mode).
- Tables using symlinks will be symlinked after any of the above commands
(Independent of the setting of --symbolic-links)
If one specifies an algorithm together with ALTER TABLE FORCE, things
will work as before (except if data conversion is required as then
the COPY algorithm is enforced).
ALTER TABLE .. OPTIMIZE ALL PARTITIONS will work as before.
Other things:
- FORCE argument added to REPAIR to allow one to first run internal
repair to fix damaged blocks and then follow it with ALTER TABLE.
- REPAIR will not update frm_version if ha_check_for_upgrade() finds
that table is still incompatible with current version. In this case the
REPAIR will end with an error.
- REPAIR for storage engines that does not have native repair, like InnoDB,
is now using ALTER TABLE FORCE.
- REPAIR csv-table USE_FRM now works.
- It did not work before as CSV tables had extension list in wrong
order.
- Default error messages length for %M increased from 128 to 256 to not
cut information from REPAIR.
- Documented HA_ADMIN_XX variables related to repair.
- Added HA_ADMIN_NEEDS_DATA_CONVERSION to signal that we have to
do data conversions when converting the table (and thus ALTER TABLE
copy algorithm is needed).
- Fixed typo in error message (caused test changes).
2024-01-29 11:52:44 +02:00
|
|
|
bool algorithm_not_specified() const
|
|
|
|
{
|
|
|
|
return requested_algorithm == ALTER_TABLE_ALGORITHM_NONE;
|
|
|
|
}
|
2023-06-29 18:03:54 +02:00
|
|
|
uint check_vcol_field(Item_field *f) const;
|
|
|
|
|
2013-03-26 00:03:13 +02:00
|
|
|
private:
|
|
|
|
Alter_info &operator=(const Alter_info &rhs); // not implemented
|
|
|
|
Alter_info(const Alter_info &rhs); // not implemented
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** Runtime context for ALTER TABLE. */
|
|
|
|
class Alter_table_ctx
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Alter_table_ctx();
|
|
|
|
|
|
|
|
Alter_table_ctx(THD *thd, TABLE_LIST *table_list, uint tables_opened_arg,
|
2018-01-07 18:03:44 +02:00
|
|
|
const LEX_CSTRING *new_db_arg, const LEX_CSTRING *new_name_arg);
|
2013-03-26 00:03:13 +02:00
|
|
|
|
|
|
|
/**
|
2021-09-09 11:58:46 +03:00
|
|
|
@return true if the table is moved to another database or a new table
|
|
|
|
created by ALTER_PARTITION_CONVERT_OUT, false otherwise.
|
2013-03-26 00:03:13 +02:00
|
|
|
*/
|
|
|
|
bool is_database_changed() const
|
2018-01-07 18:03:44 +02:00
|
|
|
{ return (new_db.str != db.str); };
|
2013-03-26 00:03:13 +02:00
|
|
|
|
|
|
|
/**
|
2021-09-09 11:58:46 +03:00
|
|
|
@return true if the table is renamed or a new table created by
|
|
|
|
ALTER_PARTITION_CONVERT_OUT, false otherwise.
|
2013-03-26 00:03:13 +02:00
|
|
|
*/
|
|
|
|
bool is_table_renamed() const
|
2018-01-07 18:03:44 +02:00
|
|
|
{ return (is_database_changed() || new_name.str != table_name.str); };
|
2013-03-26 00:03:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
@return filename (including .frm) for the new table.
|
|
|
|
*/
|
|
|
|
const char *get_new_filename() const
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(!tmp_table);
|
|
|
|
return new_filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@return path to the original table.
|
|
|
|
*/
|
|
|
|
const char *get_path() const
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(!tmp_table);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@return path to the new table.
|
|
|
|
*/
|
|
|
|
const char *get_new_path() const
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(!tmp_table);
|
|
|
|
return new_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@return path to the temporary table created during ALTER TABLE.
|
|
|
|
*/
|
|
|
|
const char *get_tmp_path() const
|
|
|
|
{ return tmp_path; }
|
|
|
|
|
2021-01-17 16:06:43 +02:00
|
|
|
const LEX_CSTRING get_tmp_cstring_path() const
|
|
|
|
{
|
|
|
|
LEX_CSTRING tmp= { tmp_path, strlen(tmp_path) };
|
|
|
|
return tmp;
|
|
|
|
};
|
|
|
|
|
2013-03-26 00:03:13 +02:00
|
|
|
/**
|
|
|
|
Mark ALTER TABLE as needing to produce foreign key error if
|
|
|
|
it deletes a row from the table being changed.
|
|
|
|
*/
|
|
|
|
void set_fk_error_if_delete_row(FOREIGN_KEY_INFO *fk)
|
|
|
|
{
|
|
|
|
fk_error_if_delete_row= true;
|
|
|
|
fk_error_id= fk->foreign_id->str;
|
|
|
|
fk_error_table= fk->foreign_table->str;
|
|
|
|
}
|
|
|
|
|
2019-05-28 15:50:11 +04:00
|
|
|
void report_implicit_default_value_error(THD *thd, const TABLE_SHARE *) const;
|
2013-03-26 00:03:13 +02:00
|
|
|
public:
|
2021-11-09 09:11:50 +02:00
|
|
|
Create_field *implicit_default_value_error_field= nullptr;
|
|
|
|
bool error_if_not_empty= false;
|
|
|
|
uint tables_opened= 0;
|
2023-04-26 15:27:01 +04:00
|
|
|
Lex_ident_db db;
|
|
|
|
Lex_ident_table table_name;
|
2021-03-30 17:06:55 +03:00
|
|
|
LEX_CSTRING storage_engine_name;
|
2018-01-07 18:03:44 +02:00
|
|
|
LEX_CSTRING alias;
|
2023-04-26 15:27:01 +04:00
|
|
|
Lex_ident_db new_db;
|
|
|
|
Lex_ident_table new_name;
|
2018-01-07 18:03:44 +02:00
|
|
|
LEX_CSTRING new_alias;
|
|
|
|
LEX_CSTRING tmp_name;
|
2021-03-30 17:06:55 +03:00
|
|
|
LEX_CSTRING tmp_storage_engine_name;
|
|
|
|
LEX_CUSTRING tmp_id, id;
|
2018-01-07 18:03:44 +02:00
|
|
|
char tmp_buff[80];
|
2021-03-30 17:06:55 +03:00
|
|
|
uchar id_buff[MY_UUID_SIZE];
|
|
|
|
char storage_engine_buff[NAME_LEN], tmp_storage_engine_buff[NAME_LEN];
|
|
|
|
bool storage_engine_partitioned;
|
|
|
|
bool tmp_storage_engine_name_partitioned;
|
MDEV-25180 Atomic ALTER TABLE
MDEV-25604 Atomic DDL: Binlog event written upon recovery does not
have default database
The purpose of this task is to ensure that ALTER TABLE is atomic even if
the MariaDB server would be killed at any point of the alter table.
This means that either the ALTER TABLE succeeds (including that triggers,
the status tables and the binary log are updated) or things should be
reverted to their original state.
If the server crashes before the new version is fully up to date and
commited, it will revert to the original table and remove all
temporary files and tables.
If the new version is commited, crash recovery will use the new version,
and update triggers, the status tables and the binary log.
The one execption is ALTER TABLE .. RENAME .. where no changes are done
to table definition. This one will work as RENAME and roll back unless
the whole statement completed, including updating the binary log (if
enabled).
Other changes:
- Added handlerton->check_version() function to allow the ddl recovery
code to check, in case of inplace alter table, if the table in the
storage engine is of the new or old version.
- Added handler->table_version() so that an engine can report the current
version of the table. This should be changed each time the table
definition changes.
- Added ha_signal_ddl_recovery_done() and
handlerton::signal_ddl_recovery_done() to inform all handlers when
ddl recovery has been done. (Needed by InnoDB).
- Added handlerton call inplace_alter_table_committed, to signal engine
that ddl_log has been closed for the alter table query.
- Added new handerton flag
HTON_REQUIRES_NOTIFY_TABLEDEF_CHANGED_AFTER_COMMIT to signal when we
should call hton->notify_tabledef_changed() during
mysql_inplace_alter_table. This was required as MyRocks and InnoDB
needed the call at different times.
- Added function server_uuid_value() to be able to generate a temporary
xid when ddl recovery writes the query to the binary log. This is
needed to be able to handle crashes during ddl log recovery.
- Moved freeing of the frm definition to end of mysql_alter_table() to
remove duplicate code and have a common exit strategy.
-------
InnoDB part of atomic ALTER TABLE
(Implemented by Marko Mäkelä)
innodb_check_version(): Compare the saved dict_table_t::def_trx_id
to determine whether an ALTER TABLE operation was committed.
We must correctly recover dict_table_t::def_trx_id for this to work.
Before purge removes any trace of DB_TRX_ID from system tables, it
will make an effort to load the user table into the cache, so that
the dict_table_t::def_trx_id can be recovered.
ha_innobase::table_version(): return garbage, or the trx_id that would
be used for committing an ALTER TABLE operation.
In InnoDB, table names starting with #sql-ib will remain special:
they will be dropped on startup. This may be revisited later in
MDEV-18518 when we implement proper undo logging and rollback
for creating or dropping multiple tables in a transaction.
Table names starting with #sql will retain some special meaning:
dict_table_t::parse_name() will not consider such names for
MDL acquisition, and dict_table_rename_in_cache() will treat such
names specially when handling FOREIGN KEY constraints.
Simplify InnoDB DROP INDEX.
Prevent purge wakeup
To ensure that dict_table_t::def_trx_id will be recovered correctly
in case the server is killed before ddl_log_complete(), we will block
the purge of any history in SYS_TABLES, SYS_INDEXES, SYS_COLUMNS
between ha_innobase::commit_inplace_alter_table(commit=true)
(purge_sys.stop_SYS()) and purge_sys.resume_SYS().
The completion callback purge_sys.resume_SYS() must be between
ddl_log_complete() and MDL release.
--------
MyRocks support for atomic ALTER TABLE
(Implemented by Sergui Petrunia)
Implement these SE API functions:
- ha_rocksdb::table_version()
- hton->check_version = rocksdb_check_versionMyRocks data dictionary
now stores table version for each table.
(Absence of table version record is interpreted as table_version=0,
that is, which means no upgrade changes are needed)
- For inplace alter table of a partitioned table, call the underlying
handlerton when checking if the table is ok. This assumes that the
partition engine commits all changes at once.
2021-03-18 12:41:08 +02:00
|
|
|
|
2013-03-26 00:03:13 +02:00
|
|
|
/**
|
|
|
|
Indicates that if a row is deleted during copying of data from old version
|
|
|
|
of table to the new version ER_FK_CANNOT_DELETE_PARENT error should be
|
|
|
|
emitted.
|
|
|
|
*/
|
2021-11-09 09:11:50 +02:00
|
|
|
bool fk_error_if_delete_row= false;
|
2013-03-26 00:03:13 +02:00
|
|
|
/** Name of foreign key for the above error. */
|
2021-11-09 09:11:50 +02:00
|
|
|
const char *fk_error_id= nullptr;
|
2013-03-26 00:03:13 +02:00
|
|
|
/** Name of table for the above error. */
|
2021-11-09 09:11:50 +02:00
|
|
|
const char *fk_error_table= nullptr;
|
|
|
|
bool modified_primary_key= false;
|
2021-03-30 17:06:55 +03:00
|
|
|
/** Indicates that we are altering temporary table */
|
2021-11-09 09:11:50 +02:00
|
|
|
bool tmp_table= false;
|
2013-03-26 00:03:13 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
char new_filename[FN_REFLEN + 1];
|
2023-06-23 13:24:02 +04:00
|
|
|
CharBuffer<NAME_LEN> new_name_buff;
|
2018-01-07 18:03:44 +02:00
|
|
|
char tmp_name_buff[NAME_LEN + 1];
|
2013-03-26 00:03:13 +02:00
|
|
|
char path[FN_REFLEN + 1];
|
|
|
|
char new_path[FN_REFLEN + 1];
|
|
|
|
char tmp_path[FN_REFLEN + 1];
|
|
|
|
|
|
|
|
Alter_table_ctx &operator=(const Alter_table_ctx &rhs); // not implemented
|
|
|
|
Alter_table_ctx(const Alter_table_ctx &rhs); // not implemented
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-08-16 14:53:30 +02:00
|
|
|
/**
|
2013-03-26 00:03:13 +02:00
|
|
|
Sql_cmd_common_alter_table represents the common properties of the ALTER TABLE
|
2010-08-16 14:53:30 +02:00
|
|
|
statements.
|
|
|
|
@todo move Alter_info and other ALTER generic structures from Lex here.
|
|
|
|
*/
|
2019-03-11 16:45:38 +01:00
|
|
|
class Sql_cmd_common_alter_table : public Sql_cmd
|
2010-08-16 14:53:30 +02:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
Constructor.
|
|
|
|
*/
|
2023-02-07 13:57:20 +02:00
|
|
|
Sql_cmd_common_alter_table() = default;
|
2010-08-16 14:53:30 +02:00
|
|
|
|
2023-02-07 13:57:20 +02:00
|
|
|
virtual ~Sql_cmd_common_alter_table() = default;
|
2010-08-16 14:53:30 +02:00
|
|
|
|
2024-06-12 09:46:26 -04:00
|
|
|
enum_sql_command sql_command_code() const override
|
2013-03-26 00:03:13 +02:00
|
|
|
{
|
|
|
|
return SQLCOM_ALTER_TABLE;
|
|
|
|
}
|
2010-08-16 14:53:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-03-26 00:03:13 +02:00
|
|
|
Sql_cmd_alter_table represents the generic ALTER TABLE statement.
|
2010-08-16 14:53:30 +02:00
|
|
|
@todo move Alter_info and other ALTER specific structures from Lex here.
|
|
|
|
*/
|
2019-05-31 15:24:40 +04:00
|
|
|
class Sql_cmd_alter_table : public Sql_cmd_common_alter_table,
|
|
|
|
public Storage_engine_name
|
2010-08-16 14:53:30 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
Constructor, used to represent a ALTER TABLE statement.
|
|
|
|
*/
|
2023-02-07 13:57:20 +02:00
|
|
|
Sql_cmd_alter_table() = default;
|
2010-08-16 14:53:30 +02:00
|
|
|
|
2023-02-07 13:57:20 +02:00
|
|
|
~Sql_cmd_alter_table() = default;
|
2010-08-16 14:53:30 +02:00
|
|
|
|
2024-06-12 09:46:26 -04:00
|
|
|
Storage_engine_name *option_storage_engine_name() override { return this; }
|
2019-05-31 15:24:40 +04:00
|
|
|
|
2024-06-12 09:46:26 -04:00
|
|
|
bool execute(THD *thd) override;
|
2010-08-16 14:53:30 +02:00
|
|
|
};
|
|
|
|
|
2013-03-26 00:03:13 +02:00
|
|
|
|
2017-05-08 02:44:55 +03:00
|
|
|
/**
|
|
|
|
Sql_cmd_alter_sequence represents the ALTER SEQUENCE statement.
|
|
|
|
*/
|
2017-11-15 19:18:28 +04:00
|
|
|
class Sql_cmd_alter_sequence : public Sql_cmd,
|
|
|
|
public DDL_options
|
2017-05-08 02:44:55 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
Constructor, used to represent a ALTER TABLE statement.
|
|
|
|
*/
|
2017-11-15 19:18:28 +04:00
|
|
|
Sql_cmd_alter_sequence(const DDL_options &options)
|
|
|
|
:DDL_options(options)
|
2017-05-08 02:44:55 +03:00
|
|
|
{}
|
|
|
|
|
2023-02-07 13:57:20 +02:00
|
|
|
~Sql_cmd_alter_sequence() = default;
|
2017-05-08 02:44:55 +03:00
|
|
|
|
2024-06-12 09:46:26 -04:00
|
|
|
enum_sql_command sql_command_code() const override
|
2017-05-08 02:44:55 +03:00
|
|
|
{
|
|
|
|
return SQLCOM_ALTER_SEQUENCE;
|
|
|
|
}
|
2024-06-12 09:46:26 -04:00
|
|
|
bool execute(THD *thd) override;
|
2017-05-08 02:44:55 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-03-26 00:03:13 +02:00
|
|
|
/**
|
|
|
|
Sql_cmd_alter_table_tablespace represents ALTER TABLE
|
|
|
|
IMPORT/DISCARD TABLESPACE statements.
|
|
|
|
*/
|
|
|
|
class Sql_cmd_discard_import_tablespace : public Sql_cmd_common_alter_table
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum enum_tablespace_op_type
|
|
|
|
{
|
|
|
|
DISCARD_TABLESPACE, IMPORT_TABLESPACE
|
|
|
|
};
|
|
|
|
|
|
|
|
Sql_cmd_discard_import_tablespace(enum_tablespace_op_type tablespace_op_arg)
|
|
|
|
: m_tablespace_op(tablespace_op_arg)
|
|
|
|
{}
|
|
|
|
|
2024-06-12 09:46:26 -04:00
|
|
|
bool execute(THD *thd) override;
|
2013-03-26 00:03:13 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
const enum_tablespace_op_type m_tablespace_op;
|
|
|
|
};
|
|
|
|
|
2010-08-16 14:53:30 +02:00
|
|
|
#endif
|