Commit 3448ceb0 authored by Marko Mäkelä's avatar Marko Mäkelä

MDEV-13564: Implement innodb_unsafe_truncate=ON for compatibility

While MariaDB Server 10.2 is not really guaranteed to be compatible
with Percona XtraBackup 2.4 (for example, the MySQL 5.7 undo log format
change that could be present in XtraBackup, but was reverted from
MariaDB in MDEV-12289), we do not want to disrupt users who have
deployed xtrabackup and MariaDB Server 10.2 in their environments.

With this change, MariaDB 10.2 will continue to use the backup-unsafe
TRUNCATE TABLE code, so that neither the undo log nor the redo log
formats will change in an incompatible way.

Undo tablespace truncation will keep using the redo log only. Recovery
or backup with old code will fail to shrink the undo tablespace files,
but the contents will be recovered just fine.

In the MariaDB Server 10.2 series only, we introduce the configuration
parameter innodb_unsafe_truncate and make it ON by default. To allow
MariaDB Backup (mariabackup) to work properly with TRUNCATE TABLE
operations, use loose_innodb_unsafe_truncate=OFF.

MariaDB Server 10.3.10 and later releases will always use the
backup-safe TRUNCATE TABLE, and this parameter will not be
added there.

recv_recovery_rollback_active(): Skip row_mysql_drop_garbage_tables()
unless innodb_unsafe_truncate=OFF. It is too unsafe to drop orphan
tables if RENAME operations are not transactional within InnoDB.

LOG_HEADER_FORMAT_10_3: Replaces LOG_HEADER_FORMAT_CURRENT.

