From e0f62ef19e6e49c9f5ea35ef74d42127067ab8f7 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Tue, 20 Jun 2023 11:32:41 +0800 Subject: [PATCH] *: fix 'duplicate entry' error when using br to restore AUTO_ID_CACHE=1 tables (#44743) ref pingcap/tidb#38442, close pingcap/tidb#44716 --- br/pkg/backup/client.go | 6 +++++- ddl/db_integration_test.go | 4 ++-- executor/show.go | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/br/pkg/backup/client.go b/br/pkg/backup/client.go index eed0290c72971..c18648acc29da 100644 --- a/br/pkg/backup/client.go +++ b/br/pkg/backup/client.go @@ -631,7 +631,11 @@ func BuildBackupSchemas( case tableInfo.IsView() || !utils.NeedAutoID(tableInfo): // no auto ID for views or table without either rowID nor auto_increment ID. default: - globalAutoID, err = autoIDAccess.RowID().Get() + if tableInfo.SepAutoInc() { + globalAutoID, err = autoIDAccess.IncrementID(tableInfo.Version).Get() + } else { + globalAutoID, err = autoIDAccess.RowID().Get() + } } if err != nil { return errors.Trace(err) diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index 2dc4786e6ee77..2b30116cb8ea9 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -3176,7 +3176,7 @@ func TestAutoIncrementForceAutoIDCache(t *testing.T) { "t CREATE TABLE `t` (\n" + " `a` int(11) NOT NULL AUTO_INCREMENT,\n" + " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![auto_id_cache] AUTO_ID_CACHE=1 */")) + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=201 /*T![auto_id_cache] AUTO_ID_CACHE=1 */")) tk.MustExec("alter table t auto_increment=100;") tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 Can't reset AUTO_INCREMENT to 100 without FORCE option, using 201 instead")) tk.MustExec("insert into t values ()") @@ -3185,7 +3185,7 @@ func TestAutoIncrementForceAutoIDCache(t *testing.T) { "t CREATE TABLE `t` (\n" + " `a` int(11) NOT NULL AUTO_INCREMENT,\n" + " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![auto_id_cache] AUTO_ID_CACHE=1 */")) + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=212 /*T![auto_id_cache] AUTO_ID_CACHE=1 */")) tk.MustExec("drop table t") } diff --git a/executor/show.go b/executor/show.go index ce0fc40cd9059..35c5af504b605 100644 --- a/executor/show.go +++ b/executor/show.go @@ -1246,7 +1246,7 @@ func constructResultOfShowCreateTable(ctx sessionctx.Context, dbName *model.CISt fmt.Fprintf(buf, " COMPRESSION='%s'", tableInfo.Compression) } - incrementAllocator := allocators.Get(autoid.RowIDAllocType) + incrementAllocator := allocators.Get(autoid.AutoIncrementType) if hasAutoIncID && incrementAllocator != nil { autoIncID, err := incrementAllocator.NextGlobalAutoID() if err != nil {