← Back to Pull requests

MDEV-41464 INSERTING/UPDATING/DELETING, OLD, NEW in multi-event triggers #5853

Open
arcivanov opened this pull request with 1191 lines diff across 16 files
View on GitHub
arcivanov arcivanov commented on Oct 11, 2026 01:23 UTC
Author

https://jira.mariadb.org/browse/MDEV-41464

A trigger defined for several events (CREATE TRIGGER ... BEFORE INSERT OR DELETE ...) did not see the event that fired it. INSERTING, UPDATING and DELETING reported the kind of the DML statement instead, OLD in a firing for an INSERT event and NEW in a firing for a DELETE event were NULL to some ways of reading them and not to others, and a value written to NEW through a routine argument could be stored in the wrong row.

1. MDEV-41464 INSERTING/UPDATING/DELETING, OLD, NEW in multi-event triggers

INSERTING, UPDATING, DELETING. The item read a per-statement stack that mysql_insert(), Sql_cmd_update::execute_inner() and Sql_cmd_delete::execute_inner() pushed the kind of their statement on, through the THD that parsed the trigger body. That was wrong for INSERT ... SELECT, REPLACE ... SELECT and LOAD DATA, which push nothing; for a statement run by the trigger body, which pushed its own kind on top; in a connection other than the one that loaded the triggers into the TABLE; for a firing of another event than the statement's own (the row REPLACE deletes, the row INSERT ... ON DUPLICATE KEY UPDATE updates, the rows ... FOR PORTION OF inserts); and as a value, where Item_int returned the constant 0. The item is now an Item_bool_func that compares its event with the event of the innermost trigger being run, read from the THD evaluating it. The statement stack and Running_stmt_guard are removed. The trigger event stack becomes one trg_event_type on Statement, which Trigger_event_guard saves and restores, so recording the event can no longer fail on allocation.

OLD on INSERT and NEW on DELETE. NULL was produced by overriding some val_() methods of Item_trigger_field, and is_null(), get_date() and val_native() went around them to the record buffer. IS NULL could return 0, DATE_FORMAT() and INET6 values returned whatever the buffer held (in the firings of REPLACE, the other of its two rows), and YEAR() failed a debug assertion. For a table that has a multi-event trigger, Table_triggers_list now keeps null_row_field, a copy of the fields whose null bit is always set, and process_triggers() binds it as OLD in an INSERT firing and as NEW in a DELETE firing. Every way of reading the field then sees NULL, and the overrides are removed.

NEW as an OUT or INOUT argument. CREATE TRIGGER refuses SET NEW.b= ... in a BEFORE trigger also defined for DELETE, but accepted NEW.b as an OUT or INOUT argument of a procedure or a stored function, and in the DELETE firing of REPLACE the value was written into the row about to be inserted. The mode of a parameter is only known once the routine is called, so the check is made there: sp_head::bind_input_param() asks the new Settable_routine_parameter::check_settable(), which Item_trigger_field answers with ER_TRG_NO_SUCH_ROW_IN_TRG, the error SET NEW gets, when the trigger is running for a DELETE event. The routine does not run. INSERT and UPDATE firings write NEW as before.

2. Replace active_dml_stmt with trg_event_type

With the statement stack gone, enum class active_dml_stmt only named the three clauses a second time. enum trg_event_type moves unchanged from sql_trigger.h to sql_basic_types.h in its place, so the item holds the event it names, val_bool() is a comparison, Item_trigger_type_of_statement::clause_name() names the clauses for both func_name_cstring() and ER_INCOMPATIBLE_EVENT_FLAG, and LEX::create_item_ident_trigger_specific() checks the event once instead of once per clause.

Testing

The End of 12.3 tests section of main.trigger holds 42 checks, each a statement and the value it must produce, and asserts at the end that every check got its value, listing those that did not. It covers each statement kind above, a trigger body's own statements, a second connection and one that has disconnected, OLD and NEW read as a value, through IS NULL, as a DATE, an INET6 and from a NOT NULL column in AFTER and BEFORE triggers, and NEW as an INOUT argument of a procedure and of a function in INSERT, UPDATE, REPLACE and DELETE firings.

On a debug build, each commit: main,innodb,binlog,funcs_1,rpl,parts,sys_vars,perfschema,compat/oracle,versioning,period,spider: Completed: All 5710 tests were successful.; embedded main,funcs_1,compat/oracle,versioning: Completed: All 1173 tests were successful.

Diff: 1191 lines (+916 / -275) across 16 files Reviewer: gkodinov Review duration: 5359m
All Vouch review confidence checks passed
Residual risk 0.26 = Change Risk (0.72) × (1 − Review Confidence (0.64)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.72 · 1191 lines diff across 16 files
0.72
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.35 · 1 substantive comment(s)
0.35
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
63%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.400
Mass File Touch mass_file_touch
0.080
Real PR Conversation Timeline
4 items
gkodinov gkodinov approved these changes on Oct 12, 2026 08:19 UTC
Approved Reviewer

Thank you for your contribution! This is a preliminary review.

LGTM. Please stand by for the final review.

gkodinov gkodinov requested changes on Oct 12, 2026 08:20 UTC
Changes Requested Reviewer

hmm, spoke too soon. main.trigger-33083 failure seems related. Please have a look

gkodinov gkodinov approved these changes on Oct 13, 2026 07:49 UTC
Approved Reviewer

Thank for test suite stabilization!

LGTM. Please stand by for the final review.

Reviewers
Assignees
arcivanov arcivanov
Labels
risk: low confidence: 63.7% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 71.5% (HIGH) · Higher = larger blast radius
71.5%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 63.7% · Lower = rushed or shallow review
63.7%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 25.9% · Safe to merge
25.9%
Repository