mirror of
https://github.com/MariaDB/server.git
synced 2025-09-25 18:39:15 +02:00

1. Access foreign keys via TABLE_SHARE::foreign_keys and TABLE_SHARE::referenced_keys; foreign_keys and referenced_keys are lists in TABLE_SHARE. 2. Remove handler FK interface: - get_foreign_key_list() - get_parent_foreign_key_list() - referenced_by_foreign_key() 3. Invalidate referenced shares on: - RENAME TABLE - DROP TABLE - RENAME COLUMN - ADD FOREIGN KEY When foreign table is created or altered by the above operations all referenced shares are closed. This blocks the operation while any referenced shares are used (when at least one its TABLE instance is locked). 4. Update referenced shares on: - CREATE TABLE On CREATE TABLE add items to referenced_keys of referenced shares. States of referenced shares are restored in case of errors. 5. Invalidate foreign shares on: - RENAME TABLE - RENAME COLUMN The above-mentioned blocking takes effect. 6. Check foreign/referenced shares consistency on: - CHECK TABLE 7. Temporary change until MDEV-21051: InnoDB fill foreign key info at handler open(). FOREIGN_KEY_INFO is refactored to FK_info holding Lex_cstring. On first TABLE open FK_info is loaded from storage engine into TABLE_SHARE. All referenced shares (if any exists) are closed. This leads to blocking of first time foreign table open while referenced tables are used. MDEV-21311 Converge Foreign_key and supplemental generated Key together mysql_prepare_create_table() does data validation and such utilities as automatic name generation. But it does that only for indexes and ignores Foreign_key objects. Now as Foreign_key data needs to be stored in FRM files as well this processing must be done for it like for any other Key objects. Replace Key::FOREIGN_KEY type with Key::foreign flag of type Key::MULTIPLE and Key::generated set to true. Construct one object with Key::foreign == true instead of two objects of type Key::FOREIGN_KEY and Key::MULTIPLE. MDEV-21051 datadict refactorings - Move read_extra2() to datadict.cc - Refactored extra2_fields to Extra2_info - build_frm_image() readability MDEV-21051 build_table_shadow_filename() refactoring mysql_prepare_alter_table() leaks fixes MDEV-21051 amend system tables locking restriction Table mysql.help_relation has foreign key to mysql.help_keyword. On bootstrap when help_relation is opened, it preopens help_keyword for READ and fails in lock_tables_check(). If system table is opened for write then fk references are opened for write. Related to: Bug#25422, WL#3984 Tests: main.lock MDEV-21051 Store and read foreign key info into/from FRM files 1. Introduce Foreign_key_io class which creates/parses binary stream containing foreign key structures. Referenced tables store there only hints about foreign tables (their db and name), they restore full info from the corresponding tables. Foreign_key_io is stored under new EXTRA2_FOREIGN_KEY_INFO field in extra2 section of FRM file. 2. Modify mysql_prepare_create_table() to generate names for foreign keys. Until InnoDB storage of foreign keys is removed, FK names must be unique across the database: the FK name must be based on table name. 3. Keep stored data in sync on DDL changes. Referenced tables update their foreign hints after following operations on foreign tables: - RENAME TABLE - DROP TABLE - CREATE TABLE - ADD FOREIGN KEY - DROP FOREIGN KEY Foreign tables update their foreign info after following operations on referenced tables: - RENAME TABLE - RENAME COLUMN 4. To achieve 3. there must be ability to rewrite extra2 section of FRM file without full reparse. FRM binary is built from primary structures like HA_CREATE_INFO and cannot be built from TABLE_SHARE. Use shadow write and rename like fast_alter_partition_table() does. Shadow FRM is new FRM file that replaces the old one. CREATE TABLE workflow: 1. Foreign_key is constructed in parser, placed into alter_info->key_list; 2. mysql_prepare_create_table() translates them to FK_info, assigns foreign_id if needed; 3. build_frm_image() writes two FK_info lists into FRM's extra2 section, for referenced keys it stores only table names (hints); 4. init_from_binary_frm_image() parses extra2 section and fills foreign_keys and referenced_keys of TABLE_SHARE. It restores referenced_keys by reading hint list of table names, opening corresponding shares and restoring FK_info from their foreign_keys. Hints resolution is done only when initializing non-temporary shares. Usually temporary share has different (temporary) name and it is impossible to resolve foreign keys by that name (as we identify them by both foreign and referenced table names). Another not unimportant reason is performance: this saves spare share acquisitions. ALTER TABLE workflow: 1. Foreign_key is constructed in parser, placed into alter_info->key_list; 2. mysql_prepare_alter_table() prepares action lists and share list of foreigns/references; 3. mysql_prepare_alter_table() locks list of foreigns/references by MDL_INTENTION_EXCLUSIVE, acquires shares; 4. prepare_create_table() converts key_list into FK_list, assigns foreign_id; 5. shadow FRM of altered table is created; 6. data is copied; 7. altered table is locked by MDL_EXCLUSIVE; 8. fk_handle_alter() processes action lists, creates FK backups, modifies shares, writes shadow FRMs; 9. altered table is closed; 10. shadow FRMs are installed; 11. altered table is renamed, FRM backup deleted; 12. (TBD in MDEV-21053) shadow FRMs installation log closed, backups deleted; On FK backup system: In case of failed DDL operation all shares that was modified must be restored into original state. This is done by FK_ddl_backup (CREATE, DROP), FK_rename_backup (RENAME), FK_alter_backup (ALTER). On STL usage: STL is used for utility not performance-critical algorithms, core structures hold native List. A wrapper was made to convert STL exception into bool error status or NULL value. MDEV-20865 fk_check_consistency() in CHECK TABLE Self-refs fix Test table_flags fix: "debug" deviation is now gone. FIXMEs: +16 -1
246 lines
11 KiB
C++
246 lines
11 KiB
C++
/* Copyright (c) 2006, 2014, Oracle and/or its affiliates.
|
|
Copyright (c) 2011, 2017, MariaDB Corporation.
|
|
|
|
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-1335 USA */
|
|
|
|
#ifndef SQL_TABLE_INCLUDED
|
|
#define SQL_TABLE_INCLUDED
|
|
|
|
#include <my_sys.h> // pthread_mutex_t
|
|
#include "m_string.h" // LEX_CUSTRING
|
|
#include "lex_charset.h"
|
|
#include "lex_ident.h"
|
|
#include "mdl.h" // MDL_request_list
|
|
#include "handler.h"
|
|
#include "table.h"
|
|
|
|
#define ERROR_INJECT(code) \
|
|
((DBUG_IF("crash_" code) && (DBUG_SUICIDE(), 0)) || \
|
|
(DBUG_IF("fail_" code) && (my_error(ER_UNKNOWN_ERROR, MYF(0)), 1)))
|
|
|
|
class Alter_info;
|
|
class Alter_table_ctx;
|
|
class Column_definition;
|
|
class Create_field;
|
|
class FK_rename_vector;
|
|
struct TABLE_LIST;
|
|
class THD;
|
|
struct TABLE;
|
|
struct handlerton;
|
|
class handler;
|
|
class String;
|
|
typedef struct st_ha_check_opt HA_CHECK_OPT;
|
|
struct HA_CREATE_INFO;
|
|
struct Table_specification_st;
|
|
typedef struct st_key KEY;
|
|
typedef struct st_key_cache KEY_CACHE;
|
|
typedef struct st_lock_param_type ALTER_PARTITION_PARAM_TYPE;
|
|
typedef struct st_order ORDER;
|
|
typedef struct st_ddl_log_state DDL_LOG_STATE;
|
|
extern LEX_CSTRING generated_by_server;
|
|
|
|
enum enum_explain_filename_mode
|
|
{
|
|
EXPLAIN_ALL_VERBOSE= 0,
|
|
EXPLAIN_PARTITIONS_VERBOSE,
|
|
EXPLAIN_PARTITIONS_AS_COMMENT
|
|
};
|
|
|
|
|
|
/* depends on errmsg.txt Database `db`, Table `t` ... */
|
|
#define EXPLAIN_FILENAME_MAX_EXTRA_LENGTH 63
|
|
|
|
/* See mysql_write_frm function comment for explanations of these flags */
|
|
/* Shadow FRM is new FRM file that replaces the old one */
|
|
#define WFRM_WRITE_SHADOW 1
|
|
#define WFRM_INSTALL_SHADOW 2
|
|
#define WFRM_KEEP_SHARE 4
|
|
#define WFRM_WRITE_CONVERTED_TO 8
|
|
#define WFRM_BACKUP_ORIGINAL 16
|
|
#define WFRM_ALTER_INFO_PREPARED 32
|
|
|
|
/* Flags for conversion functions. */
|
|
static constexpr uint FN_FROM_IS_TMP= 1 << 0;
|
|
static constexpr uint FN_TO_IS_TMP= 1 << 1;
|
|
static constexpr uint FN_IS_TMP= FN_FROM_IS_TMP | FN_TO_IS_TMP;
|
|
/* Remove .frm table metadata. */
|
|
static constexpr uint QRMT_FRM= 1 << 2;
|
|
/* Remove .par partitioning metadata. */
|
|
static constexpr uint QRMT_PAR= 1 << 3;
|
|
/* Remove handler files and high-level indexes. */
|
|
static constexpr uint QRMT_HANDLER= 1 << 4;
|
|
/* Default behaviour is to drop .FRM and handler, but not .par. */
|
|
static constexpr uint QRMT_DEFAULT= QRMT_FRM | QRMT_HANDLER;
|
|
/** Don't resolve MySQL's fake "foo.sym" symbolic directory names. */
|
|
static constexpr uint SKIP_SYMDIR_ACCESS= 1 << 5;
|
|
/** Don't check foreign key constraints while renaming table */
|
|
static constexpr uint NO_FK_CHECKS= 1 << 6;
|
|
|
|
constexpr LEX_CSTRING FK_INFIX= { C_STRING_WITH_LEN("_ibfk_") };
|
|
|
|
uint filename_to_tablename(const char *from, char *to, size_t to_length,
|
|
bool stay_quiet = false);
|
|
uint tablename_to_filename(const char *from, char *to, size_t to_length);
|
|
uint check_n_cut_mysql50_prefix(const char *from, char *to, size_t to_length);
|
|
bool check_mysql50_prefix(const char *name);
|
|
uint build_table_filename(char *buff, size_t bufflen, const char *db,
|
|
const char *table, const char *ext, uint flags);
|
|
uint build_table_shadow_filename(THD *thd, char *buff, size_t bufflen,
|
|
const char *db, const char *table_name,
|
|
bool backup= false);
|
|
uint build_table_shadow_filename(char *buff, size_t bufflen,
|
|
ALTER_PARTITION_PARAM_TYPE *lpt,
|
|
bool backup= false);
|
|
void build_lower_case_table_filename(char *buff, size_t bufflen,
|
|
const LEX_CSTRING *db,
|
|
const LEX_CSTRING *table,
|
|
uint flags);
|
|
uint build_tmptable_filename(THD* thd, char *buff, size_t bufflen);
|
|
void make_tmp_table_name(THD *thd, LEX_STRING *to, const char *prefix);
|
|
bool add_keyword_to_query(THD *thd, String *result, const LEX_CSTRING *keyword,
|
|
const LEX_CSTRING *add);
|
|
|
|
/*
|
|
mysql_create_table_no_lock can be called in one of the following
|
|
mutually exclusive situations:
|
|
|
|
- Just a normal ordinary CREATE TABLE statement that explicitly
|
|
defines the table structure.
|
|
|
|
- CREATE TABLE ... SELECT. It is special, because only in this case,
|
|
the list of fields is allowed to have duplicates, as long as one of the
|
|
duplicates comes from the select list, and the other doesn't. For
|
|
example in
|
|
|
|
CREATE TABLE t1 (a int(5) NOT NUL) SELECT b+10 as a FROM t2;
|
|
|
|
the list in alter_info->create_list will have two fields `a`.
|
|
|
|
- ALTER TABLE, that creates a temporary table #sql-xxx, which will be later
|
|
renamed to replace the original table.
|
|
|
|
- ALTER TABLE as above, but which only modifies the frm file, it only
|
|
creates an frm file for the #sql-xxx, the table in the engine is not
|
|
created.
|
|
|
|
- Assisted discovery, CREATE TABLE statement without the table structure.
|
|
|
|
These situations are distinguished by the following "create table mode"
|
|
values, where a CREATE ... SELECT is denoted by any non-negative number
|
|
(which should be the number of fields in the SELECT ... part), and other
|
|
cases use constants as defined below.
|
|
*/
|
|
#define C_ORDINARY_CREATE 0
|
|
#define C_ASSISTED_DISCOVERY -1
|
|
#define C_ALTER_TABLE -2
|
|
#define C_ALTER_TABLE_FRM_ONLY -3
|
|
|
|
int mysql_create_table_no_lock(THD *thd,
|
|
DDL_LOG_STATE *ddl_log_state,
|
|
DDL_LOG_STATE *ddl_log_state_rm,
|
|
Table_specification_st *create_info,
|
|
Alter_info *alter_info, bool *is_trans,
|
|
int create_table_mode, TABLE_LIST *table);
|
|
|
|
handler *mysql_create_frm_image(THD *thd, HA_CREATE_INFO *create_info,
|
|
Alter_info *alter_info,
|
|
Alter_table_ctx *alter_ctx,
|
|
int create_table_mode,
|
|
KEY **key_info, uint *key_count,
|
|
LEX_CUSTRING *frm);
|
|
|
|
int mysql_discard_or_import_tablespace(THD *thd, TABLE_LIST *table_list,
|
|
bool discard);
|
|
|
|
class Share_acquire_vec;
|
|
bool mysql_prepare_alter_table(THD *thd, TABLE *table,
|
|
Table_specification_st *create_info,
|
|
Alter_info *alter_info,
|
|
Alter_table_ctx *alter_ctx);
|
|
bool mysql_trans_prepare_alter_copy_data(THD *thd);
|
|
bool mysql_trans_commit_alter_copy_data(THD *thd);
|
|
bool mysql_alter_table(THD *thd, const LEX_CSTRING *new_db,
|
|
const LEX_CSTRING *new_name,
|
|
Table_specification_st *create_info,
|
|
TABLE_LIST *table_list,
|
|
class Recreate_info *recreate_info,
|
|
Alter_info *alter_info,
|
|
uint order_num, ORDER *order, bool ignore,
|
|
bool if_exists);
|
|
bool mysql_compare_tables(TABLE *table,
|
|
Alter_info *alter_info,
|
|
HA_CREATE_INFO *create_info,
|
|
bool *metadata_equal);
|
|
bool mysql_recreate_table(THD *thd, TABLE_LIST *table_list,
|
|
class Recreate_info *recreate_info,
|
|
bool table_copy);
|
|
bool mysql_rename_table(handlerton *base, const LEX_CSTRING *old_db,
|
|
const LEX_CSTRING *old_name, const LEX_CSTRING *new_db,
|
|
const LEX_CSTRING *new_name, const LEX_CUSTRING *id,
|
|
uint flags);
|
|
bool mysql_backup_table(THD* thd, TABLE_LIST* table_list);
|
|
bool mysql_restore_table(THD* thd, TABLE_LIST* table_list);
|
|
|
|
template<typename T> class List;
|
|
void fill_checksum_table_metadata_fields(THD *thd, List<Item> *fields);
|
|
bool mysql_checksum_table(THD* thd, TABLE_LIST* table_list,
|
|
HA_CHECK_OPT* check_opt);
|
|
bool mysql_rm_table(THD *thd,TABLE_LIST *tables, bool if_exists,
|
|
bool drop_temporary, bool drop_sequence,
|
|
bool dont_log_query);
|
|
int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables,
|
|
const LEX_CSTRING *db,
|
|
DDL_LOG_STATE *ddl_log_state,
|
|
bool if_exists,
|
|
bool drop_temporary, bool drop_view,
|
|
bool drop_sequence, bool drop_db,
|
|
bool dont_log_query /* MERGE: recheck calls */, bool dont_free_locks);
|
|
bool log_drop_table(THD *thd, const LEX_CSTRING *db_name,
|
|
const LEX_CSTRING *table_name, const LEX_CSTRING *handler,
|
|
bool partitioned, const LEX_CUSTRING *id,
|
|
bool temporary_table);
|
|
int get_hlindex_keys_by_open(THD *thd, const LEX_CSTRING *db,
|
|
const LEX_CSTRING *table_name, const char *path,
|
|
uint *keys, uint *total_keys);
|
|
bool quick_rm_table(THD *thd, handlerton *base, const LEX_CSTRING *db,
|
|
const LEX_CSTRING *table_name, uint flags,
|
|
const char *table_path=0);
|
|
void close_cached_table(THD *thd, TABLE *table);
|
|
void sp_prepare_create_field(THD *thd, Column_definition *sql_field);
|
|
bool mysql_write_frm(ALTER_PARTITION_PARAM_TYPE *lpt, uint flags);
|
|
int write_bin_log(THD *thd, bool clear_error,
|
|
char const *query, ulong query_length,
|
|
bool is_trans= FALSE);
|
|
int write_bin_log_with_if_exists(THD *thd, bool clear_error,
|
|
bool is_trans, bool add_if_exists,
|
|
bool commit_alter= false);
|
|
|
|
bool fk_handle_rename(THD *thd, TABLE_LIST *old_table, const Lex_ident_db *new_db,
|
|
const Lex_ident_table *new_table_name,
|
|
FK_rename_vector &fk_rename_backup);
|
|
|
|
void promote_first_timestamp_column(List<Create_field> *column_definitions);
|
|
|
|
/*
|
|
These prototypes where under INNODB_COMPATIBILITY_HOOKS.
|
|
*/
|
|
uint explain_filename(THD* thd, const char *from, char *to, uint to_length,
|
|
enum_explain_filename_mode explain_mode);
|
|
|
|
|
|
extern MYSQL_PLUGIN_IMPORT const Lex_ident_column primary_key_name;
|
|
|
|
bool check_engine(THD *, const char *, const char *, HA_CREATE_INFO *);
|
|
|
|
#endif /* SQL_TABLE_INCLUDED */
|