mariadb/storage/spider
Aleksey Midenkov a518e1dd42 MDEV-20865 Store foreign key info in TABLE_SHARE
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
2025-09-02 13:24:36 +03:00
..
mysql-test/spider Merge branch '12.0' into 12.1 2025-08-03 15:01:09 +02:00
CMakeLists.txt Fix broken clang-cl compilation 2025-04-04 08:58:40 +02:00
ha_spider.cc MDEV-27474 Spider: remove #WITH_PARTITION_STORAGE_ENGINE 2025-07-14 11:23:51 +10:00
ha_spider.h MDEV-27474 Spider: remove #WITH_PARTITION_STORAGE_ENGINE 2025-07-14 11:23:51 +10:00
spd_conn.cc MDEV-27474 Spider: remove #WITH_PARTITION_STORAGE_ENGINE 2025-07-14 11:23:51 +10:00
spd_conn.h Merge branch '10.6' into 10.11 2025-01-30 11:55:13 +01:00
spd_copy_tables.cc MDEV-27474 Spider: remove #WITH_PARTITION_STORAGE_ENGINE 2025-07-14 11:23:51 +10:00
spd_copy_tables.h MDEV-27642 Spider: remove #ifdef WITHOUT_SPIDER_BG_SEARCH 2022-02-10 14:58:26 +09:00
spd_db_conn.cc Merge branch '10.11' into 11.4 2025-07-28 19:40:10 +02:00
spd_db_conn.h Merge branch '10.5' into 10.6 2025-01-29 11:17:38 +01:00
spd_db_include.cc Merge branch '10.6' into 10.11 2024-09-11 16:10:53 +10:00
spd_db_include.h Merge branch '11.8' into 12.0 2025-07-31 20:55:47 +02:00
spd_db_mysql.cc Merge branch '11.4' into 11.8 2025-07-28 21:29:29 +02:00
spd_db_mysql.h Merge branch '10.11' into 11.4 2025-07-28 19:40:10 +02:00
spd_direct_sql.cc Merge 10.11 into 11.4 2025-03-28 13:55:21 +02:00
spd_direct_sql.h MDEV-27642 Spider: remove #ifdef WITHOUT_SPIDER_BG_SEARCH 2022-02-10 14:58:26 +09:00
spd_err.h MDEV-32627 Spider: use CONNECTION string in SQLDriverConnect 2024-08-28 16:43:07 +10:00
spd_group_by_handler.cc MDEV-27474 Spider: remove #WITH_PARTITION_STORAGE_ENGINE 2025-07-14 11:23:51 +10:00
spd_group_by_handler.h Merge branch '10.6' into 10.11 2024-10-29 15:24:38 +01:00
spd_i_s.cc Merge branch '10.6' into 10.11 2024-09-11 16:10:53 +10:00
spd_include.h MDEV-27474 Spider: remove #WITH_PARTITION_STORAGE_ENGINE 2025-07-14 11:23:51 +10:00
spd_init_query.h Merge branch '10.6' into 10.11 2024-05-10 20:02:18 +02:00
spd_malloc.cc Merge branch '11.2' into 11.4 2024-09-18 11:27:53 +10:00
spd_malloc.h MDEV-20179 Server hangs on shutdown during installation of Spider (#1368) 2019-07-30 06:16:48 +09:00
spd_param.cc MDEV-27474 Spider: remove #WITH_PARTITION_STORAGE_ENGINE 2025-07-14 11:23:51 +10:00
spd_param.h MDEV-27474 Spider: remove #WITH_PARTITION_STORAGE_ENGINE 2025-07-14 11:23:51 +10:00
spd_ping_table.cc MDEV-27474 Spider: remove #WITH_PARTITION_STORAGE_ENGINE 2025-07-14 11:23:51 +10:00
spd_ping_table.h
spd_sys_table.cc MDEV-20865 Store foreign key info in TABLE_SHARE 2025-09-02 13:24:36 +03:00
spd_sys_table.h Merge branch '10.11' into 11.0 2024-05-12 12:18:28 +02:00
spd_table.cc Merge branch '11.4' into 11.8 2025-07-28 21:29:29 +02:00
spd_table.h Merge branch '10.11' into 11.4 2025-07-28 19:40:10 +02:00
spd_trx.cc MDEV-27474 Spider: remove #WITH_PARTITION_STORAGE_ENGINE 2025-07-14 11:23:51 +10:00
spd_trx.h Merge 11.4 into 11.8 2025-04-02 14:07:01 +03:00
spd_udf.cc Merge branch '10.6' into 10.11 2025-01-30 11:55:13 +01:00
spd_udf.def
spd_udf.h Merge branch '10.6' into 10.11 2025-01-30 11:55:13 +01:00
spider.cnf Unify config syntax in default files 2020-08-01 10:31:41 +10:00