pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dean Rasheed <dean.a.rasheed@gmail.com>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix MERGE into a plain inheritance parent table.
Date: Sat, 31 May 2025 11:24:39 +0000
Message-ID: <E1uLKKM-000s2k-07@gemulon.postgresql.org> (raw)

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@anarazel.de>
Author: Dean Rasheed <dean.a.rasheed@gmail.com>
Reviewed-by: Jian He <jian.universality@gmail.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Discussion: https://postgr.es/m/4rlmjfniiyffp6b3kv4pfy4jw3pciy6mq72rdgnedsnbsx7qe5@j5hlpiwdguvc
Backpatch-through: 15

Branch
------
REL_17_STABLE

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

Modified Files
--------------
src/backend/executor/nodeModifyTable.c | 129 ++++++++++++++++++++++++++++++++-
src/test/regress/expected/merge.out    |  70 ++++++++++++++++++
src/test/regress/sql/merge.sql         |  49 +++++++++++++
3 files changed, 245 insertions(+), 3 deletions(-)



view thread (4+ messages)  latest in thread

Message-ID: <E1uLKKM-000s2k-07@gemulon.postgresql.org>
Permalink:  ../E1uLKKM-000s2k-07@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1uLKKM-000s2k-07@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 using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: dean.a.rasheed@gmail.com, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Fix MERGE into a plain inheritance parent table.
  In-Reply-To: <E1uLKKM-000s2k-07@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