Re: BUG #19942: SIGSEGV crash (and wrong results) in MERGE ... RETURNING WITH (OLD/NEW) with VIRTUAL generated colum

From: Kirill Reshke <reshkekirill(at)gmail(dot)com>
To: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
Cc: Tender Wang <tndrwang(at)gmail(dot)com>, 798604270(at)qq(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19942: SIGSEGV crash (and wrong results) in MERGE ... RETURNING WITH (OLD/NEW) with VIRTUAL generated colum
Date: 2026-11-26 20:14:01
Message-ID: CALdSSPih8J0AYMLdBZ7Y-8Q4Q3E2NxEZzwb+wh2jD-rfNKGSOw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Thu, 26 Nov 2026 at 19:43, Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com> wrote:
>
> On Thu, 26 Nov 2026 at 02:06, Tender Wang <tndrwang(at)gmail(dot)com> wrote:
> >
> > In the trigger-updatable view fix, I overlooked how target Vars in
> > RETURNING are evaluated.
> > We applied the same nulling-relid marking to the RETURNING list and
> > the MERGE action expressions.
> > As a result, RIGHT and FULL MERGE joins started marking target Vars in
> > RETURNING as nullable by the join.
> >
> > Target values in RETURNING come from the row versions supplied by
> > ModifyTable. For example, an INSERT
> > has no matching target row in the join output, but RETURNING can still
> > read the newly inserted row.
> > The extra nulling relid can cause an expanded virtual generated column
> > expression to be evaluated in the target scan,
> > before the OLD and NEW slots are available. This explains both the
> > crash and the incorrect results.
> >
> > The attached patch restores the previous marking rules for RETURNING.
> > It adds the MERGE join’s nulling relid only to
> > source Vars when a WHEN NOT MATCHED BY SOURCE action requires a LEFT
> > or FULL join.
>
> Hmm, I was thinking that actually the problem with 17550c8a was that
> it didn't properly distinguish MERGE into a trigger-updatable view
> from MERGE into a regular table (or auto-updatable view, which is the
> same by the time the planner sees it). It's only for a
> trigger-updatable view that parse->mergeTargetRelation (the expanded
> view sub-query) needs to be marked as nullable by the join. For a
> regular table (or auto-updatable view), parse->mergeTargetRelation ==
> parse->resultRelation, and its attributes are read directly, not via
> the join, so it should not be marked nullable.
>
> So I was thinking of something more like the attached v2.
>
> Regards,
> Dean

Hi!

I quickly checked patch - didn't spot any regression or functional
bug. For tests part - maybe add regress for target virtual columns in
WHEN and UPDATE action expressions, e.g.
MERGE_WHEN_NOT_MATCHED_BY_SOURCE + MERGE_WHEN_NOT_MATCHED_BY_TARGET
which uses FULL join? v2 changes nullable_relids for these but does
not exercise this code path.

something simple like

MERGE INTO t
USING s ON t.id = s.id
WHEN NOT MATCHED BY TARGET THEN DO NOTHING
WHEN NOT MATCHED BY SOURCE AND t.twice = 40
THEN UPDATE SET a = t.twice + 1;

This query doesn't show regression (works even without v2) but maybe
useful for coverage.

--
Best regards,
Kirill Reshke

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message shihao zhong 2026-11-26 20:16:58 Re: BUG #19873: planner silently drops the second ORDER BY sort key when both keys reference the same expression und
Previous Message Narayanan Venkateswaran 2026-11-26 19:23:22 Re: BUG #19737: Empty `JSON_OBJECT` cannot use documented `ON NULL` or unique-key clauses