postgres.git / summary / log / commit / refs

commit    84b36063f4092c54f0cc628e6d9c684af6128d07
Author:   Dean Rasheed <dean.a.rasheed@gmail.com>
Date:     Wed Nov 18 16:04:35 2026 +0000

    Fix transition tables for MERGE with cross-partition UPDATEs.
    
    When a MERGE UPDATE action moved a row between partitions, the old and
    new rows were not included in the transition tables of AFTER UPDATE
    statement triggers, and could instead be incorrectly included in the
    transition tables of AFTER INSERT and AFTER DELETE statement triggers.
    
    Fix by capturing both halves of a cross-partition MERGE UPDATE action
    in the UPDATE transition tables and exclude them from the DELETE and
    INSERT transition tables, as we do for a plain UPDATE.
    
    Backpatch to v15, where MERGE was introduced.
    
    Reported-by: Michael Ries <mries@salesforce.com>
    Reported-by: Tender Wang <tndrwang@gmail.com>
    Author: Tender Wang <tndrwang@gmail.com>
    Reviewed-by: shihao zhong <zhong950419@gmail.com>
    Discussion: https://postgr.es/m/CAKWDHLgETF86qEW40HcEzMgn5DdmS6yR-nSYwemXF7zFGrU%2BsQ%40mail.gmail.com
    Discussion: https://postgr.es/m/CAHewXNk8MgaFMD87s%3DxHeguzmErEjsqeroxrMyQbKyXwG5eocw%40mail.gmail.com
    Backpatch-through: 15


