MDEV-40570 Wrong trigger event flag in ODKU/REPLACE - #5497
Open
MooSayed1 wants to merge 1 commit into
Open
Conversation
…NG/UPDATING/DELETING flag in multi-event triggers The clauses INSERTING / UPDATING / DELETING available inside multi-event triggers (MDEV-10164) are evaluated by comparing the trigger's statement type with the type of DML statement on top of the THD's active-statement stack (see Item_trigger_type_of_statement::val_bool()). mysql_insert() pushes INSERTING_STMT for the whole statement via a Running_stmt_guard. However, when INSERT ... ON DUPLICATE KEY UPDATE hits a duplicate key, Write_record::insert_on_duplicate_update() fires UPDATE triggers without changing the active-statement stack, so the UPDATING clause evaluated to FALSE and INSERTING to TRUE inside those triggers. Similarly, when REPLACE deletes a conflicting row, Write_record::replace_row() fires DELETE triggers while INSERTING_STMT is still on top of the stack, so the DELETING clause evaluated to FALSE. Additionally, INSERT ... SELECT and REPLACE ... SELECT never pushed any statement type at all, so all of the clauses INSERTING / UPDATING / DELETING evaluated to FALSE for triggers fired by these statements. Fix: - push UPDATING_STMT in Write_record::insert_on_duplicate_update() around the invocation of BEFORE/AFTER UPDATE triggers; - push DELETING_STMT in Write_record::replace_row() around the invocation of BEFORE/AFTER DELETE triggers; - push INSERTING_STMT for the whole INSERT ... SELECT and REPLACE ... SELECT statements in mysql_execute_command(), the same way mysql_insert() does. The pushed value is popped automatically by the Running_stmt_guard destructor at the end of each scope.
gkodinov
approved these changes
Aug 6, 2026
gkodinov
left a comment
Member
There was a problem hiding this comment.
Thank you for your contribution! This is a preliminary review.
LGTM. Solid work!
Please stand by for the final review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Jira issue number for this PR is: MDEV-40570
Description
The INSERTING / UPDATING / DELETING clauses in multi-event triggers
(MDEV-10164) reported the wrong statement type:
These clauses are evaluated by comparing the trigger statement type with
the top of THD's active-statement stack
(
Item_trigger_type_of_statement::val_bool()insql/item.cc).mysql_insert()pushesINSERTING_STMTfor the whole statement, but:Write_record::insert_on_duplicate_update()fires UPDATE triggerswithout changing the stack — UPDATING was reported as INSERTING.
Write_record::replace_row()fires DELETE triggers for theconflicting row without changing the stack — DELETING was reported
as INSERTING.
INSERT ... SELECTandREPLACE ... SELECTnever pushed any typeat all — all three clauses evaluated to FALSE.
The fix pushes the correct
active_dml_stmtvia scopedRunning_stmt_guardin the twoWrite_recordpaths (matching thepattern in
sql_update.cc/sql_delete.cc), and pushesINSERTING_STMTfor theSQLCOM_INSERT_SELECT/SQLCOM_REPLACE_SELECTcases inmysql_execute_command().Release Notes
INSERTING/UPDATING/DELETING clauses in multi-event triggers now report
the correct value for UPDATE triggers fired by INSERT ... ON DUPLICATE
KEY UPDATE, DELETE triggers fired by REPLACE, and triggers fired by
INSERT ... SELECT / REPLACE ... SELECT.
How can this PR be tested?
New test cases cover: plain INSERT, ODKU (row changed and unchanged),
REPLACE on duplicate key, INSERT ... SELECT ... ON DUPLICATE KEY UPDATE
and REPLACE ... SELECT, verifying the flag seen by both BEFORE and
AFTER multi-event triggers.
Basing the PR against the correct MariaDB version
This is a bug fix. The bug was introduced by MDEV-10164 and the
earliest affected maintained branch is 12.3, so this PR targets
upstream/12.3.PR quality check