pgsql: Fix FOR PORTION OF for inheritance children

From: Peter Eisentraut <peter(at)eisentraut(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix FOR PORTION OF for inheritance children
Date: 2026-06-08 17:32:30
Message-ID: E1wWdpu-001cyM-2y@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix FOR PORTION OF for inheritance children

ExecForPortionOfLeftovers() assumed that any result relation with
ri_RootResultRelInfo should reinsert temporal leftovers through the
root relation. That is correct for partitioned tables, where tuple
routing is needed, but it is wrong for plain inheritance.

When UPDATE/DELETE FOR PORTION OF is run on an inheritance parent and
a child row is split, the leftover rows must be inserted back into the
child relation. Reinserting through the parent can lose child-only
columns and place the leftover rows in the wrong relation.

Fix this by distinguishing partitioned-table routing from plain
inheritance. For partitioned tables, keep using the root leftover
slot and insert through the root relation. For plain inheritance
children, use a leftover slot matching the child relation and insert
directly into the child. Also keep translating the application-time
column attno for child relations, so multiple-inheritance cases with
different attribute numbers are handled correctly.

Added an ExecInitForPortionOf function to set up the ForPortionOfState
for each child table, which keeps most of these decisions localized
instead of spread out through ExecForPortionOfLeftovers. Incidentally
clarified a comment about the rangetype stored in ForPortionOfState.

Add regression tests for UPDATE and DELETE FOR PORTION OF on
inheritance children, including a multiple-inheritance case where the
range column has a different attnum in the parent and child.

Author: jian he <jian(dot)universality(at)gmail(dot)com>
Co-authored-by: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
Reviewed-by: Paul A. Jungwirth <pj(at)illuminatedcomputing(dot)com>
Discussion: https://www.postgresql.org/message-id/flat/4245F94D-84F1-4E05-BF81-C458A6CF9901%40gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/7d13b03a2e677b78047cf537e519166dcbafa117

Modified Files
--------------
src/backend/executor/nodeModifyTable.c | 158 ++++++++++++++++++---------
src/include/nodes/execnodes.h | 3 +-
src/test/regress/expected/for_portion_of.out | 158 +++++++++++++++++++++++++++
src/test/regress/sql/for_portion_of.sql | 85 ++++++++++++++
4 files changed, 353 insertions(+), 51 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Álvaro Herrera 2026-06-08 17:50:53 pgsql: Fix syslogger NULL-pointer-dereference in EXEC_BACKEND
Previous Message Tom Lane 2026-06-08 15:49:32 pgsql: Fix missed checks for hashability of container-type equality.