mirror of
https://github.com/MariaDB/server.git
synced 2025-12-15 16:55: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
234 lines
6.1 KiB
Text
234 lines
6.1 KiB
Text
--source include/have_innodb.inc
|
|
--source include/have_debug.inc
|
|
--source include/have_sequence.inc
|
|
|
|
delimiter |;
|
|
create function get_index_id(tbl_id int, index_name char(100))
|
|
returns int
|
|
begin
|
|
declare res int;
|
|
select index_id into res from information_schema.innodb_sys_indexes where
|
|
name=index_name and table_id = tbl_id;
|
|
return res;
|
|
end|
|
|
|
|
delimiter ;|
|
|
|
|
create table t (
|
|
pk int primary key,
|
|
a int,
|
|
b int,
|
|
c int,
|
|
unique index a_key (a),
|
|
key c_key (c)
|
|
) engine=innodb stats_persistent=1;
|
|
|
|
insert into t values (1, 1, 1, 1);
|
|
|
|
set @table_id = (select table_id from information_schema.innodb_sys_tables where name='test/t');
|
|
|
|
set @a_key_id = get_index_id(@table_id, 'a_key');
|
|
set @c_key_id = get_index_id(@table_id, 'c_key');
|
|
set @primary_id = get_index_id(@table_id, 'primary');
|
|
|
|
select distinct(index_name) from mysql.innodb_index_stats where table_name = 't';
|
|
alter table t
|
|
drop index a_key,
|
|
add unique index a_key_strikes_back (a);
|
|
select distinct(index_name) from mysql.innodb_index_stats where table_name = 't';
|
|
|
|
check table t;
|
|
select @a_key_id = get_index_id(@table_id, 'a_key_strikes_back'),
|
|
@c_key_id = get_index_id(@table_id, 'c_key'),
|
|
@primary_id = get_index_id(@table_id, 'primary');
|
|
|
|
set @a_key_strikes_back_id = get_index_id(@table_id, 'a_key_strikes_back');
|
|
set @c_key_id = get_index_id(@table_id, 'c_key');
|
|
set @primary_id = get_index_id(@table_id, 'primary');
|
|
|
|
alter table t
|
|
drop index a_key_strikes_back,
|
|
add unique index a_key_returns (a),
|
|
drop primary key,
|
|
add primary key (pk),
|
|
add unique index b_key (b);
|
|
|
|
check table t;
|
|
select @a_key_strikes_back_id = get_index_id(@table_id, 'a_key_returns'),
|
|
@c_key_id = get_index_id(@table_id, 'c_key'),
|
|
@primary_id = get_index_id(@table_id, 'primary');
|
|
|
|
set @a_key_returns_id = get_index_id(@table_id, 'a_key_returns');
|
|
set @b_key_id = get_index_id(@table_id, 'b_key');
|
|
set @c_key_id = get_index_id(@table_id, 'c_key');
|
|
set @primary_id = get_index_id(@table_id, 'primary');
|
|
|
|
alter table t
|
|
drop key c_key,
|
|
add key c_key2 (c);
|
|
|
|
check table t;
|
|
select @a_key_returns_id = get_index_id(@table_id, 'a_key_returns'),
|
|
@b_key_id = get_index_id(@table_id, 'b_key'),
|
|
@c_key_id = get_index_id(@table_id, 'c_key2'),
|
|
@primary_id = get_index_id(@table_id, 'primary');
|
|
|
|
drop table t;
|
|
|
|
create table errors (
|
|
a int,
|
|
unique key a_key (a),
|
|
b int
|
|
) engine=innodb;
|
|
|
|
--error ER_CANT_DROP_FIELD_OR_KEY
|
|
alter table errors
|
|
drop key a_key,
|
|
drop key a_key,
|
|
add unique key a_key2 (a);
|
|
|
|
--error ER_CANT_DROP_FIELD_OR_KEY
|
|
alter table errors
|
|
drop key a_key,
|
|
drop key a_key2,
|
|
add unique key a_key2 (a);
|
|
|
|
--error ER_CANT_DROP_FIELD_OR_KEY
|
|
alter table errors
|
|
add key b_key (b),
|
|
drop key b_key,
|
|
add key bb_key (b);
|
|
|
|
--error ER_CANT_DROP_FIELD_OR_KEY
|
|
alter table errors
|
|
drop key a_key,
|
|
add key a_key2 (a),
|
|
drop key a_key,
|
|
add key a_key2 (a);
|
|
|
|
drop table errors;
|
|
|
|
--disable_query_log
|
|
call mtr.add_suppression("Flagged corruption of `a_key` in table `test`.`corrupted` in dict_set_index_corrupted");
|
|
--enable_query_log
|
|
|
|
create table corrupted (
|
|
a int,
|
|
key a_key (a)
|
|
) engine=innodb;
|
|
|
|
insert into corrupted values (1);
|
|
|
|
select * from corrupted;
|
|
|
|
SET @save_dbug = @@SESSION.debug_dbug;
|
|
SET debug_dbug = '+d,dict_set_index_corrupted';
|
|
check table corrupted;
|
|
SET debug_dbug = @save_dbug;
|
|
|
|
--error ER_INDEX_CORRUPT
|
|
select * from corrupted;
|
|
|
|
--error ER_INDEX_CORRUPT
|
|
alter table corrupted
|
|
drop key a_key,
|
|
add key a_key2 (a);
|
|
|
|
alter table corrupted
|
|
drop key a_key;
|
|
|
|
select * from corrupted;
|
|
|
|
check table corrupted;
|
|
|
|
drop table corrupted;
|
|
|
|
create table t (
|
|
a int,
|
|
unique key a_key (a)
|
|
) engine=innodb stats_persistent=1;
|
|
|
|
SET @save_dbug = @@SESSION.debug_dbug;
|
|
SET debug_dbug = '+d,ib_rename_index_fail1';
|
|
-- error ER_LOCK_DEADLOCK
|
|
alter table t
|
|
drop key a_key,
|
|
add unique key a_key2 (a),
|
|
algorithm=instant;
|
|
SET debug_dbug = @save_dbug;
|
|
|
|
--error ER_WRONG_NAME_FOR_INDEX
|
|
alter table t
|
|
drop key a_key,
|
|
add unique key `GEN_CLUST_INDEX` (a),
|
|
algorithm=instant;
|
|
|
|
show create table t;
|
|
|
|
drop table t;
|
|
|
|
|
|
create table rename_column_and_index (
|
|
a int,
|
|
unique index a_key(a)
|
|
) engine=innodb;
|
|
|
|
insert into rename_column_and_index values (1), (3);
|
|
|
|
alter table rename_column_and_index
|
|
change a aa int,
|
|
drop key a_key,
|
|
add unique key aa_key(aa),
|
|
algorithm=instant;
|
|
|
|
show create table rename_column_and_index;
|
|
check table rename_column_and_index;
|
|
drop table rename_column_and_index;
|
|
|
|
|
|
--echo #
|
|
--echo # MDEV-19189: ASAN memcpy-param-overlap in fill_alter_inplace_info upon adding indexes
|
|
--echo #
|
|
|
|
create table t1 (f1 int, f2 int, f3 int);
|
|
create table t2 (f2 int primary key);
|
|
alter table t1 add foreign key f (f2) references t2(f2);
|
|
alter table t1 add foreign key (f2) references t1(f2), add key (f3), add key (f1);
|
|
drop tables t1, t2;
|
|
|
|
--echo #
|
|
--echo # MDEV-21669 InnoDB: Table ... contains <n> indexes inside InnoDB, which is different from the number of indexes <n> defined in the MariaDB
|
|
--echo #
|
|
CREATE TABLE t1 (col_int INTEGER, col_char CHAR(20), col_varchar VARCHAR(500)) ENGINE=InnoDB;
|
|
SET @table_id = (SELECT table_id FROM information_schema.innodb_sys_tables WHERE name='test/t1');
|
|
ALTER TABLE t1 ADD KEY idx3 (col_varchar(9)), ADD KEY idX2 (col_char(9));
|
|
SET @idx3_key_id = get_index_id(@table_id, 'iDx3');
|
|
ALTER TABLE t1 DROP KEY iDx3, ADD KEY Idx3 (col_varchar(9));
|
|
SELECT @idx3_key_id = get_index_id(@table_id, 'Idx3');
|
|
CHECK TABLE t1 EXTENDED ;
|
|
DROP TABLE t1;
|
|
|
|
DROP FUNCTION get_index_id;
|
|
|
|
--echo #
|
|
--echo # MDEV-23356 InnoDB: Failing assertion: field->col->mtype == type, crash or ASAN failures in row_sel_convert_mysql_key_to_innobase, InnoDB indexes are inconsistent after INDEX changes
|
|
--echo #
|
|
|
|
CREATE TABLE t1 (a INT, b INT, c CHAR(8),
|
|
KEY ind1(c), KEY ind2(b)) ENGINE=InnoDB STATS_PERSISTENT=1;
|
|
|
|
INSERT INTO t1 SELECT 1, 1, 'a' FROM seq_1_to_100;
|
|
|
|
SELECT table_name, index_name, stat_name FROM mysql.innodb_index_stats;
|
|
|
|
ALTER TABLE t1 DROP INDEX ind2, ADD INDEX ind3(b),
|
|
DROP INDEX ind1, ADD INDEX ind2(c);
|
|
|
|
SELECT table_name, index_name, stat_name FROM mysql.innodb_index_stats;
|
|
|
|
ALTER TABLE t1 DROP b, FORCE;
|
|
SELECT table_name, index_name, stat_name FROM mysql.innodb_index_stats;
|
|
|
|
UPDATE t1 SET a = 1 WHERE c = 'foo';
|
|
DROP TABLE t1;
|
|
SELECT table_name, index_name, stat_name FROM mysql.innodb_index_stats;
|