From f0dfb7cda35dcb5a0704677842a2991b087026a3 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Wed, 2 Dec 2026 13:54:12 -0500 Subject: [PATCH 1/2] Don't lose parallel worker messages behind a caught error ProcessParallelMessages() clears ParallelMessagePending and then reads every worker's error queue. When it reaches a worker's ErrorResponse it rethrows the error, so anything still queued behind that message, from the same worker or a later one, is not read. Normally that is harmless: the error aborts the (sub)transaction that owns the ParallelContext, and its workers go with it. But since 0075d78947e, a PARALLEL SAFE PL/pgSQL function running in the leader can catch that error with an exception block. Only the inner subtransaction aborts; the ParallelContext belongs to an outer one and lives on. If the worker has already exited by the time the leader reads its ErrorResponse, its exit signal was folded into the same pending flag, nothing sets the flag again, and nobody notices that the worker's queue has detached. WaitForParallelWorkersToFinish() then waits for a worker that is already gone, until the statement is canceled. Fix by setting the flag again when an error escapes the reader, so the next CHECK_FOR_INTERRUPTS() reads the rest. That finds the queue detached and reports "lost connection to parallel worker". ProcessProcSignalBarrier() handles errors from its absorb functions the same way. Backpatch-through: 17 Discussion: https://postgr.es/m/ee2a12a6-89f0-4ac0-9939-7953266453de@iki.fi --- src/backend/access/transam/parallel.c | 87 ++++++++++++++++----------- 1 file changed, 53 insertions(+), 34 deletions(-) diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c index e0fef0e8d70..72419fbde07 100644 --- a/src/backend/access/transam/parallel.c +++ b/src/backend/access/transam/parallel.c @@ -1101,49 +1101,68 @@ ProcessParallelMessages(void) /* OK to process messages. Reset the flag saying there are more to do. */ ParallelMessagePending = false; - dlist_foreach(iter, &pcxt_list) + /* + * An ErrorResponse from a worker is rethrown here, and the messages + * queued behind it stay unread. If the error is caught, by a PL/pgSQL + * exception block in a parallel-safe function running in the leader for + * example, the ParallelContext outlives it and nothing would bring us + * back here for them. So set the flag again on the way out; a spare call + * finds the queues empty. + */ + PG_TRY(); { - ParallelContext *pcxt; - int i; + dlist_foreach(iter, &pcxt_list) + { + ParallelContext *pcxt; + int i; - pcxt = dlist_container(ParallelContext, node, iter.cur); - if (pcxt->worker == NULL) - continue; + pcxt = dlist_container(ParallelContext, node, iter.cur); + if (pcxt->worker == NULL) + continue; - for (i = 0; i < pcxt->nworkers_launched; ++i) - { - /* - * Read as many messages as we can from each worker, but stop when - * either (1) the worker's error queue goes away, which can happen - * if we receive a Terminate message from the worker; or (2) no - * more messages can be read from the worker without blocking. - */ - while (pcxt->worker[i].error_mqh != NULL) + for (i = 0; i < pcxt->nworkers_launched; ++i) { - shm_mq_result res; - Size nbytes; - void *data; - - res = shm_mq_receive(pcxt->worker[i].error_mqh, &nbytes, - &data, true); - if (res == SHM_MQ_WOULD_BLOCK) - break; - else if (res == SHM_MQ_SUCCESS) + /* + * Read as many messages as we can from each worker, but stop + * when either (1) the worker's error queue goes away, which + * can happen if we receive a Terminate message from the + * worker; or (2) no more messages can be read from the worker + * without blocking. + */ + while (pcxt->worker[i].error_mqh != NULL) { - StringInfoData msg; - - initStringInfo(&msg); - appendBinaryStringInfo(&msg, data, nbytes); - ProcessParallelMessage(pcxt, i, &msg); - pfree(msg.data); + shm_mq_result res; + Size nbytes; + void *data; + + res = shm_mq_receive(pcxt->worker[i].error_mqh, &nbytes, + &data, true); + if (res == SHM_MQ_WOULD_BLOCK) + break; + else if (res == SHM_MQ_SUCCESS) + { + StringInfoData msg; + + initStringInfo(&msg); + appendBinaryStringInfo(&msg, data, nbytes); + ProcessParallelMessage(pcxt, i, &msg); + pfree(msg.data); + } + else + ereport(ERROR, + (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), + errmsg("lost connection to parallel worker"))); } - else - ereport(ERROR, - (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), - errmsg("lost connection to parallel worker"))); } } } + PG_CATCH(); + { + InterruptPending = true; + ParallelMessagePending = true; + PG_RE_THROW(); + } + PG_END_TRY(); MemoryContextSwitchTo(oldcontext); base-commit: 514935de8c8925c98a13e29a974ce341207243ac -- 2.54.0