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

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: Tender Wang <tndrwang(at)gmail(dot)com>
Cc: tharakan(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19929: RETURNING OLD/NEW: segfault when used only inside a hashed subplan
Date: 2026-12-06 15:00:27
Message-ID: CAEZATCWntp86vjk=UU5NjzhMJqPqxe+HQj5e9U6LmNHz6LWfdg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Thu, 26 Nov 2026 at 14:17, Tender Wang <tndrwang(at)gmail(dot)com> wrote:
>
> > 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.

Yes, that makes sense, though it looks a bit odd to try propagating
from sstate->projLeft first, and only propagate from sstate->testexpr
if the former is NULL. It's sufficient to just always propagate from
sstate->testexpr, because if sstate->projLeft is built, it is built
from sstate->testexpr, so it cannot have any flags that
sstate->testexpr doesn't have.

> 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:
>
> 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.

Yes, I think that's probably the most practical fix.

A better fix would be to move all these flags to the ExprContext,
which is shared by all these ExprState nodes. In hindsight, that was a
design flaw. However, changing that seems a bit too much for
back-patching -- adding new fields to ExprContext risks breaking ABI
compatibility, and I think it would be a fairly large/risky change --
the JIT code in particular is quite fiddly. So I think the
flag-propagation approach is a more surgical fix, suitable for
back-patching.

Attached is v2, doing that.

I used Claude to help write the tests -- it wrote a more comprehensive
set of tests than I would have written, which is good because I think
it makes sense to be thorough.

Regards,
Dean

Attachment Content-Type Size
v2-Fix-evaluation-of-OLD-NEW-in-RETURNING-sublinks.patch text/x-patch 18.8 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Michael Banck 2026-12-06 16:09:25 Re: reltuples kept decreasing with each autovacuum run Re: BUG #18773
Previous Message Ishjaap Singh 2026-12-06 12:36:49 Re: BUG #19975: ALTER SUBSCRIPTION ... SET (slot_name, failover) updates the old slot