Skip to content

Commit a1da655

Browse files
committed
MDEV-16686 DDL in procedure propagates no locking to tables locked by DML
TABLE_LIST parsed from procedure code is transferred into tables to lock for INSERT. The procedure code is CREATE VIEW so its TABLE_LIST is parsed as TL_IGNORE, but same view exists and when existing view is opened mysql_make_view() uses same TABLE_LIST that was initialized from CREATE VIEW and then added as part of prelocking context. So existing view is opened and its table is assigned TL_IGNORE from prelocking context. Finally, INSERT has TABLE_LIST duplication: the one that was parsed from INSERT; the another one came from procedure prelocking, its lock_type came from the procedure code and the real table was found via existing view. The sequence of execution: 1. Procedure p is compiled as part of open_and_process_routine(), its code is parsed and create_or_alter_view_finalize() initializes v TABLE_LIST as TL_IGNORE; 2. Procedure p prelocking adds v to prelocking_ctx with TL_IGNORE; 3. DML prelocking adds v from prelocking_ctx; 4. View is opened, mysql_make_view() assigns t lock_type from v; 5. open_and_lock_tables() attempts to lock t with TL_IGNORE. The fix skips TL_IGNORE at 2. when table list parsed by procedure is added for prelocking: if (my_hash_insert(&m_sptabs, (uchar *)tab)) return FALSE; m_sptabs designation was defined as strictly for prelocking: /** Multi-set representing optimized list of tables to be locked by this routine. Does not include tables which are used by invoked routines. @note For prelocking-free SPs this multiset is constructed too. We do so because the same instance of sp_head may be called both in prelocked mode and in non-prelocked mode. */ HASH m_sptabs; The fix was proposed by Sergei Golubchik <serg@mariadb.org>.
1 parent 69ec1d6 commit a1da655

File tree

4 files changed

+164
-1
lines changed

4 files changed

+164
-1
lines changed

mysql-test/main/tablelock.result

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,74 @@ f1 int(11) YES NULL
5555
insert into t1 values(2);
5656
drop table t1;
5757
unlock tables;
58+
#
59+
# Bug#19988193 ASSERTION `(*TABLES)->REGINFO.LOCK_TYPE >= TL_READ'
60+
# FAILED IN LOCK_EXTERNAL
61+
#
62+
CREATE TABLE t1(a INT);
63+
CREATE PROCEDURE p1() CREATE VIEW v1 AS SELECT * FROM t1;
64+
65+
# Create trigger calling proc creating view, when view DOES NOT
66+
# exist already
67+
CREATE TRIGGER trg_p1_t1 AFTER INSERT ON t1 FOR EACH ROW CALL p1();
68+
69+
# Verify that it is possible to lock table
70+
LOCK TABLES t1 WRITE;
71+
UNLOCK TABLES;
72+
73+
# Fails, as expected
74+
INSERT INTO t1 VALUES (1);
75+
ERROR HY000: Explicit or implicit commit is not allowed in stored function or trigger
76+
77+
# Make sure v1 already exists
78+
CREATE VIEW v1 AS SELECT a+1 FROM t1;
79+
80+
# Verify that it is possible to lock table
81+
LOCK TABLES t1 WRITE;
82+
UNLOCK TABLES;
83+
84+
# Verify that we get the expected error when inserting into the table
85+
INSERT INTO t1 VALUES (1);
86+
ERROR HY000: Explicit or implicit commit is not allowed in stored function or trigger
87+
88+
# Cleanup
89+
DROP TRIGGER trg_p1_t1;
90+
DROP PROCEDURE p1;
91+
DROP VIEW v1;
92+
DROP TABLE t1;
93+
#
94+
# Bug#21198646 ASSERTION FAILED: (*TABLES)->REGINFO.LOCK_TYPE >= TL_READ
95+
# FILE LOCK.CC, LINE 356
96+
#
97+
CREATE TABLE t2(a INT);
98+
# Create procedure p1 invoking RENAME TABLE
99+
CREATE PROCEDURE p1() RENAME TABLE t2 TO t3;
100+
# Create function f1 calling p1
101+
CREATE FUNCTION f1() RETURNS INT BEGIN CALL p1(); RETURN 1; END $
102+
# Invoke function f1 and verify that we get the expected error
103+
SELECT f1();
104+
ERROR HY000: Explicit or implicit commit is not allowed in stored function or trigger
105+
# Cleanup
106+
DROP PROCEDURE p1;
107+
DROP FUNCTION f1;
108+
DROP TABLE t2;
109+
#
110+
# MDEV-16686 DDL in procedure propagates no locking to tables locked by DML
111+
#
112+
create table t (i int);
113+
create view v as select * from t;
114+
create procedure p() create view v as select * from t;
115+
create trigger tr after insert on t for each row call p();
116+
insert into t values (1), (2);
117+
ERROR HY000: Explicit or implicit commit is not allowed in stored function or trigger
118+
drop procedure p;
119+
drop view v;
120+
drop table t;
121+
CREATE TABLE t (a INT);
122+
CREATE PROCEDURE p() RENAME TABLE t TO t2;
123+
CREATE TRIGGER tt AFTER INSERT ON t FOR EACH ROW CALL p();
124+
INSERT INTO t VALUES (0);
125+
ERROR HY000: Explicit or implicit commit is not allowed in stored function or trigger
126+
drop procedure p;
127+
drop table t;
128+
# End of 10.11 tests

