pgsql: Fix varnullingrels in MERGE into trigger-updatable view.

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix varnullingrels in MERGE into trigger-updatable view.
Date: 2026-11-19 12:38:23
Message-ID: E1xU1PC-00000000Nj3-090j@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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(at)gmail(dot)com>
Author: Tender Wang <tndrwang(at)gmail(dot)com>
Reviewed-by: solai v <solai(dot)cdac(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAHewXNkYF4dZ85mFwScSd%2BbMYPLg%3DzvfL1GJz47dxpwFPPjuOg%40mail.gmail.com
Backpatch-through: 17

Branch
------
REL_19_STABLE

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

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(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Heikki Linnakangas 2026-11-19 12:49:01 pgsql: Fix setting skip_fsync on temp tables
Previous Message Heikki Linnakangas 2026-11-19 11:43:37 pgsql: Avoid rescanning prefixes when choosing a dollar-quote delimiter