mariadb/include
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
..
atomic MDEV-25870 followup - some Windows ARM64 improvements 2023-09-24 11:20:38 +02:00
mysql MDEV-36554 addendum: Assertion `is_wsrep() == wsrep_on(mysql_thd)' failed in void trx_t::commit_in_memory(const mtr_t*) 2025-08-04 14:58:18 +02:00
providers support lzma < 5.1.3alpha 2022-02-09 19:13:52 +01:00
aligned.h Revert aligned_alloc() addition from MDEV-28836 2022-08-22 09:10:40 +03:00
aria_backup.h S3 is pluggable now 2020-06-19 19:43:07 +02:00
assume_aligned.h Remove a misleading copyright message 2020-03-21 10:25:54 +02:00
big_endian.h MDEV-34970 Vector search fails to compile on s390x 2024-11-05 14:00:50 -08:00
byte_order_generic.h Fix all warnings given by UBSAN 2021-04-20 12:30:09 +03:00
byte_order_generic_x86.h Fix all warnings given by UBSAN 2021-04-20 12:30:09 +03:00
byte_order_generic_x86_64.h typo fixed. HAVE_mi_uint8korr 2023-11-02 19:42:39 +11:00
CMakeLists.txt MDEV-34348: Consolidate cmp function declarations 2024-11-23 08:14:22 -07:00
decimal.h Added typedef decimal_digits_t (uint16) for number of digits in most 2021-05-19 22:27:27 +02:00
dur_prop.h
errmsg.h test cases for MySQL bugs 2022-01-21 16:02:34 +01:00
ft_global.h MDEV-31340 Remove MY_COLLATION_HANDLER::strcasecmp() 2024-04-18 15:22:10 +04:00
handler_ername.h MDEV-9101 forgotten symbolic error representation 2025-03-06 10:00:03 +01:00
handler_state.h
hash.h Merge 11.4 into 11.7 2024-12-02 17:51:17 +02:00
heap.h MDEV-35469 Heap tables are calling mallocs to often 2025-01-05 16:40:11 +02:00
ilist.h Merge 10.6 into 10.8 2023-02-10 13:43:53 +02:00
json_lib.h Merge branch '11.8' into 12.0 2025-05-22 09:22:55 +02:00
keycache.h Merge branch '5.5' into 10.1 2019-05-11 22:19:05 +03:00
lf.h Fix typos in C comments in miscellaneous files 2025-03-24 13:36:28 +11:00
little_endian.h Update FSF address 2019-05-11 19:25:02 +03:00
m_ctype.h Cleanup#1 for MDEV-34319: DECLARE TYPE .. TABLE OF .. INDEX BY 2025-08-01 18:03:20 +02:00
m_string.h MDEV-31340 fixup: clang++-20 -Wdeprecated-literal-operator 2024-12-02 10:44:06 +02:00
ma_dyncol.h Merge 10.4 into 10.5 2023-01-03 17:08:42 +02:00
maria.h MDEV-24 Segmented key cache for Aria 2025-06-25 17:59:45 +03:00
mariadb_capi_rename.h more C API methods in the service_sql 2025-01-28 19:31:29 +01:00
my_alloc.h memroot improvement: fix savepoint support 2024-11-05 14:00:47 -08:00
my_alloca.h Merge branch '10.4' into 10.5 2023-04-24 12:43:47 +02:00
my_atomic.h MDEV-17441 fixup: Remove unused my_atomic long macros 2022-03-24 09:53:52 +02:00
my_atomic_wrapper.h Merge 10.5 into 10.6 2023-02-10 13:03:01 +02:00
my_attribute.h Get debug version to compile with gcc 7.5.0 2025-06-02 14:02:53 +03:00
my_base.h MDEV-20865 Store foreign key info in TABLE_SHARE 2025-09-02 13:24:36 +03:00
my_bit.h cleanup: spaces, casts, comments 2024-11-05 14:00:47 -08:00
my_bitmap.h Merge 10.6 into 10.11 2024-03-28 09:16:57 +02:00
my_byteorder.h Fix typos in C comments in miscellaneous files 2025-03-24 13:36:28 +11:00
my_check_opt.h Merge branch '5.5' into 10.1 2019-05-11 22:19:05 +03:00
my_cmp.h MDEV-34348: Consolidate cmp function declarations 2024-11-23 08:14:22 -07:00
my_compare.h Fix typos in C comments in miscellaneous files 2025-03-24 13:36:28 +11:00
my_compiler.h MDEV-33748 get rid of pthread_(get_/set_)specific, use thread_local 2024-06-21 13:46:41 +02:00
my_counter.h Apply clang-tidy to remove empty constructors / destructors 2023-02-09 16:09:08 +02:00
my_cpu.h Fix building with Clang and GCC on RISC-V 2025-03-21 18:28:36 +01:00
my_crypt.h Merge branch '5.5' into 10.1 2019-05-11 22:19:05 +03:00
my_dbug.h Reënable ATTRIBUTE_FORMAT on DBUG_PRINT & t/eprint 2025-02-12 10:17:44 +01:00
my_decimal_limits.h header typos 2022-12-20 08:55:48 +11:00
my_default.h MDEV-22214 mariadbd.exe calls function mysqld.exe, and crashes 2020-04-10 19:05:26 +02:00
my_dir.h Merge 10.6 into 10.7 2023-01-04 14:52:25 +02:00
my_getopt.h Fix typos in C comments in miscellaneous files 2025-03-24 13:36:28 +11:00
my_global.h MDEV-20865 Cleanups: dead code, typo. 2025-09-02 13:24:36 +03:00
my_handler_errors.h Merge 11.4 into 11.7 2025-01-09 09:41:38 +02:00
my_libwrap.h Update FSF Address 2019-05-11 21:29:06 +03:00
my_list.h Update FSF Address 2019-05-11 21:29:06 +03:00
my_md5.h Merge branch '5.5' into 10.1 2019-05-11 22:19:05 +03:00
my_minidump.h MDEV-11499 mysqltest, Windows : improve diagnostics if server fails to shutdown 2021-09-24 11:49:28 +02:00
my_net.h MDEV-25602 get rid of __WIN__ in favor of standard _WIN32 2021-06-06 13:21:03 +02:00
my_nosys.h Update FSF Address 2019-05-11 21:29:06 +03:00
my_pthread.h Merge branch '11.4' into 11.8 2025-06-18 07:43:24 +02:00
my_rdtsc.h MDEV-34815 SIGILL error when executing mariadbd compiled for RISC-V with Clang 2024-12-05 02:36:25 +11:00
my_rnd.h remove dead code 2022-07-31 14:54:37 +02:00
my_service_manager.h Merge branch '10.11' into 11.4 2025-07-28 19:40:10 +02:00
my_stack_alloc.h MDEV-36412 Concerns compilation issue on community edition for x86_64 with X32 ABI 2025-04-19 11:03:43 +03:00
my_stacktrace.h MDEV-25602 get rid of __WIN__ in favor of standard _WIN32 2021-06-06 13:21:03 +02:00
my_sys.h Merge branch '11.8' into 12.0 2025-05-22 09:22:55 +02:00
my_time.h Merge branch '11.4' into 11.8 2025-06-18 07:43:24 +02:00
my_tracker.h Changing all cost calculation to be given in milliseconds 2023-02-02 23:54:45 +03:00
my_tree.h Merge 10.2 into 10.3 2019-05-14 17:18:46 +03:00
my_uctype.h Update FSF Address 2019-05-11 21:29:06 +03:00
my_user.h Merge 10.2 into 10.3 2019-05-14 17:18:46 +03:00
my_valgrind.h Merge branch '11.8' into 12.0 2025-07-31 20:55:47 +02:00
my_virtual_mem.h MDEV-36780: InnoDB buffer pool reserves all assigned memory 2025-05-13 12:27:42 +03:00
my_xml.h Fix typos in C comments in miscellaneous files 2025-03-24 13:36:28 +11:00
myisam.h Merge 11.4 into 11.7 2024-12-02 17:51:17 +02:00
myisamchk.h MDEV-33751 Assertion `thd' failed in int temp_file_size_cb_func(tmp_file_tracking*, int) 2024-05-27 12:39:04 +02:00
myisammrg.h MDEV-31083 ASAN use-after-poison in myrg_attach_children 2023-05-23 09:16:36 +03:00
myisampack.h cleanup: remove unconditional #ifdef's 2024-11-05 14:00:47 -08:00
mysql.h fix error messages 2025-05-02 13:56:25 +02:00
mysql_com.h MDEV-33145 support for old-mode=OLD_FLUSH_STATUS 2024-05-27 12:39:03 +02:00
mysql_com_server.h Merge branch '5.5' into 10.1 2019-05-11 22:19:05 +03:00
mysql_embed.h Merge branch '10.3' into 10.4 2019-05-19 20:55:37 +02:00
mysql_time.h Extends 64 bit windows to support timestamps up to year 2106. 2024-05-27 12:39:02 +02:00
mysql_version.h.in Merge 10.3 into 10.4 2020-10-29 13:38:38 +02:00
mysqld_default_groups.h Fixed that mariadb-# binaries reads their corresponding entry from my.cnf 2019-07-18 15:32:22 +03:00
mysys_err.h MDEV-9101 Limit size of created disk temporary files and tables 2024-05-27 12:39:04 +02:00
no_valgrind_without_big.inc MDEV-31893 Valgrind reports issues in main.join_cache_notasan 2023-08-10 20:57:42 +02:00
pack.h
password.h Merge 10.2 into 10.3 2019-05-14 17:18:46 +03:00
pfs_file_provider.h perfschema compilation, test and misc fixes 2020-03-10 19:24:23 +01:00
pfs_idle_provider.h P_S 5.7.28 2020-03-10 19:24:22 +01:00
pfs_memory_provider.h P_S 5.7.28 2020-03-10 19:24:22 +01:00
pfs_metadata_provider.h P_S 5.7.28 2020-03-10 19:24:22 +01:00
pfs_socket_provider.h P_S 5.7.28 2020-03-10 19:24:22 +01:00
pfs_stage_provider.h P_S 5.7.28 2020-03-10 19:24:22 +01:00
pfs_statement_provider.h P_S 5.7.28 2020-03-10 19:24:22 +01:00
pfs_table_provider.h P_S 5.7.28 2020-03-10 19:24:22 +01:00
pfs_thread_provider.h MDEV-32537 Name threads to improve debugging experience and diagnostics. 2024-07-09 13:17:20 +02:00
pfs_transaction_provider.h cleanup: pass trxid by value 2020-03-10 19:24:23 +01:00
probes_mysql.d.base Update FSF Address 2019-05-11 21:29:06 +03:00
probes_mysql.h Update FSF Address 2019-05-11 21:29:06 +03:00
probes_mysql_nodtrace.h.in
queues.h Merge 11.4 into 11.7 2024-12-02 17:51:17 +02:00
rijndael.h Update FSF Address 2019-05-11 21:29:06 +03:00
scope.h Merge branch '11.4' into 11.7 2025-02-06 16:46:36 +01:00
service_versions.h MDEV-34680 Asynchronous and Buffered Logging for Audit Plugin. 2025-08-01 18:00:49 +02:00
source_revision.h.in
span.h Apply clang-tidy to remove empty constructors / destructors 2023-02-09 16:09:08 +02:00
sql_common.h Tag rest of my_vsnprintf users w/ ATTRIBUTE_FORMAT 2025-02-12 10:17:44 +01:00
ssl_compat.h MDEV-35581 On servers linked against WolfSSL SSL_Cipher and SSL_cipher_list are always the same 2025-07-17 09:18:17 +02:00
sslopt-case.h client support for --ssl-fp and --ssl--fplist 2024-02-04 22:19:00 +01:00
sslopt-longopts.h MDEV-28908 Confusing documentation and help output for --ssl-verify-server-cert 2025-03-31 17:57:40 +02:00
sslopt-vars.h MDEV-32473 --disable-ssl doesn't disable it 2024-02-04 22:19:19 +01:00
t_ctype.h Update FSF Address 2019-05-11 21:29:06 +03:00
thr_lock.h MDEV-36425 Extend read_only to also block share locks and super user 2025-04-28 12:59:39 +03:00
thr_timer.h MDEV-16264 - prerequisite patch, periodic thr_timer 2019-11-15 16:50:22 +01:00
typelib.h cleanup: CREATE_TYPELIB_FOR() helper 2024-11-05 14:00:47 -08:00
violite.h MDEV-14091 Support password protected SSL key in server. 2025-04-19 14:04:10 +03:00
waiting_threads.h header typos 2022-12-20 08:55:48 +11:00
welcome_copyright_notice.h MDEV-30153 ad hoc client versions are confusing 2023-01-19 12:39:37 +01:00
wqueue.h Merge 10.2 into 10.3 2019-05-14 17:18:46 +03:00
wsrep.h Reduce compilation dependencies on wsrep_mysqld.h 2022-08-31 11:05:23 +03:00