Skip to content

Commit 989ccd2

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 62df548 commit 989ccd2

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
@@ -226,6 +226,15 @@ InitializeParallelDSM(ParallelContext *pcxt)
226226
shm_toc_estimate_chunk(&pcxt->estimator, sizeof(FixedParallelState));
227227
shm_toc_estimate_keys(&pcxt->estimator, 1);
228228

229+
/*
230+
* If we manage to reach here while non-interruptible, it's unsafe to
231+
* launch any workers: we would fail to process interrupts sent by them.
232+
* We can deal with that edge case by pretending no workers were
233+
* requested.
234+
*/
235+
if (!INTERRUPTS_CAN_BE_PROCESSED())
236+
pcxt->nworkers = 0;
237+
229238
/*
230239
* Normally, the user will have requested at least one worker process, but
231240
* if by chance they have not, we can skip a bunch of things here.
@@ -463,6 +472,9 @@ InitializeParallelDSM(ParallelContext *pcxt)
463472
shm_toc_insert(pcxt->toc, PARALLEL_KEY_ENTRYPOINT, entrypointstate);
464473
}
465474

475+
/* Update nworkers_to_launch, in case we changed nworkers above. */
476+
pcxt->nworkers_to_launch = pcxt->nworkers;
477+
466478
/* Restore previous memory context. */
467479
MemoryContextSwitchTo(oldcontext);
468480
}
@@ -526,10 +538,11 @@ ReinitializeParallelWorkers(ParallelContext *pcxt, int nworkers_to_launch)
526538
{
527539
/*
528540
* The number of workers that need to be launched must be less than the
529-
* number of workers with which the parallel context is initialized.
541+
* number of workers with which the parallel context is initialized. But
542+
* the caller might not know that InitializeParallelDSM reduced nworkers,
543+
* so just silently trim the request.
530544
*/
531-
Assert(pcxt->nworkers >= nworkers_to_launch);
532-
pcxt->nworkers_to_launch = nworkers_to_launch;
545+
pcxt->nworkers_to_launch = Min(pcxt->nworkers, nworkers_to_launch);
533546
}
534547

535548
/*

src/backend/executor/nodeHashjoin.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,8 +1524,13 @@ void
15241524
ExecHashJoinReInitializeDSM(HashJoinState *state, ParallelContext *cxt)
15251525
{
15261526
int plan_node_id = state->js.ps.plan->plan_node_id;
1527-
ParallelHashJoinState *pstate =
1528-
shm_toc_lookup(cxt->toc, plan_node_id, false);
1527+
ParallelHashJoinState *pstate;
1528+
1529+
/* Nothing to do if we failed to create a DSM segment. */
1530+
if (cxt->seg == NULL)
1531+
return;
1532+
1533+
pstate = shm_toc_lookup(cxt->toc, plan_node_id, false);
15291534

15301535
/*
15311536
* 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
@@ -327,11 +327,6 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions,
327327
* if we want to allow parallel inserts in general; updates and deletes
328328
* have additional problems especially around combo CIDs.)
329329
*
330-
* We don't try to use parallel mode unless interruptible. The leader
331-
* expects ProcessInterrupts() calls to reach HandleParallelMessages().
332-
* Even if we called HandleParallelMessages() another way, starting a
333-
* parallel worker is too delay-prone to be prudent when uncancellable.
334-
*
335330
* For now, we don't try to use parallel mode if we're running inside a
336331
* parallel worker. We might eventually be able to relax this
337332
* restriction, but for now it seems best not to have parallel workers
@@ -342,7 +337,6 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions,
342337
parse->commandType == CMD_SELECT &&
343338
!parse->hasModifyingCTE &&
344339
max_parallel_workers_per_gather > 0 &&
345-
INTERRUPTS_CAN_BE_PROCESSED() &&
346340
!IsParallelWorker())
347341
{
348342
/* all the cheap tests pass, so scan the query tree */

0 commit comments

Comments
 (0)