mirror of
https://github.com/postgres/postgres.git
synced 2025-05-28 05:21:27 +03:00
Simplify executor's determination of whether to use parallelism.
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
This commit is contained in:
parent
bb93b33d7e
commit
556f7b7bc1
@ -75,14 +75,12 @@ static void InitPlan(QueryDesc *queryDesc, int eflags);
|
|||||||
static void CheckValidRowMarkRel(Relation rel, RowMarkType markType);
|
static void CheckValidRowMarkRel(Relation rel, RowMarkType markType);
|
||||||
static void ExecPostprocessPlan(EState *estate);
|
static void ExecPostprocessPlan(EState *estate);
|
||||||
static void ExecEndPlan(PlanState *planstate, EState *estate);
|
static void ExecEndPlan(PlanState *planstate, EState *estate);
|
||||||
static void ExecutePlan(EState *estate, PlanState *planstate,
|
static void ExecutePlan(QueryDesc *queryDesc,
|
||||||
bool use_parallel_mode,
|
|
||||||
CmdType operation,
|
CmdType operation,
|
||||||
bool sendTuples,
|
bool sendTuples,
|
||||||
uint64 numberTuples,
|
uint64 numberTuples,
|
||||||
ScanDirection direction,
|
ScanDirection direction,
|
||||||
DestReceiver *dest,
|
DestReceiver *dest);
|
||||||
bool execute_once);
|
|
||||||
static bool ExecCheckOneRelPerms(RTEPermissionInfo *perminfo);
|
static bool ExecCheckOneRelPerms(RTEPermissionInfo *perminfo);
|
||||||
static bool ExecCheckPermissionsModified(Oid relOid, Oid userid,
|
static bool ExecCheckPermissionsModified(Oid relOid, Oid userid,
|
||||||
Bitmapset *modifiedCols,
|
Bitmapset *modifiedCols,
|
||||||
@ -283,6 +281,9 @@ standard_ExecutorStart(QueryDesc *queryDesc, int eflags)
|
|||||||
* retrieved tuples, not for instance to those inserted/updated/deleted
|
* retrieved tuples, not for instance to those inserted/updated/deleted
|
||||||
* by a ModifyTable plan node.
|
* by a ModifyTable plan node.
|
||||||
*
|
*
|
||||||
|
* execute_once is ignored, and is present only to avoid an API break
|
||||||
|
* in stable branches.
|
||||||
|
*
|
||||||
* There is no return value, but output tuples (if any) are sent to
|
* There is no return value, but output tuples (if any) are sent to
|
||||||
* the destination receiver specified in the QueryDesc; and the number
|
* the destination receiver specified in the QueryDesc; and the number
|
||||||
* of tuples processed at the top level can be found in
|
* of tuples processed at the top level can be found in
|
||||||
@ -357,21 +358,12 @@ standard_ExecutorRun(QueryDesc *queryDesc,
|
|||||||
* run plan
|
* run plan
|
||||||
*/
|
*/
|
||||||
if (!ScanDirectionIsNoMovement(direction))
|
if (!ScanDirectionIsNoMovement(direction))
|
||||||
{
|
ExecutePlan(queryDesc,
|
||||||
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,
|
|
||||||
operation,
|
operation,
|
||||||
sendTuples,
|
sendTuples,
|
||||||
count,
|
count,
|
||||||
direction,
|
direction,
|
||||||
dest,
|
dest);
|
||||||
execute_once);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Update es_total_processed to keep track of the number of tuples
|
* Update es_total_processed to keep track of the number of tuples
|
||||||
@ -1600,22 +1592,19 @@ ExecCloseRangeTableRelations(EState *estate)
|
|||||||
* moving in the specified direction.
|
* moving in the specified direction.
|
||||||
*
|
*
|
||||||
* Runs to completion if numberTuples is 0
|
* 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
|
static void
|
||||||
ExecutePlan(EState *estate,
|
ExecutePlan(QueryDesc *queryDesc,
|
||||||
PlanState *planstate,
|
|
||||||
bool use_parallel_mode,
|
|
||||||
CmdType operation,
|
CmdType operation,
|
||||||
bool sendTuples,
|
bool sendTuples,
|
||||||
uint64 numberTuples,
|
uint64 numberTuples,
|
||||||
ScanDirection direction,
|
ScanDirection direction,
|
||||||
DestReceiver *dest,
|
DestReceiver *dest)
|
||||||
bool execute_once)
|
|
||||||
{
|
{
|
||||||
|
EState *estate = queryDesc->estate;
|
||||||
|
PlanState *planstate = queryDesc->planstate;
|
||||||
|
bool use_parallel_mode;
|
||||||
TupleTableSlot *slot;
|
TupleTableSlot *slot;
|
||||||
uint64 current_tuple_count;
|
uint64 current_tuple_count;
|
||||||
|
|
||||||
@ -1630,11 +1619,17 @@ ExecutePlan(EState *estate,
|
|||||||
estate->es_direction = direction;
|
estate->es_direction = direction;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If the plan might potentially be executed multiple times, we must force
|
* Set up parallel mode if appropriate.
|
||||||
* it to run without parallelism, because we might exit early.
|
*
|
||||||
|
* 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;
|
use_parallel_mode = false;
|
||||||
|
else
|
||||||
|
use_parallel_mode = queryDesc->plannedstmt->parallelModeNeeded;
|
||||||
|
queryDesc->already_executed = true;
|
||||||
|
|
||||||
estate->es_use_parallel_mode = use_parallel_mode;
|
estate->es_use_parallel_mode = use_parallel_mode;
|
||||||
if (use_parallel_mode)
|
if (use_parallel_mode)
|
||||||
|
@ -1278,7 +1278,7 @@ exec_simple_query(const char *query_string)
|
|||||||
(void) PortalRun(portal,
|
(void) PortalRun(portal,
|
||||||
FETCH_ALL,
|
FETCH_ALL,
|
||||||
true, /* always top level */
|
true, /* always top level */
|
||||||
true,
|
true, /* ignored */
|
||||||
receiver,
|
receiver,
|
||||||
receiver,
|
receiver,
|
||||||
&qc);
|
&qc);
|
||||||
@ -2255,7 +2255,7 @@ exec_execute_message(const char *portal_name, long max_rows)
|
|||||||
completed = PortalRun(portal,
|
completed = PortalRun(portal,
|
||||||
max_rows,
|
max_rows,
|
||||||
true, /* always top level */
|
true, /* always top level */
|
||||||
!execute_is_fetch && max_rows == FETCH_ALL,
|
true, /* ignored */
|
||||||
receiver,
|
receiver,
|
||||||
receiver,
|
receiver,
|
||||||
&qc);
|
&qc);
|
||||||
|
@ -670,6 +670,8 @@ PortalSetResultFormat(Portal portal, int nFormats, int16 *formats)
|
|||||||
* isTopLevel: true if query is being executed at backend "top level"
|
* isTopLevel: true if query is being executed at backend "top level"
|
||||||
* (that is, directly from a client command message)
|
* (that is, directly from a client command message)
|
||||||
*
|
*
|
||||||
|
* run_once: ignored, present only to avoid an API break in stable branches.
|
||||||
|
*
|
||||||
* dest: where to send output of primary (canSetTag) query
|
* dest: where to send output of primary (canSetTag) query
|
||||||
*
|
*
|
||||||
* altdest: where to send output of non-primary queries
|
* altdest: where to send output of non-primary queries
|
||||||
@ -714,10 +716,6 @@ PortalRun(Portal portal, long count, bool isTopLevel, bool run_once,
|
|||||||
*/
|
*/
|
||||||
MarkPortalActive(portal);
|
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.
|
* Set up global portal context pointers.
|
||||||
*
|
*
|
||||||
@ -922,7 +920,7 @@ PortalRunSelect(Portal portal,
|
|||||||
{
|
{
|
||||||
PushActiveSnapshot(queryDesc->snapshot);
|
PushActiveSnapshot(queryDesc->snapshot);
|
||||||
ExecutorRun(queryDesc, direction, (uint64) count,
|
ExecutorRun(queryDesc, direction, (uint64) count,
|
||||||
portal->run_once);
|
false);
|
||||||
nprocessed = queryDesc->estate->es_processed;
|
nprocessed = queryDesc->estate->es_processed;
|
||||||
PopActiveSnapshot();
|
PopActiveSnapshot();
|
||||||
}
|
}
|
||||||
@ -962,7 +960,7 @@ PortalRunSelect(Portal portal,
|
|||||||
{
|
{
|
||||||
PushActiveSnapshot(queryDesc->snapshot);
|
PushActiveSnapshot(queryDesc->snapshot);
|
||||||
ExecutorRun(queryDesc, direction, (uint64) count,
|
ExecutorRun(queryDesc, direction, (uint64) count,
|
||||||
portal->run_once);
|
false);
|
||||||
nprocessed = queryDesc->estate->es_processed;
|
nprocessed = queryDesc->estate->es_processed;
|
||||||
PopActiveSnapshot();
|
PopActiveSnapshot();
|
||||||
}
|
}
|
||||||
@ -1406,9 +1404,6 @@ PortalRunFetch(Portal portal,
|
|||||||
*/
|
*/
|
||||||
MarkPortalActive(portal);
|
MarkPortalActive(portal);
|
||||||
|
|
||||||
/* If supporting FETCH, portal can't be run-once. */
|
|
||||||
Assert(!portal->run_once);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Set up global portal context pointers.
|
* Set up global portal context pointers.
|
||||||
*/
|
*/
|
||||||
|
@ -48,7 +48,7 @@ typedef struct QueryDesc
|
|||||||
EState *estate; /* executor's query-wide state */
|
EState *estate; /* executor's query-wide state */
|
||||||
PlanState *planstate; /* tree of per-plan-node 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 */
|
bool already_executed; /* true if previously executed */
|
||||||
|
|
||||||
/* This is always set NULL by the core system, but plugins can change it */
|
/* This is always set NULL by the core system, but plugins can change it */
|
||||||
|
@ -145,7 +145,7 @@ typedef struct PortalData
|
|||||||
/* Features/options */
|
/* Features/options */
|
||||||
PortalStrategy strategy; /* see above */
|
PortalStrategy strategy; /* see above */
|
||||||
int cursorOptions; /* DECLARE CURSOR option bits */
|
int cursorOptions; /* DECLARE CURSOR option bits */
|
||||||
bool run_once; /* portal will only be run once */
|
bool run_once; /* unused */
|
||||||
|
|
||||||
/* Status data */
|
/* Status data */
|
||||||
PortalStatus status; /* see above */
|
PortalStatus status; /* see above */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user