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 wrong varnullingrels error for MERGE WHEN NOT MATCHED BY SOU
Date: Thu, 03 Oct 2024 12:49:52 +0000
Message-ID: <E1swLHD-001zfX-Ri@gemulon.postgresql.org> (raw)

Fix wrong varnullingrels error for MERGE WHEN NOT MATCHED BY SOURCE.

If a MERGE command contains WHEN NOT MATCHED BY SOURCE actions, the
source relation appears on the outer side of the join. Thus, any Vars
referring to the source in the merge join condition, actions, and
RETURNING list should be marked as nullable by the join, since they
are used in the ModifyTable node above the join. Note that this only
applies to the copy of join condition used in the executor to
distinguish MATCHED from NOT MATCHED BY SOURCE cases. Vars in the
original join condition, inside the join node itself, should not be
marked.

Failure to correctly mark these Vars led to a "wrong varnullingrels"
error in the final stage of query planning, in some circumstances. We
happened to get away without this in all previous tests, since they
all involved a ModifyTable node directly on top of the join node, so
that the top plan targetlist coincided with the output of the join,
and the varnullingrels check was more lax. However, if another plan
node, such as a one-time filter Result node, gets inserted between the
ModifyTable node and the join node, then a stricter check is applied,
which fails.

Per bug #18634 from Alexander Lakhin. Thanks to Tom Lane and Richard
Guo for review and analysis.

Back-patch to v17, where WHEN NOT MATCHED BY SOURCE support was added
to MERGE.

Discussion: https://postgr.es/m/18634-db5299c937877f2b%40postgresql.org

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/34ae54af92e006e601f808e58765cc979ca9135f

Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 39 +++++++++++++++++++++++++++++++
src/test/regress/expected/merge.out       | 29 +++++++++++++++++++++++
src/test/regress/sql/merge.sql            | 21 +++++++++++++++++
3 files changed, 89 insertions(+)



view thread (2+ messages)

Message-ID: <E1swLHD-001zfX-Ri@gemulon.postgresql.org>
Permalink:  ../E1swLHD-001zfX-Ri@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1swLHD-001zfX-Ri@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 wrong varnullingrels error for MERGE WHEN NOT MATCHED BY SOU
  In-Reply-To: <E1swLHD-001zfX-Ri@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