Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query in transaction may return rows with same primary key column value #44200

Open
crazycs520 opened this issue May 26, 2023 · 1 comment
Open
Assignees
Labels
affects-5.0 This bug affects 5.0.x versions. affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects 5.4.x versions. affects-6.0 affects-6.1 affects-6.2 affects-6.3 affects-6.4 affects-6.5 affects-6.6 affects-7.0 affects-7.1 affects-7.5 affects-8.1 severity/major sig/transaction SIG:Transaction type/bug The issue is confirmed as a bug.

Comments

@crazycs520
Copy link
Contributor

crazycs520 commented May 26, 2023

Bug Report

Please answer these questions before submitting your issue. Thanks!

This is a sub-issue of #24195 (comment)

1. Minimal reproduce step (Required)

/* t1 */ drop table if exists t;
/* t1 */ create table t (c1 varchar(10), c2 int, primary key (c1) nonclustered);
/* t1 */ insert into t values ('a', 1);
/* t1 */ begin;
/* t1 */ update t set c1='b' where c1='a';
/* t2 */ begin;
/* t2 */ select * from t;
/* t2 */ insert into t values ('a', 2); -- Blocked until t1 commited.
/* t1 */ commit;
/* t2 */ select * from t use index(primary) order by c1, c2; -- [('a', 1), ('a', 2)]

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

MySQL 8.0.29 returns:

/* t2 */ select * from t use index(primary) order by c1, c2;
+----+----+
| c1 | c2 |
+----+----+
| a  | 2  |
+----+----+

3. What did you see instead (Required)

Currently TiDB returns:

/* t2 */ select * from t use index(primary) order by c1, c2;
+----+----+
| c1 | c2 |
+----+----+
| a  | 1  |
| a  | 2  |
+----+----+

Since column c1 is a primary key, the query result breaks the primary key unique constraint.

4. What is your TiDB version? (Required)

***************************[ 1. row ]***************************
tidb_version() | Release Version: v7.2.0-alpha-277-geca4e9be28
Edition: Community
Git Commit Hash: eca4e9be284e92aca3d5fbb16e44c6a7655fdf82
Git Branch: master
UTC Build Time: 2023-05-26 02:22:59
GoVersion: go1.20.2
Race Enabled: false
TiKV Min Version: 6.2.0-alpha
Check Table Before Drop: false
Store: unistore
@crazycs520 crazycs520 added type/bug The issue is confirmed as a bug. sig/transaction SIG:Transaction severity/major labels May 26, 2023
@crazycs520 crazycs520 self-assigned this May 26, 2023
@ti-chi-bot ti-chi-bot bot added may-affects-5.1 This bug maybe affects 5.1.x versions. may-affects-5.2 This bug maybe affects 5.2.x versions. may-affects-5.3 This bug maybe affects 5.3.x versions. may-affects-5.4 This bug maybe affects 5.4.x versions. may-affects-6.1 may-affects-6.5 may-affects-7.1 labels May 26, 2023
@crazycs520 crazycs520 changed the title Query in pessimistic transaction may return rows with same unique index column value Query in transaction may return rows with same unique index column value May 26, 2023
@crazycs520 crazycs520 added affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects 5.4.x versions. affects-6.0 affects-6.1 affects-6.2 affects-6.3 affects-6.4 affects-6.5 and removed may-affects-5.1 This bug maybe affects 5.1.x versions. may-affects-5.2 This bug maybe affects 5.2.x versions. may-affects-5.3 This bug maybe affects 5.3.x versions. may-affects-5.4 This bug maybe affects 5.4.x versions. may-affects-6.1 may-affects-6.5 may-affects-7.1 labels May 26, 2023
@crazycs520 crazycs520 changed the title Query in transaction may return rows with same unique index column value Query in transaction may return rows with same primary key column value May 31, 2023
@crazycs520
Copy link
Contributor Author

crazycs520 commented May 31, 2023

For unique index, currently, both TiDB and MySQL8.0.29 query in transaction may return rows with same unique index column value. Here is a case:

/* t1 */ drop table if exists t;
/* t1 */ create table t (c1 varchar(10), c2 int, unique key idx(c1));
/* t1 */ insert into t values ('a', 1);
/* t1 */ begin;
/* t1 */ update t set c1='b' where c1='a';
/* t2 */ begin;
/* t2 */ select * from t;
/* t2 */ insert into t values ('a', 2);
/* t1 */ commit;
/* t2 */ select * from t;

For the last query, both TiDB and MySQL8.0.29 return:

+----+----+
| c1 | c2 |
+----+----+
| a  |  1 |
| a  |  2 |
+----+----+

Since column c1 is a unique index, the query result breaks the unique index constraint.

Look like MySQL only check the primary key constrain when query in transaction, without check unique index constraint. I have reported this bug to MySQL. https://bugs.mysql.com/bug.php?id=111215

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-5.0 This bug affects 5.0.x versions. affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects 5.4.x versions. affects-6.0 affects-6.1 affects-6.2 affects-6.3 affects-6.4 affects-6.5 affects-6.6 affects-7.0 affects-7.1 affects-7.5 affects-8.1 severity/major sig/transaction SIG:Transaction type/bug The issue is confirmed as a bug.
Projects
None yet
2 participants