From 7a0605d26411002b989b489a8387c92b34c3e93c Mon Sep 17 00:00:00 2001 From: Tender Wang Date: Thu, 26 Nov 2026 11:41:12 +0800 Subject: [PATCH] Propagate OLD/NEW flags from SubPlan test expressions SubPlan test expressions are compiled into separate ExprStates even though they use the caller's expression context. In a RETURNING projection, this can leave EEO_FLAG_HAS_OLD or EEO_FLAG_HAS_NEW unset in the caller, so ExecProcessReturning() does not supply an all-null slot when the corresponding row is absent. For example, DELETE ... RETURNING EXISTS (SELECT WHERE new.a = 1) can be planned as a hashed ANY SubPlan. The RETURNING projection emits a step to fetch NEW attributes, but EEO_FLAG_HAS_NEW is set only in the SubPlan's left-hand projection. The fetch step then dereferences a null slot pointer. Propagate the OLD/NEW dependency flags from the left-hand projection of a hashed SubPlan or from the test expression of a non-hashed SubPlan to the caller's ExprState. --- src/backend/executor/execExpr.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/backend/executor/execExpr.c b/src/backend/executor/execExpr.c index 91c429b894a..8ac53943179 100644 --- a/src/backend/executor/execExpr.c +++ b/src/backend/executor/execExpr.c @@ -2862,6 +2862,17 @@ ExecInitSubPlanExpr(SubPlan *subplan, sstate = ExecInitSubPlan(subplan, state->parent); + /* + * The subplan's test expression uses the caller's expression context, + * so record which OLD/NEW slots it needs in the caller's ExprState. + */ + if (sstate->projLeft != NULL) + state->flags |= sstate->projLeft->pi_state.flags & + (EEO_FLAG_HAS_OLD | EEO_FLAG_HAS_NEW); + else if (sstate->testexpr != NULL) + state->flags |= sstate->testexpr->flags & + (EEO_FLAG_HAS_OLD | EEO_FLAG_HAS_NEW); + /* add SubPlanState nodes to state->parent->subPlan */ state->parent->subPlan = lappend(state->parent->subPlan, sstate); -- 2.43.0