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
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);
*/
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();
{
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,
* 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();
{
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;
}
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)
dir = ForwardScanDirection;
/* run the plan */
- ExecutorRun(queryDesc, dir, 0, true);
+ ExecutorRun(queryDesc, dir, 0);
/* run cleanup too */
ExecutorFinish(queryDesc);
dest, NULL, NULL, 0);
ExecutorStart(qdesc, 0);
- ExecutorRun(qdesc, ForwardScanDirection, 0, true);
+ ExecutorRun(qdesc, ForwardScanDirection, 0);
ExecutorFinish(qdesc);
ExecutorEnd(qdesc);
ExecutorStart(queryDesc, 0);
/* run the plan */
- ExecutorRun(queryDesc, ForwardScanDirection, 0, true);
+ ExecutorRun(queryDesc, ForwardScanDirection, 0);
processed = queryDesc->estate->es_processed;
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;
*/
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);
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,
*/
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;
* 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
* 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;
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)
*/
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);
/* 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,
ExecutorStart(queryDesc, eflags);
- ExecutorRun(queryDesc, ForwardScanDirection, tcount, true);
+ ExecutorRun(queryDesc, ForwardScanDirection, tcount);
_SPI_current->processed = queryDesc->estate->es_processed;
(void) PortalRun(portal,
FETCH_ALL,
true, /* always top level */
- true,
receiver,
receiver,
&qc);
completed = PortalRun(portal,
max_rows,
true, /* always top level */
- !execute_is_fetch && max_rows == FETCH_ALL,
receiver,
receiver,
&qc);
/*
* Run the plan to completion.
*/
- ExecutorRun(queryDesc, ForwardScanDirection, 0, true);
+ ExecutorRun(queryDesc, ForwardScanDirection, 0);
/*
* Build command completion status data, if caller wants one.
* 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)
{
*/
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.
*
else
{
PushActiveSnapshot(queryDesc->snapshot);
- ExecutorRun(queryDesc, direction, (uint64) count,
- portal->run_once);
+ ExecutorRun(queryDesc, direction, (uint64) count);
nprocessed = queryDesc->estate->es_processed;
PopActiveSnapshot();
}
else
{
PushActiveSnapshot(queryDesc->snapshot);
- ExecutorRun(queryDesc, direction, (uint64) count,
- portal->run_once);
+ ExecutorRun(queryDesc, direction, (uint64) count);
nprocessed = queryDesc->estate->es_processed;
PopActiveSnapshot();
}
*/
MarkPortalActive(portal);
- /* If supporting FETCH, portal can't be run-once. */
- Assert(!portal->run_once);
-
/*
* Set up global portal context pointers.
*/
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 */
/* 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() */
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);
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,
/* 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 */