RelOptInfo *grouped_rel;
PathTarget *partial_grouping_target = NULL;
AggClauseCosts agg_costs;
+ AggClauseCosts agg_partial_costs; /* parallel only */
+ AggClauseCosts agg_final_costs; /* parallel only */
Size hashaggtablesize;
double dNumGroups;
double dNumPartialGroups = 0;
MemSet(&agg_costs, 0, sizeof(AggClauseCosts));
if (parse->hasAggs)
{
- count_agg_clauses(root, (Node *) target->exprs, &agg_costs);
- count_agg_clauses(root, parse->havingQual, &agg_costs);
+ count_agg_clauses(root, (Node *) target->exprs, &agg_costs, true,
+ false, false);
+ count_agg_clauses(root, parse->havingQual, &agg_costs, true, false,
+ false);
}
/*
NIL,
NIL);
+ /*
+ * Collect statistics about aggregates for estimating costs of
+ * performing aggregation in parallel.
+ */
+ MemSet(&agg_partial_costs, 0, sizeof(AggClauseCosts));
+ MemSet(&agg_final_costs, 0, sizeof(AggClauseCosts));
+ if (parse->hasAggs)
+ {
+ /* partial phase */
+ count_agg_clauses(root, (Node *) partial_grouping_target->exprs,
+ &agg_partial_costs, false, false, true);
+
+ /* final phase */
+ count_agg_clauses(root, (Node *) target->exprs, &agg_final_costs,
+ true, true, true);
+ count_agg_clauses(root, parse->havingQual, &agg_final_costs, true,
+ true, true);
+ }
+
if (can_sort)
{
/* Checked in set_grouped_rel_consider_parallel() */
parse->groupClause ? AGG_SORTED : AGG_PLAIN,
parse->groupClause,
NIL,
- &agg_costs,
+ &agg_partial_costs,
dNumPartialGroups,
false,
false,
hashaggtablesize =
estimate_hashagg_tablesize(cheapest_partial_path,
- &agg_costs,
+ &agg_partial_costs,
dNumPartialGroups);
/*
AGG_HASHED,
parse->groupClause,
NIL,
- &agg_costs,
+ &agg_partial_costs,
dNumPartialGroups,
false,
false,
parse->groupClause ? AGG_SORTED : AGG_PLAIN,
parse->groupClause,
(List *) parse->havingQual,
- &agg_costs,
+ &agg_final_costs,
dNumGroups,
true,
true,
Path *path = (Path *) linitial(grouped_rel->partial_pathlist);
hashaggtablesize = estimate_hashagg_tablesize(path,
- &agg_costs,
+ &agg_final_costs,
dNumGroups);
if (hashaggtablesize < work_mem * 1024L)
AGG_HASHED,
parse->groupClause,
(List *) parse->havingQual,
- &agg_costs,
+ &agg_final_costs,
dNumGroups,
true,
true,
{
PlannerInfo *root;
AggClauseCosts *costs;
+ bool finalizeAggs;
+ bool combineStates;
+ bool serialStates;
} count_agg_clauses_context;
typedef struct
* are no subqueries. There mustn't be outer-aggregate references either.
*/
void
-count_agg_clauses(PlannerInfo *root, Node *clause, AggClauseCosts *costs)
+count_agg_clauses(PlannerInfo *root, Node *clause, AggClauseCosts *costs,
+ bool finalizeAggs, bool combineStates, bool serialStates)
{
count_agg_clauses_context context;
context.root = root;
context.costs = costs;
+ context.finalizeAggs = finalizeAggs;
+ context.combineStates = combineStates;
+ context.serialStates = serialStates;
(void) count_agg_clauses_walker(clause, &context);
}
Form_pg_aggregate aggform;
Oid aggtransfn;
Oid aggfinalfn;
+ Oid aggcombinefn;
+ Oid aggserialfn;
+ Oid aggdeserialfn;
Oid aggtranstype;
int32 aggtransspace;
QualCost argcosts;
aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple);
aggtransfn = aggform->aggtransfn;
aggfinalfn = aggform->aggfinalfn;
+ aggcombinefn = aggform->aggcombinefn;
+ aggserialfn = aggform->aggserialfn;
+ aggdeserialfn = aggform->aggdeserialfn;
aggtranstype = aggform->aggtranstype;
aggtransspace = aggform->aggtransspace;
ReleaseSysCache(aggTuple);
if (aggref->aggorder != NIL || aggref->aggdistinct != NIL)
costs->numOrderedAggs++;
- /* add component function execution costs to appropriate totals */
- costs->transCost.per_tuple += get_func_cost(aggtransfn) * cpu_operator_cost;
- if (OidIsValid(aggfinalfn))
- costs->finalCost += get_func_cost(aggfinalfn) * cpu_operator_cost;
+ /*
+ * Add the appropriate component function execution costs to
+ * appropriate totals.
+ */
+ if (context->combineStates)
+ {
+ /* charge for combining previously aggregated states */
+ costs->transCost.per_tuple += get_func_cost(aggcombinefn) * cpu_operator_cost;
- /* also add the input expressions' cost to per-input-row costs */
- cost_qual_eval_node(&argcosts, (Node *) aggref->args, context->root);
- costs->transCost.startup += argcosts.startup;
- costs->transCost.per_tuple += argcosts.per_tuple;
+ /* charge for deserialization, when appropriate */
+ if (context->serialStates && OidIsValid(aggdeserialfn))
+ costs->transCost.per_tuple += get_func_cost(aggdeserialfn) * cpu_operator_cost;
+ }
+ else
+ costs->transCost.per_tuple += get_func_cost(aggtransfn) * cpu_operator_cost;
+
+ if (context->finalizeAggs)
+ {
+ if (OidIsValid(aggfinalfn))
+ costs->finalCost += get_func_cost(aggfinalfn) * cpu_operator_cost;
+ }
+ else if (context->serialStates)
+ {
+ if (OidIsValid(aggserialfn))
+ costs->finalCost += get_func_cost(aggserialfn) * cpu_operator_cost;
+ }
/*
- * Add any filter's cost to per-input-row costs.
- *
- * XXX Ideally we should reduce input expression costs according to
- * filter selectivity, but it's not clear it's worth the trouble.
+ * Some costs will already have been incurred by the initial aggregate
+ * node, so we mustn't include these again.
*/
- if (aggref->aggfilter)
+ if (!context->combineStates)
{
- cost_qual_eval_node(&argcosts, (Node *) aggref->aggfilter,
- context->root);
+ /* add the input expressions' cost to per-input-row costs */
+ cost_qual_eval_node(&argcosts, (Node *) aggref->args, context->root);
costs->transCost.startup += argcosts.startup;
costs->transCost.per_tuple += argcosts.per_tuple;
+
+ /*
+ * Add any filter's cost to per-input-row costs.
+ *
+ * XXX Ideally we should reduce input expression costs according
+ * to filter selectivity, but it's not clear it's worth the
+ * trouble.
+ */
+ if (aggref->aggfilter)
+ {
+ cost_qual_eval_node(&argcosts, (Node *) aggref->aggfilter,
+ context->root);
+ costs->transCost.startup += argcosts.startup;
+ costs->transCost.per_tuple += argcosts.per_tuple;
+ }
}
/*