1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-15 19:21:59 +03:00

Force rescanning of parallel-aware scan nodes below a Gather[Merge].

The ExecReScan machinery contains various optimizations for postponing
or skipping rescans of plan subtrees; for example a HashAgg node may
conclude that it can re-use the table it built before, instead of
re-reading its input subtree.  But that is wrong if the input contains
a parallel-aware table scan node, since the portion of the table scanned
by the leader process is likely to vary from one rescan to the next.
This explains the timing-dependent buildfarm failures we saw after
commit a2b70c89c.

The established mechanism for showing that a plan node's output is
potentially variable is to mark it as depending on some runtime Param.
Hence, to fix this, invent a dummy Param (one that has a PARAM_EXEC
parameter number, but carries no actual value) associated with each Gather
or GatherMerge node, mark parallel-aware nodes below that node as dependent
on that Param, and arrange for ExecReScanGather[Merge] to flag that Param
as changed whenever the Gather[Merge] node is rescanned.

This solution breaks an undocumented assumption made by the parallel
executor logic, namely that all rescans of nodes below a Gather[Merge]
will happen synchronously during the ReScan of the top node itself.
But that's fundamentally contrary to the design of the ExecReScan code,
and so was doomed to fail someday anyway (even if you want to argue
that the bug being fixed here wasn't a failure of that assumption).
A follow-on patch will address that issue.  In the meantime, the worst
that's expected to happen is that given very bad timing luck, the leader
might have to do all the work during a rescan, because workers think
they have nothing to do, if they are able to start up before the eventual
ReScan of the leader's parallel-aware table scan node has reset the
shared scan state.

Although this problem exists in 9.6, there does not seem to be any way
for it to manifest there.  Without GatherMerge, it seems that a plan tree
that has a rescan-short-circuiting node below Gather will always also
have one above it that will short-circuit in the same cases, preventing
the Gather from being rescanned.  Hence we won't take the risk of
back-patching this change into 9.6.  But v10 needs it.

Discussion: https://postgr.es/m/CAA4eK1JkByysFJNh9M349u_nNjqETuEnY_y1VUc_kJiU0bxtaQ@mail.gmail.com
This commit is contained in:
Tom Lane
2017-08-30 09:29:55 -04:00
parent 00f6d5c2c3
commit 7df2c1f8da
11 changed files with 155 additions and 24 deletions

View File

@ -79,6 +79,7 @@ static Node *process_sublinks_mutator(Node *node,
process_sublinks_context *context);
static Bitmapset *finalize_plan(PlannerInfo *root,
Plan *plan,
int gather_param,
Bitmapset *valid_params,
Bitmapset *scan_params);
static bool finalize_primnode(Node *node, finalize_primnode_context *context);
@ -2217,12 +2218,15 @@ void
SS_finalize_plan(PlannerInfo *root, Plan *plan)
{
/* No setup needed, just recurse through plan tree. */
(void) finalize_plan(root, plan, root->outer_params, NULL);
(void) finalize_plan(root, plan, -1, root->outer_params, NULL);
}
/*
* Recursive processing of all nodes in the plan tree
*
* gather_param is the rescan_param of an ancestral Gather/GatherMerge,
* or -1 if there is none.
*
* valid_params is the set of param IDs supplied by outer plan levels
* that are valid to reference in this plan node or its children.
*
@ -2249,7 +2253,9 @@ SS_finalize_plan(PlannerInfo *root, Plan *plan)
* can be handled more cleanly.
*/
static Bitmapset *
finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params,
finalize_plan(PlannerInfo *root, Plan *plan,
int gather_param,
Bitmapset *valid_params,
Bitmapset *scan_params)
{
finalize_primnode_context context;
@ -2302,6 +2308,18 @@ finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params,
finalize_primnode((Node *) plan->targetlist, &context);
finalize_primnode((Node *) plan->qual, &context);
/*
* If it's a parallel-aware scan node, mark it as dependent on the parent
* Gather/GatherMerge's rescan Param.
*/
if (plan->parallel_aware)
{
if (gather_param < 0)
elog(ERROR, "parallel-aware plan node is not below a Gather");
context.paramids =
bms_add_member(context.paramids, gather_param);
}
/* Check additional node-type-specific fields */
switch (nodeTag(plan))
{
@ -2512,6 +2530,7 @@ finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params,
bms_add_members(context.paramids,
finalize_plan(root,
(Plan *) lfirst(lc),
gather_param,
valid_params,
scan_params));
}
@ -2542,6 +2561,7 @@ finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params,
bms_add_members(context.paramids,
finalize_plan(root,
(Plan *) lfirst(l),
gather_param,
valid_params,
scan_params));
}
@ -2558,6 +2578,7 @@ finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params,
bms_add_members(context.paramids,
finalize_plan(root,
(Plan *) lfirst(l),
gather_param,
valid_params,
scan_params));
}
@ -2574,6 +2595,7 @@ finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params,
bms_add_members(context.paramids,
finalize_plan(root,
(Plan *) lfirst(l),
gather_param,
valid_params,
scan_params));
}
@ -2590,6 +2612,7 @@ finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params,
bms_add_members(context.paramids,
finalize_plan(root,
(Plan *) lfirst(l),
gather_param,
valid_params,
scan_params));
}
@ -2606,6 +2629,7 @@ finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params,
bms_add_members(context.paramids,
finalize_plan(root,
(Plan *) lfirst(l),
gather_param,
valid_params,
scan_params));
}
@ -2697,13 +2721,51 @@ finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params,
&context);
break;
case T_Gather:
/* child nodes are allowed to reference rescan_param, if any */
locally_added_param = ((Gather *) plan)->rescan_param;
if (locally_added_param >= 0)
{
valid_params = bms_add_member(bms_copy(valid_params),
locally_added_param);
/*
* We currently don't support nested Gathers. The issue so
* far as this function is concerned would be how to identify
* which child nodes depend on which Gather.
*/
Assert(gather_param < 0);
/* Pass down rescan_param to child parallel-aware nodes */
gather_param = locally_added_param;
}
/* rescan_param does *not* get added to scan_params */
break;
case T_GatherMerge:
/* child nodes are allowed to reference rescan_param, if any */
locally_added_param = ((GatherMerge *) plan)->rescan_param;
if (locally_added_param >= 0)
{
valid_params = bms_add_member(bms_copy(valid_params),
locally_added_param);
/*
* We currently don't support nested Gathers. The issue so
* far as this function is concerned would be how to identify
* which child nodes depend on which Gather.
*/
Assert(gather_param < 0);
/* Pass down rescan_param to child parallel-aware nodes */
gather_param = locally_added_param;
}
/* rescan_param does *not* get added to scan_params */
break;
case T_ProjectSet:
case T_Hash:
case T_Material:
case T_Sort:
case T_Unique:
case T_Gather:
case T_GatherMerge:
case T_SetOp:
case T_Group:
/* no node-type-specific fields need fixing */
@ -2717,6 +2779,7 @@ finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params,
/* Process left and right child plans, if any */
child_params = finalize_plan(root,
plan->lefttree,
gather_param,
valid_params,
scan_params);
context.paramids = bms_add_members(context.paramids, child_params);
@ -2726,6 +2789,7 @@ finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params,
/* right child can reference nestloop_params as well as valid_params */
child_params = finalize_plan(root,
plan->righttree,
gather_param,
bms_union(nestloop_params, valid_params),
scan_params);
/* ... and they don't count as parameters used at my level */
@ -2737,6 +2801,7 @@ finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params,
/* easy case */
child_params = finalize_plan(root,
plan->righttree,
gather_param,
valid_params,
scan_params);
}