mysql-test/main/tablelock.test

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
--source include/have_innodb.inc
2+
13
#
24
# Test of lock tables
35
#
@@ -62,3 +64,91 @@ drop table t1;
6264
unlock tables;
6365

6466
# End of 5.0 tests
67+
68+
69+
--echo #
70+
--echo # Bug#19988193 ASSERTION `(*TABLES)->REGINFO.LOCK_TYPE >= TL_READ'
71+
--echo # FAILED IN LOCK_EXTERNAL
72+
--echo #
73+
74+
CREATE TABLE t1(a INT);
75+
CREATE PROCEDURE p1() CREATE VIEW v1 AS SELECT * FROM t1;
76+
--echo
77+
--echo # Create trigger calling proc creating view, when view DOES NOT
78+
--echo # exist already
79+
CREATE TRIGGER trg_p1_t1 AFTER INSERT ON t1 FOR EACH ROW CALL p1();
80+
--echo
81+
--echo # Verify that it is possible to lock table
82+
LOCK TABLES t1 WRITE;
83+
UNLOCK TABLES;
84+
--echo
85+
--echo # Fails, as expected
86+
--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
87+
INSERT INTO t1 VALUES (1);
88+
--echo
89+
--echo # Make sure v1 already exists
90+
CREATE VIEW v1 AS SELECT a+1 FROM t1;
91+
--echo
92+
--echo # Verify that it is possible to lock table
93+
LOCK TABLES t1 WRITE;
94+
UNLOCK TABLES;
95+
--echo
96+
--echo # Verify that we get the expected error when inserting into the table
97+
--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
98+
INSERT INTO t1 VALUES (1);
99+
--echo
100+
--echo # Cleanup
101+
DROP TRIGGER trg_p1_t1;
102+
DROP PROCEDURE p1;
103+
DROP VIEW v1;
104+
DROP TABLE t1;
105+
106+
107+
--echo #
108+
--echo # Bug#21198646 ASSERTION FAILED: (*TABLES)->REGINFO.LOCK_TYPE >= TL_READ
109+
--echo # FILE LOCK.CC, LINE 356
110+
--echo #
111+
112+
CREATE TABLE t2(a INT);
113+
114+
--echo # Create procedure p1 invoking RENAME TABLE
115+
CREATE PROCEDURE p1() RENAME TABLE t2 TO t3;
116+
117+
--echo # Create function f1 calling p1
118+
DELIMITER $;
119+
CREATE FUNCTION f1() RETURNS INT BEGIN CALL p1(); RETURN 1; END $
120+
DELIMITER ;$
121+
122+
--echo # Invoke function f1 and verify that we get the expected error
123+
--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
124+
SELECT f1();
125+
126+
--echo # Cleanup
127+
DROP PROCEDURE p1;
128+
DROP FUNCTION f1;
129+
DROP TABLE t2;
130+
131+
--echo #
132+
--echo # MDEV-16686 DDL in procedure propagates no locking to tables locked by DML
133+
--echo #
134+
create table t (i int);
135+
create view v as select * from t;
136+
create procedure p() create view v as select * from t;
137+
create trigger tr after insert on t for each row call p();
138+
--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
139+
insert into t values (1), (2);
140+
141+
# cleanup
142+
drop procedure p;
143+
drop view v;
144+
drop table t;
145+
146+
CREATE TABLE t (a INT);
147+
CREATE PROCEDURE p() RENAME TABLE t TO t2;
148+
CREATE TRIGGER tt AFTER INSERT ON t FOR EACH ROW CALL p();
149+
--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
150+
INSERT INTO t VALUES (0);
151+
drop procedure p;
152+
drop table t;
153+
154+
--echo # End of 10.11 tests

sql/sp_head.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5122,7 +5122,8 @@ sp_head::merge_table_list(THD *thd, TABLE_LIST *table, LEX *lex_for_tmp_check)
51225122
}
51235123

51245124
for (; table ; table= table->next_global)
5125-
if (!table->derived && !table->schema_table && !table->table_function)
5125+
if (!table->derived && !table->schema_table && !table->table_function &&
5126+
table->lock_type != TL_IGNORE)
51265127
{
51275128
/*
51285129
Structure of key for the multi-set is "db\0table\0alias\0".

sql/sql_base.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5925,6 +5925,7 @@ bool lock_tables(THD *thd, TABLE_LIST *tables, uint count, uint flags)
59255925
found_first_not_own= 1;
59265926
if (!table->placeholder())
59275927
{
5928+
DBUG_ASSERT(table->lock_type != TL_IGNORE);
59285929
*(ptr++)= table->table;
59295930
if (!found_first_not_own)
59305931
table->table->query_id= thd->query_id;

0 commit comments

Comments
 (0)