pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Noah Misch <noah@leadboat.com>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Don't leave a stale slot in 'tcs_original_insert_tuple'
Date: Mon, 09 Nov 2026 14:21:48 +0000
Message-ID: <E1xQQFo-000000003Gm-2RBK@gemulon.postgresql.org> (raw)

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 <jacek@enveritas.org>
Reported-by: Timothy Ball <timball@enveritas.org>
Reported-by: Claude and Ada Logics
Co-authored-by: Jacek Bylina <jacek@enveritas.org>
Co-authored-by: Jacob Robertson <jacob@enveritas.org>
Co-authored-by: Nigel Gott <nigel@enveritas.org>
Reviewed-by: Amit Langote <amitlangote09@gmail.com>
Backpatch-through: 15
Security: CVE-2026-116312

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/398eb35f0011ff6af4f126748b7da5f91126a710
Author: Heikki Linnakangas <heikki.linnakangas@iki.fi>

Modified Files
--------------
src/backend/commands/copyfrom.c        | 15 +++----
src/backend/commands/trigger.c         | 33 +++++++++++-----
src/backend/executor/execReplication.c |  4 +-
src/backend/executor/nodeModifyTable.c | 37 +++++++-----------
src/include/commands/trigger.h         | 11 +-----
src/test/regress/expected/triggers.out | 71 ++++++++++++++++++++++++++++++++++
src/test/regress/sql/triggers.sql      | 69 +++++++++++++++++++++++++++++++++
7 files changed, 190 insertions(+), 50 deletions(-)



view thread (3+ messages)  latest in thread

Message-ID: <E1xQQFo-000000003Gm-2RBK@gemulon.postgresql.org>
Permalink:  ../E1xQQFo-000000003Gm-2RBK@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xQQFo-000000003Gm-2RBK@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: noah@leadboat.com
  Subject: Re: pgsql: Don't leave a stale slot in 'tcs_original_insert_tuple'
  In-Reply-To: <E1xQQFo-000000003Gm-2RBK@gemulon.postgresql.org>
  References: <E1xQQFo-000000003Gm-2RBK@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox