Re: BUG #19929: RETURNING OLD/NEW: segfault when used only inside a hashed subplan

From: Tender Wang <tndrwang(at)gmail(dot)com>
To: tharakan(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
Subject: Re: BUG #19929: RETURNING OLD/NEW: segfault when used only inside a hashed subplan
Date: 2026-11-26 14:17:23
Message-ID: CAHewXN=K96NLn7T5ddWbh0vEhxjf+Z-LiDv8VwCrwjL=K_h9cw@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日周四 11:56写道:
>
> I tried propagating the OLD/NEW dependency flags to the caller’s
> ExprState in ExecInitSubPlanExpr().
> The attached patch takes the flags from the left-hand projection for a
> hashed SubPlan and from the test expression for a non-hashed SubPlan.
> With the patch applied, the DELETE above completes and returns false
> as expected.
>
> Since this goes back to commit 80feb727c86, which added OLD/NEW
> support to RETURNING, I’ve CC’d Dean.
>

I found one more case with AI tools while testing v1. A whole-row NEW
reference in a hashed ANY still gives the wrong result:

CREATE TABLE rw (a int, b text);
INSERT INTO rw VALUES (1, 'x');

DELETE FROM rw WHERE a = 1
RETURNING new.*::text = ANY (SELECT '(,)'::text);

On master, this crashes. With v1 applied, it returns true, but the
expected result is SQL NULL because DELETE has no NEW row.
I also saw the same issue with system columns. On v1, comparing
new.tableoid::text against '0' with ANY returns true,
and the corresponding new.xmin::text case raises “cannot retrieve a
system column in this context.”

It looks like v1 supplies the all-null slot, but ExecEvalWholeRowVar()
checks EEO_FLAG_NEW_IS_NULL in the ExprState that evaluates the
whole-row reference.
In this case, that is the hashed SubPlan’s projLeft, while
ExecProcessReturning() sets the flag only in the outer RETURNING
state.
As a result, projLeft turns the all-null slot into the composite value
whose text representation is (,).

One possible fix is to copy the caller’s OLD_IS_NULL and NEW_IS_NULL
bits to the SubPlan’s projLeft or testexpr in ExecEvalSubPlan(),
before each evaluation.
This would replace the previous values of those bits so the child
state reflects the current row. I haven’t tested this follow-up change
yet.

--
Tender Wang

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Dean Rasheed 2026-11-26 14:43:22 Re: BUG #19942: SIGSEGV crash (and wrong results) in MERGE ... RETURNING WITH (OLD/NEW) with VIRTUAL generated colum
Previous Message Alvaro Herrera 2026-11-26 12:56:28 Re: BUG #19928: pg_upgrade fails on v18 schema with NOT NULL ... NO INHERIT and inheritance