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

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: Tender Wang <tndrwang(at)gmail(dot)com>
Cc: Kirill Reshke <reshkekirill(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 14:43:22
Message-ID: CAEZATCV_yBGZEpNzdetcKmCh8+DESxGmvKyXUcVs58rAK7yaRQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

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

Attachment Content-Type Size
v2-Fix-virtual-generated-column-evaluation-in-MERGE-RETURNING.patch text/x-patch 6.2 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Fujii Masao 2026-11-26 14:59:28 Re: Lost walsender wakeup stalls synchronous commits until the standby's next reply (16+)
Previous Message Tender Wang 2026-11-26 14:17:23 Re: BUG #19929: RETURNING OLD/NEW: segfault when used only inside a hashed subplan