mirror of
https://github.com/MariaDB/server.git
synced 2025-12-16 09:15:43 +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
265 lines
7 KiB
Text
265 lines
7 KiB
Text
--source include/have_innodb.inc
|
|
--source include/not_embedded.inc
|
|
|
|
--echo #
|
|
--echo # Bug #18806829 OPENING INNODB TABLES WITH MANY FOREIGN KEY
|
|
--echo # REFERENCES IS SLOW/CRASHES SEMAPHORE
|
|
--echo #
|
|
|
|
create table t1 (f1 int primary key) engine=innodb;
|
|
insert into t1 values (5);
|
|
insert into t1 values (2882);
|
|
insert into t1 values (10);
|
|
|
|
let $fk_tables = 120;
|
|
|
|
--disable_query_log
|
|
let $i = $fk_tables;
|
|
while ($i)
|
|
{
|
|
eval create table fk_$i (f1 int primary key,
|
|
constraint pc$i foreign key (f1) references t1(f1)
|
|
on delete cascade on update cascade) engine=innodb;
|
|
eval insert into fk_$i values (5);
|
|
eval insert into fk_$i values (2882);
|
|
eval insert into fk_$i values (10);
|
|
dec $i;
|
|
}
|
|
--enable_query_log
|
|
|
|
--source include/restart_mysqld.inc
|
|
|
|
update t1 set f1 = 28 where f1 = 2882;
|
|
|
|
select * from fk_120;
|
|
select * from fk_1;
|
|
select * from fk_50;
|
|
|
|
--disable_query_log
|
|
let $i = $fk_tables;
|
|
while ($i)
|
|
{
|
|
eval drop table fk_$i;
|
|
dec $i;
|
|
}
|
|
--enable_query_log
|
|
|
|
drop table t1;
|
|
|
|
--echo #
|
|
--echo # Check if restrict is working fine.
|
|
--echo #
|
|
|
|
create table t1 (f1 int primary key) engine=innodb;
|
|
|
|
let $fk_tables = 30;
|
|
|
|
--disable_query_log
|
|
let $i = $fk_tables;
|
|
while ($i)
|
|
{
|
|
eval create table fk_$i (f1 int primary key,
|
|
constraint pc$i foreign key (f1) references t1(f1)
|
|
on delete restrict on update restrict) engine=innodb;
|
|
eval insert into t1 values ($i);
|
|
eval insert into fk_$i values ($i);
|
|
dec $i;
|
|
}
|
|
--enable_query_log
|
|
|
|
--source include/restart_mysqld.inc
|
|
|
|
--error ER_ROW_IS_REFERENCED_2
|
|
delete from t1 where f1 = 29;
|
|
select * from fk_29;
|
|
|
|
--disable_query_log
|
|
let $i = $fk_tables;
|
|
while ($i)
|
|
{
|
|
eval drop table fk_$i;
|
|
dec $i;
|
|
}
|
|
--enable_query_log
|
|
|
|
drop table t1;
|
|
|
|
#
|
|
# MDEV-7672: Crash creating an InnoDB table with foreign keys
|
|
#
|
|
|
|
CREATE TABLE t1 (
|
|
id int(11) NOT NULL AUTO_INCREMENT,
|
|
f1 int(11) DEFAULT NULL,
|
|
PRIMARY KEY (id),
|
|
CONSTRAINT fk1 FOREIGN KEY (f1) REFERENCES t1 (id) ON DELETE CASCADE
|
|
) ENGINE=InnoDB;
|
|
|
|
--error ER_NO_SUCH_TABLE
|
|
CREATE TABLE t2 (
|
|
id int(11) NOT NULL AUTO_INCREMENT,
|
|
f2 int(11) NOT NULL,
|
|
f3 int(11) NOT NULL,
|
|
PRIMARY KEY (`id`),
|
|
CONSTRAINT fk2 FOREIGN KEY (f2) REFERENCES t1 (`id`) ON DELETE CASCADE,
|
|
CONSTRAINT fk3 FOREIGN KEY (f3) REFERENCES t3 (id) ON DELETE CASCADE
|
|
) ENGINE=InnoDB;
|
|
|
|
show warnings;
|
|
|
|
CREATE TABLE t2 (
|
|
id int(11) NOT NULL AUTO_INCREMENT,
|
|
f2 int(11) NOT NULL,
|
|
f3 int(11) NOT NULL,
|
|
PRIMARY KEY (`id`),
|
|
CONSTRAINT fk2 FOREIGN KEY (f2) REFERENCES t1 (`id`) ON DELETE CASCADE
|
|
) ENGINE=InnoDB;
|
|
|
|
--error ER_NO_SUCH_TABLE
|
|
ALTER TABLE t2 ADD CONSTRAINT fk3 FOREIGN KEY (f3) REFERENCES t3 (id) ON DELETE CASCADE;
|
|
|
|
show warnings;
|
|
|
|
drop table t2;
|
|
drop table t1;
|
|
|
|
#
|
|
# MDEV-9142 :Adding Constraint with no database reference
|
|
# results in ERROR 1046 (3D000) at line 13: No database selected
|
|
#
|
|
CREATE DATABASE kg_test1;
|
|
CREATE DATABASE kg_test2;
|
|
|
|
CREATE TABLE `kg_test1`.`group` (
|
|
Id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
|
|
CREATE TABLE `kg_test1`.`person` (
|
|
`Id` INT(11) NOT NULL AUTO_INCREMENT,
|
|
`Name` VARCHAR(50) NOT NULL,
|
|
PRIMARY KEY (`Id`),
|
|
CONSTRAINT `fk_person_group` FOREIGN KEY (`Id`) REFERENCES `group` (`Id`)
|
|
) ENGINE=INNODB DEFAULT CHARSET=utf8;
|
|
|
|
show create table `kg_test1`.`person`;
|
|
|
|
--error ER_NO_SUCH_TABLE
|
|
CREATE TABLE `kg_test2`.`person2` (
|
|
`Id` INT(11) NOT NULL AUTO_INCREMENT,
|
|
`Name` VARCHAR(50) NOT NULL,
|
|
PRIMARY KEY (`Id`),
|
|
CONSTRAINT `fk_person_group` FOREIGN KEY (`Id`) REFERENCES `group` (`Id`)
|
|
) ENGINE=INNODB DEFAULT CHARSET=utf8;
|
|
|
|
CREATE TABLE `kg_test2`.`person2` (
|
|
`Id` INT(11) NOT NULL AUTO_INCREMENT,
|
|
`Name` VARCHAR(50) NOT NULL,
|
|
PRIMARY KEY (`Id`),
|
|
CONSTRAINT `fk_person_group` FOREIGN KEY (`Id`) REFERENCES `kg_test1`.`group` (`Id`)
|
|
) ENGINE=INNODB DEFAULT CHARSET=utf8;
|
|
|
|
show create table `kg_test2`.`person2`;
|
|
|
|
SHOW WARNINGS;
|
|
DROP DATABASE kg_test2;
|
|
DROP DATABASE kg_test1;
|
|
|
|
#
|
|
# MDEV-7627: Some symbols in table name can cause to Error Code: 1050 when created FK
|
|
#
|
|
|
|
CREATE TABLE `#departaments` (
|
|
`id_depart` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
PRIMARY KEY (`id_depart`)
|
|
) ENGINE=INNODB DEFAULT CHARSET=utf8;
|
|
|
|
CREATE TABLE `#departaments_tree` (
|
|
`id_depart` INT(10) UNSIGNED NOT NULL,
|
|
`id_depart_in` INT(10) UNSIGNED NOT NULL,
|
|
PRIMARY KEY (`id_depart`,`id_depart_in`),
|
|
CONSTRAINT `#departaments_tree_ibfk_1` FOREIGN KEY (`id_depart`) REFERENCES `#departaments` (`id_depart`)
|
|
) ENGINE=INNODB DEFAULT CHARSET=utf8;
|
|
|
|
ALTER TABLE `#departaments_tree`
|
|
ADD FOREIGN KEY (`id_depart_in`) REFERENCES `#departaments`(`id_depart`);
|
|
|
|
SHOW CREATE TABLE `#departaments_tree`;
|
|
|
|
DROP TABLE `#departaments_tree`;
|
|
DROP TABLE `#departaments`;
|
|
|
|
CREATE TABLE `boroda` (
|
|
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
`a` INT(11) UNSIGNED DEFAULT NULL,
|
|
`b` INT(11) UNSIGNED DEFAULT NULL,
|
|
PRIMARY KEY (`id`),
|
|
KEY `a` (`a`),
|
|
CONSTRAINT `1` FOREIGN KEY (`a`) REFERENCES `boroda` (`id`)
|
|
) ENGINE=INNODB DEFAULT CHARSET=utf8;
|
|
|
|
ALTER TABLE `boroda`
|
|
ADD FOREIGN KEY (`b`) REFERENCES `boroda`(`id`);
|
|
|
|
ALTER TABLE `boroda` DROP FOREIGN KEY `2`;
|
|
|
|
RENAME TABLE `boroda` TO `#boroda`;
|
|
|
|
ALTER TABLE `#boroda`
|
|
ADD FOREIGN KEY (`b`) REFERENCES `#boroda`(`id`);
|
|
|
|
SHOW CREATE TABLE `#boroda`;
|
|
DROP TABLE `#boroda`;
|
|
|
|
CREATE TABLE `boroda` (
|
|
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
`a` INT(11) UNSIGNED DEFAULT NULL,
|
|
`b` INT(11) UNSIGNED DEFAULT NULL,
|
|
PRIMARY KEY (`id`),
|
|
KEY `a` (`a`),
|
|
CONSTRAINT `1` FOREIGN KEY (`a`) REFERENCES `boroda` (`id`)
|
|
) ENGINE=INNODB DEFAULT CHARSET=utf8;
|
|
|
|
RENAME TABLE `boroda` TO `bor#oda`;
|
|
|
|
ALTER TABLE `bor#oda`
|
|
ADD FOREIGN KEY (`b`) REFERENCES `bor#oda`(`id`);
|
|
|
|
SHOW CREATE TABLE `bor#oda`;
|
|
DROP TABLE `bor#oda`;
|
|
|
|
--echo #
|
|
--echo # MDEV-21127 Assertion `(size_t)(ptr - buf) < MAX_TEXT - 4' failed in key_text::key_text
|
|
--echo #
|
|
create table tx (ax char(255) unique, bx char(255) unique) engine innodb;
|
|
--error ER_FK_NO_INDEX_PARENT
|
|
CREATE TABLE t1 (
|
|
a012345678901234567890123456789012345678901 char(255),
|
|
b char(255),
|
|
FOREIGN KEY ( a012345678901234567890123456789012345678901, b ) REFERENCES tx (ax, bx)
|
|
) ENGINE=InnoDB;
|
|
|
|
--error ER_WRONG_FK_DEF
|
|
CREATE TABLE t1 (
|
|
a012345678901234567 int,
|
|
b int,
|
|
c0123456789012345678 int,
|
|
FOREIGN KEY (a012345678901234567,c0123456789012345678,b) REFERENCES tx (x1,x2,x3)
|
|
) ENGINE=InnoDB;
|
|
|
|
drop table tx;
|
|
|
|
--echo #
|
|
--echo # MDEV-25642 InnoDB rename table copy DDL fails
|
|
--echo # while dropping the table
|
|
--echo #
|
|
call mtr.add_suppression("InnoDB: In ALTER TABLE `test`.`t1` has or is referenced in foreign key constraints which are not compatible with the new table definition.");
|
|
|
|
CREATE TABLE t1 (a VARCHAR(10) NOT NULL PRIMARY KEY) ENGINE=InnoDB;
|
|
CREATE TABLE t1_fk (a VARCHAR(40), KEY a (a), FULLTEXT KEY(a), CONSTRAINT fk FOREIGN KEY(a) REFERENCES t1 (a) ON UPDATE CASCADE) ENGINE=InnoDB;
|
|
ALTER TABLE t1 RENAME TO tm1, ALGORITHM=COPY;
|
|
SELECT * FROM INFORMATION_SCHEMA.INNODB_SYS_FOREIGN;
|
|
# Enable SET FOREIGN_KEY_CHECKS after fixing MDEV-25885
|
|
SET FOREIGN_KEY_CHECKS=0;
|
|
CREATE TABLE t1 (c1 BIGINT NOT NULL, c2 BIGINT NOT NULL, PRIMARY KEY(c1), UNIQUE KEY(c2)) ENGINE=MEMORY;
|
|
ALTER TABLE t1 ENGINE=InnoDB, ALGORITHM=COPY;
|
|
DROP TABLE t1, tm1, t1_fk;
|