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

From: Tender Wang <tndrwang(at)gmail(dot)com>
To: Kirill Reshke <reshkekirill(at)gmail(dot)com>
Cc: 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 02:06:16
Message-ID: CAHewXN=aDgPDaszKsOk9mOedj_uRqHqO3mJvcdtKAX4sUfxCeg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi,

Tender Wang <tndrwang(at)gmail(dot)com> 于2026年11月26日周四 08:52写道:
>
> Hi,
>
> Kirill Reshke <reshkekirill(at)gmail(dot)com> 于2026年11月26日周四 02:07写道:
>
> > > This test case triggers a crash:
> > > This test case triggers a wrong result:
>
> > Looks like regression since 17550c8a
> >
>
> I confirmed that this is a regression introduced by 17550c8a change to
> transform_MERGE_to_join(), which started applying the MERGE join’s
> nulling relids to target Vars in the RETURNING list.
> Skipping that step for JOIN_RIGHT makes both examples return the
> expected results. I also confirmed that the crash still occurs with a
> FULL JOIN.
>
> I’m looking into the fix.
>

Thanks for the report.
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.

The patch includes regression tests for RIGHT and FULL joins covering
both reported examples. I used an AI tool to help write the new test
cases.

--
Tender Wang

Attachment Content-Type Size
0001-Fix-virtual-generated-column-evaluation-in-MERGE-RET.patch text/plain 7.2 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Previous Message Tender Wang 2026-11-26 00:52:04 Re: BUG #19942: SIGSEGV crash (and wrong results) in MERGE ... RETURNING WITH (OLD/NEW) with VIRTUAL generated colum