log_init(), log_group_file_header_flush(),
srv_prepare_to_delete_redo_log_files(),
innobase_start_or_create_for_mysql(): Choose the redo log format
and subformat based on the value of innodb_unsafe_truncate.
parent 07815d95
This diff is collapsed.
#
# WL#6501: make truncate table atomic
#
--source include/have_innodb.inc
--source include/have_debug.inc
--source include/big_test.inc
# Valgrind would complain about memory leaks when we crash on purpose.
--source include/not_valgrind.inc
# Embedded server does not support crashing
--source include/not_embedded.inc
# Avoid CrashReporter popup on Mac
--source include/not_crashrep.inc
# suppress expected warnings
call mtr.add_suppression("does not exist in the InnoDB internal");
################################################################################
#
# Will test following scenarios:
# 1. Hit crash point on completing drop of all indexes before creation of index
# is commenced.
# 2. Hit crash point after data is updated to system-table and in-memory dict.
#
################################################################################
#-----------------------------------------------------------------------------
#
# create test-bed
#
let $per_table = `select @@innodb_file_per_table`;
eval set global innodb_file_per_table = on;
let $WL6501_TMP_DIR = `select @@tmpdir`;
let $WL6501_DATA_DIR = `select @@datadir`;
let SEARCH_FILE = $MYSQLTEST_VARDIR/log/my_restart.err;
#-----------------------------------------------------------------------------
#
# 1. Hit crash point on completing drop of all indexes before creation of index
# is commenced.
#
--echo "1. Hit crash point on completing drop of all indexes before creation"
--echo " of index is commenced."
eval set global innodb_file_per_table = $wl6501_file_per_table;
set innodb_strict_mode=off;
--disable_warnings
eval create $wl6501_temp table t (
i int, f float, c char,
primary key pk(i), unique findex(f), index ck(c))
engine = innodb row_format = $wl6501_row_fmt
key_block_size = $wl6501_kbs;
--enable_warnings
insert into t values (1, 1.1, 'a'), (2, 2.2, 'b'), (3, 3.3, 'c');
select * from t;
check table t;
#
set session debug = "+d,ib_trunc_crash_drop_reinit_done_create_to_start";
--source include/expect_crash.inc
--error 2013
truncate table t;
#
--source include/start_mysqld.inc
check table t;
#-----------------------------------------------------------------------------
#
# 2. Hit crash point after data is updated to system-table and in-memory dict.
#
--echo "2. Hit crash point after data is updated to system-table and"
--echo " in-memory dict."
eval set global innodb_file_per_table = $wl6501_file_per_table;
set innodb_strict_mode=off;
--disable_warnings
eval create $wl6501_temp table t (
i int, f float, c char,
primary key pk(i), unique findex(f), index ck(c))
engine = innodb row_format = $wl6501_row_fmt
key_block_size = $wl6501_kbs;
--enable_warnings
insert into t values (1, 1.1, 'a'), (2, 2.2, 'b'), (3, 3.3, 'c');
select * from t;
check table t;
#
set session debug = "+d,ib_trunc_crash_on_updating_dict_sys_info";
--source include/expect_crash.inc
--error 2013
truncate table t;
#
--source include/start_mysqld.inc
check table t;
#-----------------------------------------------------------------------------
#
# remove test-bed
#
eval set global innodb_file_per_table = $per_table;
begin;
update t1 set c = 'MariaDB';
update t1 set c = 'InnoDB';
eval set global debug_dbug = '+d,$SEARCH_PATTERN';
commit;
--source include/shutdown_mysqld.inc
--source include/search_pattern_in_file.inc
--source include/start_mysqld.inc
#
# Bug #23070734 CONCURRENT TRUNCATE TABLES CAUSE STALLS
#
SET @ahi= @@global.innodb_adaptive_hash_index;
SET GLOBAL innodb_adaptive_hash_index=OFF;
SET GLOBAL innodb_adaptive_hash_index=ON;
Test_1 :- Check if DDL operations are possible on
table being truncated. Also check if
DDL operations on other tables succeed.
create table t1 (f1 int,f2 int,key(f2),f3 int) engine=innodb;
create index idx1 on t1(f3);
create table t2 (f1 int,f2 int,key(f2),f3 int) engine=innodb;
create table t3 (f1 int,f2 int,key(f2)) engine=innodb;
insert into t1 values (10,20,30),(30,40,50),(50,60,70);
insert into t1 select * from t1;
insert into t1 select * from t1;
insert into t1 select * from t1;
insert into t2 values (10,20,30),(30,40,50),(50,60,70);
insert into t2 select * from t2;
insert into t2 select * from t2;
insert into t2 select * from t2;
insert into t3 values (10,20),(30,40),(50,50);
insert into t3 select * from t3;
insert into t3 select * from t3;
SET session lock_wait_timeout = 1;
connect con1,localhost,root,,;
SET DEBUG_SYNC= 'buffer_pool_scan SIGNAL started WAIT_FOR finish_scan';
truncate table t1;
connection default;
SET DEBUG_SYNC= 'now WAIT_FOR started';
Check Analyze table. Gives lock time out error.
analyze table t1;
Table Op Msg_type Msg_text
test.t1 analyze Error Lock wait timeout exceeded; try restarting transaction
test.t1 analyze status Operation failed
Check if we can turn off auto recalculation.
alter table t1 STATS_AUTO_RECALC=0;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
Check if we can turn off persistent stats on the table
alter table t1 STATS_PERSISTENT=0;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
Check if DML is possible on table being truncated
insert into t1 values (10,89,99);
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
Check if DDL is possible on table being truncated
alter table t1 add column f4 int;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
check if table can be created with the same name
create table t1 (bd int) engine=innodb;
Got one of the listed errors
check if index can be created on table being truncated
create index idx1 on t1(f1);
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
Check if DROP of table is possible during truncate
drop table t1;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
Check if select is possible during truncate
select * from t1;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
select * from t2 where t2.f1=t1.f1;
ERROR 42S22: Unknown column 't1.f1' in 'where clause'
Check concurrent truncate operation on table;
truncate table t1;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
Check concurrent selects and inserts on the other table
insert into t3 values (10,20),(30,40),(50,50);
select * from t3 where f1=40;
f1 f2
Concurrent truncate on other tables
truncate table t2;
Concurrent alters on the other tables
alter table t2 add column f4 int;
check if index can be created on the other table
create index idx1 on t2(f3);
Check if we can turn off persistent stats off entire instance
SET GLOBAL innodb_stats_persistent=off;
connect con2,localhost,root,,;
set global innodb_adaptive_hash_index=off;
connection default;
SET DEBUG_SYNC= 'now SIGNAL finish_scan';
connection con1;
disconnect con1;
connection con2;
disconnect con2;
connection default;
SET DEBUG_SYNC= 'RESET';
SET GLOBAL innodb_adaptive_hash_index=@ahi;
drop table t1,t2,t3;
SET @save_dbug = @@SESSION.debug_dbug;
call mtr.add_suppression("InnoDB: Flagged corruption of .* in table `test`\\.`t` in TRUNCATE TABLE");
# 1. Error in assigning undo logs for truncate action
CREATE TABLE t (i int PRIMARY KEY, f float UNIQUE, c char(100), INDEX ck(c))
ENGINE = InnoDB;
insert into t values (1, 1.1, 'a'), (2, 2.2, 'b'), (3, 3.3, 'c');
check table t;
Table Op Msg_type Msg_text
test.t check status OK
SET debug_dbug = '+d,ib_err_trunc_assigning_undo_log';
truncate table t;
ERROR HY000: Got error 168 "Unknown (generic) error from engine" from storage engine InnoDB
SET debug_dbug = @save_dbug;
check table t;
Table Op Msg_type Msg_text
test.t check status OK
select * from t;
i f c
1 1.1 a
2 2.2 b
3 3.3 c
# 2. Error while preparing for truncate
SET debug_dbug = '+d,ib_err_trunc_preparing_for_truncate';
truncate table t;
ERROR HY000: Got error 168 "Unknown (generic) error from engine" from storage engine InnoDB
SET debug_dbug = @save_dbug;
check table t;
Table Op Msg_type Msg_text
test.t check status OK
select * from t;
i f c
1 1.1 a
2 2.2 b
3 3.3 c
# 3. Error while dropping/creating indexes
SET debug_dbug = '+d,ib_err_trunc_drop_index';
truncate table t;
ERROR HY000: Got error 168 "Unknown (generic) error from engine" from storage engine InnoDB
SET debug_dbug = @save_dbug;
check table t;
Table Op Msg_type Msg_text
test.t check Warning InnoDB: Index PRIMARY is marked as corrupted
test.t check error Corrupt
select * from t;
Got one of the listed errors
drop table t;
CREATE TABLE t (i int PRIMARY KEY, f float UNIQUE, c char(100), INDEX ck(c))
ENGINE = InnoDB;
insert into t values (1, 1.1, 'a'), (2, 2.2, 'b'), (3, 3.3, 'c');
check table t;
Table Op Msg_type Msg_text
test.t check status OK
SET debug_dbug = '+d,ib_err_trunc_create_index';
truncate table t;
ERROR HY000: Got error 168 "Unknown (generic) error from engine" from storage engine InnoDB
SET debug_dbug = @save_dbug;
check table t;
Table Op Msg_type Msg_text
test.t check Warning InnoDB: Index PRIMARY is marked as corrupted
test.t check error Corrupt
select * from t;
Got one of the listed errors
drop table t;
CREATE TABLE t (i int PRIMARY KEY, f float UNIQUE, c char(100), INDEX ck(c))
ENGINE = InnoDB;
insert into t values (1, 1.1, 'a'), (2, 2.2, 'b'), (3, 3.3, 'c');
check table t;
Table Op Msg_type Msg_text
test.t check status OK
SET debug_dbug = '+d,ib_err_trunc_temp_recreate_index';
truncate table t;
SET debug_dbug = @save_dbug;
check table t;
Table Op Msg_type Msg_text
test.t check status OK
select * from t;
i f c
drop table t;
# 4. Error while completing truncate of table involving FTS
CREATE TABLE t (i int PRIMARY KEY, f float UNIQUE, c char(100),
FULLTEXT INDEX(c)) ENGINE = InnoDB;
insert into t values (1, 1.1, 'mysql is now oracle company'),
(2, 2.2, 'innodb is part of mysql'),
(3, 3.3, 'innodb is default storage engine of mysql');
check table t;
Table Op Msg_type Msg_text
test.t check status OK
SET debug_dbug = '+d,ib_err_trunc_temp_recreate_index';
truncate table t;
SET debug_dbug = @save_dbug;
check table t;
Table Op Msg_type Msg_text
test.t check status OK
select * from t;
i f c
drop table t;
# 5. Error while updating sys-tables
CREATE TABLE t (i int PRIMARY KEY, f float UNIQUE, c char(100),
FULLTEXT INDEX(c)) ENGINE = InnoDB;
insert into t values (1, 1.1, 'mysql is now oracle company'),
(2, 2.2, 'innodb is part of mysql'),
(3, 3.3, 'innodb is default storage engine of mysql');
check table t;
Table Op Msg_type Msg_text
test.t check status OK
SET debug_dbug = '+d,ib_err_trunc_temp_recreate_index';
truncate table t;
SET debug_dbug = @save_dbug;
check table t;
Table Op Msg_type Msg_text
test.t check status OK
select * from t order by i;
i f c
drop table t;
#
# Bug #23070734 CONCURRENT TRUNCATE TABLES CAUSE STALLS
#
create table t1 (f1 int ,f2 int,key(f2)) engine=innodb;
begin;
insert into t1 values (10,45),(20,78),(30,88),(40,23),(50,78),(60,11),(70,56),(80,90);
delete from t1;
connect con2,localhost,root,,;
START TRANSACTION WITH CONSISTENT SNAPSHOT;
connection default;
SET @saved_frequency = @@GLOBAL.innodb_purge_rseg_truncate_frequency;
SET GLOBAL innodb_purge_rseg_truncate_frequency = 1;
commit;
connect con1,localhost,root,,;
SET DEBUG_SYNC= 'buffer_pool_scan SIGNAL started WAIT_FOR finish_scan';
truncate table t1;
connection con2;
SET DEBUG_SYNC= 'now WAIT_FOR started';
COMMIT;
disconnect con2;
connection default;
InnoDB 0 transactions not purged
SET GLOBAL innodb_purge_rseg_truncate_frequency = @saved_frequency;
SET DEBUG_SYNC = 'now SIGNAL finish_scan';
connection con1;
disconnect con1;
connection default;
SET DEBUG_SYNC = 'RESET';
drop table t1;
SET GLOBAL innodb_stats_persistent= ON;
CREATE TABLE t1 (t TEXT) ENGINE=InnoDB STATS_PERSISTENT=1;
connect con1,localhost,root,,test;
SET DEBUG_SYNC='ib_trunc_table_trunc_completing SIGNAL committed WAIT_FOR ever';
TRUNCATE TABLE t1;
connection default;
SET DEBUG_SYNC='now WAIT_FOR committed';
disconnect con1;
SELECT COUNT(*) FROM t1;
COUNT(*)
0
DROP TABLE t1;
--loose-skip-innodb-unsafe-truncate
--loose-innodb-sys-tables
--loose-skip-innodb-unsafe-truncate
--loose-skip-innodb-unsafe-truncate
--loose-skip-innodb-unsafe-truncate
--loose-skip-innodb-unsafe-truncate
--source include/have_innodb.inc
--source include/have_debug.inc
--source include/have_debug_sync.inc
--source include/count_sessions.inc
--echo #
--echo # Bug #23070734 CONCURRENT TRUNCATE TABLES CAUSE STALLS
--echo #
SET @ahi= @@global.innodb_adaptive_hash_index;
# Ensure that there is no adaptive hash index on any system tables,
# or any other tables than the ones that we are creating below.
SET GLOBAL innodb_adaptive_hash_index=OFF;
SET GLOBAL innodb_adaptive_hash_index=ON;
--echo Test_1 :- Check if DDL operations are possible on
--echo table being truncated. Also check if
--echo DDL operations on other tables succeed.
create table t1 (f1 int,f2 int,key(f2),f3 int) engine=innodb;
create index idx1 on t1(f3);
create table t2 (f1 int,f2 int,key(f2),f3 int) engine=innodb;
create table t3 (f1 int,f2 int,key(f2)) engine=innodb;
insert into t1 values (10,20,30),(30,40,50),(50,60,70);
insert into t1 select * from t1;
insert into t1 select * from t1;
insert into t1 select * from t1;
insert into t2 values (10,20,30),(30,40,50),(50,60,70);
insert into t2 select * from t2;
insert into t2 select * from t2;
insert into t2 select * from t2;
insert into t3 values (10,20),(30,40),(50,50);
insert into t3 select * from t3;
insert into t3 select * from t3;
SET session lock_wait_timeout = 1;
connect (con1,localhost,root,,);
SET DEBUG_SYNC= 'buffer_pool_scan SIGNAL started WAIT_FOR finish_scan';
send truncate table t1;
connection default;
SET DEBUG_SYNC= 'now WAIT_FOR started';
--echo Check Analyze table. Gives lock time out error.
analyze table t1;
--echo Check if we can turn off auto recalculation.
--error ER_LOCK_WAIT_TIMEOUT
alter table t1 STATS_AUTO_RECALC=0;
--echo Check if we can turn off persistent stats on the table
--error ER_LOCK_WAIT_TIMEOUT
alter table t1 STATS_PERSISTENT=0;
--echo Check if DML is possible on table being truncated
--error ER_LOCK_WAIT_TIMEOUT
insert into t1 values (10,89,99);
--echo Check if DDL is possible on table being truncated
--error ER_LOCK_WAIT_TIMEOUT
alter table t1 add column f4 int;
--echo check if table can be created with the same name
--error ER_TABLE_EXISTS_ERROR,ER_LOCK_WAIT_TIMEOUT
create table t1 (bd int) engine=innodb;
--echo check if index can be created on table being truncated
--error ER_LOCK_WAIT_TIMEOUT
create index idx1 on t1(f1);
--echo Check if DROP of table is possible during truncate
--error ER_LOCK_WAIT_TIMEOUT
drop table t1;
--echo Check if select is possible during truncate
--error ER_LOCK_WAIT_TIMEOUT
select * from t1;
--error ER_BAD_FIELD_ERROR
select * from t2 where t2.f1=t1.f1;
--echo Check concurrent truncate operation on table;
--error ER_LOCK_WAIT_TIMEOUT
truncate table t1;
--echo Check concurrent selects and inserts on the other table
insert into t3 values (10,20),(30,40),(50,50);
select * from t3 where f1=40;
--echo Concurrent truncate on other tables
truncate table t2;
--echo Concurrent alters on the other tables
alter table t2 add column f4 int;
--echo check if index can be created on the other table
create index idx1 on t2(f3);
--echo Check if we can turn off persistent stats off entire instance
SET GLOBAL innodb_stats_persistent=off;
connect (con2,localhost,root,,);
send set global innodb_adaptive_hash_index=off;
connection default;
SET DEBUG_SYNC= 'now SIGNAL finish_scan';
connection con1;
reap;
disconnect con1;
connection con2;
reap;
disconnect con2;
connection default;
SET DEBUG_SYNC= 'RESET';
SET GLOBAL innodb_adaptive_hash_index=@ahi;
drop table t1,t2,t3;
--source include/wait_until_count_sessions.inc
--loose-skip-innodb-unsafe-truncate
# This test is based on innodb_zip.wl6501_error_1 in MySQL 5.7.
--source include/have_innodb.inc
--source include/innodb_row_format.inc
--source include/have_debug.inc
SET @save_dbug = @@SESSION.debug_dbug;
call mtr.add_suppression("InnoDB: Flagged corruption of .* in table `test`\\.`t` in TRUNCATE TABLE");
--echo # 1. Error in assigning undo logs for truncate action
CREATE TABLE t (i int PRIMARY KEY, f float UNIQUE, c char(100), INDEX ck(c))
ENGINE = InnoDB;
insert into t values (1, 1.1, 'a'), (2, 2.2, 'b'), (3, 3.3, 'c');
check table t;
#
SET debug_dbug = '+d,ib_err_trunc_assigning_undo_log';
--error ER_GET_ERRNO
truncate table t;
SET debug_dbug = @save_dbug;
check table t;
select * from t;
--echo # 2. Error while preparing for truncate
SET debug_dbug = '+d,ib_err_trunc_preparing_for_truncate';
--error ER_GET_ERRNO
truncate table t;
SET debug_dbug = @save_dbug;
check table t;
select * from t;
--echo # 3. Error while dropping/creating indexes
SET debug_dbug = '+d,ib_err_trunc_drop_index';
--error ER_GET_ERRNO
truncate table t;
SET debug_dbug = @save_dbug;
check table t;
--error ER_TABLE_CORRUPT,ER_GET_ERRNO
select * from t;
drop table t;
CREATE TABLE t (i int PRIMARY KEY, f float UNIQUE, c char(100), INDEX ck(c))
ENGINE = InnoDB;
insert into t values (1, 1.1, 'a'), (2, 2.2, 'b'), (3, 3.3, 'c');
check table t;
SET debug_dbug = '+d,ib_err_trunc_create_index';
--error ER_GET_ERRNO
truncate table t;
SET debug_dbug = @save_dbug;
check table t;
--error ER_TABLE_CORRUPT,ER_GET_ERRNO
select * from t;
drop table t;
CREATE TABLE t (i int PRIMARY KEY, f float UNIQUE, c char(100), INDEX ck(c))
ENGINE = InnoDB;
insert into t values (1, 1.1, 'a'), (2, 2.2, 'b'), (3, 3.3, 'c');
check table t;
SET debug_dbug = '+d,ib_err_trunc_temp_recreate_index';
truncate table t;
SET debug_dbug = @save_dbug;
check table t;
select * from t;
drop table t;
--echo # 4. Error while completing truncate of table involving FTS
CREATE TABLE t (i int PRIMARY KEY, f float UNIQUE, c char(100),
FULLTEXT INDEX(c)) ENGINE = InnoDB;
insert into t values (1, 1.1, 'mysql is now oracle company'),
(2, 2.2, 'innodb is part of mysql'),
(3, 3.3, 'innodb is default storage engine of mysql');
check table t;
SET debug_dbug = '+d,ib_err_trunc_temp_recreate_index';
truncate table t;
SET debug_dbug = @save_dbug;
check table t;
select * from t;
drop table t;
--echo # 5. Error while updating sys-tables
CREATE TABLE t (i int PRIMARY KEY, f float UNIQUE, c char(100),
FULLTEXT INDEX(c)) ENGINE = InnoDB;
insert into t values (1, 1.1, 'mysql is now oracle company'),
(2, 2.2, 'innodb is part of mysql'),
(3, 3.3, 'innodb is default storage engine of mysql');
check table t;
SET debug_dbug = '+d,ib_err_trunc_temp_recreate_index';
truncate table t;
SET debug_dbug = @save_dbug;
check table t;
select * from t order by i;
drop table t;
--loose-skip-innodb-unsafe-truncate
--innodb-purge-threads=1
--innodb-purge-batch-size=1
--innodb-stats-persistent=OFF
--source include/have_innodb.inc
--source include/have_debug.inc
--source include/have_debug_sync.inc
--source include/count_sessions.inc
--echo #
--echo # Bug #23070734 CONCURRENT TRUNCATE TABLES CAUSE STALLS
--echo #
create table t1 (f1 int ,f2 int,key(f2)) engine=innodb;
begin;
insert into t1 values (10,45),(20,78),(30,88),(40,23),(50,78),(60,11),(70,56),(80,90);
delete from t1;
connect (con2,localhost,root,,);
# Stop the purge thread
START TRANSACTION WITH CONSISTENT SNAPSHOT;
connection default;
# Ensure that the history list length will actually be decremented by purge.
SET @saved_frequency = @@GLOBAL.innodb_purge_rseg_truncate_frequency;
SET GLOBAL innodb_purge_rseg_truncate_frequency = 1;
commit;
connect (con1,localhost,root,,);
SET DEBUG_SYNC= 'buffer_pool_scan SIGNAL started WAIT_FOR finish_scan';
send truncate table t1;
connection con2;
SET DEBUG_SYNC= 'now WAIT_FOR started';
# Allow purge to proceed, by discarding our read view.
COMMIT;
disconnect con2;
connection default;
--source include/wait_all_purged.inc
SET GLOBAL innodb_purge_rseg_truncate_frequency = @saved_frequency;
SET DEBUG_SYNC = 'now SIGNAL finish_scan';
connection con1;
reap;
disconnect con1;
connection default;
SET DEBUG_SYNC = 'RESET';
drop table t1;
--source include/wait_until_count_sessions.inc
--source include/have_innodb.inc
--source include/have_debug.inc
--source include/have_debug_sync.inc
SET GLOBAL innodb_stats_persistent= ON;
CREATE TABLE t1 (t TEXT) ENGINE=InnoDB STATS_PERSISTENT=1;
--connect (con1,localhost,root,,test)
SET DEBUG_SYNC='ib_trunc_table_trunc_completing SIGNAL committed WAIT_FOR ever';
--send
TRUNCATE TABLE t1;
--connection default
SET DEBUG_SYNC='now WAIT_FOR committed';
--source include/restart_mysqld.inc
--disconnect con1
SELECT COUNT(*) FROM t1;
DROP TABLE t1;
#
# load tables with some significant amount of data and then truncate it.
#
#-----------------------------------------------------------------------------
#
# create test-bed
#
let $per_table = `select @@innodb_file_per_table`;
let $format = `select @@innodb_file_format`;
let $WL6501_TMP_DIR = `select @@tmpdir`;
let $WL6501_DATA_DIR = `select @@datadir`;
set innodb_strict_mode=OFF;
#-----------------------------------------------------------------------------
#
# create procedure to load data
#
delimiter |;
create procedure populate()
begin
declare i int default 1;
while (i <= 5000) do
insert into t1 values (i, 'a', 'b');
insert into t2 values (i, 'a', 'b');
insert into t3 values (i, 'a', 'b');
set i = i + 1;
end while;
end|
create procedure populate_small()
begin
declare i int default 10001;
while (i <= 12000) do
insert into t1 values (i, 'c', 'd');
insert into t2 values (i, 'a', 'b');
insert into t3 values (i, 'a', 'b');
set i = i + 1;
end while;
end|
delimiter ;|
#-----------------------------------------------------------------------------
#
# create and load the tables.
#
eval set global innodb_file_per_table = $wl6501_file_per_table;
--replace_regex /[0-9]+/NUMBER/
eval create table t1
(i int, c1 char(100), c2 char(100),
index c1_idx(c1))
engine=innodb row_format=$wl6501_row_fmt
key_block_size=$wl6501_kbs;
eval create table t2
(i int, c1 char(100), c2 char(100),
index c1_idx(c1))
engine=innodb row_format=$wl6501_row_fmt
key_block_size=$wl6501_kbs;
eval create temporary table t3
(i int, c1 char(100), c2 char(100),
index c1_idx(c1))
engine=innodb row_format=$wl6501_row_fmt
key_block_size=$wl6501_kbs;
#
select count(*) from t1;
select count(*) from t2;
select count(*) from t3;
begin;
call populate();
commit;
select count(*) from t1;
select count(*) from t2;
select count(*) from t3;
#
truncate table t1;
select count(*) from t1;
select count(*) from t2;
select count(*) from t3;
#
call populate_small();
select count(*) from t1;
select count(*) from t2;
select count(*) from t3;
#
truncate table t2;
truncate table t3;
select count(*) from t1;
select count(*) from t2;
select count(*) from t3;
#
call populate_small();
select count(*) from t1;
select count(*) from t2;
select count(*) from t3;
#
drop table t1;
drop table t2;
drop table t3;
#-----------------------------------------------------------------------------
#
# drop the procedure
#
drop procedure populate;
drop procedure populate_small;
#-----------------------------------------------------------------------------
#
# remove test-bed
#
eval set global innodb_file_format = $format;
eval set global innodb_file_per_table = $per_table;
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
set innodb_strict_mode=OFF;
create procedure populate()
begin
declare i int default 1;
while (i <= 5000) do
insert into t1 values (i, 'a', 'b');
insert into t2 values (i, 'a', 'b');
insert into t3 values (i, 'a', 'b');
set i = i + 1;
end while;
end|
create procedure populate_small()
begin
declare i int default 10001;
while (i <= 12000) do
insert into t1 values (i, 'c', 'd');
insert into t2 values (i, 'a', 'b');
insert into t3 values (i, 'a', 'b');
set i = i + 1;
end while;
end|
set global innodb_file_per_table = 1;
create table tNUMBER
(i int, cNUMBER char(NUMBER), cNUMBER char(NUMBER),
index cNUMBER_idx(cNUMBER))
engine=innodb row_format=compact
key_block_size=NUMBER;
Warnings:
Warning NUMBER InnoDB: ignoring KEY_BLOCK_SIZE=NUMBER unless ROW_FORMAT=COMPRESSED.
create table t2
(i int, c1 char(100), c2 char(100),
index c1_idx(c1))
engine=innodb row_format=compact
key_block_size=16;
Warnings:
Warning 1478 InnoDB: ignoring KEY_BLOCK_SIZE=16 unless ROW_FORMAT=COMPRESSED.
create temporary table t3
(i int, c1 char(100), c2 char(100),
index c1_idx(c1))
engine=innodb row_format=compact
key_block_size=16;
Warnings:
Warning 1478 InnoDB: KEY_BLOCK_SIZE is ignored for TEMPORARY TABLE.
Warning 1478 InnoDB: ignoring KEY_BLOCK_SIZE=16.
select count(*) from t1;
count(*)
0
select count(*) from t2;
count(*)
0
select count(*) from t3;
count(*)
0
begin;
call populate();
commit;
select count(*) from t1;
count(*)
5000
select count(*) from t2;
count(*)
5000
select count(*) from t3;
count(*)
5000
truncate table t1;
select count(*) from t1;
count(*)
0
select count(*) from t2;
count(*)
5000
select count(*) from t3;
count(*)
5000
call populate_small();
select count(*) from t1;
count(*)
2000
select count(*) from t2;
count(*)
7000
select count(*) from t3;
count(*)
7000
truncate table t2;
truncate table t3;
select count(*) from t1;
count(*)
2000
select count(*) from t2;
count(*)
0
select count(*) from t3;
count(*)
0
call populate_small();
select count(*) from t1;
count(*)
4000
select count(*) from t2;
count(*)
2000
select count(*) from t3;
count(*)
2000
drop table t1;
drop table t2;
drop table t3;
drop procedure populate;
drop procedure populate_small;
set global innodb_file_format = Barracuda;
Warnings:
Warning 131 Using innodb_file_format is deprecated and the parameter may be removed in future releases. See https://mariadb.com/kb/en/library/xtradbinnodb-file-format/
set global innodb_file_per_table = 1;
set innodb_strict_mode=OFF;
create procedure populate()
begin
declare i int default 1;
while (i <= 5000) do
insert into t1 values (i, 'a', 'b');
insert into t2 values (i, 'a', 'b');
insert into t3 values (i, 'a', 'b');
set i = i + 1;
end while;
end|
create procedure populate_small()
begin
declare i int default 10001;
while (i <= 12000) do
insert into t1 values (i, 'c', 'd');
insert into t2 values (i, 'a', 'b');
insert into t3 values (i, 'a', 'b');
set i = i + 1;
end while;
end|
set global innodb_file_per_table = 1;
create table tNUMBER
(i int, cNUMBER char(NUMBER), cNUMBER char(NUMBER),
index cNUMBER_idx(cNUMBER))
engine=innodb row_format=compressed
key_block_size=NUMBER;
create table t2
(i int, c1 char(100), c2 char(100),
index c1_idx(c1))
engine=innodb row_format=compressed
key_block_size=16;
create temporary table t3
(i int, c1 char(100), c2 char(100),
index c1_idx(c1))
engine=innodb row_format=compressed
key_block_size=16;
Warnings:
Warning 1478 InnoDB: KEY_BLOCK_SIZE is ignored for TEMPORARY TABLE.
Warning 1478 InnoDB: ignoring KEY_BLOCK_SIZE=16.
Warning 1478 InnoDB: ROW_FORMAT=COMPRESSED is ignored for TEMPORARY TABLE.
Warning 1478 InnoDB: assuming ROW_FORMAT=DYNAMIC.
select count(*) from t1;
count(*)
0
select count(*) from t2;
count(*)
0
select count(*) from t3;
count(*)
0
begin;
call populate();
commit;
select count(*) from t1;
count(*)
5000
select count(*) from t2;
count(*)
5000
select count(*) from t3;
count(*)
5000
truncate table t1;
select count(*) from t1;
count(*)
0
select count(*) from t2;
count(*)
5000
select count(*) from t3;
count(*)
5000
call populate_small();
select count(*) from t1;
count(*)
2000
select count(*) from t2;
count(*)
7000
select count(*) from t3;
count(*)
7000
truncate table t2;
truncate table t3;
select count(*) from t1;
count(*)
2000
select count(*) from t2;
count(*)
0
select count(*) from t3;
count(*)
0
call populate_small();
select count(*) from t1;
count(*)
4000
select count(*) from t2;
count(*)
2000
select count(*) from t3;
count(*)
2000
drop table t1;
drop table t2;
drop table t3;
drop procedure populate;
drop procedure populate_small;
set global innodb_file_format = Barracuda;
Warnings:
Warning 131 Using innodb_file_format is deprecated and the parameter may be removed in future releases. See https://mariadb.com/kb/en/library/xtradbinnodb-file-format/
set global innodb_file_per_table = 1;
set innodb_strict_mode=OFF;
create procedure populate()
begin
declare i int default 1;
while (i <= 5000) do
insert into t1 values (i, 'a', 'b');
insert into t2 values (i, 'a', 'b');
insert into t3 values (i, 'a', 'b');
set i = i + 1;
end while;
end|
create procedure populate_small()
begin
declare i int default 10001;
while (i <= 12000) do
insert into t1 values (i, 'c', 'd');
insert into t2 values (i, 'a', 'b');
insert into t3 values (i, 'a', 'b');
set i = i + 1;
end while;
end|
set global innodb_file_per_table = 0;
create table tNUMBER
(i int, cNUMBER char(NUMBER), cNUMBER char(NUMBER),
index cNUMBER_idx(cNUMBER))
engine=innodb row_format=compact
key_block_size=NUMBER;
Warnings:
Warning NUMBER InnoDB: KEY_BLOCK_SIZE requires innodb_file_per_table.
Warning NUMBER InnoDB: ignoring KEY_BLOCK_SIZE=NUMBER.
create table t2
(i int, c1 char(100), c2 char(100),
index c1_idx(c1))
engine=innodb row_format=compact
key_block_size=16;
Warnings:
Warning 1478 InnoDB: KEY_BLOCK_SIZE requires innodb_file_per_table.
Warning 1478 InnoDB: ignoring KEY_BLOCK_SIZE=16.
create temporary table t3
(i int, c1 char(100), c2 char(100),
index c1_idx(c1))
engine=innodb row_format=compact
key_block_size=16;
Warnings:
Warning 1478 InnoDB: KEY_BLOCK_SIZE is ignored for TEMPORARY TABLE.
Warning 1478 InnoDB: ignoring KEY_BLOCK_SIZE=16.
select count(*) from t1;
count(*)
0
select count(*) from t2;
count(*)
0
select count(*) from t3;
count(*)
0
begin;
call populate();
commit;
select count(*) from t1;
count(*)
5000
select count(*) from t2;
count(*)
5000
select count(*) from t3;
count(*)
5000
truncate table t1;
select count(*) from t1;
count(*)
0
select count(*) from t2;
count(*)
5000
select count(*) from t3;
count(*)
5000
call populate_small();
select count(*) from t1;
count(*)
2000
select count(*) from t2;
count(*)
7000
select count(*) from t3;
count(*)
7000
truncate table t2;
truncate table t3;
select count(*) from t1;
count(*)
2000
select count(*) from t2;
count(*)
0
select count(*) from t3;
count(*)
0
call populate_small();
select count(*) from t1;
count(*)
4000
select count(*) from t2;
count(*)
2000
select count(*) from t3;
count(*)
2000
drop table t1;
drop table t2;
drop table t3;
drop procedure populate;
drop procedure populate_small;
set global innodb_file_format = Barracuda;
Warnings:
Warning 131 Using innodb_file_format is deprecated and the parameter may be removed in future releases. See https://mariadb.com/kb/en/library/xtradbinnodb-file-format/
set global innodb_file_per_table = 1;
--loose-innodb-sys-tables
--loose-innodb-sys-tablespaces
--loose-innodb-sys-datafiles
--loose-skip-innodb-unsafe-truncate
This diff is collapsed.
#
# WL#6501: make truncate table atomic
#
# TC tries to hit crash point during truncate of
# compressed non-temp table residing in single tablespace
# with page-size=16k
--source include/have_innodb.inc
--source include/have_innodb_16k.inc
--source include/have_debug.inc
--source include/big_test.inc
# Valgrind would complain about memory leaks when we crash on purpose.
--source include/not_valgrind.inc
# Embedded server does not support crashing
--source include/not_embedded.inc
# Avoid CrashReporter popup on Mac
--source include/not_crashrep.inc
let $wl6501_file_per_table = 1;
let $wl6501_row_fmt = compressed;
let $wl6501_kbs = 16;
--source suite/innodb/include/innodb_wl6501_crash.inc
#
# WL#6501: make truncate table atomic
#
# TC tries to hit crash point during truncate of
# compressed non-temp table residing in single tablespace.
# with page-size=4k
--source include/have_innodb.inc
--source include/have_innodb_4k.inc
--source include/have_debug.inc
--source include/big_test.inc
# Valgrind would complain about memory leaks when we crash on purpose.
--source include/not_valgrind.inc
# Embedded server does not support crashing
--source include/not_embedded.inc
# Avoid CrashReporter popup on Mac
--source include/not_crashrep.inc
let $wl6501_file_per_table = 1;
let $wl6501_row_fmt = compressed;
let $wl6501_kbs = 4;
--source suite/innodb/include/innodb_wl6501_crash.inc
let $wl6501_temp = temporary;
--source suite/innodb/include/innodb_wl6501_crash_temp.inc
#
# WL#6501: make truncate table atomic
#
# TC tries to hit crash point during truncate of
# compressed non-temp table residing in single tablespace.
# with page-size=8k
--source include/have_innodb.inc
--source include/have_innodb_8k.inc
--source include/have_debug.inc
--source include/big_test.inc
# Valgrind would complain about memory leaks when we crash on purpose.
--source include/not_valgrind.inc
# Embedded server does not support crashing
--source include/not_embedded.inc
# Avoid CrashReporter popup on Mac
--source include/not_crashrep.inc
let $wl6501_file_per_table = 1;
let $wl6501_row_fmt = compressed;
let $wl6501_kbs = 8;
--source suite/innodb/include/innodb_wl6501_crash.inc
#
# WL#6501: make truncate table atomic
#
# load table with some significiant amount of data
# and then try truncate
--source include/have_innodb.inc
--source include/have_debug.inc
--source include/big_test.inc
--source include/have_innodb_16k.inc
# Valgrind would complain about memory leaks when we crash on purpose.
--source include/not_valgrind.inc
# Embedded server does not support crashing
--source include/not_embedded.inc
# Avoid CrashReporter popup on Mac
--source include/not_crashrep.inc
# Single-Tablespace/Non-Compressed
let $wl6501_file_per_table = 1;
let $wl6501_row_fmt = compact;
let $wl6501_kbs = 16;
--source suite/innodb_zip/include/innodb_wl6501_scale.inc
# Single-Tablespace/Compressed
let $wl6501_file_per_table = 1;
let $wl6501_row_fmt = compressed;
let $wl6501_kbs = 16;
--source suite/innodb_zip/include/innodb_wl6501_scale.inc
# System-Tablespace/Non-Compressed
let $wl6501_file_per_table = 0;
let $wl6501_row_fmt = compact;
let $wl6501_kbs = 16;
--source suite/innodb_zip/include/innodb_wl6501_scale.inc
......@@ -2974,6 +2974,20 @@ NUMERIC_BLOCK_SIZE 0
ENUM_VALUE_LIST NULL
READ_ONLY YES
COMMAND_LINE_ARGUMENT REQUIRED
VARIABLE_NAME INNODB_UNSAFE_TRUNCATE
SESSION_VALUE NULL
GLOBAL_VALUE ON
GLOBAL_VALUE_ORIGIN COMPILE-TIME
DEFAULT_VALUE ON
VARIABLE_SCOPE GLOBAL
VARIABLE_TYPE BOOLEAN
VARIABLE_COMMENT Use backup-unsafe TRUNCATE TABLE for compatibility with xtrabackup (on by default)
NUMERIC_MIN_VALUE NULL
NUMERIC_MAX_VALUE NULL
NUMERIC_BLOCK_SIZE NULL
ENUM_VALUE_LIST OFF,ON
READ_ONLY YES
COMMAND_LINE_ARGUMENT OPTIONAL
VARIABLE_NAME INNODB_USE_ATOMIC_WRITES
SESSION_VALUE NULL
GLOBAL_VALUE ON
......
......@@ -1140,6 +1140,73 @@ dict_recreate_index_tree(
return(FIL_NULL);
}
/*******************************************************************//**
Truncates the index tree but don't update SYSTEM TABLES.
@return DB_SUCCESS or error */
dberr_t
dict_truncate_index_tree_in_mem(
/*============================*/
dict_index_t* index) /*!< in/out: index */
{
mtr_t mtr;
bool truncate;
ulint space = index->space;
ut_ad(mutex_own(&dict_sys->mutex));
ut_ad(dict_table_is_temporary(index->table));
ulint type = index->type;
ulint root_page_no = index->page;
if (root_page_no == FIL_NULL) {
/* The tree has been freed. */
ib::warn() << "Trying to TRUNCATE a missing index of table "
<< index->table->name << "!";
truncate = false;
} else {
truncate = true;
}
bool found;
const page_size_t page_size(fil_space_get_page_size(space,
&found));
if (!found) {
/* It is a single table tablespace and the .ibd file is
missing: do nothing */
ib::warn()
<< "Trying to TRUNCATE a missing .ibd file of table "
<< index->table->name << "!";
}
/* If table to truncate resides in its on own tablespace that will
be re-created on truncate then we can ignore freeing of existing
tablespace objects. */
if (truncate) {
btr_free(page_id_t(space, root_page_no), page_size);
}
mtr_start(&mtr);
mtr_set_log_mode(&mtr, MTR_LOG_NO_REDO);
root_page_no = btr_create(
type, space, page_size, index->id, index, NULL, &mtr);
DBUG_EXECUTE_IF("ib_err_trunc_temp_recreate_index",
root_page_no = FIL_NULL;);
index->page = root_page_no;
mtr_commit(&mtr);
return(index->page == FIL_NULL ? DB_ERROR : DB_SUCCESS);
}
/*********************************************************************//**
Creates a table create graph.
@return own: table create node */
......
......@@ -3112,6 +3112,154 @@ void fil_truncate_log(fil_space_t* space, ulint size, mtr_t* mtr)
NULL, space->flags & ~FSP_FLAGS_MEM_MASK, mtr);
}
/** Truncate the tablespace to needed size.
@param[in] space_id id of tablespace to truncate
@param[in] size_in_pages truncate size.
@return true if truncate was successful. */
bool
fil_truncate_tablespace(
ulint space_id,
ulint size_in_pages)
{
/* Step-1: Prepare tablespace for truncate. This involves
stopping all the new operations + IO on that tablespace
and ensuring that related pages are flushed to disk. */
if (fil_prepare_for_truncate(space_id) != DB_SUCCESS) {
return(false);
}
/* Step-2: Invalidate buffer pool pages belonging to the tablespace
to re-create. Remove all insert buffer entries for the tablespace */
buf_LRU_flush_or_remove_pages(space_id, NULL);
/* Step-3: Truncate the tablespace and accordingly update
the fil_space_t handler that is used to access this tablespace. */
mutex_enter(&fil_system->mutex);
fil_space_t* space = fil_space_get_by_id(space_id);
/* The following code must change when InnoDB supports
multiple datafiles per tablespace. */
ut_a(UT_LIST_GET_LEN(space->chain) == 1);
fil_node_t* node = UT_LIST_GET_FIRST(space->chain);
ut_ad(node->is_open());
space->size = node->size = size_in_pages;
bool success = os_file_truncate(node->name, node->handle, 0);
if (success) {
os_offset_t size = os_offset_t(size_in_pages) * UNIV_PAGE_SIZE;
success = os_file_set_size(
node->name, node->handle, size,
FSP_FLAGS_HAS_PAGE_COMPRESSION(space->flags));
if (success) {
space->stop_new_ops = false;
space->is_being_truncated = false;
}
}
mutex_exit(&fil_system->mutex);
return(success);
}
/*******************************************************************//**
Prepare for truncating a single-table tablespace.
1) Check pending operations on a tablespace;
2) Remove all insert buffer entries for the tablespace;
@return DB_SUCCESS or error */
dberr_t
fil_prepare_for_truncate(
/*=====================*/
ulint id) /*!< in: space id */
{
char* path = 0;
fil_space_t* space = 0;
ut_a(!is_system_tablespace(id));
dberr_t err = fil_check_pending_operations(
id, FIL_OPERATION_TRUNCATE, &space, &path);
ut_free(path);
if (err == DB_TABLESPACE_NOT_FOUND) {
ib::error() << "Cannot truncate tablespace " << id
<< " because it is not found in the tablespace"
" memory cache.";
}
return(err);
}
/** Reinitialize the original tablespace header with the same space id
for single tablespace
@param[in] table table belongs to tablespace
@param[in] size size in blocks
@param[in] trx Transaction covering truncate */
void
fil_reinit_space_header_for_table(
dict_table_t* table,
ulint size,
trx_t* trx)
{
ulint id = table->space;
ut_a(!is_system_tablespace(id));
/* Invalidate in the buffer pool all pages belonging
to the tablespace. The buffer pool scan may take long
time to complete, therefore we release dict_sys->mutex
and the dict operation lock during the scan and aquire
it again after the buffer pool scan.*/
/* Release the lock on the indexes too. So that
they won't violate the latch ordering. */
dict_table_x_unlock_indexes(table);
row_mysql_unlock_data_dictionary(trx);
/* Lock the search latch in shared mode to prevent user
from disabling AHI during the scan */
btr_search_s_lock_all();
DEBUG_SYNC_C("buffer_pool_scan");
buf_LRU_flush_or_remove_pages(id, NULL);
btr_search_s_unlock_all();
row_mysql_lock_data_dictionary(trx);
dict_table_x_lock_indexes(table);
/* Remove all insert buffer entries for the tablespace */
ibuf_delete_for_discarded_space(id);
mutex_enter(&fil_system->mutex);
fil_space_t* space = fil_space_get_by_id(id);
/* The following code must change when InnoDB supports
multiple datafiles per tablespace. */
ut_a(UT_LIST_GET_LEN(space->chain) == 1);
fil_node_t* node = UT_LIST_GET_FIRST(space->chain);
space->size = node->size = size;
mutex_exit(&fil_system->mutex);
mtr_t mtr;
mtr_start(&mtr);
mtr.set_named_space(id);
fsp_header_init(id, size, &mtr);
mtr_commit(&mtr);
}
#ifdef UNIV_DEBUG
/** Increase redo skipped count for a tablespace.
@param[in] id space id */
......@@ -4971,6 +5119,7 @@ fil_io(
if (space->id != TRX_SYS_SPACE
&& UT_LIST_GET_LEN(space->chain) == 1
&& (srv_is_tablespace_truncated(space->id)
|| space->is_being_truncated
|| srv_was_tablespace_truncated(space))
&& req_type.is_read()) {
......@@ -5877,6 +6026,7 @@ truncate_t::truncate(
}
space->stop_new_ops = false;
space->is_being_truncated = false;
/* If we opened the file in this function, close it. */
if (!already_open) {
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment