From 5935e3ad17b4e89f6a267c60bfc9ad2756997ba2 Mon Sep 17 00:00:00 2001 From: Rahul Malik Date: Wed, 22 Mar 2023 13:16:07 +0530 Subject: [PATCH] PS-8687 Incorrect autoinc in imported tablespace after restart server https://jira.percona.com/browse/PS-8687 Problem: If server is restarted after an import of tablespace. It does not show correct autoinc value and throws duplicate error Analysis: Server presist autoinc value only if table->autoinc_field_no is set During import tablespace server sets autoinc_field_no to UNDEF which makes inserts not to presist autoinc in dd_buffer table Fix: Call dict_table_autoinc_set_col_pos after import tablespace to set the autoinc_field_no which makes inserts to presist autoinc inside row_ins_clust_index_entry_low --- .../suite/innodb/r/export_autoinc.result | 39 ++++++++++++++ mysql-test/suite/innodb/t/export_autoinc.test | 53 +++++++++++++++++++ storage/innobase/handler/ha_innodb.cc | 2 + 3 files changed, 94 insertions(+) create mode 100644 mysql-test/suite/innodb/r/export_autoinc.result create mode 100644 mysql-test/suite/innodb/t/export_autoinc.test diff --git a/mysql-test/suite/innodb/r/export_autoinc.result b/mysql-test/suite/innodb/r/export_autoinc.result new file mode 100644 index 000000000000..f1878d880eda --- /dev/null +++ b/mysql-test/suite/innodb/r/export_autoinc.result @@ -0,0 +1,39 @@ +drop table if exists t1; +CREATE TABLE t1(i serial); +INSERT INTO t1 VALUES(null); +CREATE TABLE t2(i int , j serial); +INSERT INTO t2 VALUES(1, null); +FLUSH TABLES test.t1 FOR EXPORT; +UNLOCK TABLES; +ALTER TABLE test.t1 DISCARD TABLESPACE; +ALTER TABLE test.t1 IMPORT TABLESPACE; +CHECK TABLE test.t1; +Table Op Msg_type Msg_text +test.t1 check status OK +SHOW CREATE TABLE test.t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `i` bigint unsigned NOT NULL AUTO_INCREMENT, + UNIQUE KEY `i` (`i`) +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci +SELECT * FROM test.t1; +i +1 +# Flush the table and put IBD/CFG files aside for later import. +FLUSH TABLE t1 FOR EXPORT; +"copy done" +UNLOCK TABLES; +ALTER TABLE t1 DISCARD TABLESPACE; +ALTER TABLE t1 IMPORT TABLESPACE; +INSERT INTO t1 VALUES(null); +FLUSH TABLE t2 FOR EXPORT; +"copy done" +UNLOCK TABLES; +ALTER TABLE t2 DISCARD TABLESPACE; +ALTER TABLE t2 IMPORT TABLESPACE; +INSERT INTO t2 VALUES(1, null); +# restart +INSERT INTO t1 VALUES(null); +DROP TABLE t1; +INSERT INTO t2 VALUES(3,null); +DROP TABLE t2; diff --git a/mysql-test/suite/innodb/t/export_autoinc.test b/mysql-test/suite/innodb/t/export_autoinc.test new file mode 100644 index 000000000000..5f85b342030c --- /dev/null +++ b/mysql-test/suite/innodb/t/export_autoinc.test @@ -0,0 +1,53 @@ +--disable_warnings +drop table if exists t1; +--enable_warnings + +CREATE TABLE t1(i serial); +INSERT INTO t1 VALUES(null); + +CREATE TABLE t2(i int , j serial); +INSERT INTO t2 VALUES(1, null); +--source suite/innodb/include/import.inc + +--echo # Flush the table and put IBD/CFG files aside for later import. +FLUSH TABLE t1 FOR EXPORT; + +--copy_file $MYSQLD_DATADIR/test/t1.ibd $MYSQLD_DATADIR/t1.ibd_back +--echo "copy done" +--copy_file $MYSQLD_DATADIR/test/t1.cfg $MYSQLD_DATADIR/t1.cfg_back + +UNLOCK TABLES; + +ALTER TABLE t1 DISCARD TABLESPACE; + +--copy_file $MYSQLD_DATADIR/t1.ibd_back $MYSQLD_DATADIR/test/t1.ibd +--copy_file $MYSQLD_DATADIR/t1.cfg_back $MYSQLD_DATADIR/test/t1.cfg + +ALTER TABLE t1 IMPORT TABLESPACE; + +INSERT INTO t1 VALUES(null); + +FLUSH TABLE t2 FOR EXPORT; + +--copy_file $MYSQLD_DATADIR/test/t2.ibd $MYSQLD_DATADIR/t2.ibd_back +--echo "copy done" +--copy_file $MYSQLD_DATADIR/test/t2.cfg $MYSQLD_DATADIR/t2.cfg_back + +UNLOCK TABLES; + +ALTER TABLE t2 DISCARD TABLESPACE; + +--copy_file $MYSQLD_DATADIR/t2.ibd_back $MYSQLD_DATADIR/test/t2.ibd +--copy_file $MYSQLD_DATADIR/t2.cfg_back $MYSQLD_DATADIR/test/t2.cfg + +ALTER TABLE t2 IMPORT TABLESPACE; + +INSERT INTO t2 VALUES(1, null); + +--source include/restart_mysqld.inc + +INSERT INTO t1 VALUES(null); +DROP TABLE t1; + +INSERT INTO t2 VALUES(3,null); +DROP TABLE t2; diff --git a/storage/innobase/handler/ha_innodb.cc b/storage/innobase/handler/ha_innodb.cc index 6cbd4bf10aba..8e811669c839 100644 --- a/storage/innobase/handler/ha_innodb.cc +++ b/storage/innobase/handler/ha_innodb.cc @@ -15890,6 +15890,8 @@ int ha_innobase::discard_or_import_tablespace(bool discard, err = row_import_for_mysql(dict_table, table_def, m_prebuilt); if (err == DB_SUCCESS) { + dict_table_autoinc_set_col_pos( + dict_table, table->found_next_number_field->field_index()); info(HA_STATUS_TIME | HA_STATUS_CONST | HA_STATUS_VARIABLE | HA_STATUS_AUTO); }