Skip to content

Commit 0235a52

Browse files
committed
MDEV-10087 mysqld_update()/mysql_delete() continues execution even after subquery with JOIN gets error from storage engine
The issue is that record_should_be_deleted() returns true in mysql_delete() even if sub-select with join gets error from storage engine when DELETE FROM ... WHERE ... IN (SELECT ...) statement is executed. The same is true for mysql_update() where select->skip_record() returns true even if sub-select with join gets error from storage engine. In the test case if sub-select is chosen as deadlock victim the whole transaction is rolled back during sub-select execution, but mysql_delete()/mysql_update() continues transaction execution and invokes table->delete_row() as record_should_be_deleted() wrongly returns true in mysql_delete() and table->update_row() as select->skip_record(thd) wrongly returns 1 for mysql_update(). record_should_be_deleted() wrogly returns true because thd->is_error() returns false SQL_SELECT::skip_record() invoked from record_should_be_deleted(). It's supposed that THD error should be set in rr_handle_error() called from rr_sequential() during sub-select JOIN::exec_inner() execution. But rr_handle_error() does not set THD error because READ_RECORD::print_error is not set in JOIN_TAB::read_record. READ_RECORD::print_error should be initialized in init_read_record()/init_read_record_idx(). But make_join_readinfo() does not invoke init_read_record()/init_read_record_idx() for JOIN_TAB::read_record. The fix is to set JOIN_TAB::read_record.print_error in make_join_readinfo(), i.e. in the same place where JOIN_TAB::read_record.table is set. Reviewed by Sergey Petrunya.
1 parent 0ffdcf6 commit 0235a52

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
CREATE TABLE t1 (
2+
pkey int NOT NULL PRIMARY KEY,
3+
c int
4+
) ENGINE=InnoDB;
5+
INSERT INTO t1 VALUES(1,1);
6+
CREATE TABLE t2 (
7+
pkey int NOT NULL PRIMARY KEY,
8+
c int
9+
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
10+
INSERT INTO t2 VALUES (2, NULL);
11+
CREATE TABLE t3 (c int) engine = InnoDB;
12+
INSERT INTO t3 VALUES (10), (20), (30), (40), (50);
13+
connect con1, localhost,root,,;
14+
connection default;
15+
START TRANSACTION;
16+
UPDATE t3 SET c=c+1000;
17+
SELECT * FROM t1 FOR UPDATE;
18+
pkey c
19+
1 1
20+
connection con1;
21+
START TRANSACTION;
22+
DELETE FROM t2 WHERE c NOT IN (SELECT ref_0.pkey FROM t1 AS ref_0 INNER JOIN t1 AS ref_1 ON ref_0.c = ref_0.pkey);
23+
connection default;
24+
SELECT * FROM t2 FOR UPDATE;
25+
pkey c
26+
2 NULL
27+
COMMIT;
28+
connection con1;
29+
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
30+
COMMIT;
31+
connection default;
32+
START TRANSACTION;
33+
UPDATE t3 SET c=c+1000;
34+
SELECT * FROM t1 FOR UPDATE;
35+
pkey c
36+
1 1
37+
connection con1;
38+
START TRANSACTION;
39+
UPDATE t2 SET pkey=pkey+10 WHERE c NOT IN (SELECT ref_0.pkey FROM t1 AS ref_0 INNER JOIN t1 AS ref_1 ON ref_0.c = ref_0.pkey);
40+
connection default;
41+
SELECT * FROM t2 FOR UPDATE;
42+
pkey c
43+
2 NULL
44+
COMMIT;
45+
connection con1;
46+
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
47+
COMMIT;
48+
disconnect con1;
49+
connection default;
50+
DROP TABLE t1,t2,t3;
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
--source include/have_innodb.inc
2+
--source include/count_sessions.inc
3+
4+
CREATE TABLE t1 (
5+
pkey int NOT NULL PRIMARY KEY,
6+
c int
7+
) ENGINE=InnoDB;
8+
9+
INSERT INTO t1 VALUES(1,1);
10+
11+
CREATE TABLE t2 (
12+
pkey int NOT NULL PRIMARY KEY,
13+
c int
14+
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
15+
16+
INSERT INTO t2 VALUES (2, NULL);
17+
18+
# The following table is to increase tansaction weight on deadlock resolution
19+
CREATE TABLE t3 (c int) engine = InnoDB;
20+
INSERT INTO t3 VALUES (10), (20), (30), (40), (50);
21+
22+
--let $i= 2
23+
--let $delete= 2
24+
--let $update= 1
25+
--connect(con1, localhost,root,,)
26+
27+
while($i) {
28+
--connection default
29+
START TRANSACTION; # trx 1
30+
# The following update is necessary to increase the transaction weight, which is
31+
# calculated as the number of locks + the number of undo records during deadlock
32+
# report. Victim's transaction should have minimum weight. We need trx 2 to be
33+
# choosen as victim, that's why we need to increase the current transaction
34+
# weight.
35+
UPDATE t3 SET c=c+1000;
36+
SELECT * FROM t1 FOR UPDATE;
37+
38+
--connection con1
39+
START TRANSACTION; # trx 2
40+
# 1) read record from t2, lock it
41+
# 2) check if the read record should be deleted, i.e. read record from t1,
42+
# as the record from t1 is locked by trx 1, the subselect will be suspended.
43+
# see 'while' loop in mysql_delete() or mysql_update() and
44+
# select->skip_record(thd) call for details.
45+
if ($i == $delete) {
46+
--send DELETE FROM t2 WHERE c NOT IN (SELECT ref_0.pkey FROM t1 AS ref_0 INNER JOIN t1 AS ref_1 ON ref_0.c = ref_0.pkey)
47+
}
48+
if ($i == $update) {
49+
--send UPDATE t2 SET pkey=pkey+10 WHERE c NOT IN (SELECT ref_0.pkey FROM t1 AS ref_0 INNER JOIN t1 AS ref_1 ON ref_0.c = ref_0.pkey)
50+
}
51+
52+
--connection default
53+
let $wait_condition=
54+
SELECT count(*) = 1 FROM information_schema.processlist
55+
WHERE (state = 'Sending data' OR state = "Updating")
56+
AND (info LIKE 'delete from t2 where%' OR
57+
info LIKE 'UPDATE t2 SET pkey=pkey+10 WHERE%');
58+
--source include/wait_condition.inc
59+
60+
# The record from t2 is locked by the previous delete, so trx 2 is waiting for
61+
# trx 1, and trx 1 will be blocked by trx 2 with the following SELECT. So we
62+
# have deadlock here. And trx 2 is chosen as deadlock victim as trx 1 has
63+
# greater weight.
64+
SELECT * FROM t2 FOR UPDATE;
65+
COMMIT;
66+
67+
--connection con1
68+
# If the bug is not fixed, there will be assertion failure as
69+
# mysql_delete()/mysql_update() will continue execution despite its subselect
70+
# got deadlock error
71+
--error ER_LOCK_DEADLOCK
72+
--reap
73+
COMMIT;
74+
--dec $i
75+
}
76+
77+
--disconnect con1
78+
79+
--connection default
80+
DROP TABLE t1,t2,t3;
81+
--source include/wait_until_count_sessions.inc

sql/sql_select.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12509,6 +12509,7 @@ make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after)
1250912509
uint jcl= tab->used_join_cache_level;
1251012510
tab->read_record.table= table;
1251112511
tab->read_record.unlock_row= rr_unlock_row;
12512+
tab->read_record.print_error= true;
1251212513
tab->sorted= sorted;
1251312514
sorted= 0; // only first must be sorted
1251412515

0 commit comments

Comments
 (0)