pgsql: Fix MERGE into a plain inheritance parent table.

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix MERGE into a plain inheritance parent table.
Date: 2025-05-31 11:24:39
Message-ID: E1uLKKM-000s2m-0A@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix MERGE into a plain inheritance parent table.

When a MERGE's target table is the parent of an inheritance tree, any
INSERT actions insert into the parent table using ModifyTableState's
rootResultRelInfo. However, there are two bugs in the way this is
initialized:

1. ExecInitMerge() incorrectly uses a different ResultRelInfo entry
from ModifyTableState's resultRelInfo array to build the insert
projection, which may not be compatible with rootResultRelInfo.

2. ExecInitModifyTable() does not fully initialize rootResultRelInfo.
Specifically, ri_WithCheckOptions, ri_WithCheckOptionExprs,
ri_returningList, and ri_projectReturning are not initialized.

This can lead to crashes, or incorrect query results due to failing to
check WCO's or process the RETURNING list for INSERT actions.

Fix both these bugs in ExecInitMerge(), noting that it is only
necessary to fully initialize rootResultRelInfo if the MERGE has
INSERT actions and the target table is a plain inheritance parent.

Backpatch to v15, where MERGE was introduced.

Reported-by: Andres Freund <andres(at)anarazel(dot)de>
Author: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
Reviewed-by: Jian He <jian(dot)universality(at)gmail(dot)com>
Reviewed-by: Tender Wang <tndrwang(at)gmail(dot)com>
Discussion: https://postgr.es/m/4rlmjfniiyffp6b3kv4pfy4jw3pciy6mq72rdgnedsnbsx7qe5@j5hlpiwdguvc
Backpatch-through: 15

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/3611794affb6b52cece75aff58cf49e782a46442

Modified Files
--------------
src/backend/executor/nodeModifyTable.c | 93 ++++++++++++++++++++++++++++++++--
src/test/regress/expected/merge.out | 58 +++++++++++++++++++++
src/test/regress/sql/merge.sql | 41 +++++++++++++++
3 files changed, 189 insertions(+), 3 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Etsuro Fujita 2025-06-01 08:34:49 pgsql: postgres_fdw: Inherit the local transaction's access/deferrable
Previous Message Michael Paquier 2025-05-31 00:41:15 pgsql: Change internal plan ID type from uint64 to int64