mirror of
https://github.com/MariaDB/server.git
synced 2025-12-16 17:25:46 +01: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
118 lines
6.5 KiB
Text
118 lines
6.5 KiB
Text
CREATE TABLE t1 (
|
|
id int(11) NOT NULL PRIMARY KEY,
|
|
a int(11) NOT NULL,
|
|
b int(11) NOT NULL,
|
|
c int not null,
|
|
CONSTRAINT test FOREIGN KEY (b) REFERENCES t1 (id)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
|
|
CREATE TABLE t2 (
|
|
id int(11) NOT NULL PRIMARY KEY,
|
|
a int(11) NOT NULL,
|
|
b int(11) NOT NULL,
|
|
c int not null,
|
|
CONSTRAINT mytest FOREIGN KEY (c) REFERENCES t1(id),
|
|
CONSTRAINT test FOREIGN KEY (b) REFERENCES t2 (id)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
|
|
show warnings;
|
|
Level Code Message
|
|
drop table t2,t1;
|
|
create table t1(a int) engine=innodb;
|
|
create table t2(a int, constraint a foreign key a (a) references t1(a)) engine=innodb;
|
|
ERROR HY000: Missing index for constraint 't2.a' in the referenced table 't1'
|
|
show warnings;
|
|
Level Code Message
|
|
Error 1822 Missing index for constraint 't2.a' in the referenced table 't1'
|
|
drop table t1;
|
|
create table t1(a int unique, b int) engine=innodb;
|
|
create table t2(a int, b int, foreign key (a) references t1(a), foreign key (b) references t1(b)) engine=innodb;
|
|
ERROR HY000: Missing index for constraint 't2.2' in the referenced table 't1'
|
|
show warnings;
|
|
Level Code Message
|
|
Error 1822 Missing index for constraint 't2.2' in the referenced table 't1'
|
|
drop table t1;
|
|
create table t1(a int not null primary key, b int) engine=innodb;
|
|
create table t2(a int, b int, constraint a foreign key a (a) references t1(a),
|
|
constraint a foreign key a (a) references t1(b)) engine=innodb;
|
|
ERROR HY000: Duplicate FOREIGN KEY constraint name 'a'
|
|
show warnings;
|
|
Level Code Message
|
|
Error 1826 Duplicate FOREIGN KEY constraint name 'a'
|
|
create table t2(a int, b int, constraint a foreign key a (a) references t1(a)) engine=innodb;
|
|
alter table t2 add constraint b foreign key (b) references t2(b);
|
|
ERROR HY000: Can't create table `test`.`t2` (errno: 150 "Foreign key constraint is incorrectly formed")
|
|
show warnings;
|
|
Level Code Message
|
|
Warning 150 Alter table `test`.`t2` with foreign key `b` constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns.
|
|
Error 1005 Can't create table `test`.`t2` (errno: 150 "Foreign key constraint is incorrectly formed")
|
|
Warning 1215 Cannot add foreign key constraint for `t2`
|
|
drop table t2, t1;
|
|
create table t1 (f1 integer primary key) engine=innodb;
|
|
alter table t1 add constraint c1 foreign key (f1) references t11(f1);
|
|
ERROR 42S02: Table 'test.t11' doesn't exist
|
|
show warnings;
|
|
Level Code Message
|
|
drop table t1;
|
|
create temporary table t1(a int not null primary key, b int, key(b)) engine=innodb;
|
|
create temporary table t2(a int, foreign key(a) references t1(a)) engine=innodb;
|
|
ERROR HY000: Can't create table `test`.`t2` (errno: 150 "Foreign key constraint is incorrectly formed")
|
|
show warnings;
|
|
Level Code Message
|
|
Warning 150 Create table `mysqld.1`.`t2` with foreign key constraint failed. Referenced table `mysqld.1`.`t1` not found in the data dictionary close to foreign key(a) references t1(a)) engine=innodb.
|
|
Error 1005 Can't create table `test`.`t2` (errno: 150 "Foreign key constraint is incorrectly formed")
|
|
Warning 1215 Cannot add foreign key constraint
|
|
alter table t1 add foreign key(b) references t1(a);
|
|
ERROR HY000: Can't create table `test`.`#sql-temporary` (errno: 150 "Foreign key constraint is incorrectly formed")
|
|
show warnings;
|
|
Level Code Message
|
|
Warning 150 Alter table `mysqld.1`.`t1` with foreign key constraint failed. Referenced table `mysqld.1`.`t1` not found in the data dictionary close to foreign key(b) references t1(a).
|
|
Error 1005 Can't create table `test`.`#sql-temporary` (errno: 150 "Foreign key constraint is incorrectly formed")
|
|
Warning 1215 Cannot add foreign key constraint
|
|
create temporary table t2(a int, foreign key(a) references t1(a)) engine=innodb;
|
|
ERROR 42S02: Table 'test.t1' doesn't exist
|
|
show warnings;
|
|
Level Code Message
|
|
alter table t1 add foreign key(b) references t1(a);
|
|
ERROR HY000: Can't create table `test`.`t1` (errno: 150 "Foreign key constraint is incorrectly formed")
|
|
show warnings;
|
|
Level Code Message
|
|
Warning 150 Create table `test`.`t1` with foreign key constraint failed. Temporary tables can't have foreign key constraints.
|
|
Error 1005 Can't create table `test`.`t1` (errno: 150 "Foreign key constraint is incorrectly formed")
|
|
Warning 1215 Cannot add foreign key constraint for `t1`
|
|
drop table t1;
|
|
create table t1(a int not null primary key, b int, key(b)) engine=innodb;
|
|
alter table t1 add foreign key(a,b) references t1(a);
|
|
ERROR 42000: Incorrect foreign key definition for 'foreign key without name': Key reference and table reference don't match
|
|
show warnings;
|
|
Level Code Message
|
|
Error 1239 Incorrect foreign key definition for 'foreign key without name': Key reference and table reference don't match
|
|
drop table t1;
|
|
create table t1(a int not null primary key, b int, key(b)) engine=innodb;
|
|
alter table t1 add foreign key(a) references t1(a,b);
|
|
ERROR 42000: Incorrect foreign key definition for 'foreign key without name': Key reference and table reference don't match
|
|
show warnings;
|
|
Level Code Message
|
|
Error 1239 Incorrect foreign key definition for 'foreign key without name': Key reference and table reference don't match
|
|
drop table t1;
|
|
create table t1 (f1 integer not null primary key) engine=innodb;
|
|
alter table t1 add constraint c1 foreign key (f1) references t1(f1) on update set null;
|
|
ERROR HY000: Can't create table `test`.`t1` (errno: 150 "Foreign key constraint is incorrectly formed")
|
|
show warnings;
|
|
Level Code Message
|
|
Warning 150 Alter table `test`.`t1` with foreign key `c1` constraint failed. You have defined a SET NULL condition but column 'f1' is defined as NOT NULL.
|
|
Error 1005 Can't create table `test`.`t1` (errno: 150 "Foreign key constraint is incorrectly formed")
|
|
Warning 1215 Cannot add foreign key constraint for `t1`
|
|
create table t2(a int not null, foreign key(a) references t1(f1) on delete set null) engine=innodb;
|
|
ERROR HY000: Can't create table `test`.`t2` (errno: 150 "Foreign key constraint is incorrectly formed")
|
|
show warnings;
|
|
Level Code Message
|
|
Warning 150 Create table `test`.`t2` with foreign key `1` constraint failed. You have defined a SET NULL condition but column 'a' is defined as NOT NULL.
|
|
Error 1005 Can't create table `test`.`t2` (errno: 150 "Foreign key constraint is incorrectly formed")
|
|
Warning 1215 Cannot add foreign key constraint for `t2`
|
|
drop table t1;
|
|
create table t1 (id int not null primary key, f1 int, f2 int, key(f1)) engine=innodb;
|
|
create table t2(a char(20), key(a), foreign key(a) references t1(f1)) engine=innodb;
|
|
ERROR 42000: Incorrect foreign key definition for 'a': foreign-referenced fields type mismatch
|
|
show warnings;
|
|
Level Code Message
|
|
Error 1239 Incorrect foreign key definition for 'a': foreign-referenced fields type mismatch
|
|
drop table t1;
|