pingcap / pingcap/tidb

Update with sub query uses incorrect snapshot in RR isolation level

Open
#45,677 1 comment 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)

init database with 1 row as below.
```mysql
create table t(a int, b int);
insert into t values(1,1);
```
Then start two session and update that row in session2 after session1 starts a transaction.
Then execute a update with sub query in session1.
```mysql
session1 > begin;
session2 > update t set b=2 where a=1; -- now the data is (1,2)
session1 > update t set b=3 where b=(select b from t where b=2); -- if update reads the latest committed data, it will update the database to (1,3)
session1 > commit;
session1 > select * from t; -- check the final database statement
```
### 2. What did you expect to see? (Required)

The last select returns:
```mysql
MySQL [test]> select * from t;
+------+------+
| a | b |
+------+------+
| 1 | 3 |
+------+------+
1 row in set (0.00 sec)
```

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

The last select returns:
```mysql
MySQL [test]> select * from t;
+------+------+
| a | b |
+------+------+
| 1 | 2 |
+------+------+
1 row in set (0.00 sec)
```

We also check the behaviour of normal update in the case below.

```mysql
-- start init
create table t(a int, b int);
insert into t values(1,1);
-- end init, and the database statement is the same as the case above.
session1 > begin;
session2 > update t set b=2 where a=1; -- now the data is (1,2)
session1 > update t set b=3 where b=2; -- if update reads the latest committed data, it will update the database to (1,3)
session1 > commit;
session1 > select * from t; -- check the final database statement
```

Then it returns:
```mysql
MySQL [test]> select * from t;
+------+------+
| a | b |
+------+------+
| 1 | 3 |
+------+------+
1 row in set (0.00 sec)
```
as expected.

Note that ` update t set b=3 where b=2;` and ` update t set b=3 where b=(select b from t where b=2);` have the same meaning in these two cases. It;s confusing to have different results.

### 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 |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
```

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.