mirror of
https://github.com/MariaDB/server.git
synced 2025-09-26 19:09:13 +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
288 lines
9.2 KiB
C++
288 lines
9.2 KiB
C++
#ifndef UNIREG_INCLUDED
|
|
#define UNIREG_INCLUDED
|
|
|
|
/*
|
|
Copyright (c) 2000, 2011, Oracle and/or its affiliates.
|
|
|
|
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 Street, Fifth Floor, Boston, MA 02110-1335 USA */
|
|
|
|
|
|
#include <mysql_version.h> /* FRM_VER */
|
|
|
|
/* Extra functions used by unireg library */
|
|
|
|
#ifndef NO_ALARM_LOOP
|
|
#define NO_ALARM_LOOP /* lib5 and popen can't use alarm */
|
|
#endif
|
|
|
|
/* These paths are converted to other systems (WIN95) before use */
|
|
|
|
#define LANGUAGE "english/"
|
|
#define ERRMSG_FILE "errmsg.sys"
|
|
#define TEMP_PREFIX "MY"
|
|
#define LOG_PREFIX "ML"
|
|
#define PROGDIR "bin/"
|
|
#ifndef MYSQL_DATADIR
|
|
#define MYSQL_DATADIR "data/"
|
|
#endif
|
|
#ifndef SHAREDIR
|
|
#define SHAREDIR "share/"
|
|
#endif
|
|
#ifndef PLUGINDIR
|
|
#define PLUGINDIR "lib/plugin"
|
|
#endif
|
|
|
|
#define MAX_ERROR_RANGES 4 /* 1000-2000, 2000-3000, 3000-4000, 4000-5000 */
|
|
#define ERRORS_PER_RANGE 1000
|
|
|
|
#define DEFAULT_ERRMSGS my_default_lc_messages->errmsgs->errmsgs
|
|
#define CURRENT_THD_ERRMSGS (current_thd)->variables.errmsgs
|
|
|
|
#ifndef mysqld_error_find_printf_error_used
|
|
#define ER_DEFAULT(X) DEFAULT_ERRMSGS[((X)-ER_ERROR_FIRST) / ERRORS_PER_RANGE][(X)% ERRORS_PER_RANGE]
|
|
#define ER_THD(thd,X) ((thd)->variables.errmsgs[((X)-ER_ERROR_FIRST) / ERRORS_PER_RANGE][(X) % ERRORS_PER_RANGE])
|
|
#define ER(X) ER_THD(current_thd, (X))
|
|
#endif
|
|
#define ER_THD_OR_DEFAULT(thd,X) ((thd) ? ER_THD(thd, (X)) : ER_DEFAULT(X))
|
|
|
|
#define SPECIAL_USE_LOCKS 1 /* Lock used databases */
|
|
#define SPECIAL_NO_NEW_FUNC 2 /* Skip new functions */
|
|
#define SPECIAL_SKIP_SHOW_DB 4 /* Don't allow 'show db' */
|
|
#define SPECIAL_WAIT_IF_LOCKED 8 /* Wait if locked database */
|
|
#define SPECIAL_SAME_DB_NAME 16 /* form name = file name */
|
|
#define SPECIAL_ENGLISH 32 /* English error messages */
|
|
#define SPECIAL_NO_RESOLVE 64 /* Obsolete */
|
|
#define SPECIAL_NO_PRIOR 128 /* Obsolete */
|
|
#define SPECIAL_BIG_SELECTS 256 /* Don't use heap tables */
|
|
#define SPECIAL_NO_HOST_CACHE 512 /* Don't cache hosts */
|
|
#define SPECIAL_SHORT_LOG_FORMAT 1024
|
|
#define SPECIAL_SAFE_MODE 2048
|
|
#define SPECIAL_LOG_QUERIES_NOT_USING_INDEXES 4096 /* Obsolete */
|
|
|
|
/* Extern defines */
|
|
#define store_record(A,B) memcpy((A)->B,(A)->record[0],(size_t) (A)->s->reclength)
|
|
#define restore_record(A,B) memcpy((A)->record[0],(A)->B,(size_t) (A)->s->reclength)
|
|
#define cmp_record(A,B) memcmp((A)->record[0],(A)->B,(size_t) (A)->s->reclength)
|
|
#define empty_record(A) { \
|
|
restore_record((A),s->default_values); \
|
|
if ((A)->s->null_bytes) \
|
|
bfill((A)->null_flags,(A)->s->null_bytes,255); \
|
|
}
|
|
|
|
/* Defines for use with openfrm, openprt and openfrd */
|
|
|
|
#define READ_ALL (1 << 0) /* openfrm: Read all parameters */
|
|
#define EXTRA_RECORD (1 << 3) /* Reserve space for an extra record */
|
|
#define DELAYED_OPEN (1 << 12) /* Open table later */
|
|
#define OPEN_VIEW_NO_PARSE (1 << 14) /* Open frm only if it's a view,
|
|
but do not parse view itself */
|
|
/**
|
|
This flag is used in function get_all_tables() which fills
|
|
I_S tables with data which are retrieved from frm files and storage engine
|
|
The flag means that we need to open FRM file only to get necessary data.
|
|
*/
|
|
#define OPEN_FRM_FILE_ONLY (1 << 15)
|
|
/**
|
|
This flag is used in function get_all_tables() which fills
|
|
I_S tables with data which are retrieved from frm files and storage engine
|
|
The flag means that we need to process tables only to get necessary data.
|
|
Views are not processed.
|
|
*/
|
|
#define OPEN_TABLE_ONLY (1 << 16)
|
|
/**
|
|
This flag is used in function get_all_tables() which fills
|
|
I_S tables with data which are retrieved from frm files and storage engine
|
|
The flag means that we need to process views only to get necessary data.
|
|
Tables are not processed.
|
|
*/
|
|
#define OPEN_VIEW_ONLY (1 << 17)
|
|
/**
|
|
This flag is used in function get_all_tables() which fills
|
|
I_S tables with data which are retrieved from frm files and storage engine.
|
|
The flag means that we need to open a view using
|
|
open_normal_and_derived_tables() function.
|
|
*/
|
|
#define OPEN_VIEW_FULL (1 << 18)
|
|
/**
|
|
This flag is used in function get_all_tables() which fills
|
|
I_S tables with data which are retrieved from frm files and storage engine.
|
|
The flag means that I_S table uses optimization algorithm.
|
|
*/
|
|
#define OPTIMIZE_I_S_TABLE (1 << 19)
|
|
/**
|
|
This flag is used to instruct tdc_open_view() to check metadata version.
|
|
*/
|
|
#define CHECK_METADATA_VERSION (1 << 20)
|
|
|
|
/*
|
|
The flag means that we need to process trigger files only.
|
|
*/
|
|
#define OPEN_TRIGGER_ONLY (1 << 21)
|
|
|
|
/**
|
|
This flag is used in information schema to determine if handling function
|
|
can treat open result extensively and provide some user output even if
|
|
table open fails.
|
|
*/
|
|
#define I_S_EXTENDED_ERROR_HANDLING (1 << 22)
|
|
|
|
/*
|
|
Minimum length pattern before Turbo Boyer-Moore is used
|
|
for SELECT "text" LIKE "%pattern%", excluding the two
|
|
wildcards in class Item_func_like.
|
|
*/
|
|
#define MIN_TURBOBM_PATTERN_LEN 3
|
|
|
|
/*
|
|
Defines for binary logging.
|
|
Do not decrease the value of BIN_LOG_HEADER_SIZE.
|
|
Do not even increase it before checking code.
|
|
*/
|
|
|
|
#define BIN_LOG_HEADER_SIZE 4
|
|
|
|
#define DEFAULT_KEY_CACHE_NAME "default"
|
|
|
|
|
|
/* Include prototypes for unireg */
|
|
|
|
#include "mysqld_error.h"
|
|
#include "structs.h" /* All structs we need */
|
|
#include "sql_list.h" /* List<> */
|
|
#include "field.h" /* Create_field */
|
|
|
|
|
|
LEX_CUSTRING build_frm_image(THD *thd, const LEX_CSTRING &table,
|
|
HA_CREATE_INFO *create_info,
|
|
List<Create_field> &create_fields,
|
|
uint keys, KEY *key_info, FK_list &foreign_keys,
|
|
FK_list &referenced_keys,
|
|
handler *db_file);
|
|
|
|
static inline bool is_binary_frm_header(const uchar *head)
|
|
{
|
|
return head[0] == 254
|
|
&& head[1] == 1
|
|
&& head[2] >= FRM_VER
|
|
&& head[2] <= FRM_VER_CURRENT;
|
|
}
|
|
|
|
|
|
class Key;
|
|
class Foreign_key;
|
|
class Foreign_key_io: public BinaryStringBuffer<512>
|
|
{
|
|
TABLE_SHARE *share;
|
|
KEY *key_info;
|
|
|
|
public:
|
|
static const ulonglong fk_io_version= 0;
|
|
|
|
struct Pos
|
|
{
|
|
uchar *pos;
|
|
const uchar *end;
|
|
Pos(LEX_CUSTRING& image)
|
|
{
|
|
pos= const_cast<uchar *>(image.str);
|
|
end= pos + image.length;
|
|
}
|
|
};
|
|
|
|
/* read */
|
|
private:
|
|
static bool read_length(size_t &out, Pos &p)
|
|
{
|
|
ulonglong num= safe_net_field_length_ll(&p.pos, p.end - p.pos);
|
|
if (!p.pos || num > UINT_MAX32)
|
|
return true;
|
|
out= (uint32_t) num;
|
|
return false;
|
|
}
|
|
static bool read_ll(longlong &out, Pos &p)
|
|
{
|
|
longlong num= (longlong) safe_net_field_length_ll(&p.pos, p.end - p.pos);
|
|
if (!p.pos || num > INT_MAX32 || num < INT_MIN32)
|
|
return true;
|
|
out= (int32_t) num;
|
|
return false;
|
|
}
|
|
static bool read_string(Lex_cstring &to, MEM_ROOT *mem_root, Pos &p)
|
|
{
|
|
if (read_length(to.length, p) || p.pos + to.length > p.end)
|
|
return true; // Not enough data
|
|
if (!to.length)
|
|
return false;
|
|
to.str= strmake_root(mem_root, (char *) p.pos, to.length);
|
|
if (!to.str)
|
|
{
|
|
my_error(ER_OUT_OF_RESOURCES, MYF(0));
|
|
return true;
|
|
}
|
|
p.pos+= to.length;
|
|
DBUG_ASSERT(p.pos <= p.end);
|
|
return false;
|
|
}
|
|
static void read_uuid(uchar *to, Pos &p)
|
|
{
|
|
memcpy(to, p.pos, MY_UUID_SIZE);
|
|
p.pos+= MY_UUID_SIZE;
|
|
DBUG_ASSERT(p.pos <= p.end);
|
|
}
|
|
public:
|
|
Foreign_key_io(TABLE_SHARE *share) :
|
|
share{share}, key_info{share->key_info} {}
|
|
bool parse(THD *thd, LEX_CUSTRING &image);
|
|
|
|
/* write */
|
|
private:
|
|
static uchar *store_length(uchar *pos, ulonglong length)
|
|
{
|
|
return net_store_length(pos, length);
|
|
}
|
|
static uchar *store_ll(uchar *pos, longlong data)
|
|
{
|
|
return net_store_length(pos, (ulonglong) data);
|
|
}
|
|
static uchar *store_string(uchar *pos, const LEX_CSTRING &str, bool nullable= false)
|
|
{
|
|
DBUG_ASSERT(nullable || str.length);
|
|
pos= store_length(pos, str.length);
|
|
if (str.length)
|
|
memcpy(pos, str.str, str.length);
|
|
return pos + str.length;
|
|
}
|
|
static uchar *store_uuid(uchar *pos, uchar *uuid)
|
|
{
|
|
memcpy(pos, uuid, MY_UUID_SIZE);
|
|
return pos + MY_UUID_SIZE;
|
|
}
|
|
static ulonglong string_size(Lex_cstring str)
|
|
{
|
|
return net_length_size(str.length) + str.length;
|
|
}
|
|
static ulonglong ll_size(longlong num)
|
|
{
|
|
return net_length_size((ulonglong) num);
|
|
}
|
|
public:
|
|
Foreign_key_io(KEY *key_info) :
|
|
share{NULL}, key_info{key_info} {}
|
|
ulonglong fk_size(FK_info &fk);
|
|
ulonglong hint_size(FK_info &rk);
|
|
void store_fk(FK_info &fk, uchar *&pos);
|
|
bool store(THD *thd, FK_list &foreign_keys, FK_list &referenced_keys);
|
|
};
|
|
|
|
#endif
|