Read Duplicate Key in Transaction
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
Please answer these questions before submitting your issue. Thanks!
### 1. Minimal reproduce step (Required)
init a table with a primary key and a attribute column:
```mysql
create table t (k int, v int , primary key(k) NONCLUSTERED);
insert into t values(1,1);
```
Then start 2 sessions under repeatable read isolation:
| session1 | session2 |
| ------------------------ | :------------------------------------ |
| begin; | |
| | begin; |
| delete from t where k=1; | |
| commit; | |
| | insert into t values(1,2); -- query A |
| | select * from t where k=1; -- query B |
### 2. What did you expect to see? (Required)
Query B returns (1,2), as MySQL does, since query A is success and primary key should not be duplicated.
### 3. What did you see instead (Required)
Returns both (1,1) and (1,2), which is against the unique constraint of primary key:
```mysql
+---+------+
| k | v |
+---+------+
| 1 | 1 |
| 1 | 2 |
+---+------+
2 rows in set (0.00 sec)
```
Though SELECT and INSERT may read different snapshots in the same tranasction, each snapshot should follow the unique constraint.
But it seems that query B returns data from both snapshot at transaction's begin and snapshot of query A, while there is a constraint conflict between the two snapshots.
Maybe checking the constraint of data when executing SELECT can help fixing the bug.
### 4. What is your TiDB version? (Required)
```mysql
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version()
|
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v7.1.0
Edition: Community
Git Commit Hash: 635a4362235e8a3c0043542e629532e3c7bb2756
Git Branch: heads/refs/tags/v7.1.0
UTC Build Time: 2023-05-30 10:58:57
GoVersion: go1.20.3
Race Enabled: false
TiKV Min Version: 6.2.0-alpha
Check Table Before Drop: false
Store: tikv |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
```
Contributor guide
Assessment
This issue has not been assessed yet.