Skip to content

Commit 7a12894

Browse files
committed
Fixed the bug mdev12992.
When the SELECT query from a trigger that used a subquery in its SELECT list was prepared the counter select_n_having_items was incremented in the constructor Item::Item(THD *thd). As a result each invocation of the trigger required more and more memory for the ref_pointer_array for this SELECT. Made sure that the counter st_select_lex::select_n_having_items would be incremented only at the first execution of such trigger.
1 parent b175c41 commit 7a12894

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

mysql-test/r/trigger.result

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,3 +2368,14 @@ tr1 1 2016-01-01 10:10:10.33
23682368
tr2 2 2016-01-01 10:10:10.99
23692369
drop table t1;
23702370
set time_zone= @@global.time_zone;
2371+
#
2372+
# MDEV-12992: Increasing memory consumption
2373+
with each invocation of trigger
2374+
#
2375+
CREATE TABLE t1 (a INT);
2376+
INSERT INTO t1 VALUES (1);
2377+
CREATE TABLE t2 (b INT);
2378+
CREATE TRIGGER tr
2379+
AFTER UPDATE ON t1 FOR EACH ROW SELECT (SELECT b FROM t2) INTO @x;
2380+
# Running 20000 queries
2381+
DROP TABLE t1,t2;

mysql-test/t/trigger.test

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2676,3 +2676,27 @@ select trigger_name, action_order, created from information_schema.triggers
26762676
where event_object_table = 't1' and trigger_schema='test';
26772677
drop table t1;
26782678
set time_zone= @@global.time_zone;
2679+
2680+
--echo #
2681+
--echo # MDEV-12992: Increasing memory consumption
2682+
--echo with each invocation of trigger
2683+
--echo #
2684+
2685+
--let $n= 20000
2686+
2687+
CREATE TABLE t1 (a INT);
2688+
INSERT INTO t1 VALUES (1);
2689+
CREATE TABLE t2 (b INT);
2690+
CREATE TRIGGER tr
2691+
AFTER UPDATE ON t1 FOR EACH ROW SELECT (SELECT b FROM t2) INTO @x;
2692+
2693+
--disable_query_log
2694+
--echo # Running $n queries
2695+
while ($n)
2696+
{
2697+
UPDATE t1 SET a = 2;
2698+
--dec $n
2699+
}
2700+
--enable_query_log
2701+
2702+
DROP TABLE t1,t2;

sql/item.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,8 @@ Item::Item(THD *thd):
490490
command => we should check thd->lex->current_select on zero (thd->lex
491491
can be uninitialised)
492492
*/
493-
if (thd->lex->current_select)
493+
if (thd->lex->current_select &&
494+
thd->stmt_arena->is_stmt_prepare_or_first_sp_execute())
494495
{
495496
enum_parsing_place place=
496497
thd->lex->current_select->parsing_place;

0 commit comments

Comments
 (0)