1
--source include/have_innodb_plugin.inc
4
--echo # Bug #40113: Embedded SELECT inside UPDATE or DELETE can timeout
8
CREATE TABLE t1 (a int, b int, PRIMARY KEY (a,b)) ENGINE=InnoDB;
10
INSERT INTO t1 (a,b) VALUES (1070109,99);
12
CREATE TABLE t2 (b int, a int, PRIMARY KEY (b)) ENGINE=InnoDB;
14
INSERT INTO t2 (b,a) VALUES (7,1070109);
20
SELECT b FROM t2 WHERE b=7 FOR UPDATE;
22
CONNECT (addconroot, localhost, root,,);
23
CONNECTION addconroot;
27
--error ER_LOCK_WAIT_TIMEOUT
28
SELECT b FROM t2 WHERE b=7 FOR UPDATE;
30
--error ER_LOCK_WAIT_TIMEOUT
31
INSERT INTO t1 (a) VALUES ((SELECT a FROM t2 WHERE b=7));
33
--error ER_LOCK_WAIT_TIMEOUT
34
UPDATE t1 SET a='7000000' WHERE a=(SELECT a FROM t2 WHERE b=7);
36
--error ER_LOCK_WAIT_TIMEOUT
37
DELETE FROM t1 WHERE a=(SELECT a FROM t2 WHERE b=7);
42
DISCONNECT addconroot;
46
--echo # End of 5.0 tests
49
--echo # Bug#46539 Various crashes on INSERT IGNORE SELECT + SELECT
53
drop table if exists t1;
55
create table t1 (a int primary key auto_increment,
56
b int, index(b)) engine=innodb;
57
insert into t1 (b) values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
60
select * from t1 where b=5 for update;
61
connect (con1, localhost, root,,);
63
--error ER_LOCK_WAIT_TIMEOUT
64
insert ignore into t1 (b) select a as b from t1;
70
set autocommit=default;
74
--echo # Bug #37183 insert ignore into .. select ... hangs
75
--echo # after deadlock was encountered
77
connect (con1,localhost,root,,);
78
create table t1(id int primary key,v int)engine=innodb;
79
insert into t1 values (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7);
80
create table t2 like t1;
84
update t1 set v=id*2 where id=1;
88
update t1 set v=id*2 where id=2;
92
update t1 set v=id*2 where id=2;
96
insert ignore into t2 select * from t1 where id=1;
108
--echo # Bug#41756 Strange error messages about locks from InnoDB
111
drop table if exists t1;
113
--echo # In the default transaction isolation mode, and/or with
114
--echo # innodb_locks_unsafe_for_binlog=OFF, handler::unlock_row()
115
--echo # in InnoDB does nothing.
116
--echo # Thus in order to reproduce the condition that led to the
117
--echo # warning, one needs to relax isolation by either
118
--echo # setting a weaker tx_isolation value, or by turning on
119
--echo # the unsafe replication switch.
120
--echo # For testing purposes, choose to tweak the isolation level,
121
--echo # since it's settable at runtime, unlike
122
--echo # innodb_locks_unsafe_for_binlog, which is
123
--echo # only a command-line switch.
125
set @@session.tx_isolation="read-committed";
127
--echo # Prepare data. We need a table with a unique index,
128
--echo # for join_read_key to be used. The other column
129
--echo # allows to control what passes WHERE clause filter.
130
create table t1 (a int primary key, b int) engine=innodb;
131
--echo # Let's make sure t1 has sufficient amount of rows
132
--echo # to exclude JT_ALL access method when reading it,
133
--echo # i.e. make sure that JT_EQ_REF(a) is always preferred.
134
insert into t1 values (1,1), (2,null), (3,1), (4,1),
135
(5,1), (6,1), (7,1), (8,1), (9,1), (10,1),
136
(11,1), (12,1), (13,1), (14,1), (15,1),
137
(16,1), (17,1), (18,1), (19,1), (20,1);
139
--echo # Demonstrate that for the SELECT statement
140
--echo # used later in the test JT_EQ_REF access method is used.
144
select 1 from t1 natural join (select 2 as a, 1 as b union all
145
select 2 as a, 2 as b) as t2 for update;
148
--echo # Demonstrate that the reported SELECT statement
149
--echo # no longer produces warnings.
151
select 1 from t1 natural join (select 2 as a, 1 as b union all
152
select 2 as a, 2 as b) as t2 for update;
155
--echo # Demonstrate that due to lack of inter-sweep "reset" function,
156
--echo # we keep some non-matching records locked, even though we know
157
--echo # we could unlock them.
158
--echo # To do that, show that if there is only one distinct value
159
--echo # for a in t2 (a=2), we will keep record (2,null) in t1 locked.
160
--echo # But if we add another value for "a" to t2, say 6,
161
--echo # join_read_key cache will be pruned at least once,
162
--echo # and thus record (2, null) in t1 will get unlocked.
165
select 1 from t1 natural join (select 2 as a, 1 as b union all
166
select 2 as a, 2 as b) as t2 for update;
167
connect (con1,localhost,root,,);
169
--echo # Switching to connection con1
171
--echo # We should be able to delete all records from t1 except (2, null),
172
--echo # since they were not locked.
174
--echo # Delete in series of 3 records so that full scan
175
--echo # is not used and we're not blocked on record (2,null)
176
delete from t1 where a in (1,3,4);
177
delete from t1 where a in (5,6,7);
178
delete from t1 where a in (8,9,10);
179
delete from t1 where a in (11,12,13);
180
delete from t1 where a in (14,15,16);
181
delete from t1 where a in (17,18);
182
delete from t1 where a in (19,20);
184
--echo # Record (2, null) is locked. This is actually unnecessary,
185
--echo # because the previous select returned no rows.
186
--echo # Just demonstrate the effect.
188
--error ER_LOCK_WAIT_TIMEOUT
192
--echo # Switching to connection default
195
--echo # Show that the original contents of t1 is intact:
199
--echo # Have a one more record in t2 to show that
200
--echo # if join_read_key cache is purned, the current
201
--echo # row under the cursor is unlocked (provided, this row didn't
202
--echo # match the partial WHERE clause, of course).
203
--echo # Sic: the result of this test dependent on the order of retrieval
204
--echo # of records --echo # from the derived table, if !
205
--echo # We use DELETE to disable the JOIN CACHE. This DELETE modifies no
206
--echo # records. It also should leave no InnoDB row locks.
209
delete t1.* from t1 natural join (select 2 as a, 2 as b union all
210
select 0 as a, 0 as b) as t2;
211
--echo # Demonstrate that nothing was deleted form t1
214
--echo # Switching to connection con1
217
--echo # Since there is another distinct record in the derived table
218
--echo # the previous matching record in t1 -- (2,null) -- was unlocked.
220
--echo # We will need the contents of the table again.
225
--echo # Switching to connection default
230
--echo # Before this patch, we could wrongly unlock a record
231
--echo # that was cached and later used in a join. Demonstrate that
232
--echo # this is no longer the case.
233
--echo # Sic: this test is also order-dependent (i.e. the
234
--echo # the bug would show up only if the first record in the union
235
--echo # is retreived and processed first.
237
--echo # Verify that JT_EQ_REF is used.
240
select 1 from t1 natural join (select 3 as a, 2 as b union all
241
select 3 as a, 1 as b) as t2 for update;
243
--echo # Lock the record.
244
select 1 from t1 natural join (select 3 as a, 2 as b union all
245
select 3 as a, 1 as b) as t2 for update;
246
--echo # Switching to connection con1
249
--echo # We should not be able to delete record (3,1) from t1,
250
--echo # (previously it was possible).
252
--error ER_LOCK_WAIT_TIMEOUT
253
delete from t1 where a=3;
254
--echo # Switching to connection default
259
set @@session.tx_isolation=default;
263
--echo # End of 5.1 tests