src/backend/executor/nodeModifyTable.c | 69 +++++++++++++++++----------------- src/test/regress/expected/triggers.out | 31 +++++++++++++++ src/test/regress/sql/triggers.sql | 30 +++++++++++++++ 3 files changed, 95 insertions(+), 35 deletions(-) diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index f942c5926a5..41051c6c491 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -857,7 +857,7 @@ ExecInsert(ModifyTableContext *context, List *recheckIndexes = NIL; TupleTableSlot *planSlot = context->planSlot; TupleTableSlot *result = NULL; - TransitionCaptureState *ar_insert_trig_tcs; + TransitionCaptureState *transition_capture; TupleTableSlot *tcs_original_insert_tuple = NULL; ModifyTable *node = (ModifyTable *) mtstate->ps.plan; OnConflictAction onconflict = node->onConflictAction; @@ -1257,29 +1257,31 @@ ExecInsert(ModifyTableContext *context, */ if (mtstate->mt_transition_capture) mtstate->mt_transition_capture->tcs_original_insert_tuple = tcs_original_insert_tuple; - ar_insert_trig_tcs = mtstate->mt_transition_capture; - if (mtstate->operation == CMD_UPDATE && mtstate->mt_transition_capture - && mtstate->mt_transition_capture->tcs_update_new_table) - { - ExecARUpdateTriggers(estate, resultRelInfo, - NULL, NULL, - NULL, - NULL, - slot, - NULL, - mtstate->mt_transition_capture, - false); - /* - * We've already captured the NEW TABLE row, so make sure any AR - * INSERT trigger fired below doesn't capture it again. - */ - ar_insert_trig_tcs = NULL; + transition_capture = mtstate->mt_transition_capture; + if (transition_capture && + (mtstate->operation == CMD_UPDATE || + (mtstate->operation == CMD_MERGE && + mtstate->mt_merge_action->mas_action->commandType == CMD_UPDATE))) + { + if (transition_capture->tcs_update_new_table) + ExecARUpdateTriggers(estate, resultRelInfo, + NULL, NULL, + NULL, + NULL, + slot, + NULL, + transition_capture, + false); + + /* A moved row does not belong in the INSERT transition table. */ + transition_capture = NULL; } /* AFTER ROW INSERT Triggers */ ExecARInsertTriggers(estate, resultRelInfo, slot, recheckIndexes, - ar_insert_trig_tcs); + transition_capture); + if (mtstate->mt_transition_capture) mtstate->mt_transition_capture->tcs_original_insert_tuple = NULL; @@ -1531,7 +1533,7 @@ ExecDeleteEpilogue(ModifyTableContext *context, ResultRelInfo *resultRelInfo, { ModifyTableState *mtstate = context->mtstate; EState *estate = context->estate; - TransitionCaptureState *ar_delete_trig_tcs; + TransitionCaptureState *transition_capture; /* * If this delete is the result of a partition key update that moved the @@ -1539,26 +1541,23 @@ ExecDeleteEpilogue(ModifyTableContext *context, ResultRelInfo *resultRelInfo, * if there is one. We need to do this separately for DELETE and INSERT * because they happen on different tables. */ - ar_delete_trig_tcs = mtstate->mt_transition_capture; - if (mtstate->operation == CMD_UPDATE && mtstate->mt_transition_capture && - mtstate->mt_transition_capture->tcs_update_old_table) + transition_capture = mtstate->mt_transition_capture; + if (transition_capture && changingPart) { - ExecARUpdateTriggers(estate, resultRelInfo, - NULL, NULL, - tupleid, oldtuple, - NULL, NULL, mtstate->mt_transition_capture, - false); - - /* - * We've already captured the OLD TABLE row, so make sure any AR - * DELETE trigger fired below doesn't capture it again. - */ - ar_delete_trig_tcs = NULL; + if (transition_capture->tcs_update_old_table) + ExecARUpdateTriggers(estate, resultRelInfo, + NULL, NULL, + tupleid, oldtuple, + NULL, NULL, transition_capture, + false); + + /* A moved row does not belong in the DELETE transition table. */ + transition_capture = NULL; } /* AFTER ROW DELETE Triggers */ ExecARDeleteTriggers(estate, resultRelInfo, tupleid, oldtuple, - ar_delete_trig_tcs, changingPart); + transition_capture, changingPart); } /* ---------------------------------------------------------------- diff --git a/src/test/regress/expected/triggers.out b/src/test/regress/expected/triggers.out index 07500a0f1e7..533edf777fd 100644 --- a/src/test/regress/expected/triggers.out +++ b/src/test/regress/expected/triggers.out @@ -3378,6 +3378,37 @@ select * from merge_conv_table order by a; (2 rows) drop table merge_conv_table; +-- A MERGE UPDATE that moves a row must capture it as an update. +create table merge_move (a int, b text) partition by list (a); +create table merge_move_p1 partition of merge_move for values in (1, 2); +create table merge_move_p2 partition of merge_move for values in (3, 4); +insert into merge_move values (1, 'move'), (2, 'delete'); +create trigger move_ins after insert on merge_move + referencing new table as new_table + for each statement execute function dump_insert(); +create trigger move_upd after update on merge_move + referencing old table as old_table new table as new_table + for each statement execute function dump_update(); +create trigger move_del after delete on merge_move + referencing old table as old_table + for each statement execute function dump_delete(); +merge into merge_move t using (values (1), (2), (4)) s(a) on t.a = s.a +when matched and s.a = 1 then update set a = 3 +when matched then delete +when not matched then insert values (s.a, 'insert'); +NOTICE: trigger = move_del, old table = (2,delete) +NOTICE: trigger = move_upd, old table = (1,move), new table = (3,move) +NOTICE: trigger = move_ins, new table = (4,insert) +-- The move must stay out of INSERT and DELETE tables even without an +-- UPDATE transition table. +drop trigger move_upd on merge_move; +merge into merge_move t using (values (3), (4), (2)) s(a) on t.a = s.a +when matched and s.a = 3 then update set a = 1 +when matched then delete +when not matched then insert values (s.a, 'insert'); +NOTICE: trigger = move_del, old table = (4,insert) +NOTICE: trigger = move_ins, new table = (2,insert) +drop table merge_move; -- cleanup drop function dump_insert(); drop function dump_update(); diff --git a/src/test/regress/sql/triggers.sql b/src/test/regress/sql/triggers.sql index ca8367c9012..71cc91259f9 100644 --- a/src/test/regress/sql/triggers.sql +++ b/src/test/regress/sql/triggers.sql @@ -2554,6 +2554,36 @@ select * from merge_conv_table order by a; drop table merge_conv_table; +-- A MERGE UPDATE that moves a row must capture it as an update. +create table merge_move (a int, b text) partition by list (a); +create table merge_move_p1 partition of merge_move for values in (1, 2); +create table merge_move_p2 partition of merge_move for values in (3, 4); +insert into merge_move values (1, 'move'), (2, 'delete'); +create trigger move_ins after insert on merge_move + referencing new table as new_table + for each statement execute function dump_insert(); +create trigger move_upd after update on merge_move + referencing old table as old_table new table as new_table + for each statement execute function dump_update(); +create trigger move_del after delete on merge_move + referencing old table as old_table + for each statement execute function dump_delete(); + +merge into merge_move t using (values (1), (2), (4)) s(a) on t.a = s.a +when matched and s.a = 1 then update set a = 3 +when matched then delete +when not matched then insert values (s.a, 'insert'); + +-- The move must stay out of INSERT and DELETE tables even without an +-- UPDATE transition table. +drop trigger move_upd on merge_move; +merge into merge_move t using (values (3), (4), (2)) s(a) on t.a = s.a +when matched and s.a = 3 then update set a = 1 +when matched then delete +when not matched then insert values (s.a, 'insert'); + +drop table merge_move; + -- cleanup drop function dump_insert(); drop function dump_update(); [parent: 23a8402f8bdb]