pingcap / tidb

TiDB - the open-source, cloud-native, distributed SQL database designed for modern applications.
https://pingcap.com
Apache License 2.0
37.23k stars 5.84k forks source link

`insert` get duplicate key on auto_increment column after `rename table` #55846

Open joechenrh opened 2 months ago

joechenrh commented 2 months ago

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

This error is found by #52688. There are two ways to reproduce this error.

  1. Apply the following change on master and run TestRenameTable
diff --git a/pkg/executor/test/ddl/main_test.go b/pkg/executor/test/ddl/main_test.go
index f5f05d63d2..7159aefdb9 100644
--- a/pkg/executor/test/ddl/main_test.go
+++ b/pkg/executor/test/ddl/main_test.go
@@ -24,7 +24,7 @@ import (
 )

 func TestMain(m *testing.M) {
-   autoid.SetStep(5000)
+   autoid.SetStep(1)
    config.UpdateGlobal(func(conf *config.Config) {
        conf.Log.SlowThreshold = 30000 // 30s
        conf.TiKVClient.AsyncCommit.SafeWindow = 0
-- 
2.39.3 (Apple Git-146)

Start tidb server and run following SQLs:

create database test1;
create database test2;
create table test1.t (a int primary key auto_increment);
insert into test1.t values();
rename table test1.t to test2.t;
drop database test1;

Restart server and run

insert into test2.t values();

2. What did you expect to see? (Required)

  1. Test passed.
  2. Insert succeed.

3. What did you see instead (Required)

  1. Test failed.
  2. ERROR 1062 (23000): Duplicate entry '1' for key 't.PRIMARY'

4. What is your TiDB version? (Required)

joechenrh commented 2 months ago

/severity major

joechenrh commented 2 months ago

/remove-label may-affects-5.4 /remove-label may-affects-6.1 /remove-label may-affects-6.5 /remove-label may-affects-7.1 /remove-label may-affects-7.5 /remove-label may-affects-8.1

tiancaiamao commented 2 months ago

This one is the same with https://github.com/pingcap/tidb/issues/53329 And also caused by https://github.com/pingcap/tidb/pull/47892