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

Started by PG Bug reporting form7 days ago3 messagesbugs

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

The following bug has been logged on the website:

Bug reference: 19929
Logged by: Robins Tharakan
Email address: tharakan@gmail.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

v17 and older don't have OLD/NEW in RETURNING.
A release build (-O2, no --enable-cassert) crashes the same way.

Error Log
=========
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);
LOG: terminating any other active server processes
LOG: all server processes terminated; reinitializing

Backtrace
=========
#0 slot_getsomeattrs (slot=0x0, attnum=1) at tuptable.h:379
#1 ExecInterpExpr (...) at execExprInterp.c:684
#2 ExecInterpExprStillValid (...) at execExprInterp.c:2309
#3 ExecEvalExprNoReturn (...) at executor.h:431
#4 ExecEvalExprNoReturnSwitchContext (...) at executor.h:472
#5 ExecProject (projInfo=...) at executor.h:504
#6 ExecProcessReturning (..., isDelete=true, oldSlot=...,
newSlot=0x0, ...) at nodeModifyTable.c:366
#7 ExecDelete (...) at nodeModifyTable.c:1936
#8 ExecModifyTable (...) at nodeModifyTable.c:4809
#9 ExecProcNodeFirst (...) at execProcnode.c:469

For the INSERT variant, the same frames with isDelete=false,
oldSlot=0x0, from ExecInsert().

I see a partial signature overlap with bug #19752[1], but
that was a GROUP BY, and think it doesn't hurt to submit this
nonetheless.

Found using SQLsmith, SQLReduce, creduce and Claude.

-
Robins Tharakan
Databricks

Reference:
1. https://commitfest.postgresql.org/patch/7545/

#2Tender Wang
tndrwang@gmail.com
In reply to: PG Bug reporting form (#1)
Re: BUG #19929: RETURNING OLD/NEW: segfault when used only inside a hashed subplan

Hi,

PG Bug reporting form <noreply@postgresql.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@gmail.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

Attachments:

t259829_2
0001-Propagate-OLD-NEW-flags-from-SubPlan-test-expression.patchtext/plain; charset=US-ASCII; name=0001-Propagate-OLD-NEW-flags-from-SubPlan-test-expression.patchDownload+11-1
#3Tender Wang
tndrwang@gmail.com
In reply to: Tender Wang (#2)
Re: BUG #19929: RETURNING OLD/NEW: segfault when used only inside a hashed subplan

Hi,

Tender Wang <tndrwang@gmail.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