--source include/have_innodb.inc # Restarting is not supported when testing the embedded server. --source include/not_embedded.inc --echo # --echo # MDEV-6076 Persistent AUTO_INCREMENT for InnoDB --echo # --echo # WL#6204 InnoDB persistent max value for autoinc columns --echo # --echo # Most of this test case is copied from the test innodb.autoinc_persist --echo # that was introduced in MySQL 8.0.0. The observable behaviour --echo # of MDEV-6076 is equivalent to WL#6204, with the exception that --echo # there is less buffering taking place and redo log checkpoints --echo # are not being treated specially. --echo # Due to less buffering, there is no debug instrumentation testing --echo # for MDEV-6076. --echo # --echo # Pre-create several tables SET SQL_MODE='STRICT_ALL_TABLES'; CREATE TABLE t1(a TINYINT AUTO_INCREMENT KEY) ENGINE = InnoDB; INSERT INTO t1 VALUES(0), (0), (0), (0), (-1), (-10), (0), (20), (30), (31); SELECT * FROM t1; CREATE TABLE t2(a TINYINT UNSIGNED AUTO_INCREMENT KEY) ENGINE = InnoDB; --error ER_WARN_DATA_OUT_OF_RANGE INSERT INTO t2 VALUES(-5); INSERT INTO t2 VALUES(0), (0), (0), (0), (8), (10), (0), (20), (30), (31); SELECT * FROM t2; CREATE TABLE t3(a SMALLINT AUTO_INCREMENT KEY) ENGINE = InnoDB; INSERT INTO t3 VALUES(0), (0), (0), (0), (-1), (-10), (0), (20), (30), (31), (1024), (4096); SELECT * FROM t3; CREATE TABLE t4(a SMALLINT UNSIGNED AUTO_INCREMENT KEY) ENGINE = InnoDB; --error ER_WARN_DATA_OUT_OF_RANGE INSERT INTO t4 VALUES(-5); INSERT INTO t4 VALUES(0), (0), (0), (0), (8), (10), (0), (20), (30), (31), (1024), (4096); SELECT * FROM t4; CREATE TABLE t5(a MEDIUMINT AUTO_INCREMENT KEY) ENGINE = InnoDB; INSERT INTO t5 VALUES(0), (0), (0), (0), (-1), (-10), (0), (20), (30), (31), (1000000), (1000005); SELECT * FROM t5; CREATE TABLE t6(a MEDIUMINT UNSIGNED AUTO_INCREMENT KEY) ENGINE = InnoDB; --error ER_WARN_DATA_OUT_OF_RANGE INSERT INTO t6 VALUES(-5); INSERT INTO t6 VALUES(0), (0), (0), (0), (8), (10), (0), (20), (30), (31), (1000000), (1000005); SELECT * FROM t6; CREATE TABLE t7(a INT AUTO_INCREMENT KEY) ENGINE = InnoDB; INSERT INTO t7 VALUES(0), (0), (0), (0), (-1), (-10), (0), (20), (30), (31), (100000000), (100000008); SELECT * FROM t7; CREATE TABLE t8(a INT UNSIGNED AUTO_INCREMENT KEY) ENGINE = InnoDB; --error ER_WARN_DATA_OUT_OF_RANGE INSERT INTO t8 VALUES(-5); INSERT INTO t8 VALUES(0), (0), (0), (0), (8), (10), (0), (20), (30), (31), (100000000), (100000008); SELECT * FROM t8; CREATE TABLE t9(a BIGINT AUTO_INCREMENT KEY) ENGINE = InnoDB; INSERT INTO t9 VALUES(0), (0), (0), (0), (-1), (-10), (0), (20), (30), (31), (100000000000), (100000000006); SELECT * FROM t9; CREATE TABLE t10(a BIGINT UNSIGNED AUTO_INCREMENT KEY) ENGINE = InnoDB; --error ER_WARN_DATA_OUT_OF_RANGE INSERT INTO t10 VALUES(-5); INSERT INTO t10 VALUES(0), (0), (0), (0), (8), (10), (0), (20), (30), (31), (100000000000), (100000000006); SELECT * FROM t10; CREATE TABLE t11(a FLOAT AUTO_INCREMENT KEY) ENGINE = InnoDB; INSERT INTO t11 VALUES(0), (0), (0), (0), (-1), (-10), (0), (20), (30), (31); SELECT * FROM t11; CREATE TABLE t12(a DOUBLE AUTO_INCREMENT KEY) ENGINE = InnoDB; INSERT INTO t12 VALUES(0), (0), (0), (0), (-1), (-10), (0), (20), (30), (31); SELECT * FROM t12; --echo # Scenario 1: Normal restart, to test if the counters are persisted --echo # Scenario 2: Delete some values, to test the counters should not be the --echo # one which is the largest in current table DELETE FROM t1 WHERE a > 30; SELECT MAX(a) AS `Expect 30` FROM t1; DELETE FROM t3 WHERE a > 2000; SELECT MAX(a) AS `Expect 2000` FROM t3; DELETE FROM t5 WHERE a > 1000000; SELECT MAX(a) AS `Expect 1000000` FROM t5; DELETE FROM t7 WHERE a > 100000000; SELECT MAX(a) AS `Expect 100000000` FROM t7; DELETE FROM t9 WHERE a > 100000000000; SELECT MAX(a) AS `Expect 100000000000` FROM t9; CREATE TABLE t13(a INT AUTO_INCREMENT PRIMARY KEY) ENGINE = InnoDB, AUTO_INCREMENT = 1234; --source include/restart_mysqld.inc SHOW CREATE TABLE t13; INSERT INTO t13 VALUES(0); SELECT a AS `Expect 1234` FROM t13; DROP TABLE t13; INSERT INTO t1 VALUES(0), (0); SELECT MAX(a) AS `Expect 33` FROM t1; INSERT INTO t3 VALUES(0), (0); SELECT MAX(a) AS `Expect 4098` FROM t3; INSERT INTO t5 VALUES(0), (0); SELECT MAX(a) AS `Expect 1000007` FROM t5; INSERT INTO t7 VALUES(0), (0); SELECT MAX(a) AS `Expect 100000010` FROM t7; INSERT INTO t9 VALUES(0), (0); SELECT MAX(a) AS `Expect 100000000008` FROM t9; --echo # Scenario 3: Insert some bigger counters, the next counter should start --echo # from there INSERT INTO t1 VALUES(40), (0); INSERT INTO t1 VALUES(42), (0); SELECT a AS `Expect 43, 42` FROM t1 ORDER BY a DESC LIMIT 4; INSERT INTO t3 VALUES(5000), (0); INSERT INTO t3 VALUES(5010), (0); SELECT a AS `Expect 5011, 5010` FROM t3 ORDER BY a DESC LIMIT 4; INSERT INTO t5 VALUES(1000010), (0); INSERT INTO t5 VALUES(1000020), (0); SELECT a AS `Expect 1000021, 1000020` FROM t5 ORDER BY a DESC LIMIT 4; INSERT INTO t7 VALUES(100000020), (0); INSERT INTO t7 VALUES(100000030), (0); SELECT a AS `Expect 100000031, 100000030` FROM t7 ORDER BY a DESC LIMIT 4; INSERT INTO t9 VALUES(100000000010), (0); INSERT INTO t9 VALUES(100000000020), (0); SELECT a AS `Expect 100000000021, 100000000020` FROM t9 ORDER BY a DESC LIMIT 4; --echo # Scenario 4: Update some values, to test the counters should be updated --echo # to the bigger value, but not smaller value. INSERT INTO t1 VALUES(50), (55); # Updating to bigger value will update the auto-increment counter UPDATE t1 SET a = 105 WHERE a = 5; # Updating to smaller value will not update the counter UPDATE t1 SET a = 100 WHERE a = 55; --echo # This should insert 102, 106, 107, and make next counter 109. INSERT INTO t1 VALUES(102), (0), (0); SELECT a AS `Expect 107, 106` FROM t1 ORDER BY a DESC LIMIT 2; DELETE FROM t1 WHERE a > 105; INSERT INTO t1 VALUES(0); SELECT MAX(a) AS `Expect 109` FROM t1; --echo # Test the same things on t3, t5, t7, t9, to test if DDTableBuffer would --echo # be updated accordingly INSERT INTO t3 VALUES(60), (65); # Updating to bigger value will update the auto-increment counter UPDATE t3 SET a = 6005 WHERE a = 5; # Updating to smaller value will not update the counter UPDATE t3 SET a = 6000 WHERE a = 60; --echo # This should insert 6002, 6006, 6007, and make next counter 6009. INSERT INTO t3 VALUES(6002), (0), (0); SELECT a AS `Expect 6007, 6006` FROM t3 ORDER BY a DESC LIMIT 2; DELETE FROM t3 WHERE a > 6005; INSERT INTO t3 VALUES(0); SELECT MAX(a) AS `Expect 6009` FROM t3; INSERT INTO t5 VALUES(100), (200); # Updating to bigger value will update the auto-increment counter UPDATE t5 SET a = 1000105 WHERE a = 5; # Updating to smaller value will not update the counter UPDATE t5 SET a = 1000100 WHERE a = 100; --echo # This should insert 1000102, 1000106, 1000107, and make next counter --echo # 1000109. INSERT INTO t5 VALUES(1000102), (0), (0); SELECT a AS `Expect 1000107, 1000106` FROM t5 ORDER BY a DESC LIMIT 2; DELETE FROM t5 WHERE a > 1000105; INSERT INTO t5 VALUES(0); SELECT MAX(a) AS `Expect 1000109` FROM t5; INSERT INTO t7 VALUES(100), (200); # Updating to bigger value will update the auto-increment counter UPDATE t7 SET a = 100000105 WHERE a = 5; # Updating to smaller value will not update the counter UPDATE t7 SET a = 100000100 WHERE a = 100; --echo # This should insert 100000102, 1100000106, 100000107, and make next --echo # counter 100000109. INSERT INTO t7 VALUES(100000102), (0), (0); SELECT a AS `Expect 100000107, 100000106` FROM t7 ORDER BY a DESC LIMIT 2; DELETE FROM t7 WHERE a > 100000105; INSERT INTO t7 VALUES(0); SELECT MAX(a) AS `Expect 100000109` FROM t7; INSERT INTO t9 VALUES(100), (200); # Updating to bigger value will update the auto-increment counter UPDATE t9 SET a = 100000000105 WHERE a = 5; # Updating to smaller value will not update the counter UPDATE t9 SET a = 100000000100 WHERE a = 100; --echo # This should insert 100000000102, 100000000106, 100000000107, and make --echo # next counter 100000000109. INSERT INTO t9 VALUES(100000000102), (0), (0); SELECT a AS `Expect 100000000107, 100000000106` FROM t9 ORDER BY a DESC LIMIT 2; DELETE FROM t9 WHERE a > 100000000105; INSERT INTO t9 VALUES(0); SELECT MAX(a) AS `Expect 100000000109` FROM t9; --source include/restart_mysqld.inc INSERT INTO t1 VALUES(0), (0); SELECT a AS `Expect 110, 111` FROM t1 ORDER BY a DESC LIMIT 2; INSERT INTO t3 VALUES(0), (0); SELECT a AS `Expect 6010, 6011` FROM t3 ORDER BY a DESC LIMIT 2; INSERT INTO t5 VALUES(0), (0); SELECT a AS `Expect 1100111, 1100110` FROM t5 ORDER BY a DESC LIMIT 2; INSERT INTO t7 VALUES(0), (0); SELECT a AS `Expect 100000111, 100000110` FROM t7 ORDER BY a DESC LIMIT 2; INSERT INTO t9 VALUES(0), (0); SELECT a AS `Expect 100000000111, 100000000110` FROM t9 ORDER BY a DESC LIMIT 2; --echo # Scenario 5: Test kill the server INSERT INTO t1 VALUES(125); DELETE FROM t1 WHERE a = 125; INSERT INTO t3 VALUES(6100); DELETE FROM t3 WHERE a = 6100; INSERT INTO t5 VALUES(1100200); DELETE FROM t5 WHERE a = 1100200; INSERT INTO t7 VALUES(100000200); DELETE FROM t7 WHERE a = 100000200; --echo # Ensure that all changes before the server is killed are persisted. set global innodb_flush_log_at_trx_commit=1; INSERT INTO t9 VALUES(100000000200); DELETE FROM t9 WHERE a = 100000000200; --let $shutdown_timeout=0 --source include/restart_mysqld.inc INSERT INTO t1 VALUES(0); SELECT a AS `Expect 126` FROM t1 ORDER BY a DESC LIMIT 1; INSERT INTO t3 VALUES(0); SELECT a AS `Expect 6101` FROM t3 ORDER BY a DESC LIMIT 1; INSERT INTO t5 VALUES(0); SELECT a AS `Expect 1100201` FROM t5 ORDER BY a DESC LIMIT 1; INSERT INTO t7 VALUES(0); SELECT a AS `Expect 100000201` FROM t7 ORDER BY a DESC LIMIT 1; INSERT INTO t9 VALUES(0); SELECT a AS `Expect 100000000201` FROM t9 ORDER BY a DESC LIMIT 1; --echo # Scenario 6: Test truncate will reset the counters to 0 TRUNCATE TABLE t1; TRUNCATE TABLE t3; TRUNCATE TABLE t5; TRUNCATE TABLE t7; TRUNCATE TABLE t9; INSERT INTO t1 VALUES(0), (0); SELECT * FROM t1; INSERT INTO t3 VALUES(0), (0); SELECT * FROM t3; INSERT INTO t5 VALUES(0), (0); SELECT * FROM t5; INSERT INTO t7 VALUES(0), (0); SELECT * FROM t7; INSERT INTO t9 VALUES(0), (0); SELECT * FROM t9; --echo # Ensure that all changes before the server is killed are persisted. set global innodb_flush_log_at_trx_commit=1; TRUNCATE TABLE t1; TRUNCATE TABLE t3; TRUNCATE TABLE t5; TRUNCATE TABLE t7; TRUNCATE TABLE t9; --echo # Scenario 7: Test explicit rename table won't change the counter RENAME TABLE t9 to t19; INSERT INTO t19 VALUES(0), (0); SELECT * FROM t19; --source include/restart_mysqld.inc INSERT INTO t1 VALUES(0), (0); SELECT * FROM t1; INSERT INTO t3 VALUES(0), (0); SELECT * FROM t3; INSERT INTO t5 VALUES(0), (0); SELECT * FROM t5; INSERT INTO t7 VALUES(0), (0); SELECT * FROM t7; INSERT INTO t19 VALUES(0), (0); SELECT * FROM t19; DELETE FROM t19 WHERE a = 4; RENAME TABLE t19 to t9; INSERT INTO t9 VALUES(0), (0); SELECT * FROM t9; TRUNCATE TABLE t9; INSERT INTO t9 VALUES(0), (0); SELECT * FROM t9; --echo # Scenario 8: Test ALTER TABLE operations INSERT INTO t3 VALUES(0), (0), (100), (200), (1000); SELECT * FROM t3; DELETE FROM t3 WHERE a > 300; SELECT MAX(a) AS `Expect 200` FROM t3; --echo # This will not change the counter to 150, but to 201, which is the next --echo # of current max counter in the table ALTER TABLE t3 AUTO_INCREMENT = 150; SHOW CREATE TABLE t3; INSERT INTO t3 VALUES(0); SELECT MAX(a) AS `Expect 201` FROM t3; --echo # This will change the counter to 500, which is bigger than any counter --echo # in the table ALTER TABLE t3 AUTO_INCREMENT = 500; SHOW CREATE TABLE t3; INSERT INTO t3 VALUES(0); SELECT MAX(a) AS `Expect 500` FROM t3; TRUNCATE TABLE t3; ALTER TABLE t3 AUTO_INCREMENT = 100; SHOW CREATE TABLE t3; INSERT INTO t3 VALUES(0), (0); SELECT * FROM t3; INSERT INTO t3 VALUES(150), (180); UPDATE t3 SET a = 200 WHERE a = 150; INSERT INTO t3 VALUES(220); --echo # This still fails to set to 120, but just 221 ALTER TABLE t3 AUTO_INCREMENT = 120; SHOW CREATE TABLE t3; INSERT INTO t3 VALUES(0); SELECT MAX(a) AS `Expect 221` FROM t3; DELETE FROM t3 WHERE a > 120; ALTER TABLE t3 AUTO_INCREMENT = 120; SHOW CREATE TABLE t3; --echo # MDEV-6076: Test adding an AUTO_INCREMENT COLUMN CREATE TABLE mdev6076a (b INT) ENGINE=InnoDB; INSERT INTO mdev6076a VALUES(2),(1); CREATE TABLE mdev6076b (b INT) ENGINE=InnoDB; INSERT INTO mdev6076b VALUES(2),(1); --error ER_ALTER_OPERATION_NOT_SUPPORTED_REASON ALTER TABLE mdev6076a ADD COLUMN a SERIAL FIRST, LOCK=NONE; ALTER TABLE mdev6076a ADD COLUMN a SERIAL FIRST, ALGORITHM=INPLACE; ALTER TABLE mdev6076b ADD COLUMN a SERIAL FIRST, AUTO_INCREMENT=100, ALGORITHM=INPLACE; --echo # MDEV-6076: Test root page split and page_create_empty() CREATE TABLE mdev6076empty (b SERIAL, pad CHAR(255) NOT NULL DEFAULT '') ENGINE=InnoDB; BEGIN; --echo # Insert records in descending order of AUTO_INCREMENT, --echo # causing a page split on the very last insert. --echo # Without the fix in btr_page_empty() this would lose the counter value. --echo # Without the fix in page_create_empty() the counter value would be lost --echo # when ROLLBACK deletes the last row. --disable_query_log let $i= 55; while ($i) { eval INSERT INTO mdev6076empty SET b=$i; dec $i; } --enable_query_log ROLLBACK; --source include/restart_mysqld.inc INSERT INTO t3 VALUES(0); SELECT MAX(a) AS `Expect 120` FROM t3; INSERT INTO mdev6076a SET b=NULL; SELECT * FROM mdev6076a; INSERT INTO mdev6076b SET b=NULL; SELECT * FROM mdev6076b; INSERT INTO mdev6076empty SET b=NULL; SELECT * FROM mdev6076empty; DROP TABLE mdev6076a, mdev6076b, mdev6076empty; INSERT INTO t3 VALUES(0), (0), (200), (210); --echo # Test the different algorithms in ALTER TABLE --let $template = t3 --let $algorithm = INPLACE --let $table = t_inplace --source suite/innodb/include/autoinc_persist_alter.inc --let $algorithm = COPY --let $table = t_copy --source suite/innodb/include/autoinc_persist_alter.inc --echo # Scenario 9: Test the sql_mode = NO_AUTO_VALUE_ON_ZERO CREATE TABLE t30 (a INT NOT NULL AUTO_INCREMENT PRIMARY KEY, b INT, key(b)) ENGINE = InnoDB; set SQL_MODE = NO_AUTO_VALUE_ON_ZERO; INSERT INTO t30 VALUES(NULL, 1), (200, 2), (0, 3); INSERT INTO t30(b) VALUES(4), (5), (6), (7); SELECT * FROM t30 ORDER BY b; ALTER TABLE t30 MODIFY b MEDIUMINT; SELECT * FROM t30 ORDER BY b; --echo # Ensure that all changes before the server is killed are persisted. set global innodb_flush_log_at_trx_commit=1; CREATE TABLE t31 (a INT) ENGINE = InnoDB; INSERT INTO t31 VALUES(1), (2); ALTER TABLE t31 ADD b INT AUTO_INCREMENT PRIMARY KEY; INSERT INTO t31 VALUES(3, 0), (4, NULL), (5, NULL); --error ER_DUP_ENTRY INSERT INTO t31 VALUES(6, 0); SELECT * FROM t31; SET SQL_MODE = 0; --echo # Scenario 10: Rollback would not rollback the counter CREATE TABLE t32 ( a BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT) ENGINE=InnoDB; INSERT INTO t32 VALUES(0), (0); --echo # Ensure that all changes before the server is killed are persisted. set global innodb_flush_log_at_trx_commit=1; START TRANSACTION; INSERT INTO t32 VALUES(0), (0); SELECT MAX(a) AS `Expect 4` FROM t32; DELETE FROM t32 WHERE a >= 2; ROLLBACK; --echo # Scenario 11: Test duplicate primary key/secondary key will not stop --echo # increasing the counter CREATE TABLE t33 ( a BIGINT NOT NULL PRIMARY KEY, b BIGINT NOT NULL AUTO_INCREMENT, KEY(b)) ENGINE = InnoDB; INSERT INTO t33 VALUES(1, NULL); INSERT INTO t33 VALUES(2, NULL); --error ER_DUP_ENTRY INSERT INTO t33 VALUES(2, NULL); INSERT INTO t33 VALUES(3, NULL); SELECT MAX(b) AS `Expect 4` FROM t33; TRUNCATE TABLE t33; INSERT INTO t33 VALUES(1, NULL); INSERT INTO t33 VALUES(2, NULL); set global innodb_flush_log_at_trx_commit=1; START TRANSACTION; UPDATE t33 SET a = 10 WHERE a = 1; --error ER_DUP_ENTRY INSERT INTO t33 VALUES(2, NULL); COMMIT; --source include/restart_mysqld.inc --echo # This will not insert 0 INSERT INTO t31(a) VALUES(6), (0); SELECT * FROM t31; DROP TABLE t31; set SQL_MODE = NO_AUTO_VALUE_ON_ZERO; DELETE FROM t30 WHERE a = 0; UPDATE t30 set a = 0 where b = 5; SELECT * FROM t30 ORDER BY b; DELETE FROM t30 WHERE a = 0; UPDATE t30 SET a = NULL WHERE b = 6; UPDATE t30 SET a = 300 WHERE b = 7; SELECT * FROM t30 ORDER BY b; SET SQL_MODE = 0; SELECT MAX(a) AS `Expect 2` FROM t32; INSERT INTO t32 VALUES(0), (0); SELECT MAX(a) AS `Expect 6` FROM t32; FLUSH TABLES t33 FOR EXPORT; let MYSQLD_DATADIR=`select @@datadir`; perl; do "$ENV{MTR_SUITE_DIR}/include/innodb-util.pl"; ib_backup_tablespaces("test", "t33"); EOF UNLOCK TABLES; DROP TABLE t33; CREATE TABLE t33 ( a BIGINT NOT NULL PRIMARY KEY, b BIGINT NOT NULL AUTO_INCREMENT, KEY(b)) ENGINE = InnoDB; ALTER TABLE t33 DISCARD TABLESPACE; perl; do "$ENV{MTR_SUITE_DIR}/include/innodb-util.pl"; ib_discard_tablespaces("test", "t33"); ib_restore_tablespaces("test", "t33"); EOF ALTER TABLE t33 IMPORT TABLESPACE; INSERT INTO t33 VALUES(3, NULL); SELECT MAX(b) AS `Expect 4` FROM t33; SELECT * FROM t33; DROP TABLE t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t30, t32, t33;