From 5b4c19ee0a4aae40232a0724fbb5c0b6bc216843 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Wed, 2 Dec 2026 13:54:12 -0500 Subject: [PATCH 2/2] Make a parallel apply worker's error FATAL inside a subtransaction When a parallel apply worker fails, the leader apply worker raises "logical replication parallel apply worker exited due to error", and the errdetail says the leader will now exit. It is an ERROR so that start_apply()'s PG_CATCH() can honor disable_on_error and report the subscription error before exiting. But the leader may be running user code when the message arrives, a trigger on a replicated table for example. If that code is inside a PL/pgSQL exception block, the block catches the ERROR, aborts only its subtransaction, and the leader carries on with a dead worker until it trips over it later, with "could not send data to shared-memory queue" for example. The leader never starts a subtransaction of its own, so use FATAL when IsSubTransaction() is true, the same test report_recovery_conflict() uses to decide that an ERROR is not enough. At top level it stays an ERROR, so disable_on_error keeps working; making it FATAL unconditionally breaks that, and 023_twophase_stream with it. Reported-by: Heikki Linnakangas Backpatch-through: 16 Discussion: https://postgr.es/m/ee2a12a6-89f0-4ac0-9939-7953266453de@iki.fi --- src/backend/replication/logical/applyparallelworker.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/backend/replication/logical/applyparallelworker.c b/src/backend/replication/logical/applyparallelworker.c index b9730de4c70..3b84c7a7987 100644 --- a/src/backend/replication/logical/applyparallelworker.c +++ b/src/backend/replication/logical/applyparallelworker.c @@ -1040,11 +1040,18 @@ ProcessParallelApplyMessage(StringInfo msg) /* * The actual error must have been reported by the parallel apply * worker. + * + * The leader must exit, as the errdetail says. It never starts a + * subtransaction of its own, so if we are in one, user code (a + * PL/pgSQL exception block in a trigger, say) could catch an + * ERROR and carry on; use FATAL then. At top level, ERROR lets + * start_apply() honor disable_on_error before exiting. */ - ereport(ERROR, + ereport(IsSubTransaction() ? FATAL : ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("logical replication parallel apply worker exited due to error"), errdetail("The leader apply worker will now exit and terminate any other parallel apply workers."))); + pg_unreachable(); /* * Don't need to do anything about NoticeResponse and -- 2.54.0