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 <[email protected]> Author: Tender Wang <[email protected]> Reviewed-by: solai v <[email protected]> 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(-)
