Skip to content

Commit 6268587

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 054701a commit 6268587

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

src/backend/access/transam/parallel.c

+16-3
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.
@@ -462,6 +471,9 @@ InitializeParallelDSM(ParallelContext *pcxt)
462471
shm_toc_insert(pcxt->toc, PARALLEL_KEY_ENTRYPOINT, entrypointstate);
463472
}
464473

474+
/* Update nworkers_to_launch, in case we changed nworkers above. */
475+
pcxt->nworkers_to_launch = pcxt->nworkers;
476+
465477
/* Restore previous memory context. */
466478
MemoryContextSwitchTo(oldcontext);
467479
}
@@ -525,10 +537,11 @@ ReinitializeParallelWorkers(ParallelContext *pcxt, int nworkers_to_launch)
525537
{
526538
/*
527539
* The number of workers that need to be launched must be less than the
528-
* number of workers with which the parallel context is initialized.
540+
* number of workers with which the parallel context is initialized. But
541+
* the caller might not know that InitializeParallelDSM reduced nworkers,
542+
* so just silently trim the request.
529543
*/
530-
Assert(pcxt->nworkers >= nworkers_to_launch);
531-
pcxt->nworkers_to_launch = nworkers_to_launch;
544+
pcxt->nworkers_to_launch = Min(pcxt->nworkers, nworkers_to_launch);
532545
}
533546

534547
/*

src/backend/executor/nodeHashjoin.c

+7-2
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

-6
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,6 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions,
332332
* if we want to allow parallel inserts in general; updates and deletes
333333
* have additional problems especially around combo CIDs.)
334334
*
335-
* We don't try to use parallel mode unless interruptible. The leader
336-
* expects ProcessInterrupts() calls to reach HandleParallelMessages().
337-
* Even if we called HandleParallelMessages() another way, starting a
338-
* parallel worker is too delay-prone to be prudent when uncancellable.
339-
*
340335
* For now, we don't try to use parallel mode if we're running inside a
341336
* parallel worker. We might eventually be able to relax this
342337
* restriction, but for now it seems best not to have parallel workers
@@ -347,7 +342,6 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions,
347342
parse->commandType == CMD_SELECT &&
348343
!parse->hasModifyingCTE &&
349344
max_parallel_workers_per_gather > 0 &&
350-
INTERRUPTS_CAN_BE_PROCESSED() &&
351345
!IsParallelWorker())
352346
{
353347
/* all the cheap tests pass, so scan the query tree */

0 commit comments

Comments
 (0)