pingcap / pingcap/tidb

Select can return duplicate values in primary key column, whereas the transaction can still commit

Open
#37,925 2 comments 0 reactions 0 assignees View on GitHub
affects-5.0 affects-5.1 affects-5.2 affects-5.3 affects-5.4 affects-6.0 affects-6.1 affects-6.2 affects-6.3 affects-6.4 affects-6.5 may-affects-4.0 severity/moderate sig/transaction type/bug
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
drop table if exists tbl_4;
create table tbl_4 ( col_16 decimal ( 38 , 4 ) not null ,col_17 time default '23:52:24.00' ,col_18 char ( 235 ) collate utf8_bin ,col_19 tinyint unsigned not null default 151 ,col_20 smallint default 9203 , primary key ( col_20 ) /*T![clustered_index] nonclustered */ ,key idx_8 ( col_18 ( 1 ) ) ) charset utf8mb4 collate utf8mb4_bin ;

insert into tbl_4 values ( 661,'17:22:57.00','^0',155,-31298 );
insert into tbl_4 values ( 3579.0326,null,'n~KgM',220,-31136 );
insert into tbl_4 values ( 1.4,'05:06:55.00','Q+~(AUo!36S2OD9',35,-12681 );
insert into tbl_4 values ( 66.9,'10:04:39.00','_9ILR!QJ',145,-15820 );
insert into tbl_4 values ( 29082.6117,'00:37:01.00','mdTuS((uApbUxJIAO',196,-30900 );
insert into tbl_4 values ( 57,'11:09:40.00','=@%~n@w+9q',77,30926 );
insert into tbl_4 values ( 308382.06,null,'~Ewt)Z+1mlLzafKu',195,15880 );
insert into tbl_4 values ( 0.26,'03:32:16.00','_$lL2m0AFK9fQQs',178,-29720 );
insert into tbl_4 values ( 993.7,'10:14:55.00','m~a1nM(Xsf_#h7*',4,26689 );
insert into tbl_4 values ( 0.998,'19:57:45.00','I5Q8Xx1',66,-10830 );

begin pessimistic;
update tbl_4 set tbl_4.col_17 = '11:16:44.00' ,tbl_4.col_17 = '03:51:50.00' ,tbl_4.col_16 = 9 where tbl_4.col_20 in ( -31298 ,-10876 ,-31136 ,-29720 ) and not( tbl_4.col_18 != 'n~KgM' ) ;
select col_20 from tbl_4 order by col_20;
commit;
```

### 2. What did you expect to see? (Required)

The `select` returns duplicated values on col_20 which is the primary key. I suppose the transaction should not be allowed to commit.

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

The select result is
```
select col_20 from tbl_4 order by col_20;
+--------+
| col_20 |
+--------+
| -31298 |
| -31298 |
| -31136 |
| -30900 |
| -29720 |
| -29720 |
| -15820 |
| -12681 |
| -10830 |
| 15880 |
| 26689 |
| 30926 |
+--------+
```

And the commit succeeds.

Also note that if we `select * from tbl_4` the result is fine.

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

Master nightly.

```
Release Version: v6.3.0-alpha
Edition: Community
Git Commit Hash: 29f83a0b25926390c757f71a84baa2322a289f78
Git Branch: heads/refs/tags/v6.3.0-alpha
UTC Build Time: 2022-09-18 14:25:30
GoVersion: go1.19
Race Enabled: false
TiKV Min Version: 6.2.0-alpha
Check Table Before Drop: false
Store: tikv
```

Contributor guide

Open the contributing guide

Research direction

Start by running the supplied SQL reproduction against the stated TiDB version and compare the SELECT and COMMIT behavior. The fix is complete when the primary-key query cannot return duplicate col_20 values and the transaction behavior matches the expected consistency guarantee.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, sql
Domain
databases, distributed-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.