mirror of
https://github.com/MariaDB/server.git
synced 2025-12-17 01:35: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
311 lines
10 KiB
Text
311 lines
10 KiB
Text
--source include/have_innodb.inc
|
|
--source include/have_debug.inc
|
|
--source include/have_debug_sync.inc
|
|
|
|
--enable_connect_log
|
|
|
|
--echo #
|
|
--echo # Bug #19471516 SERVER CRASHES WHEN EXECUTING ALTER TABLE
|
|
--echo # ADD FOREIGN KEY
|
|
--echo #
|
|
|
|
CREATE TABLE `department` (`department_id` INT, `department_people_fk` INT,
|
|
PRIMARY KEY (`department_id`)) engine=innodb;
|
|
|
|
CREATE TABLE `title` (`title_id` INT, `title_manager_fk` INT,
|
|
`title_reporter_fk` INT, PRIMARY KEY (`title_id`)) engine=innodb;
|
|
|
|
CREATE TABLE `people` (`people_id` INT, PRIMARY KEY (`people_id`)) engine=innodb;
|
|
|
|
ALTER TABLE `department` ADD FOREIGN KEY (`department_people_fk`) REFERENCES
|
|
`people` (`people_id`);
|
|
|
|
ALTER TABLE `title` ADD FOREIGN KEY (`title_manager_fk`) REFERENCES `people`
|
|
(`people_id`);
|
|
|
|
ALTER TABLE `title` ADD FOREIGN KEY (`title_reporter_fk`) REFERENCES `people`
|
|
(`people_id`);
|
|
|
|
drop table title, department, people;
|
|
|
|
#
|
|
# FK and prelocking:
|
|
# child table accesses (reads and writes) wait for locks.
|
|
#
|
|
create table t1 (a int primary key, b int) engine=innodb;
|
|
create table t2 (c int primary key, d int,
|
|
foreign key (d) references t1 (a) on update cascade) engine=innodb;
|
|
insert t1 values (1,1),(2,2),(3,3);
|
|
insert t2 values (4,1),(5,2),(6,3);
|
|
flush table t2 with read lock; # this takes MDL_SHARED_NO_WRITE
|
|
connect (con1,localhost,root);
|
|
--error ER_ROW_IS_REFERENCED_2
|
|
delete from t1 where a=2;
|
|
send update t1 set a=10 where a=1;
|
|
connection default;
|
|
let $wait_condition= select 1 from information_schema.processlist where state='Waiting for table metadata lock';
|
|
source include/wait_condition.inc;
|
|
unlock tables;
|
|
connection con1;
|
|
reap;
|
|
connection default;
|
|
lock table t2 write; # this takes MDL_SHARED_NO_READ_WRITE
|
|
connection con1;
|
|
send delete from t1 where a=2;
|
|
connection default;
|
|
let $wait_condition= select 1 from information_schema.processlist where state='Waiting for table metadata lock';
|
|
source include/wait_condition.inc;
|
|
unlock tables;
|
|
connection con1;
|
|
--error ER_ROW_IS_REFERENCED_2
|
|
reap;
|
|
connection default;
|
|
unlock tables;
|
|
disconnect con1;
|
|
|
|
# but privileges should not be checked
|
|
create user foo;
|
|
grant select,update on test.t1 to foo;
|
|
connect(foo,localhost,foo);
|
|
update t1 set a=30 where a=3;
|
|
disconnect foo;
|
|
connection default;
|
|
select * from t2;
|
|
drop table t2, t1;
|
|
drop user foo;
|
|
|
|
#
|
|
# MDEV-16465 Invalid (old?) table or database name or hang in ha_innobase::delete_table and log semaphore wait upon concurrent DDL with foreign keys
|
|
#
|
|
create table t1 (f1 int primary key) engine=innodb;
|
|
create table t2 (f2 int primary key) engine=innodb;
|
|
create table t3 (f3 int primary key, foreign key (f3) references t2(f2)) engine=innodb;
|
|
insert into t1 values (1),(2),(3),(4),(5);
|
|
insert into t2 values (1),(2),(3),(4),(5);
|
|
insert into t3 values (1),(2),(3),(4),(5);
|
|
connect con1,localhost,root;
|
|
set debug_sync='alter_table_before_rename_result_table signal g1 wait_for g2';
|
|
send alter table t2 add constraint foreign key (f2) references t1(f1) on delete cascade on update cascade;
|
|
connection default;
|
|
let $conn=`select connection_id()`;
|
|
set debug_sync='before_execute_sql_command wait_for g1';
|
|
send update t1 set f1 = f1 + 100000 limit 2;
|
|
connect con2,localhost,root;
|
|
let $wait_condition= select 1 from information_schema.processlist where state='Waiting for table metadata lock' and info like 'update t1 %';
|
|
source include/wait_condition.inc;
|
|
--replace_result $conn UPDATE
|
|
eval kill query $conn;
|
|
disconnect con2;
|
|
connection default;
|
|
error ER_QUERY_INTERRUPTED;
|
|
reap;
|
|
set debug_sync='now signal g2';
|
|
connection con1;
|
|
reap;
|
|
show create table t2;
|
|
disconnect con1;
|
|
connection default;
|
|
select * from t2 where f2 not in (select f1 from t1);
|
|
select * from t3 where f3 not in (select f2 from t2);
|
|
drop table t3;
|
|
drop table t2;
|
|
drop table t1;
|
|
set debug_sync='reset';
|
|
|
|
|
|
--echo #
|
|
--echo # MDEV-17595 - Server crashes in copy_data_between_tables or
|
|
--echo # Assertion `thd->transaction.stmt.is_empty() ||
|
|
--echo # (thd->state_flags & Open_tables_state::BACKUPS_AVAIL)'
|
|
--echo # fails in close_tables_for_reopen upon concurrent
|
|
--echo # ALTER TABLE and FLUSH
|
|
--echo #
|
|
CREATE TABLE t1 (a INT, KEY(a)) ENGINE=InnoDB;
|
|
INSERT INTO t1 VALUES(1),(2);
|
|
CREATE TABLE t2 (b INT, KEY(b)) ENGINE=InnoDB;
|
|
INSERT INTO t2 VALUES(2);
|
|
ALTER TABLE t2 ADD FOREIGN KEY(b) REFERENCES t1(a), LOCK=EXCLUSIVE;
|
|
DROP TABLE t2, t1;
|
|
|
|
|
|
--echo #
|
|
--echo # MDEV-16060 - InnoDB: Failing assertion: ut_strcmp(index->name, key->name)
|
|
--echo #
|
|
CREATE TABLE t1 (`pk` INT PRIMARY KEY) ENGINE=InnoDB;
|
|
CREATE TABLE t2 LIKE t1;
|
|
FLUSH TABLES;
|
|
|
|
SET debug_sync='alter_table_intermediate_table_created SIGNAL ready WAIT_FOR go';
|
|
send ALTER TABLE t1 ADD FOREIGN KEY(pk) REFERENCES t2(pk) ON UPDATE CASCADE;
|
|
|
|
connect con1, localhost, root;
|
|
SET debug_sync='now WAIT_FOR ready';
|
|
SET lock_wait_timeout=0;
|
|
--error ER_LOCK_WAIT_TIMEOUT
|
|
UPDATE t2 SET pk=10 WHERE pk=1;
|
|
PREPARE stmt FROM 'UPDATE t2 SET pk=10 WHERE pk=1';
|
|
DEALLOCATE PREPARE stmt;
|
|
SET debug_sync='now SIGNAL go';
|
|
|
|
connection default;
|
|
reap;
|
|
|
|
# Cleanup
|
|
disconnect con1;
|
|
|
|
connection default;
|
|
SET debug_sync='reset';
|
|
SHOW OPEN TABLES FROM test;
|
|
DROP TABLE t1, t2;
|
|
|
|
#
|
|
# FK and prelocking:
|
|
# child table accesses (reads and writes) wait for locks.
|
|
#
|
|
create table t1 (a int primary key, b int) engine=innodb;
|
|
create table t2 (c int primary key, d int,
|
|
foreign key (d) references t1 (a) on update cascade) engine=innodb;
|
|
insert t1 values (1,1),(2,2),(3,3);
|
|
insert t2 values (4,1),(5,2),(6,3);
|
|
flush table t2 with read lock; # this takes MDL_SHARED_NO_WRITE
|
|
connect (con1,localhost,root);
|
|
--error ER_ROW_IS_REFERENCED_2
|
|
delete from t1 where a=2;
|
|
send update t1 set a=10 where a=1;
|
|
connection default;
|
|
let $wait_condition= select 1 from information_schema.processlist where state='Waiting for table metadata lock';
|
|
source include/wait_condition.inc;
|
|
unlock tables;
|
|
connection con1;
|
|
reap;
|
|
connection default;
|
|
lock table t2 write; # this takes MDL_SHARED_NO_READ_WRITE
|
|
connection con1;
|
|
send delete from t1 where a=2;
|
|
connection default;
|
|
let $wait_condition= select 1 from information_schema.processlist where state='Waiting for table metadata lock';
|
|
source include/wait_condition.inc;
|
|
unlock tables;
|
|
connection con1;
|
|
--error ER_ROW_IS_REFERENCED_2
|
|
reap;
|
|
connection default;
|
|
unlock tables;
|
|
disconnect con1;
|
|
|
|
# but privileges should not be checked
|
|
create user foo;
|
|
grant select,update on test.t1 to foo;
|
|
connect(foo,localhost,foo);
|
|
update t1 set a=30 where a=3;
|
|
disconnect foo;
|
|
connection default;
|
|
select * from t2;
|
|
drop table t2, t1;
|
|
drop user foo;
|
|
|
|
--echo #
|
|
--echo # MDEV-17187 table doesn't exist in engine after ALTER other tables
|
|
--echo # with CONSTRAINTs
|
|
--echo #
|
|
|
|
set foreign_key_checks=on;
|
|
create table t1 (id int not null primary key) engine=innodb;
|
|
create table t2 (id int not null primary key, fid int not null,
|
|
CONSTRAINT fk_fid FOREIGN KEY (fid) REFERENCES t1 (id))engine=innodb;
|
|
|
|
insert into t1 values (1), (2), (3);
|
|
insert into t2 values (1, 1), (2, 1), (3, 2);
|
|
|
|
set foreign_key_checks=off;
|
|
alter table t2 drop index fk_fid;
|
|
set foreign_key_checks=on;
|
|
|
|
--error ER_ROW_IS_REFERENCED_2
|
|
delete from t1 where id=2;
|
|
--error ER_NO_REFERENCED_ROW_2
|
|
insert into t2 values(4, 99);
|
|
|
|
select * from t1;
|
|
select * from t2;
|
|
|
|
set foreign_key_checks=off;
|
|
delete from t1 where id=2;
|
|
insert into t2 values(4, 99);
|
|
set foreign_key_checks=on;
|
|
|
|
select * from t1;
|
|
select * from t2;
|
|
|
|
show create table t1;
|
|
show create table t2;
|
|
|
|
# Optional: test DROP TABLE without any prior ha_innobase::open().
|
|
# This was tested manually, but it would cause --embedded to skip the test,
|
|
# and the restart would significantly increase the running time.
|
|
# --source include/restart_mysqld.inc
|
|
|
|
--error ER_ROW_IS_REFERENCED_2
|
|
drop table t1,t2;
|
|
--error ER_BAD_TABLE_ERROR
|
|
drop table t2, t1;
|
|
|
|
--echo #
|
|
--echo # MDEV-23470 InnoDB: Failing assertion: cmp < 0 in
|
|
--echo # row_ins_check_foreign_constraint
|
|
--echo #
|
|
CREATE TABLE t1(f1 INT NOT NULL PRIMARY KEY, f2 INT NOT NULL)ENGINE=InnoDB;
|
|
CREATE TABLE t2(f1 VARCHAR(100), f2 INT NOT NULL,
|
|
INDEX(f2))ENGINE=InnoDB;
|
|
|
|
INSERT INTO t1 VALUES(99, 2);
|
|
ALTER TABLE t2 ADD FOREIGN KEY(f2) REFERENCES t1(f1);
|
|
|
|
SET FOREIGN_KEY_CHECKS=0;
|
|
DROP INDEX f2 ON t2;
|
|
SET FOREIGN_KEY_CHECKS=1;
|
|
--error ER_NO_REFERENCED_ROW_2
|
|
INSERT INTO t2 VALUES('G', 3);
|
|
DROP TABLE t2, t1;
|
|
SET FOREIGN_KEY_CHECKS=DEFAULT;
|
|
|
|
--error ER_CANT_CREATE_TABLE
|
|
CREATE TABLE t1(a SERIAL) ENGINE=InnoDB ROW_FORMAT=COMPRESSED PAGE_COMPRESSED=1;
|
|
SHOW WARNINGS;
|
|
|
|
--echo # End of 10.5 tests
|
|
|
|
--echo #
|
|
--echo # MDEV-35598 foreign key error is unnecessary truncated
|
|
--echo #
|
|
set names utf8;
|
|
create table t1яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш
|
|
(f1яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш int not null primary key,
|
|
f2яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш int not null
|
|
) engine=innodb;
|
|
create table t2яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш
|
|
(f1яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш varchar(100),
|
|
f2яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш int not null,
|
|
index i2яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш
|
|
(f2яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш)
|
|
) engine=innodb;
|
|
|
|
insert t1яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш values(99, 2);
|
|
alter table t2яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш add foreign key(f2яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш) references t1яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш(f1яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш);
|
|
|
|
--error ER_NO_REFERENCED_ROW_2
|
|
insert t2яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш values('g', 3);
|
|
drop table t2яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш,
|
|
t1яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш;
|
|
|
|
--echo # End of 10.6 tests
|
|
|
|
--echo #
|
|
--echo # MDEV-37077 Crash in innobase_get_foreign_key_info()
|
|
--echo #
|
|
|
|
CREATE TABLE t (id INT PRIMARY KEY, f INT NOT NULL, KEY(f)) ENGINE=InnoDB;
|
|
--error ER_FK_INCORRECT_OPTION
|
|
SET STATEMENT FOREIGN_KEY_CHECKS = OFF FOR
|
|
ALTER TABLE t ADD FOREIGN KEY (f) REFERENCES t (id) ON UPDATE SET NULL;
|
|
DROP TABLE t;
|