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 varnullingrels in MERGE into trigger-updatable view.
Date: Thu, 19 Nov 2026 12:38:23 +0000
Message-ID: <E1xU1PC-00000000Nj1-04Wu@gemulon.postgresql.org> (raw)

Fix varnullingrels in MERGE into trigger-updatable view.

This fixes another "wrong varnullingrels" error in MERGE, triggered
when there are WHEN NOT MATCHED [BY TARGET] actions, and the target
relation is a trigger-updatable view, and the source is a join.

When MERGE requires an outer join, Vars used above that join must be
marked nullable if they belong to one of its nullable sides. However,
transform_MERGE_to_join() handled this asymmetrically. For left and
full joins, it marked source Vars in the merge join condition, action
expressions, and RETURNING list. However, for right and full joins, it
only marked target Vars in the query targetlist. Thus, some Vars, such
as target Vars in action expressions and the RETURNING list, could
remain unmarked.

Such Vars could later be added to the processed targetlist for
evaluation by ModifyTable. This could leave an upper plan node with a
Var whose varnullingrels did not match the corresponding Var produced
by its child, leading to a "wrong varnullingrels" error during setrefs
processing.

To fix, refactor the code so that source and target Vars are handled
in the same way, wherever they appear in the query above the join. For
any kind of outer join, source and/or target Vars in the query
targetlist, action expressions, and RETURNING list are now marked. In
addition, for left and full joins, we do the same for the modified
copy of the merge join condition that is evaluated above the join for
WHEN NOT MATCHED BY SOURCE processing, leaving the original join
condition in the JoinExpr unchanged.

Backpatch to v17, where MERGE into updatable views was added.

Reported-by: Tender Wang <tndrwang@gmail.com>
Author: Tender Wang <tndrwang@gmail.com>
Reviewed-by: solai v <solai.cdac@gmail.com>
Discussion: https://postgr.es/m/CAHewXNkYF4dZ85mFwScSd%2BbMYPLg%3DzvfL1GJz47dxpwFPPjuOg%40mail.gmail.com
Backpatch-through: 17

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/17550c8a71419c7020dad41502eebfecf89729bc

Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 86 ++++++++++++++++---------------
src/test/regress/expected/merge.out       | 16 ++++++
src/test/regress/sql/merge.sql            | 21 ++++++++
3 files changed, 82 insertions(+), 41 deletions(-)



view thread (4+ messages)  latest in thread

Message-ID: <E1xU1PC-00000000Nj1-04Wu@gemulon.postgresql.org>
Permalink:  ../E1xU1PC-00000000Nj1-04Wu@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xU1PC-00000000Nj1-04Wu@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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: dean.a.rasheed@gmail.com
  Subject: Re: pgsql: Fix varnullingrels in MERGE into trigger-updatable view.
  In-Reply-To: <E1xU1PC-00000000Nj1-04Wu@gemulon.postgresql.org>
  References: <E1xU1PC-00000000Nj1-04Wu@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