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 03:56:06
Message-ID: CAHewXN=hGV328tJA6ju+igkJCGnHvX9Kw1UUWZ7_KM9UkTa0FQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi,

PG Bug reporting form <noreply(at)postgresql(dot)org> 于2026年11月26日周四 01:56写道:
>
> The following bug has been logged on the website:
>
> Bug reference: 19929
> Logged by: Robins Tharakan
> Email address: tharakan(at)gmail(dot)com
> PostgreSQL version: Unsupported/Unknown
> Operating system: Debian 13 (x86_64)
> Description:
>
> A DELETE whose RETURNING list reads NEW only inside a sub-select that
> the planner hashes makes the client backend dereference a NULL slot
> pointer and die with signal 11; the postmaster then restarts the other
> backends. The same happens for OLD in an INSERT, and for MERGE's DELETE
> and INSERT actions:
>
> LOG: client backend (PID 1488746) was terminated by signal 11:
> Segmentation fault
> DETAIL: Failed process was running: DELETE FROM t RETURNING
> EXISTS (SELECT WHERE new.a = 1);
>
> In a DELETE, NEW is all NULL, so the query should return one row, f.
> With new.a also in the RETURNING list itself, it does: (NULL, f).
>
>
> SQL
> ===
> CREATE TABLE t (a int);
> INSERT INTO t VALUES (1);
>
> -- In a DELETE, NEW is all NULL, so this should return f.
> DELETE FROM t RETURNING EXISTS (SELECT WHERE new.a = 1);
>
> -- Variants, each crashes the same way (run one at a time):
>
> -- OLD in an INSERT (OLD is all NULL there)
> INSERT INTO t VALUES (2) RETURNING old.a IN (SELECT 1);
>
> -- NEW in a DELETE, with IN instead of EXISTS
> DELETE FROM t RETURNING new.a IN (SELECT 1);
>
> -- MERGE's DELETE action
> MERGE INTO t USING (VALUES (1)) v(x) ON t.a = v.x
> WHEN MATCHED THEN DELETE
> RETURNING EXISTS (SELECT WHERE new.a = 1);
>
> -- Doesn't crash: new.a also in the RETURNING list (returns NULL, f)
> DELETE FROM t RETURNING new.a, EXISTS (SELECT WHERE new.a = 1);
>
> The plan shows the hashed subplan:
>
> Delete on public.t
> Output: (ANY (new.a = (hashed SubPlan exists_to_any_1).col1))
> -> Seq Scan on public.t
> Output: t.ctid
> SubPlan exists_to_any_1
> -> Result
> Output: 1
>
>
> Commit
> ======
> Started: 80feb727c86
> Reproducible: HEAD (9b5d4e33317) through REL_18_STABLE

I stepped through executor initialization in gdb. After
ExecInitSubPlan() had built the left-hand projection, the two
ExprStates had different values for EEO_FLAG_HAS_NEW (0x04):

(gdb) p/x sstate->projLeft->pi_state.flags & 0x04
$12 = 0x4
(gdb) p/x state->flags & 0x04
$13 = 0x0

Here, state is the ExprState for the RETURNING projection in
ExecInitSubPlanExpr(). Its setup walker has already emitted an
EEOP_NEW_FETCHSOME step for new.a, but the HAS_NEW bit is set only
in the SubPlan's left-hand projection.

When ExecProcessReturning() sees a NULL newSlot, it checks the
RETURNING projection's HAS_NEW bit to decide whether to supply an
all-null slot. Since the bit is clear, ecxt_newtuple is set to NULL.
The fetch step then dereferences that pointer before the SubPlan
runs.
...
if (newSlot)
econtext->ecxt_newtuple = newSlot;
else if (projectReturning->pi_state.flags & EEO_FLAG_HAS_NEW)
econtext->ecxt_newtuple = ExecGetAllNullSlot(estate, resultRelInfo);
else
econtext->ecxt_newtuple = NULL; /* No references to NEW columns */
...

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.

--
Tender Wang

Attachment Content-Type Size
0001-Propagate-OLD-NEW-flags-from-SubPlan-test-expression.patch text/plain 1.9 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Previous Message Hayato Kuroda (Fujitsu) 2026-11-26 02:21:02 RE: BUG #19950: pg_terminate_backend rejects a positive bigint timeout as negative