mirror of
https://github.com/MariaDB/server.git
synced 2025-12-11 14:55:44 +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
267 lines
8.1 KiB
Text
267 lines
8.1 KiB
Text
--source include/have_profiling.inc
|
|
--source include/no_view_protocol.inc
|
|
|
|
--disable_ps2_protocol
|
|
# Verify that the protocol isn't violated if we ask for profiling info
|
|
# before profiling has recorded anything.
|
|
show profiles;
|
|
show profile all;
|
|
|
|
# default is OFF
|
|
show session variables like 'profil%';
|
|
select @@profiling;
|
|
|
|
# But size is okay
|
|
set @start_value= @@global.profiling_history_size;
|
|
set global profiling_history_size=100;
|
|
show global variables like 'profil%';
|
|
|
|
# turn on for testing
|
|
set session profiling = ON;
|
|
set session profiling_history_size=30; # small enough to overflow
|
|
|
|
# verify it is active
|
|
show session variables like 'profil%';
|
|
select @@profiling;
|
|
|
|
# Profiling is a descriptive look into the way the server operated
|
|
# in retrospect. Chad doesn't think it's wise to include the result
|
|
# log, as this creates a proscriptive specification about how the
|
|
# server should work in the future -- or it forces everyone who
|
|
# changes the server significantly to record the test results again,
|
|
# and that violates the spirit of our tests. Please don't include
|
|
# execution-specific data here, as in all of the "show profile" and
|
|
# information_schema.profiling results.
|
|
|
|
create table t1 (
|
|
a int,
|
|
b int
|
|
);
|
|
insert into t1 values (1,1), (2,null), (3, 4);
|
|
insert into t1 values (5,1), (6,null), (7, 4);
|
|
insert into t1 values (1,1), (2,null), (3, 4);
|
|
insert into t1 values (5,1), (6,null), (7, 4);
|
|
select max(x) from (select sum(a) as x from t1 group by b) as teeone;
|
|
insert into t1 select * from t1;
|
|
select count(*) from t1;
|
|
insert into t1 select * from t1;
|
|
insert into t1 select * from t1;
|
|
insert into t1 select * from t1;
|
|
select count(*) from t1;
|
|
insert into t1 select * from t1;
|
|
insert into t1 select * from t1;
|
|
insert into t1 select * from t1;
|
|
select count(*) from t1;
|
|
select sum(a) from t1;
|
|
select sum(a) from t1 group by b;
|
|
select sum(a) + sum(b) from t1 group by b;
|
|
select max(x) from (select sum(a) as x from t1 group by b) as teeone;
|
|
select '012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890' as big_string;
|
|
|
|
--enable_result_log
|
|
--replace_column 2 #
|
|
show profiles;
|
|
|
|
--disable_ps_protocol
|
|
--disable_result_log
|
|
###--replace_column 2 # 3 # 4 #
|
|
show profile for query 15;
|
|
###--replace_column 2 # 3 # 4 #
|
|
show profile cpu for query 15;
|
|
###--replace_column 2 # 3 # 4 # 5 # 6 #
|
|
show profile cpu, block io for query 15;
|
|
###--replace_column 2 # 3 # 4 #
|
|
show profile cpu for query 9 limit 2 offset 2;
|
|
show profile cpu for query 10 limit 0;
|
|
--error 0,ER_WRONG_ARGUMENTS
|
|
show profile cpu for query 65534;
|
|
###--replace_column 2 #
|
|
show profile memory;
|
|
###--replace_column 2 # 3 # 4 #
|
|
show profile block io;
|
|
###--replace_column 2 # 3 # 4 #
|
|
show profile context switches;
|
|
###--replace_column 2 # 3 # 4 #
|
|
show profile page faults;
|
|
###--replace_column 2 # 3 # 4 #
|
|
show profile ipc;
|
|
###--replace_column 2 #
|
|
show profile swaps limit 1 offset 2;
|
|
###--replace_column 2 # 5 #
|
|
show profile source;
|
|
show profile all for query 0 limit 0;
|
|
###--replace_column 2 # 3 # 4 # 5 # 6 # 7 # 8 # 9 # 10 # 11 # 12 # 13 # 16 #
|
|
show profile all for query 15;
|
|
###--replace_column 2 # 3 # 4 # 5 # 6 # 7 # 8 # 9 # 10 # 11 # 12 # 13 # 16 #
|
|
--enable_ps_protocol
|
|
|
|
select * from information_schema.profiling;
|
|
select query_id, state, duration from information_schema.profiling;
|
|
select query_id, sum(duration) from information_schema.profiling group by query_id;
|
|
select query_id, count(*) from information_schema.profiling group by query_id;
|
|
select sum(duration) from information_schema.profiling;
|
|
|
|
# Broken down into number of stages and duration of each query.
|
|
select query_id, count(*), sum(duration) from information_schema.profiling group by query_id;
|
|
select CPU_user, CPU_system, Context_voluntary, Context_involuntary, Block_ops_in, Block_ops_out, Messages_sent, Messages_received, Page_faults_major, Page_faults_minor, Swaps, Source_function, Source_file, Source_line from information_schema.profiling;
|
|
|
|
|
|
--enable_result_log
|
|
drop table if exists t1, t2, t3;
|
|
create table t1 (id int );
|
|
create table t2 (id int not null);
|
|
create table t3 (id int not null primary key);
|
|
insert into t1 values (1), (2), (3);
|
|
insert into t2 values (1), (2), (3);
|
|
insert into t3 values (1), (2), (3);
|
|
|
|
--replace_column 2 #
|
|
show profiles;
|
|
|
|
select * from t1;
|
|
--replace_column 2 #
|
|
show profiles;
|
|
--echo This ^^ should end in "select * from t1;"
|
|
|
|
delete from t1;
|
|
insert into t1 values (1), (2), (3);
|
|
insert into t1 values (1), (2), (3);
|
|
|
|
select * from t1;
|
|
--replace_column 2 #
|
|
show profiles;
|
|
|
|
# Turning profiling off does freeze it
|
|
set session profiling = OFF;
|
|
select sum(id) from t1;
|
|
--replace_column 2 #
|
|
show profiles;
|
|
|
|
## Verify that the various juggling of THD contexts doesn't affect profiling.
|
|
|
|
## Functions and procedures
|
|
set session profiling = ON;
|
|
select @@profiling;
|
|
create function f1() returns varchar(50) return 'hello';
|
|
select @@profiling;
|
|
select * from t1 where id <> f1();
|
|
select @@profiling;
|
|
|
|
set session profiling = OFF;
|
|
drop table if exists profile_log;
|
|
create table profile_log (how_many int);
|
|
|
|
--disable_warnings
|
|
drop procedure if exists p1;
|
|
drop procedure if exists p2;
|
|
drop procedure if exists p3;
|
|
--enable_warnings
|
|
|
|
delimiter //;
|
|
create procedure p1 ()
|
|
modifies sql data
|
|
begin
|
|
set profiling = ON;
|
|
select 'This p1 should show up in profiling';
|
|
insert into profile_log select count(*) from information_schema.profiling;
|
|
end//
|
|
create procedure p2()
|
|
deterministic
|
|
begin
|
|
set profiling = ON;
|
|
call p1();
|
|
select 'This p2 should show up in profiling';
|
|
end//
|
|
create procedure p3 ()
|
|
reads sql data
|
|
begin
|
|
set profiling = ON;
|
|
select 'This p3 should show up in profiling';
|
|
show profile;
|
|
end//
|
|
delimiter ;//
|
|
|
|
--disable_result_log
|
|
--echo first call to p1
|
|
call p1;
|
|
select * from profile_log;
|
|
--echo second call to p1
|
|
call p1;
|
|
select * from profile_log;
|
|
--echo third call to p1
|
|
call p1;
|
|
select * from profile_log;
|
|
set session profiling = OFF;
|
|
call p2;
|
|
set session profiling = OFF;
|
|
call p3;
|
|
|
|
--replace_column 1 # 2 #
|
|
show profiles;
|
|
--enable_result_log
|
|
|
|
drop procedure if exists p1;
|
|
drop procedure if exists p2;
|
|
drop procedure if exists p3;
|
|
drop table if exists profile_log;
|
|
|
|
## Triggers
|
|
set session profiling = ON;
|
|
drop table if exists t2;
|
|
create table t2 (id int not null);
|
|
create trigger t2_bi before insert on t2 for each row set @x=0;
|
|
select @@profiling;
|
|
insert into t2 values (1), (2), (3);
|
|
select @@profiling;
|
|
|
|
## ALTER TABLE
|
|
set session profiling = ON;
|
|
drop table if exists t1, t2;
|
|
create table t1 (id int not null primary key);
|
|
create table t2 (id int not null primary key, id1 int not null);
|
|
select @@profiling;
|
|
alter table t2 add foreign key (id1) references t1 (id) on delete cascade;
|
|
select @@profiling;
|
|
|
|
## Table LOCKing
|
|
lock table t1 write;
|
|
select @@profiling;
|
|
unlock table;
|
|
select @@profiling;
|
|
|
|
## Transactions
|
|
set autocommit=0;
|
|
select @@profiling, @@autocommit;
|
|
begin;
|
|
select @@profiling;
|
|
insert into t1 values (1);
|
|
insert into t2 values (1,1);
|
|
--echo testing referential integrity cascade
|
|
delete from t1 where id = 1;
|
|
select @@profiling;
|
|
--echo testing rollback
|
|
--disable_warnings
|
|
rollback;
|
|
--enable_warnings
|
|
select @@profiling;
|
|
--echo testing commit
|
|
begin;
|
|
select @@profiling;
|
|
commit;
|
|
select @@profiling;
|
|
|
|
drop table if exists t2, t1, t3;
|
|
drop view if exists v1;
|
|
drop function if exists f1;
|
|
|
|
## Multiple queries in one packet. Combo statements don't work with ps-proto.
|
|
#--eval select 1; select 2; select 3;
|
|
## two continuations, one starting
|
|
#select state from information_schema.profiling where seq=1 order by query_id desc limit 3;
|
|
--enable_ps2_protocol
|
|
|
|
## last thing in the file
|
|
set session profiling = OFF;
|
|
set global profiling_history_size= @start_value;
|
|
##
|
|
--echo End of 5.0 tests
|