Don't leave a stale slot in 'tcs_original_insert_tuple'

When ExecInsert() returns, after a partition-routed insertion, it can
leave the 'mtstate->mt_transition_capture->tcs_original_insert_tuple'
field pointing to the original slot.  If ExecARInsertTriggers() or
ExecARUpdateTriggers() is called later for a different tuple, it will
add the stale 'tcs_original_insert_tuple' to the trigger transition
table, instead of the new tuple.

ExecCrossPartitionUpdate() recognized this problem and reset the field
after calling ExecInsert(), but ExecMergeNotMatched() didn't get the
memo, so a MERGE that did an INSERT followed by an UPDATE action would
use the wrong tuple for the trigger transition table on the UPDATE.

There is another near miss in ExecBatchInsert(): it also calls
ExecARInsertTriggers() with the incorrect tuple already set in
'tcs_original_insert_tuple', but because we don't currently support
transition tables with foreign tables, you get an error before that
causes any harm.  Similarly, batching in CopyFrom() is not supported
with statement triggers.

The way the TransitionCaptureState->tcs_original_insert_tuple field is
set and reset is error prone, as this bug shows.  Its purpose is to
smuggle the original tuple to ExecARInsertTriggers() and
ExecARUpdateTriggers() functions -- it always corresponds to new tuple
that's passed as the 'slot' or 'newslot' argument.  That's a pretty
cavalier way of passing an argument, and prone to mistakes.  In
'master', make it a normal argument to those functions instead and
remove 'tcs_original_insert_tuple' field altogether.  In hindsight, we
should've done that in commit 6973533650c0 already, which removed the
only other field in the struct that had to be set correctly for the
new tuple on each call.  In stable branches, to avoid breaking ABI
compatibility, just fix the immediate issue by setting the
'tcs_original_insert_tuple' field closer to the ExecAR*Triggers()
calls that need it, and resetting it immediately afterwards.

In the passing, remove a stale comment in ExecInsert() that talked
about the "active tuple conversion map".  That was removed in commit
6973533650c0.

Add a regression test; the existing MERGE transition-table test uses a
non-partitioned target and does not reach this path.

Reported-by: Jacek Bylina <[email protected]>
Reported-by: Timothy Ball <[email protected]>
Reported-by: Claude and Ada Logics
Co-authored-by: Jacek Bylina <[email protected]>
Co-authored-by: Jacob Robertson <[email protected]>
Co-authored-by: Nigel Gott <[email protected]>
Reviewed-by: Amit Langote <[email protected]>
Backpatch-through: 15
Security: CVE-2026-116312

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/851876b487564226ab62f9e78556cfc8891294d1
Author: Heikki Linnakangas <[email protected]>

Modified Files
--------------
src/backend/executor/nodeModifyTable.c | 30 +++++++-------
src/test/regress/expected/triggers.out | 71 ++++++++++++++++++++++++++++++++++
src/test/regress/sql/triggers.sql      | 69 +++++++++++++++++++++++++++++++++
3 files changed, 155 insertions(+), 15 deletions(-)

Reply via email to