pingcap / pingcap/tidb

unexpected behavior of a transaction for the same data item

Open
#53,807 6 comments 0 reactions 0 assignees View on GitHub
type/question
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)

```SQL
/* init */ CREATE TABLE t0 (c0 INT);
/* init */ INSERT INTO t0 (c0) VALUES(1),(2);

/* t1 */ BEGIN;
/* t2 */ BEGIN;
/* t1 */ SELECT * FROM t0;
/* t2 */ SELECT * FROM t0;
/* t1 */ UPDATE t0 SET c0=123;
/* t2 */ DELETE FROM t0 WHERE ((t0.c0)>=(10));
/* t1 */ SELECT * FROM t0;
/* t1 */ COMMIT;
/* t2 */ SELECT * FROM t0;
/* t2 */ COMMIT;

INSERT INTO t0 (c0) VALUES(1),(2); /* After transaction 1,2 commit and before transaction 3,4 start */

/* t3 */ BEGIN;
/* t4 */ BEGIN;
/* t3 */ SELECT * FROM t0;
/* t4 */ SELECT * FROM t0;
/* t3 */ UPDATE t0 SET c0=123;
/* t4 */ DELETE FROM t0 WHERE ((t0.c0)>=(10));
/* t3 */ SELECT * FROM t0;
/* t3 */ COMMIT;
/* t4 */ SELECT * FROM t0;
/* t4 */ COMMIT;
```
### 2. What did you expect to see? (Required)

```SQL
mysql> /* t4 */ SELECT * FROM t0;
+------+
| c0 |
+------+
| 1 |
| 2 |
+------+
```

### 3. What did you see instead (Required)

```SQL
mysql> SHOW VARIABLES LIKE 'transaction_isolation';
+-----------------------+-----------------+
| Variable_name | Value |
+-----------------------+-----------------+
| transaction_isolation | REPEATABLE-READ |
+-----------------------+-----------------+
1 row in set (0.00 sec)
mysql> /* t1 */ BEGIN;
mysql> /* t2 */ BEGIN;
mysql> /* t1 */ SELECT * FROM t0;
+------+
| c0 |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
mysql> /* t2 */ SELECT * FROM t0;
+------+
| c0 |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
mysql> /* t1 */ UPDATE t0 SET c0=123;
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> /* t2 */ DELETE FROM t0 WHERE ((t0.c0)>=(10)); // Not Block
Query OK, 0 rows affected (0.00 sec)
mysql> /* t1 */ SELECT * FROM t0;
+------+
| c0 |
+------+
| 123 |
| 123 |
+------+
2 rows in set (0.00 sec)
mysql> /* t1 */ COMMIT;
mysql> /* t2 */ SELECT * FROM t0;
+------+
| c0 |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
mysql> /* t2 */ COMMIT;

mysql> INSERT INTO t0 (c0) VALUES(1),(2); /* After transaction 1,2 commit and before transaction 3,4 start */
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> /* t3 */ BEGIN;
mysql> /* t4 */ BEGIN;
mysql> /* t3 */ SELECT * FROM t0;
+------+
| c0 |
+------+
| 123 |
| 123 |
| 1 |
| 2 |
+------+
4 rows in set (0.01 sec)
mysql> /* t4 */ SELECT * FROM t0;
+------+
| c0 |
+------+
| 123 |
| 123 |
| 1 |
| 2 |
+------+
4 rows in set (0.00 sec)
mysql> /* t3 */ UPDATE t0 SET c0=123;
Query OK, 2 rows affected (0.00 sec)
Rows matched: 4 Changed: 2 Warnings: 0
mysql> /* t4 */ DELETE FROM t0 WHERE ((t0.c0)>=(10)); // Block
Query OK, 4 rows affected (8.01 sec)
mysql> /* t3 */ SELECT * FROM t0;
+------+
| c0 |
+------+
| 123 |
| 123 |
| 123 |
| 123 |
+------+
4 rows in set (0.00 sec)
mysql> /* t3 */ COMMIT;
mysql> /* t4 */ SELECT * FROM t0;
Empty set (0.00 sec) // **Logic Bug**
mysql> /* t4 */ COMMIT;
```

### 4. What is your TiDB version? (Required)

Release Version: v8.1.0
Edition: Community
Git Commit Hash: 945d07c5d5c7a1ae212f6013adfb187f2de24b23
Git Branch: HEAD
UTC Build Time: 2024-05-21 03:51:57
GoVersion: go1.21.10
Race Enabled: false
Check Table Before Drop: false
Store: tikv

### 5. Logic
According to the definition of isolation level RR, transaction T4 should not see changes from transaction T3, just as transaction T2 does not see changes from T1. Therefore, DB should not delete the entire table.

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.