Wrong varnullingrels for MERGE into a trigger-updatable view

Поиск
Список
Период
Сортировка
Искать

Wrong varnullingrels for MERGE into a trigger-updatable view

От:
Tender Wang <tndrwang@gmail.com>
Дата:
Вложения:
Hi,

I found another “wrong varnullingrels” error involving MERGE into a
trigger-updatable view:

CREATE TABLE mt (id int, k int, v text);
CREATE VIEW mv AS SELECT id, k, v FROM mt;

CREATE FUNCTION mv_insert() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
  RETURN NEW;
END
$$;

CREATE TRIGGER mv_insert
  INSTEAD OF INSERT ON mv
  FOR EACH ROW EXECUTE FUNCTION mv_insert();

CREATE TABLE ms (id int, k int, v text);
INSERT INTO ms VALUES (1, 1, 'source');

MERGE INTO mv t
USING ms s RIGHT JOIN ms w ON s.k = w.k
ON w.k = t.k AND t.k = 0
WHEN MATCHED AND s.k < t.id THEN DO NOTHING
WHEN NOT MATCHED AND s.v IS NOT NULL THEN
  INSERT (v) VALUES (s.v);


On the current master, this fails during planning with an error like:

ERROR:  wrong varnullingrels (b) (expected (b 6)) for Var 7/1

transform_MERGE_to_join() constructs a right join for the
WHEN NOT MATCHED BY TARGET action, making the target relation a
nullable side of the MERGE join.  It marks target Vars in
parse->targetList as nullable by that join, but does not do the same
for target Vars in MERGE action expressions or the RETURNING list.

In this example, the “t.id” Var from the MATCHED action qual is later
added to the processed target list for evaluation by ModifyTable.  It
does not contain the MERGE join's nullingrel, while the corresponding
Var produced by the child join does.  The mismatch is eventually
detected by setrefs.c.

My initial fix mirrored the existing

if (jointype == JOIN_LEFT || jointype == JOIN_FULL)

block for right and full joins, using the target relation instead of
the source relation. However, that duplicated most of the code for
processing action quals, action target lists, and the RETURNING list.

The attached patch instead refactors the existing handling. It first
collects all relation RT indexes on nullable sides of the MERGE join:

- source relids for a left join;
- the target relid for a right join;
- both for a full join.

It then uses that set to mark Vars in the query target list, MERGE
action expressions, and RETURNING list.  For left and full joins, it
also marks the copy of the merge join condition evaluated above the
join for NOT MATCHED BY SOURCE processing.  The original condition in
the JoinExpr remains unchanged.

Thoughts?

-- 
Tender Wang

Re: Wrong varnullingrels for MERGE into a trigger-updatable view

От:
Dean Rasheed <dean.a.rasheed@gmail.com>
Дата:
On Fri, 13 Nov 2026 at 13:13, Tender Wang  wrote:
>
> I found another “wrong varnullingrels” error involving MERGE into a
> trigger-updatable view:

Ah, yes, well spotted.

The test case can a simplified a little:

DROP TABLE IF EXISTS src, tgt CASCADE;
CREATE TABLE src(a int, b int);
CREATE TABLE tgt(a int, b int);
CREATE VIEW tgt_v AS SELECT * FROM tgt;

CREATE OR REPLACE 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);

> The attached patch instead refactors the existing handling. It first
> collects all relation RT indexes on nullable sides of the MERGE join:
>
> - source relids for a left join;
> - the target relid for a right join;
> - both for a full join.
>
> It then uses that set to mark Vars in the query target list, MERGE
> action expressions, and RETURNING list.  For left and full joins, it
> also marks the copy of the merge join condition evaluated above the
> join for NOT MATCHED BY SOURCE processing.  The original condition in
> the JoinExpr remains unchanged.

That sounds reasonable.

Release freeze for v19 is getting near, so rather than rushing to fit
this in, I'll leave it until next week, when the freeze on the v19
branch is over.

Regards,
Dean


Re: Wrong varnullingrels for MERGE into a trigger-updatable view

От:
Tender Wang <tndrwang@gmail.com>
Дата:
Вложения:
Hi Dean,

Dean Rasheed  于2026年11月13日周五 23:44写道:
>
> On Fri, 13 Nov 2026 at 13:13, Tender Wang  wrote:
> >
> > I found another “wrong varnullingrels” error involving MERGE into a
> > trigger-updatable view:
>
> Ah, yes, well spotted.
>
> The test case can a simplified a little:
>

Thanks for simplifying the test case.  I have used that version for
the regression test in the attached v2 patch.


-- 
Tender Wang

Re: Wrong varnullingrels for MERGE into a trigger-updatable view

От:
solai v <solai.cdac@gmail.com>
Дата:

Hi all,

I tested the v2 patch for the issue related to incorrect varnullingrels handling when executing MERGE on a trigger-updatable view.

I was able to reproduce the reported error before applying the patch. After applying the patch and rebuilding PostgreSQL, I ran the same test again, and the MERGE statement completed successfully without the previous wrong varnullingrels error.

The output was:

MERGE 0

I also reviewed the code changes in prepjointree.c. The patch appears to address the issue by ensuring that the nulling-relid information is updated consistently across the relevant MERGE expressions.

I ran the MERGE regression tests as well, and all tests passed.

Based on the reproduction, code review, and regression test results, the patch looks good to me.

Regards,

solai

Re: Wrong varnullingrels for MERGE into a trigger-updatable view

От:
Dean Rasheed <dean.a.rasheed@gmail.com>
Дата:
On Wed, 18 Nov 2026 at 04:09, solai v  wrote:
>
> Based on the reproduction, code review, and regression test results, the patch looks good to me.

Thanks. I have pushed and back-patched this to v17, with some
additional tidying up.

I moved the processing of the merge join condition to the final WHEN
NOT MATCHED BY SOURCE block, because it might not have been
immediately obvious to a reader why that was only being processed for
left and full joins. Moving it to the final block makes it clearer
that it is processed whenever it is used.

Regards,
Dean


FAQ