pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Fix varnullingrels in MERGE into trigger-updatable view.
4+ messages / 1 participants
[nested] [flat]

* pgsql: Fix varnullingrels in MERGE into trigger-updatable view.
@ 2026-11-19 12:38  Dean Rasheed <dean.a.rasheed@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Dean Rasheed @ 2026-11-19 12:38 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* pgsql: Fix varnullingrels in MERGE into trigger-updatable view.
@ 2026-11-19 12:38  Dean Rasheed <dean.a.rasheed@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Dean Rasheed @ 2026-11-19 12:38 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* pgsql: Fix varnullingrels in MERGE into trigger-updatable view.
@ 2026-11-19 12:38  Dean Rasheed <dean.a.rasheed@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Dean Rasheed @ 2026-11-19 12:38 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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
------
REL_18_STABLE

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

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



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* pgsql: Fix varnullingrels in MERGE into trigger-updatable view.
@ 2026-11-19 12:38  Dean Rasheed <dean.a.rasheed@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Dean Rasheed @ 2026-11-19 12:38 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/901eb483e0fff340ae3fe496ad2a2d1856f38693

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



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2026-11-19 12:38 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 12:38 pgsql: Fix varnullingrels in MERGE into trigger-updatable view. Dean Rasheed <dean.a.rasheed@gmail.com>
2026-11-19 12:38 pgsql: Fix varnullingrels in MERGE into trigger-updatable view. Dean Rasheed <dean.a.rasheed@gmail.com>
2026-11-19 12:38 pgsql: Fix varnullingrels in MERGE into trigger-updatable view. Dean Rasheed <dean.a.rasheed@gmail.com>
2026-11-19 12:38 pgsql: Fix varnullingrels in MERGE into trigger-updatable view. Dean Rasheed <dean.a.rasheed@gmail.com>

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