Simplify executor's determination of whether to use parallelism.
authorTom Lane <[email protected]>
Mon, 9 Dec 2024 19:38:19 +0000 (14:38 -0500)
committerTom Lane <[email protected]>
Mon, 9 Dec 2024 19:38:19 +0000 (14:38 -0500)
Our parallel-mode code only works when we are executing a query
in full, so ExecutePlan must disable parallel mode when it is
asked to do partial execution.  The previous logic for this
involved passing down a flag (variously named execute_once or
run_once) from callers of ExecutorRun or PortalRun.  This is
overcomplicated, and unsurprisingly some of the callers didn't
get it right, since it requires keeping state that not all of
them have handy; not to mention that the requirements for it were
undocumented.  That led to assertion failures in some corner
cases.  The only state we really need for this is the existing
QueryDesc.already_executed flag, so let's just put all the
responsibility in ExecutePlan.  (It could have been done in
ExecutorRun too, leading to a slightly shorter patch -- but if
there's ever more than one caller of ExecutePlan, it seems better
to have this logic in the subroutine than the callers.)

This makes those ExecutorRun/PortalRun parameters unnecessary.
In master it seems okay to just remove them, returning the
API for those functions to what it was before parallelism.
Such an API break is clearly not okay in stable branches,
but for them we can just leave the parameters in place after
documenting that they do nothing.

Per report from Yugo Nagata, who also reviewed and tested
this patch.  Back-patch to all supported branches.

Discussion: https://postgr.es/m/20241206062549.710dc01cf91224809dd6c0e1@sraoss.co.jp

19 files changed:
contrib/auto_explain/auto_explain.c
contrib/pg_stat_statements/pg_stat_statements.c
src/backend/commands/copyto.c
src/backend/commands/createas.c
src/backend/commands/explain.c
src/backend/commands/extension.c
src/backend/commands/matview.c
src/backend/commands/portalcmds.c
src/backend/commands/prepare.c
src/backend/executor/execMain.c
src/backend/executor/execParallel.c
src/backend/executor/functions.c
src/backend/executor/spi.c
src/backend/tcop/postgres.c
src/backend/tcop/pquery.c
src/include/executor/execdesc.h
src/include/executor/executor.h
src/include/tcop/pquery.h
src/include/utils/portal.h

index 623a674f99c451f84ddc9861971c3ccacbd93803..f2eaa8e4949da9169eea3095c3427074743cb032 100644 (file)
@@ -79,7 +79,7 @@ static ExecutorEnd_hook_type prev_ExecutorEnd = NULL;
 static void explain_ExecutorStart(QueryDesc *queryDesc, int eflags);
 static void explain_ExecutorRun(QueryDesc *queryDesc,
                                ScanDirection direction,
-                               uint64 count, bool execute_once);
+                               uint64 count);
 static void explain_ExecutorFinish(QueryDesc *queryDesc);
 static void explain_ExecutorEnd(QueryDesc *queryDesc);
 
@@ -321,15 +321,15 @@ explain_ExecutorStart(QueryDesc *queryDesc, int eflags)
  */
 static void
 explain_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction,
