Skip to content

Commit 943b653

Browse files
committed
Improve fix for not entering parallel mode when holding interrupts.
Commit ac04aa8 put the shutoff for this into the planner, which is not ideal because it doesn't prevent us from re-using a previously made parallel plan. Revert the planner change and instead put the shutoff into InitializeParallelDSM, modeling it on the existing code there for recovering from failure to allocate a DSM segment. However, that code path is mostly untested, and testing a bit harder showed there's at least one bug: ExecHashJoinReInitializeDSM is not prepared for us to have skipped doing parallel DSM setup. I also thought the Assert in ReinitializeParallelWorkers is pretty ill-advised, and replaced it with a silent Min() operation. The existing test case added by ac04aa8 serves fine to test this version of the fix, so no change needed there. Patch by me, but thanks to Noah Misch for the core idea that we could shut off worker creation when !INTERRUPTS_CAN_BE_PROCESSED. Back-patch to v12, as ac04aa8 was. Discussion: https://postgr.es/m/CAC-SaSzHUKT=vZJ8MPxYdC_URPfax+yoA1hKTcF4ROz_Q6z0_Q@mail.gmail.com
1 parent a0cdfc8 commit 943b653

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

src/backend/access/transam/parallel.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,15 @@ InitializeParallelDSM(ParallelContext *pcxt)
230230
shm_toc_estimate_chunk(&pcxt->estimator, sizeof(FixedParallelState));
231231
shm_toc_estimate_keys(&pcxt->estimator, 1);
232232

233+
/*
234+
* If we manage to reach here while non-interruptible, it's unsafe to
235+
* launch any workers: we would fail to process interrupts sent by them.
236+
* We can deal with that edge case by pretending no workers were
237+
* requested.
238+
*/
239+
if (!INTERRUPTS_CAN_BE_PROCESSED())
240+
pcxt->nworkers = 0;
241+
233242
/*
234243
* Normally, the user will have requested at least one worker process, but
235244
* if by chance they have not, we can skip a bunch of things here.
@@ -476,6 +485,9 @@ InitializeParallelDSM(ParallelContext *pcxt)
476485
shm_toc_insert(pcxt->toc, PARALLEL_KEY_ENTRYPOINT, entrypointstate);
477486
}
478487

488+
/* Update nworkers_to_launch, in case we changed nworkers above. */
489+
pcxt->nworkers_to_launch = pcxt->nworkers;
490+
479491
/* Restore previous memory context. */
480492
MemoryContextSwitchTo(oldcontext);
481493
}
@@ -539,10 +551,11 @@ ReinitializeParallelWorkers(ParallelContext *pcxt, int nworkers_to_launch)
539551
{
540552
/*
541553
* The number of workers that need to be launched must be less than the
542-
* number of workers with which the parallel context is initialized.
554+
* number of workers with which the parallel context is initialized. But
555+
* the caller might not know that InitializeParallelDSM reduced nworkers,
556+
* so just silently trim the request.
543557
*/
544-
Assert(pcxt->nworkers >= nworkers_to_launch);
545-
pcxt->nworkers_to_launch = nworkers_to_launch;
558+
pcxt->nworkers_to_launch = Min(pcxt->nworkers, nworkers_to_launch);
546559
}
547560

548561
/*

src/backend/executor/nodeHashjoin.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,8 +1609,13 @@ void
16091609
ExecHashJoinReInitializeDSM(HashJoinState *state, ParallelContext *pcxt)
16101610
{
16111611
int plan_node_id = state->js.ps.plan->plan_node_id;
1612-
ParallelHashJoinState *pstate =
1613-
shm_toc_lookup(pcxt->toc, plan_node_id, false);
1612+
ParallelHashJoinState *pstate;
1613+
1614+
/* Nothing to do if we failed to create a DSM segment. */
1615+
if (pcxt->seg == NULL)
1616+
return;
1617+
1618+
pstate = shm_toc_lookup(pcxt->toc, plan_node_id, false);
16141619

16151620
/*
16161621
* It would be possible to reuse the shared hash table in single-batch

src/backend/optimizer/plan/planner.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,6 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions,
340340
* we want to allow parallel inserts in general; updates and deletes have
341341
* additional problems especially around combo CIDs.)
342342
*
343-
* We don't try to use parallel mode unless interruptible. The leader
344-
* expects ProcessInterrupts() calls to reach HandleParallelMessages().
345-
* Even if we called HandleParallelMessages() another way, starting a
346-
* parallel worker is too delay-prone to be prudent when uncancellable.
347-
*
348343
* For now, we don't try to use parallel mode if we're running inside a
349344
* parallel worker. We might eventually be able to relax this
350345
* restriction, but for now it seems best not to have parallel workers
@@ -355,7 +350,6 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions,
355350
parse->commandType == CMD_SELECT &&
356351
!parse->hasModifyingCTE &&
357352
max_parallel_workers_per_gather > 0 &&
358-
INTERRUPTS_CAN_BE_PROCESSED() &&
359353
!IsParallelWorker())
360354
{
361355
/* all the cheap tests pass, so scan the query tree */

0 commit comments

Comments
 (0)