Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e89a71f

Browse files
committed
Pass InitPlan values to workers via Gather (Merge).
If a PARAM_EXEC parameter is used below a Gather (Merge) but the InitPlan that computes it is attached to or above the Gather (Merge), force the value to be computed before starting parallelism and pass it down to all workers. This allows us to use parallelism in cases where it previously would have had to be rejected as unsafe. We do - in this case - lose the optimization that the value is only computed if it's actually used. An alternative strategy would be to have the first worker that needs the value compute it, but one downside of that approach is that we'd then need to select a parallel-safe path to compute the parameter value; it couldn't for example contain a Gather (Merge) node. At some point in the future, we might want to consider both approaches. Independent of that consideration, there is a great deal more work that could be done to make more kinds of PARAM_EXEC parameters parallel-safe. This infrastructure could be used to allow a Gather (Merge) on the inner side of a nested loop (although that's not a very appealing plan) and cases where the InitPlan is attached below the Gather (Merge) could be addressed as well using various techniques. But this is a good start. Amit Kapila, reviewed and revised by me. Reviewing and testing from Kuntal Ghosh, Haribabu Kommi, and Tushar Ahuja. Discussion: http://postgr.es/m/CAA4eK1LV0Y1AUV4cUCdC+sYOx0Z0-8NAJ2Pd9=UKsbQ5Sr7+JQ@mail.gmail.com
1 parent ff2d435 commit e89a71f

File tree

17 files changed

+419
-23
lines changed

17 files changed

+419
-23
lines changed

src/backend/commands/explain.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ static void show_tidbitmap_info(BitmapHeapScanState *planstate,
107107
static void show_instrumentation_count(const char *qlabel, int which,
108108
PlanState *planstate, ExplainState *es);
109109
static void show_foreignscan_info(ForeignScanState *fsstate, ExplainState *es);
110+
static void show_eval_params(Bitmapset *bms_params, ExplainState *es);
110111
static const char *explain_get_index_name(Oid indexId);
111112
static void show_buffer_usage(ExplainState *es, const BufferUsage *usage);
112113
static void ExplainIndexScanDetails(Oid indexid, ScanDirection indexorderdir,
@@ -1441,6 +1442,11 @@ ExplainNode(PlanState *planstate, List *ancestors,
14411442
planstate, es);
14421443
ExplainPropertyInteger("Workers Planned",
14431444
gather->num_workers, es);
1445+
1446+
/* Show params evaluated at gather node */
1447+
if (gather->initParam)
1448+
show_eval_params(gather->initParam, es);
1449+
14441450
if (es->analyze)
14451451
{
14461452
int nworkers;
@@ -1463,6 +1469,11 @@ ExplainNode(PlanState *planstate, List *ancestors,
14631469
planstate, es);
14641470
ExplainPropertyInteger("Workers Planned",
14651471
gm->num_workers, es);
1472+
1473+
/* Show params evaluated at gather-merge node */
1474+
if (gm->initParam)
1475+
show_eval_params(gm->initParam, es);
1476+
14661477
if (es->analyze)
14671478
{
14681479
int nworkers;
@@ -2487,6 +2498,29 @@ show_foreignscan_info(ForeignScanState *fsstate, ExplainState *es)
24872498
}
24882499
}
24892500

2501+
/*
2502+
* Show initplan params evaluated at Gather or Gather Merge node.
2503+
*/
2504+
static void
2505+
show_eval_params(Bitmapset *bms_params, ExplainState *es)
2506+
{
2507+
int paramid = -1;
2508+
List *params = NIL;
2509+
2510+
Assert(bms_params);
2511+
2512+
while ((paramid = bms_next_member(bms_params, paramid)) >= 0)
2513+
{
2514+
char param[32];
2515+
2516+
snprintf(param, sizeof(param), "$%d", paramid);
2517+
params = lappend(params, pstrdup(param));
2518+
}
2519+
2520+
if (params)
2521+
ExplainPropertyList("Params Evaluated", params, es);
2522+
}
2523+
24902524
/*
24912525
* Fetch the name of an index in an EXPLAIN
24922526
*

src/backend/executor/execExprInterp.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,33 @@ ExecEvalParamExec(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
19261926
*op->resnull = prm->isnull;
19271927
}
19281928

1929+
/*
1930+
* ExecEvalParamExecParams
1931+
*
1932+
* Execute the subplan stored in PARAM_EXEC initplans params, if not executed
1933+
* till now.
1934+
*/
1935+
void
1936+
ExecEvalParamExecParams(Bitmapset *params, EState *estate)
1937+
{
1938+
ParamExecData *prm;
1939+
int paramid;
1940+
1941+
paramid = -1;
1942+
while ((paramid = bms_next_member(params, paramid)) >= 0)
1943+
{
1944+
prm = &(estate->es_param_exec_vals[paramid]);
1945+
1946+
if (prm->execPlan != NULL)
1947+
{
1948+
/* Parameter not evaluated yet, so go do it */
1949+
ExecSetParamPlan(prm->execPlan, GetPerTupleExprContext(estate));
1950+
/* ExecSetParamPlan should have processed this param... */
1951+
Assert(prm->execPlan == NULL);
1952+
}
1953+
}
1954+
}
1955+
19291956
/*
19301957
* Evaluate a PARAM_EXTERN parameter.
19311958
*

0 commit comments

Comments
 (0)