Commit f329fe1c authored by Dmitry Shulga's avatar Dmitry Shulga

MDEV-31799 Unexpected ER_TRG_NO_SUCH_ROW_IN_TRG and server crash after ALTER TABLE

This bug report was caused by implementation of the task MDEV-5816
(Stored programs: validation of stored program statements).
Changing metadata of a table that has a trigger on AFTER UPDATE or AFTER DELETE
resulted in unexpected output of the error ER_TRG_NO_SUCH_ROW_IN_TR.
It was caused by the fact that characteristics of the trigger dependent on
changed table's metadata wasn't set in a new lex object created on re-parsing
of a failing trigger statement.

To fix the bug the data member lex->trg_chistics.action_time and
lex->trg_chistics.event must be set into real values of the trigger
whose statement being re-parsed.
parent 00089ead
...@@ -1911,4 +1911,58 @@ CALL p1; ...@@ -1911,4 +1911,58 @@ CALL p1;
# Clean up # Clean up
DROP PROCEDURE p1; DROP PROCEDURE p1;
DROP TABLE t1; DROP TABLE t1;
#
# MDEV-31799 Unexpected ER_TRG_NO_SUCH_ROW_IN_TRG and server crash after ALTER TABLE
#
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (1),(2),(3),(4);
CREATE TABLE t2 (b INT);
# Check that AFTER DELETE trigger is re-compiled on changing table's metadata
CREATE TRIGGER tr AFTER DELETE ON t1 FOR EACH ROW INSERT INTO t2 (b) VALUES (OLD.a);
DELETE FROM t1 LIMIT 1;
SELECT * FROM t2;
b
1
ALTER TABLE t2 FORCE;
DELETE FROM t1 LIMIT 1;
SELECT * FROM t2;
b
1
2
DELETE FROM t1 LIMIT 1;
SELECT * FROM t2;
b
1
2
3
DROP TRIGGER tr;
# Check that AFTER UPDATE trigger is re-compiled on changing table's metadata
CREATE TRIGGER tr AFTER UPDATE ON t1 FOR EACH ROW INSERT INTO t2 (b) VALUES (OLD.a);
# Remove records interted by AFTER DELETE trogger
TRUNCATE TABLE t1;
TRUNCATE TABLE t2;
INSERT INTO t1 VALUES (1);
UPDATE t1 SET a = 2;
# Above statement should insert the row (1) into the table t2
# Expected output contains one row: (1)
SELECT * FROM t2;
b
1
ALTER TABLE t2 FORCE;
# The following statement should insert the row (2) into the table t2
UPDATE t1 SET a = 3;
# Expected output contains two rows: (1), (2)
SELECT * FROM t2;
b
1
2
# The following statement should insert the row (3) into the table t2
UPDATE t1 SET a = 5;
# Expected output contains three rows: (1), (2), (3)
SELECT * FROM t2;
b
1
2
3
DROP TABLE t1, t2;
SET sql_mode = default; SET sql_mode = default;
...@@ -2713,4 +2713,50 @@ CALL p1; ...@@ -2713,4 +2713,50 @@ CALL p1;
DROP PROCEDURE p1; DROP PROCEDURE p1;
DROP TABLE t1; DROP TABLE t1;
--echo #
--echo # MDEV-31799 Unexpected ER_TRG_NO_SUCH_ROW_IN_TRG and server crash after ALTER TABLE
--echo #
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (1),(2),(3),(4);
CREATE TABLE t2 (b INT);
--echo # Check that AFTER DELETE trigger is re-compiled on changing table's metadata
CREATE TRIGGER tr AFTER DELETE ON t1 FOR EACH ROW INSERT INTO t2 (b) VALUES (OLD.a);
DELETE FROM t1 LIMIT 1;
SELECT * FROM t2;
ALTER TABLE t2 FORCE;
DELETE FROM t1 LIMIT 1;
SELECT * FROM t2;
DELETE FROM t1 LIMIT 1;
SELECT * FROM t2;
DROP TRIGGER tr;
--echo # Check that AFTER UPDATE trigger is re-compiled on changing table's metadata
CREATE TRIGGER tr AFTER UPDATE ON t1 FOR EACH ROW INSERT INTO t2 (b) VALUES (OLD.a);
--echo # Remove records interted by AFTER DELETE trogger
TRUNCATE TABLE t1;
TRUNCATE TABLE t2;
INSERT INTO t1 VALUES (1);
UPDATE t1 SET a = 2;
--echo # Above statement should insert the row (1) into the table t2
--echo # Expected output contains one row: (1)
SELECT * FROM t2;
ALTER TABLE t2 FORCE;
--echo # The following statement should insert the row (2) into the table t2
UPDATE t1 SET a = 3;
--echo # Expected output contains two rows: (1), (2)
SELECT * FROM t2;
--echo # The following statement should insert the row (3) into the table t2
UPDATE t1 SET a = 5;
--echo # Expected output contains three rows: (1), (2), (3)
SELECT * FROM t2;
# Cleanup
DROP TABLE t1, t2;
SET sql_mode = default; SET sql_mode = default;
...@@ -722,7 +722,21 @@ LEX* sp_lex_instr::parse_expr(THD *thd, sp_head *sp, LEX *sp_instr_lex) ...@@ -722,7 +722,21 @@ LEX* sp_lex_instr::parse_expr(THD *thd, sp_head *sp, LEX *sp_instr_lex)
sp_instr_cursor_copy_struct) and in some cases for sp_instr_set. sp_instr_cursor_copy_struct) and in some cases for sp_instr_set.
*/ */
if (sp_instr_lex == nullptr) if (sp_instr_lex == nullptr)
{
thd->lex= new (thd->mem_root) st_lex_local; thd->lex= new (thd->mem_root) st_lex_local;
lex_start(thd);
if (sp->m_handler->type() == SP_TYPE_TRIGGER)
{
/*
In case the trigger's statement being re-parsed, the correct trigger's
context (trigger event type and action time) should be copied from
trigger's sp_head to the new lex object.
*/
thd->lex->trg_chistics.action_time=
thd->spcont->m_sp->m_trg->action_time;
thd->lex->trg_chistics.event= thd->spcont->m_sp->m_trg->event;
}
}
else else
{ {
sp_lex_cursor* cursor_lex= sp_instr_lex->get_lex_for_cursor(); sp_lex_cursor* cursor_lex= sp_instr_lex->get_lex_for_cursor();
...@@ -742,8 +756,6 @@ LEX* sp_lex_instr::parse_expr(THD *thd, sp_head *sp, LEX *sp_instr_lex) ...@@ -742,8 +756,6 @@ LEX* sp_lex_instr::parse_expr(THD *thd, sp_head *sp, LEX *sp_instr_lex)
DBUG_ASSERT(thd->lex == sp_instr_lex); DBUG_ASSERT(thd->lex == sp_instr_lex);
} }
lex_start(thd);
thd->lex->sphead= sp; thd->lex->sphead= sp;
thd->lex->spcont= m_ctx; thd->lex->spcont= m_ctx;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment