pingcap / pingcap/tidb

Inconsistent Auto-increment Primary Key Allocation Under Concurrent Operations Violates Transaction Consistency

Open
#67,400 2 comments 0 reactions 0 assignees View on GitHub
contribution may-affects-7.1 may-affects-7.5 may-affects-8.1 may-affects-8.5 severity/major sig/transaction type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

Under the REPEATABLE-READ isolation level, a specific sequence of concurrent operations causes a gap in the auto-increment primary key allocation. This violates transaction consistency because a rolled-back transaction should not have a permanent effect on the auto-increment sequence, leading to discontinuity in the primary key values.

### 1. Minimal reproduce step (Required)
CREATE TABLE mtest(c0 DOUBLE, c1 DOUBLE, c2 INT PRIMARY KEY AUTO_INCREMENT) ;
INSERT INTO mtest(c0, c2) VALUES (0.6, NULL);
INSERT INTO mtest(c1, c2) VALUES (0.3, NULL);
/* s1 /BEGIN;
/* s2 /BEGIN;
/* s2 /INSERT IGNORE INTO mtest(c1, c2) VALUES (0.63, NULL);
/* s2 /DELETE FROM mtest WHERE TRUE;
/* s2 /ROLLBACK;
/* s1 /INSERT IGNORE INTO mtest(c0, c1, c2) VALUES (0.11, 0.09, NULL);
/* s1 /COMMIT;
mysql> select * from mtest;
+------+------+----+
| c0 | c1 | c2 |
+------+------+----+
| 0.6 | NULL | 1 |
| NULL | 0.3 | 2 |
| 0.11 | 0.09 | 4 |
+------+------+----+
3 rows in set (0.00 sec)
### 2. What did you expect to see? (Required)
--------- Expected result:---------
mysql> select * from mtest;
+------+------+----+
| c0 | c1 | c2 |
+------+------+----+
| 0.6 | NULL | 1 |
| NULL | 0.3 | 2 |
| 0.11 | 0.09 | 3 |
+------+------+----+
3 rows in set (0.00 sec)
### 3. What did you see instead (Required)
mysql> select * from mtest;
+------+------+----+
| c0 | c1 | c2 |
+------+------+----+
| 0.6 | NULL | 1 |
| NULL | 0.3 | 2 |
| 0.11 | 0.09 | 4 |
+------+------+----+
3 rows in set (0.00 sec)
### 4. What is your TiDB version? (Required)
Release Version: v7.5.1
Edition: Community
Git Commit Hash: 7d16cc79e81bbf573124df3fd9351c26963f3e70
Git Branch: heads/refs/tags/v7.5.1
UTC Build Time: 2024-02-27 14:28:32
GoVersion: go1.21.6
Race Enabled: false
Check Table Before Drop: false
Store: tikv

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.