mirror of
https://github.com/MariaDB/server.git
synced 2025-10-03 06:19:16 +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
183 lines
5.4 KiB
C++
183 lines
5.4 KiB
C++
#ifndef TABLE_CACHE_H_INCLUDED
|
|
#define TABLE_CACHE_H_INCLUDED
|
|
/* Copyright (c) 2000, 2012, Oracle and/or its affiliates.
|
|
Copyright (c) 2010, 2011 Monty Program Ab
|
|
Copyright (C) 2013 Sergey Vojtovich and MariaDB Foundation
|
|
|
|
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 */
|
|
|
|
|
|
#include "table.h"
|
|
|
|
struct Share_free_tables
|
|
{
|
|
typedef I_P_List <TABLE, TABLE_share> List;
|
|
List list;
|
|
/** Avoid false sharing between instances */
|
|
char pad[CPU_LEVEL1_DCACHE_LINESIZE];
|
|
};
|
|
|
|
|
|
struct TDC_element
|
|
{
|
|
uchar m_key[NAME_LEN + 1 + NAME_LEN + 1];
|
|
uint m_key_length;
|
|
bool flushed;
|
|
TABLE_SHARE *share;
|
|
|
|
/**
|
|
Protects ref_count, m_flush_tickets, all_tables, flushed, all_tables_refs.
|
|
*/
|
|
mysql_mutex_t LOCK_table_share;
|
|
mysql_cond_t COND_release;
|
|
TDC_element *next, **prev; /* Link to unused shares */
|
|
uint ref_count; /* How many TABLE objects uses this */
|
|
uint all_tables_refs; /* Number of refs to all_tables */
|
|
/**
|
|
List of tickets representing threads waiting for the share to be flushed.
|
|
*/
|
|
Wait_for_flush_list m_flush_tickets;
|
|
/*
|
|
Doubly-linked (back-linked) lists of used and unused TABLE objects
|
|
for this share.
|
|
*/
|
|
All_share_tables_list all_tables;
|
|
/** Avoid false sharing between TDC_element and free_tables */
|
|
char pad[CPU_LEVEL1_DCACHE_LINESIZE];
|
|
Share_free_tables free_tables[1];
|
|
|
|
inline void wait_for_refs(uint my_refs);
|
|
void flush(THD *thd, bool mark_flushed);
|
|
void flush_unused(bool mark_flushed);
|
|
};
|
|
|
|
|
|
extern ulong tdc_size;
|
|
extern ulong tc_size;
|
|
extern uint32 tc_instances;
|
|
|
|
extern bool tdc_init(void);
|
|
extern void tdc_start_shutdown(void);
|
|
extern void tdc_deinit(void);
|
|
extern ulong tdc_records(void);
|
|
extern void tdc_purge(bool all);
|
|
extern TDC_element *tdc_lock_share(THD *thd, const char *db,
|
|
const char *table_name);
|
|
extern void tdc_unlock_share(TDC_element *element);
|
|
int tdc_share_is_cached(THD *thd, const char *db, const char *table_name);
|
|
extern TABLE_SHARE *tdc_acquire_share(THD *thd, TABLE_LIST *tl, uint flags,
|
|
TABLE **out_table= 0);
|
|
extern void tdc_release_share(TABLE_SHARE *share);
|
|
void tdc_remove_referenced_share(THD *thd, TABLE_SHARE *share);
|
|
void tdc_remove_table(THD *thd, const char *db, const char *table_name);
|
|
|
|
extern int tdc_wait_for_old_version(THD *thd, const char *db,
|
|
const char *table_name,
|
|
ulong wait_timeout, uint deadlock_weight);
|
|
extern int tdc_iterate(THD *thd, my_hash_walk_action action, void *argument,
|
|
bool no_dups= false);
|
|
|
|
extern uint tc_records(void);
|
|
int show_tc_active_instances(THD *thd, SHOW_VAR *var, void *buff,
|
|
system_status_var *, enum enum_var_type scope);
|
|
extern void tc_purge();
|
|
extern void tc_add_table(THD *thd, TABLE *table);
|
|
extern void tc_release_table(TABLE *table);
|
|
extern TABLE *tc_acquire_table(THD *thd, TDC_element *element);
|
|
|
|
|
|
class Share_lock
|
|
{
|
|
public:
|
|
TDC_element *element;
|
|
|
|
Share_lock(THD *thd, const char *db, const char *table)
|
|
{
|
|
element= tdc_lock_share(thd, db, table);
|
|
}
|
|
~Share_lock()
|
|
{
|
|
if (element)
|
|
tdc_unlock_share(element);
|
|
}
|
|
};
|
|
|
|
|
|
class Share_acquire
|
|
{
|
|
public:
|
|
bool flush_unused;
|
|
TABLE_SHARE *share;
|
|
|
|
Share_acquire() : flush_unused(false), share(NULL) {}
|
|
template <class TABLE_NAME>
|
|
Share_acquire(THD *thd, TABLE_NAME &tn, uint flags= 0) : flush_unused(false)
|
|
{
|
|
acquire(thd, tn, flags);
|
|
}
|
|
Share_acquire(const Share_acquire &src)= delete;
|
|
|
|
// NB: noexcept is required for STL containers
|
|
Share_acquire(Share_acquire &&src) noexcept :
|
|
flush_unused(src.flush_unused), share(src.share)
|
|
{
|
|
src.share= NULL;
|
|
}
|
|
~Share_acquire();
|
|
bool fk_error(THD *thd, bool use_check_foreign= true) const;
|
|
void acquire(THD *thd, TABLE_LIST &tl, uint flags= 0);
|
|
void acquire(THD *thd, Table_name &tn, uint flags= 0)
|
|
{
|
|
TABLE_LIST tl;
|
|
tl.init_one_table(&tn.db, &tn.name, NULL, TL_IGNORE);
|
|
return acquire(thd, tl, flags);
|
|
}
|
|
void release()
|
|
{
|
|
if (share)
|
|
{
|
|
if (flush_unused)
|
|
share->tdc->flush_unused(true);
|
|
tdc_release_share(share);
|
|
share= NULL;
|
|
}
|
|
}
|
|
};
|
|
|
|
|
|
struct Share_map: public mbd::map<Table_name, Share_acquire, Table_name_lt>
|
|
{};
|
|
|
|
|
|
/**
|
|
Create a table cache key for non-temporary table.
|
|
|
|
@param key Buffer for key (must be at least NAME_LEN*2+2 bytes).
|
|
@param db Database name.
|
|
@param table_name Table name.
|
|
|
|
@return Length of key.
|
|
*/
|
|
|
|
inline uint tdc_create_key(char *key, const char *db, const char *table_name)
|
|
{
|
|
/*
|
|
In theory caller should ensure that both db and table_name are
|
|
not longer than NAME_LEN bytes. In practice we play safe to avoid
|
|
buffer overruns.
|
|
*/
|
|
return (uint) (strmake(strmake(key, db, NAME_LEN) + 1, table_name,
|
|
NAME_LEN) - key + 1);
|
|
}
|
|
#endif /* TABLE_CACHE_H_INCLUDED */
|