-                   uint64 count, bool execute_once)
+                   uint64 count)
 {
    nesting_level++;
    PG_TRY();
    {
        if (prev_ExecutorRun)
-           prev_ExecutorRun(queryDesc, direction, count, execute_once);
+           prev_ExecutorRun(queryDesc, direction, count);
        else
-           standard_ExecutorRun(queryDesc, direction, count, execute_once);
+           standard_ExecutorRun(queryDesc, direction, count);
    }
    PG_FINALLY();
    {
index 49c657b3e07810ee180d5bd8929e1b9864f9e6e7..602cae54ffd25afecb198f2e940aeb09c0118748 100644 (file)
@@ -335,7 +335,7 @@ static PlannedStmt *pgss_planner(Query *parse,
 static void pgss_ExecutorStart(QueryDesc *queryDesc, int eflags);
 static void pgss_ExecutorRun(QueryDesc *queryDesc,
                             ScanDirection direction,
-                            uint64 count, bool execute_once);
+                            uint64 count);
 static void pgss_ExecutorFinish(QueryDesc *queryDesc);
 static void pgss_ExecutorEnd(QueryDesc *queryDesc);
 static void pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
@@ -1021,16 +1021,15 @@ pgss_ExecutorStart(QueryDesc *queryDesc, int eflags)
  * ExecutorRun hook: all we need do is track nesting depth
  */
 static void
-pgss_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count,
-                bool execute_once)
+pgss_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count)
 {
    nesting_level++;
    PG_TRY();
    {
        if (prev_ExecutorRun)
-           prev_ExecutorRun(queryDesc, direction, count, execute_once);
+           prev_ExecutorRun(queryDesc, direction, count);
        else
-           standard_ExecutorRun(queryDesc, direction, count, execute_once);
+           standard_ExecutorRun(queryDesc, direction, count);
    }
    PG_FINALLY();
    {
index f55e6d9675193a60fcf102f887df26faebb88224..161a0f8b0a5de1f85932324fdf92ce1d38135200 100644 (file)
@@ -880,7 +880,7 @@ DoCopyTo(CopyToState cstate)
    else
    {
        /* run the plan --- the dest receiver will send tuples */
-       ExecutorRun(cstate->queryDesc, ForwardScanDirection, 0, true);
+       ExecutorRun(cstate->queryDesc, ForwardScanDirection, 0);
        processed = ((DR_copy *) cstate->queryDesc->dest)->processed;
    }
 
index 5c92e48a56c6e89e23a72345d974cba893c88ddb..466376af9be251e09259b682beab4e5a3f46a72e 100644 (file)
@@ -340,7 +340,7 @@ ExecCreateTableAs(ParseState *pstate, CreateTableAsStmt *stmt,
        ExecutorStart(queryDesc, GetIntoRelEFlags(into));
 
        /* run the plan to completion */
-       ExecutorRun(queryDesc, ForwardScanDirection, 0, true);
+       ExecutorRun(queryDesc, ForwardScanDirection, 0);
 
        /* save the rowcount if we're given a qc to fill */
        if (qc)
index a3f1d53d7a57342e19b90c2b503c67744fe89941..3078f5c1a3f07ee051aa4c241cb250bfda90c2bd 100644 (file)
@@ -719,7 +719,7 @@ ExplainOnePlan(PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es,
            dir = ForwardScanDirection;
 
        /* run the plan */
-       ExecutorRun(queryDesc, dir, 0, true);
+       ExecutorRun(queryDesc, dir, 0);
 
        /* run cleanup too */
        ExecutorFinish(queryDesc);
index af6bd8ff42613cffb64b1ed1b22f8ba4f0620eba..e683c520a820bf660e037dddd9109d83c1b0ab26 100644 (file)
@@ -912,7 +912,7 @@ execute_sql_string(const char *sql, const char *filename)
                                        dest, NULL, NULL, 0);
 
                ExecutorStart(qdesc, 0);
-               ExecutorRun(qdesc, ForwardScanDirection, 0, true);
+               ExecutorRun(qdesc, ForwardScanDirection, 0);
                ExecutorFinish(qdesc);
                ExecutorEnd(qdesc);
 
index 010097873d1d6a68f64b947c5ee438877f22efc6..694da8291e772420fa5debee2c9ed427f8c06bb0 100644 (file)
@@ -446,7 +446,7 @@ refresh_matview_datafill(DestReceiver *dest, Query *query,
    ExecutorStart(queryDesc, 0);
 
    /* run the plan */
-   ExecutorRun(queryDesc, ForwardScanDirection, 0, true);
+   ExecutorRun(queryDesc, ForwardScanDirection, 0);
 
    processed = queryDesc->estate->es_processed;
 
index ac52ca25e99398ac34e43d4f8c34b31a5347d77b..dc68264242fe120966e277b7dc81900886a61c88 100644 (file)
@@ -427,7 +427,7 @@ PersistHoldablePortal(Portal portal)
                                        NULL);
 
        /* Fetch the result set into the tuplestore */
-       ExecutorRun(queryDesc, direction, 0, false);
+       ExecutorRun(queryDesc, direction, 0);
 
        queryDesc->dest->rDestroy(queryDesc->dest);
        queryDesc->dest = NULL;
index a93f970a292ece3354e16505d653584ed6ecfbe0..39a71c1de2aa5f8998c5f1daa133d4fed0e7a1b2 100644 (file)
@@ -252,7 +252,7 @@ ExecuteQuery(ParseState *pstate,
     */
    PortalStart(portal, paramLI, eflags, GetActiveSnapshot());
 
-   (void) PortalRun(portal, count, false, true, dest, dest, qc);
+   (void) PortalRun(portal, count, false, dest, dest, qc);
 
    PortalDrop(portal, false);
 
index 5ca856fd279a3e2ca0b3e668d372e90eb1373445..13f3fcdaee93f6f750d22b28c5f042cf9a5a7880 100644 (file)
@@ -77,14 +77,12 @@ static void InitPlan(QueryDesc *queryDesc, int eflags);
 static void CheckValidRowMarkRel(Relation rel, RowMarkType markType);
 static void ExecPostprocessPlan(EState *estate);
 static void ExecEndPlan(PlanState *planstate, EState *estate);
-static void ExecutePlan(EState *estate, PlanState *planstate,
-                       bool use_parallel_mode,
+static void ExecutePlan(QueryDesc *queryDesc,
                        CmdType operation,
                        bool sendTuples,
                        uint64 numberTuples,
                        ScanDirection direction,
-                       DestReceiver *dest,
-                       bool execute_once);
+                       DestReceiver *dest);
 static bool ExecCheckOneRelPerms(RTEPermissionInfo *perminfo);
 static bool ExecCheckPermissionsModified(Oid relOid, Oid userid,
                                         Bitmapset *modifiedCols,
@@ -294,18 +292,17 @@ standard_ExecutorStart(QueryDesc *queryDesc, int eflags)
  */
 void
 ExecutorRun(QueryDesc *queryDesc,
-           ScanDirection direction, uint64 count,
-           bool execute_once)
+           ScanDirection direction, uint64 count)
 {
    if (ExecutorRun_hook)
-       (*ExecutorRun_hook) (queryDesc, direction, count, execute_once);
+       (*ExecutorRun_hook) (queryDesc, direction, count);
    else
-       standard_ExecutorRun(queryDesc, direction, count, execute_once);
+       standard_ExecutorRun(queryDesc, direction, count);
 }
 
 void
 standard_ExecutorRun(QueryDesc *queryDesc,
-                    ScanDirection direction, uint64 count, bool execute_once)
+                    ScanDirection direction, uint64 count)
 {
    EState     *estate;
    CmdType     operation;
@@ -354,21 +351,12 @@ standard_ExecutorRun(QueryDesc *queryDesc,
     * run plan
     */
    if (!ScanDirectionIsNoMovement(direction))
-   {
-       if (execute_once && queryDesc->already_executed)
-           elog(ERROR, "can't re-execute query flagged for single execution");
-       queryDesc->already_executed = true;
-
-       ExecutePlan(estate,
-                   queryDesc->planstate,
-                   queryDesc->plannedstmt->parallelModeNeeded,
+       ExecutePlan(queryDesc,
                    operation,
                    sendTuples,
                    count,
                    direction,
-                   dest,
-                   execute_once);
-   }
+                   dest);
 
    /*
     * Update es_total_processed to keep track of the number of tuples
@@ -1601,22 +1589,19 @@ ExecCloseRangeTableRelations(EState *estate)
  *     moving in the specified direction.
  *
  *     Runs to completion if numberTuples is 0
- *
- * Note: the ctid attribute is a 'junk' attribute that is removed before the
- * user can see it
  * ----------------------------------------------------------------
  */
 static void
-ExecutePlan(EState *estate,
-           PlanState *planstate,
-           bool use_parallel_mode,
+ExecutePlan(QueryDesc *queryDesc,
            CmdType operation,
            bool sendTuples,
            uint64 numberTuples,
            ScanDirection direction,
-           DestReceiver *dest,
-           bool execute_once)
+           DestReceiver *dest)
 {
+   EState     *estate = queryDesc->estate;
+   PlanState  *planstate = queryDesc->planstate;
+   bool        use_parallel_mode;
    TupleTableSlot *slot;
    uint64      current_tuple_count;
 
@@ -1631,11 +1616,17 @@ ExecutePlan(EState *estate,
    estate->es_direction = direction;
 
    /*
-    * If the plan might potentially be executed multiple times, we must force
-    * it to run without parallelism, because we might exit early.
+    * Set up parallel mode if appropriate.
+    *
+    * Parallel mode only supports complete execution of a plan.  If we've
+    * already partially executed it, or if the caller asks us to exit early,
+    * we must force the plan to run without parallelism.
     */
-   if (!execute_once)
+   if (queryDesc->already_executed || numberTuples != 0)
        use_parallel_mode = false;
+   else
+       use_parallel_mode = queryDesc->plannedstmt->parallelModeNeeded;
+   queryDesc->already_executed = true;
 
    estate->es_use_parallel_mode = use_parallel_mode;
    if (use_parallel_mode)
index bfb3419efb7b1280e855931a7e5228f3b3611d13..846ec727deb274796c8b8ac95626d3564e55fd6b 100644 (file)
@@ -1471,8 +1471,7 @@ ParallelQueryMain(dsm_segment *seg, shm_toc *toc)
     */
    ExecutorRun(queryDesc,
                ForwardScanDirection,
-               fpes->tuples_needed < 0 ? (int64) 0 : fpes->tuples_needed,
-               true);
+               fpes->tuples_needed < 0 ? (int64) 0 : fpes->tuples_needed);
 
    /* Shut down the executor */
    ExecutorFinish(queryDesc);
index 8d1fda2ddc0032ac5ae386c7deb11a899c56f67e..3b2f78b2197f6164c3d103d14f8bfcc4f1ac7282 100644 (file)
@@ -894,7 +894,7 @@ postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache)
        /* Run regular commands to completion unless lazyEval */
        uint64      count = (es->lazyEval) ? 1 : 0;
 
-       ExecutorRun(es->qd, ForwardScanDirection, count, !fcache->returnsSet || !es->lazyEval);
+       ExecutorRun(es->qd, ForwardScanDirection, count);
 
        /*
         * If we requested run to completion OR there was no tuple returned,
index 2fb2e73604eed826edabf1ee03a0ee6b489e267a..c1d8fd08c6c74eaeb077a939e1663713d349650e 100644 (file)
@@ -2929,7 +2929,7 @@ _SPI_pquery(QueryDesc *queryDesc, bool fire_triggers, uint64 tcount)
 
    ExecutorStart(queryDesc, eflags);
 
-   ExecutorRun(queryDesc, ForwardScanDirection, tcount, true);
+   ExecutorRun(queryDesc, ForwardScanDirection, tcount);
 
    _SPI_current->processed = queryDesc->estate->es_processed;
 
index 42af7680456ec0053772ca2b93e06fe32941366e..e0a603f42bbce2892ee7802efe4edc9cddaceb15 100644 (file)
@@ -1283,7 +1283,6 @@ exec_simple_query(const char *query_string)
        (void) PortalRun(portal,
                         FETCH_ALL,
                         true,  /* always top level */
-                        true,
                         receiver,
                         receiver,
                         &qc);
@@ -2260,7 +2259,6 @@ exec_execute_message(const char *portal_name, long max_rows)
    completed = PortalRun(portal,
                          max_rows,
                          true, /* always top level */
-                         !execute_is_fetch && max_rows == FETCH_ALL,
                          receiver,
                          receiver,
                          &qc);
index 0c45fcf318f7fe31c0ea4bc223f255451ced373c..89d704df8d16a6c1fced53609e9d2d6aedbc26ec 100644 (file)
@@ -157,7 +157,7 @@ ProcessQuery(PlannedStmt *plan,
    /*
     * Run the plan to completion.
     */
-   ExecutorRun(queryDesc, ForwardScanDirection, 0, true);
+   ExecutorRun(queryDesc, ForwardScanDirection, 0);
 
    /*
     * Build command completion status data, if caller wants one.
@@ -681,7 +681,7 @@ PortalSetResultFormat(Portal portal, int nFormats, int16 *formats)
  * suspended due to exhaustion of the count parameter.
  */
 bool
-PortalRun(Portal portal, long count, bool isTopLevel, bool run_once,
+PortalRun(Portal portal, long count, bool isTopLevel,
          DestReceiver *dest, DestReceiver *altdest,
          QueryCompletion *qc)
 {
@@ -714,10 +714,6 @@ PortalRun(Portal portal, long count, bool isTopLevel, bool run_once,
     */
    MarkPortalActive(portal);
 
-   /* Set run_once flag.  Shouldn't be clear if previously set. */
-   Assert(!portal->run_once || run_once);
-   portal->run_once = run_once;
-
    /*
     * Set up global portal context pointers.
     *
@@ -921,8 +917,7 @@ PortalRunSelect(Portal portal,
        else
        {
            PushActiveSnapshot(queryDesc->snapshot);
-           ExecutorRun(queryDesc, direction, (uint64) count,
-                       portal->run_once);
+           ExecutorRun(queryDesc, direction, (uint64) count);
            nprocessed = queryDesc->estate->es_processed;
            PopActiveSnapshot();
        }
@@ -961,8 +956,7 @@ PortalRunSelect(Portal portal,
        else
        {
            PushActiveSnapshot(queryDesc->snapshot);
-           ExecutorRun(queryDesc, direction, (uint64) count,
-                       portal->run_once);
+           ExecutorRun(queryDesc, direction, (uint64) count);
            nprocessed = queryDesc->estate->es_processed;
            PopActiveSnapshot();
        }
@@ -1406,9 +1400,6 @@ PortalRunFetch(Portal portal,
     */
    MarkPortalActive(portal);
 
-   /* If supporting FETCH, portal can't be run-once. */
-   Assert(!portal->run_once);
-
    /*
     * Set up global portal context pointers.
     */
index 0a7274e26c834cfeb946d8a0f942a31fc342bb79..79e8b6311145ccaf39bf31e68543bacac28d1746 100644 (file)
@@ -48,7 +48,7 @@ typedef struct QueryDesc
    EState     *estate;         /* executor's query-wide state */
    PlanState  *planstate;      /* tree of per-plan-node state */
 
-   /* This field is set by ExecutorRun */
+   /* This field is set by ExecutePlan */
    bool        already_executed;   /* true if previously executed */
 
    /* This is always set NULL by the core system, but plugins can change it */
index 69c3ebff00a0095002cd3352e72f266fd74121bd..892cf055cdfa9008d6a6a4fe6affa47e9c4d98af 100644 (file)
@@ -78,8 +78,7 @@ extern PGDLLIMPORT ExecutorStart_hook_type ExecutorStart_hook;
 /* Hook for plugins to get control in ExecutorRun() */
 typedef void (*ExecutorRun_hook_type) (QueryDesc *queryDesc,
                                       ScanDirection direction,
-                                      uint64 count,
-                                      bool execute_once);
+                                      uint64 count);
 extern PGDLLIMPORT ExecutorRun_hook_type ExecutorRun_hook;
 
 /* Hook for plugins to get control in ExecutorFinish() */
@@ -200,9 +199,9 @@ ExecGetJunkAttribute(TupleTableSlot *slot, AttrNumber attno, bool *isNull)
 extern void ExecutorStart(QueryDesc *queryDesc, int eflags);
 extern void standard_ExecutorStart(QueryDesc *queryDesc, int eflags);
 extern void ExecutorRun(QueryDesc *queryDesc,
-                       ScanDirection direction, uint64 count, bool execute_once);
+                       ScanDirection direction, uint64 count);
 extern void standard_ExecutorRun(QueryDesc *queryDesc,
-                                ScanDirection direction, uint64 count, bool execute_once);
+                                ScanDirection direction, uint64 count);
 extern void ExecutorFinish(QueryDesc *queryDesc);
 extern void standard_ExecutorFinish(QueryDesc *queryDesc);
 extern void ExecutorEnd(QueryDesc *queryDesc);
index 073fb323bc1283dfcd99fb0651096960aa6c59d1..37ba3160e542502196c5a35beddb941fb4b894ed 100644 (file)
@@ -36,7 +36,7 @@ extern void PortalSetResultFormat(Portal portal, int nFormats,
                                  int16 *formats);
 
 extern bool PortalRun(Portal portal, long count, bool isTopLevel,
-                     bool run_once, DestReceiver *dest, DestReceiver *altdest,
+                     DestReceiver *dest, DestReceiver *altdest,
                      QueryCompletion *qc);
 
 extern uint64 PortalRunFetch(Portal portal,
index 29f49829f25dbc5cd14a011a24c7e289e999d9ae..afe605a8ed852955ecbc4bac092c2524202f3769 100644 (file)
@@ -145,7 +145,6 @@ typedef struct PortalData
    /* Features/options */
    PortalStrategy strategy;    /* see above */
    int         cursorOptions;  /* DECLARE CURSOR option bits */
-   bool        run_once;       /* portal will only be run once */
 
    /* Status data */
    PortalStatus status;        /* see above */