pingcap / pingcap/tidb

Inconsistent error when modifying primary key due to inconsistent blocking

Open
#54,745 1 comment 0 reactions 0 assignees View on GitHub
sig/transaction type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

When I tested Repeatable Read isolation levels on TiDB, I found a exception.
A simplified test case with result is as follows.
The sql (/* T2 */ UPDATE t0 SET c0 = 3 where c0 > 0;) should be blocked until T1 commits, but not in TiDB.

### 1. Minimal reproduce step (Required)

```sql
CREATE TABLE t0(c0 INT primary key);
INSERT INTO t0 VALUES (1);
INSERT INTO t0 VALUES (-1);

/* T1 */ BEGIN;
/* T2 */ BEGIN;
/* T1 */ SELECT * FROM t0;
/* T2 */ SELECT * FROM t0;
/* T1 */ INSERT INTO t0(c0) VALUES (2);
/* T2 */ UPDATE t0 SET c0 = 3 where c0 > 0;
/* T1 */ SELECT * FROM t0;
/* T2 */ SELECT * FROM t0;
/* T1 */ COMMIT;
/* T2 */ COMMIT;
```
### 2. What did you expect to see? (Required)
In MySQL
```sql
/* T1 */ BEGIN;
/* T2 */ BEGIN;
/* T1 */ SELECT * FROM t0;
+------+
| c0 |
+------+
| 1 |
| -1 |
+------+
2 rows in set (0.00 sec)
/* T2 */ SELECT * FROM t0;
+------+
| c0 |
+------+
| 1 |
| -1 |
+------+
2 rows in set (0.00 sec)
/* T1 */ INSERT INTO t0(c0) VALUES (2);
Query OK, 1 row affected (0.00 sec)
/* T2 */ UPDATE t0 SET c0 = 3 where c0 > 0; (This sql will be blocked until T1 commits.)
ERROR 1062 (23000): Duplicate entry '3' for key 'PRIMARY'
/* T1 */ SELECT * FROM t0;
+------+
| c0 |
+------+
| 1 |
| -1 |
| 2 |
+------+
3 rows in set (0.00 sec)
/* T1 */ COMMIT;
/* T2 */ SELECT * FROM t0;
+----+
| c0 |
+----+
| -1 |
| 1 |
+----+
2 rows in set (0.00 sec)
/* T2 */ COMMIT;
```
### 3. What did you see instead (Required)
```sql
/* T1 */ BEGIN;
/* T2 */ BEGIN;
/* T1 */ SELECT * FROM t0;
/* T2 */ SELECT * FROM t0;
+------+
| c0 |
+------+
| 1 |
| -1 |
+------+
2 rows in set (0.00 sec)
/* T1 */ INSERT INTO t0(c0) VALUES (2);
Query OK, 1 row affected (0.00 sec)
/* T2 */ UPDATE t0 SET c0 = 3 where c0 > 0; (This sql will not be blocked)
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
/* T1 */ SELECT * FROM t0;
+------+
| c0 |
+------+
| 1 |
| -1 |
| 2 |
+------+
3 rows in set (0.00 sec)
/* T2 */ SELECT * FROM t0;
+----+
| c0 |
+----+
| -1 |
| 3 |
+----+
2 rows in set (0.00 sec)
/* T1 */ COMMIT;
/* T2 */ COMMIT;
```
### 4. What is your TiDB version? (Required)

Release Version: v8.2.0
Edition: Community
Git Commit Hash: 821e491a20fbab36604b36b647b5bae26a2c1418
Git Branch: HEAD
UTC Build Time: 2024-07-05 09:16:25
GoVersion: go1.21.10
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.