postgres.git / summary / log / commit / refs

commit    a7373be91caa97d978eaf902a23dfd39d5661952
Author:   Dean Rasheed <dean.a.rasheed@gmail.com>
Date:     Thu Nov 19 12:36:04 2026 +0000

    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


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(-) diff --git a/src/backend/optimizer/prep/prepjointree.c b/src/backend/optimizer/prep/prepjointree.c index 0e08b0d763a..9398f286377 100644 --- a/src/backend/optimizer/prep/prepjointree.c +++ b/src/backend/optimizer/prep/prepjointree.c @@ -200,6 +200,8 @@ transform_MERGE_to_join(Query *parse) FromExpr *target; Node *source; int sourcerti; + Relids nullable_relids; + Relids nullingrel; if (parse->commandType != CMD_MERGE) return; @@ -309,63 +311,57 @@ transform_MERGE_to_join(Query *parse) parse->jointree->quals = NULL; /* - * If necessary, mark parse->targetlist entries that refer to the target - * as nullable by the join. Normally the targetlist will be empty for a - * MERGE, but if the target is a trigger-updatable view, it will contain a - * whole-row Var referring to the expanded view query. + * Find all relations nullable by the join. If that includes the source + * relation, and it is itself a join, we must include all relations under + * it, since there may be Vars above the new join referring to relations + * under the source join. That cannot happen for the target relation, + * even if it's an expanded view subquery. */ - if (parse->targetList != NIL && - (jointype == JOIN_RIGHT || jointype == JOIN_FULL)) - parse->targetList = (List *) - add_nulling_relids((Node *) parse->targetList, - bms_make_singleton(parse->mergeTargetRelation), - bms_make_singleton(joinrti)); + if (jointype == JOIN_LEFT) + nullable_relids = get_relids_in_jointree(source, true, true); + else if (jointype == JOIN_RIGHT) + nullable_relids = bms_make_singleton(parse->mergeTargetRelation); + else if (jointype == JOIN_FULL) + { + nullable_relids = get_relids_in_jointree(source, true, true); + nullable_relids = bms_add_member(nullable_relids, + parse->mergeTargetRelation); + } + else + nullable_relids = NULL; + + nullingrel = nullable_relids ? bms_make_singleton(joinrti) : NULL; /* - * If the source relation is on the outer side of the join, mark any - * source relation Vars in the join condition, actions, and RETURNING list - * as nullable by the join. These Vars will be added to the targetlist by - * preprocess_targetlist(), so it's important to mark them correctly here. + * Mark Vars used above the join as nullable by the join. This includes + * Vars in the query targetlist, action expressions, and RETURNING list. + * Vars from the latter expressions will be added to the targetlist by + * preprocess_targetlist(), so they must be marked correctly here. * - * It might seem that this is not necessary for Vars in the join - * condition, since it is inside the join, but it is also needed above the - * join (in the ModifyTable node) to distinguish between the MATCHED and - * NOT MATCHED BY SOURCE cases -- see ExecMergeMatched(). Note that this - * creates a modified copy of the join condition, for use above the join, - * without modifying the original join condition, inside the join. + * Normally the query targetlist is empty for MERGE, but for a + * trigger-updatable view it contains a whole-row Var referring to the + * expanded view query. */ - if (jointype == JOIN_LEFT || jointype == JOIN_FULL) + if (nullable_relids != NULL) { - Relids sourcerelids; - - /* - * If the source relation is a join, we must mark any Vars referring - * to relations under the join as well as the join relation itself. - */ - sourcerelids = get_relids_in_jointree(source, true, true); - - parse->mergeJoinCondition = - add_nulling_relids(parse->mergeJoinCondition, - sourcerelids, - bms_make_singleton(joinrti)); + parse->targetList = (List *) + add_nulling_relids((Node *) parse->targetList, + nullable_relids, nullingrel); foreach_node(MergeAction, action, parse->mergeActionList) { action->qual = add_nulling_relids(action->qual, - sourcerelids, - bms_make_singleton(joinrti)); + nullable_relids, nullingrel); action->targetList = (List *) add_nulling_relids((Node *) action->targetList, - sourcerelids, - bms_make_singleton(joinrti)); + nullable_relids, nullingrel); } parse->returningList = (List *) add_nulling_relids((Node *) parse->returningList, - sourcerelids, - bms_make_singleton(joinrti)); + nullable_relids, nullingrel); } /* @@ -388,10 +384,9 @@ transform_MERGE_to_join(Query *parse) Var *var; NullTest *ntest; - /* source wholerow Var (nullable by the new join) */ + /* source wholerow Var */ var = makeWholeRowVar(rt_fetch(sourcerti, parse->rtable), sourcerti, 0, false); - var->varnullingrels = bms_make_singleton(joinrti); /* "src IS NOT NULL" check */ ntest = makeNode(NullTest); @@ -403,6 +398,15 @@ transform_MERGE_to_join(Query *parse) /* combine it with the original join condition */ parse->mergeJoinCondition = (Node *) make_and_qual((Node *) ntest, parse->mergeJoinCondition); + + /* + * Mark Vars used in the new join condition as nullable by the join, + * since it will be used above the join. This creates a copy, without + * modifying the original join condition inside the join. + */ + parse->mergeJoinCondition = + add_nulling_relids(parse->mergeJoinCondition, + nullable_relids, nullingrel); } else parse->mergeJoinCondition = NULL; /* join condition not needed */ diff --git a/src/test/regress/expected/merge.out b/src/test/regress/expected/merge.out index 06b7b543346..28b84c71e7d 100644 --- a/src/test/regress/expected/merge.out +++ b/src/test/regress/expected/merge.out @@ -2920,6 +2920,22 @@ SELECT * FROM bug18634t; DROP TABLE bug18634t CASCADE; NOTICE: drop cascades to view bug18634v DROP TABLE bug18634s, bug18634s2; +-- Test nullingrels for target Vars above the right join generated for MERGE +-- into a trigger-updatable view with a join source. +CREATE TABLE src (a int, b int); +CREATE TABLE tgt (a int, b int); +CREATE VIEW tgt_v AS SELECT * FROM tgt; +CREATE FUNCTION tgt_v_ins_fn() RETURNS trigger + LANGUAGE plpgsql AS $$ BEGIN RETURN new; END $$; +CREATE TRIGGER tgt_v_ins_trig INSTEAD OF INSERT ON tgt_v + FOR EACH ROW EXECUTE FUNCTION tgt_v_ins_fn(); +MERGE INTO tgt_v t + USING (src s JOIN src s2 ON s2.b = s.b) ON t.b = s.b AND t.b = 0 + WHEN MATCHED AND t.b = 0 THEN DO NOTHING + WHEN NOT MATCHED THEN INSERT VALUES (s.a, s.b); +DROP VIEW tgt_v; +DROP FUNCTION tgt_v_ins_fn(); +DROP TABLE src, tgt; -- prepare RESET SESSION AUTHORIZATION; -- try a system catalog diff --git a/src/test/regress/sql/merge.sql b/src/test/regress/sql/merge.sql index 37aef4de68a..91a8428e979 100644 --- a/src/test/regress/sql/merge.sql +++ b/src/test/regress/sql/merge.sql @@ -1882,6 +1882,27 @@ SELECT * FROM bug18634t; DROP TABLE bug18634t CASCADE; DROP TABLE bug18634s, bug18634s2; +-- Test nullingrels for target Vars above the right join generated for MERGE +-- into a trigger-updatable view with a join source. +CREATE TABLE src (a int, b int); +CREATE TABLE tgt (a int, b int); +CREATE VIEW tgt_v AS SELECT * FROM tgt; + +CREATE FUNCTION tgt_v_ins_fn() RETURNS trigger + LANGUAGE plpgsql AS $$ BEGIN RETURN new; END $$; + +CREATE TRIGGER tgt_v_ins_trig INSTEAD OF INSERT ON tgt_v + FOR EACH ROW EXECUTE FUNCTION tgt_v_ins_fn(); + +MERGE INTO tgt_v t + USING (src s JOIN src s2 ON s2.b = s.b) ON t.b = s.b AND t.b = 0 + WHEN MATCHED AND t.b = 0 THEN DO NOTHING + WHEN NOT MATCHED THEN INSERT VALUES (s.a, s.b); + +DROP VIEW tgt_v; +DROP FUNCTION tgt_v_ins_fn(); +DROP TABLE src, tgt; + -- prepare RESET SESSION AUTHORIZATION; [parent: 0c806e75dabc]