Fix mistaken failure to allow parallelism in corner case.
authorRobert Haas
Fri, 27 Oct 2017 14:04:01 +0000 (16:04 +0200)
committerRobert Haas
Fri, 27 Oct 2017 14:04:10 +0000 (16:04 +0200)
If we try to run a parallel plan in serial mode because, for example,
it's going to be scanned via a cursor, but for some reason we're
already in parallel mode (for example because an outer query is
running in parallel), we'd incorrectly try to launch workers.
Fix by adding a flag to the EState, so that we can be certain that
ExecutePlan() and ExecGather()/ExecGatherMerge() will have the same
idea about whether we are executing serially or in parallel.

Report and fix by Amit Kapila with help from Kuntal Ghosh.  A few
tweaks by me.

Discussion: http://postgr.es/m/CAA4eK1+_BuZrmVCeua5Eqnm4Co9DAXdM5HPAOE2J19ePbR912Q@mail.gmail.com

src/backend/executor/execMain.c
src/backend/executor/execUtils.c
src/backend/executor/nodeGather.c
src/backend/executor/nodeGatherMerge.c
src/include/nodes/execnodes.h

index ac7e0fb48b314f327b3e669e0125a180b187d425..684470928a4cc9f0ff96ad5c78b20fefcc80004c 100644 (file)
@@ -1704,6 +1704,7 @@ ExecutePlan(EState *estate,
    if (!execute_once || dest->mydest == DestIntoRel)
        use_parallel_mode = false;
 
+   estate->es_use_parallel_mode = use_parallel_mode;
    if (use_parallel_mode)
        EnterParallelMode();
 
index c3988468795e2920010ad072eac8c34e4417400f..a5925ca7e3bbae34f9473b4e9b3bfd4039b4715e 100644 (file)
@@ -156,6 +156,8 @@ CreateExecutorState(void)
    estate->es_epqScanDone = NULL;
    estate->es_sourceText = NULL;
 
+   estate->es_use_parallel_mode = false;
+
    /*
     * Return the executor state structure
     */
index 3be735f5c3393388ea71d462ae519c86769104bc..89f592828c1a4bcfbe08ae5adb3caf6faa5ad09f 100644 (file)
@@ -149,7 +149,7 @@ ExecGather(PlanState *pstate)
         * Sometimes we might have to run without parallelism; but if parallel
         * mode is active then we can try to fire up some workers.
         */
-       if (gather->num_workers > 0 && IsInParallelMode())
+       if (gather->num_workers > 0 && estate->es_use_parallel_mode)
        {
            ParallelContext *pcxt;
 
index c0c285bd611647c26e1e2e477b1e8841580884fd..6b173543564188e368fe5b781c0fa7c8a12b3281 100644 (file)
@@ -193,7 +193,7 @@ ExecGatherMerge(PlanState *pstate)
         * Sometimes we might have to run without parallelism; but if parallel
         * mode is active then we can try to fire up some workers.
         */
-       if (gm->num_workers > 0 && IsInParallelMode())
+       if (gm->num_workers > 0 && estate->es_use_parallel_mode)
        {
            ParallelContext *pcxt;
 
index 9c790baaaa5ccb0e0c0f2af495b2b20cc02d22e8..4f5da00c863c9755fbd9caa7089f47078bb986c7 100644 (file)
@@ -507,6 +507,8 @@ typedef struct EState
    bool       *es_epqTupleSet; /* true if EPQ tuple is provided */
    bool       *es_epqScanDone; /* true if EPQ tuple has been fetched */
 
+   bool        es_use_parallel_mode; /* can we use parallel workers? */
+
    /* The per-query shared memory area to use for parallel execution. */
    struct dsa_area *es_query_dsa;
 } EState;