pgsql: Avoid RETURNING side effects for FOR PORTION OF leftovers.

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Avoid RETURNING side effects for FOR PORTION OF leftovers.
Date: 2026-07-28 08:48:16
Message-ID: E1wodU0-00000000d2I-3CLw@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Avoid RETURNING side effects for FOR PORTION OF leftovers.

UPDATE/DELETE ... FOR PORTION OF inserts leftover rows for the
untouched parts of the original row. These hidden inserts should not
affect the command tag or ROW_COUNT, so they call ExecInsert() with
canSetTag set to false.

However, ExecInsert() still processed the RETURNING list whenever the
target ResultRelInfo had ri_projectReturning set. That caused
RETURNING expressions to be evaluated for leftover rows even though
their results were discarded. As a result, expressions with side
effects and information-leaking functions could be executed on the
leftover rows, in addition to the visibly updated or deleted row.

Fix by having ExecInsert() skip RETURNING processing when it is
handling an internal FOR PORTION OF leftover insert. Use both the
presence of a FOR PORTION OF clause and mtstate->operation ==
CMD_INSERT for this check, so that the auxiliary INSERT of a
cross-partition UPDATE with a FOR PORTION OF clause still processes
RETURNING normally.

Back-patch to v19, where support for FOR PORTION OF was added.

Author: Chao Li <lic(at)highgo(dot)com>
Reviewed-by: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
Reviewed-by: Paul A Jungwirth <pj(at)illuminatedcomputing(dot)com>
Discussion: https://postgr.es/m/07C125E5-F6ED-460C-A394-E6503DAE18FB@gmail.com
Backpatch-through: 19

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/fd92f74b91d079430ef4544713a90b7b6aa8faee

Modified Files
--------------
src/backend/executor/nodeModifyTable.c | 14 +++++++++--
src/test/regress/expected/for_portion_of.out | 36 +++++++++++++++++++++-------
src/test/regress/sql/for_portion_of.sql | 16 ++++++++++---
3 files changed, 52 insertions(+), 14 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Alexander Korotkov 2026-07-28 09:37:21 pgsql: Restore vacuum_delay_point() in GIN posting-tree leaf vacuum
Previous Message Michael Paquier 2026-07-28 02:10:55 pgsql: Fix portability issue in authentication test 003_peer