List *rollup_groupclauses);
static void set_grouped_rel_consider_parallel(PlannerInfo *root,
RelOptInfo *grouped_rel,
- PathTarget *target);
-static Size estimate_hashagg_tablesize(Path *path, AggClauseCosts *agg_costs,
+ PathTarget *target,
+ const AggClauseCosts *agg_costs);
+static Size estimate_hashagg_tablesize(Path *path,
+ const AggClauseCosts *agg_costs,
double dNumGroups);
static RelOptInfo *create_grouping_paths(PlannerInfo *root,
RelOptInfo *input_rel,
*/
static void
set_grouped_rel_consider_parallel(PlannerInfo *root, RelOptInfo *grouped_rel,
- PathTarget *target)
+ PathTarget *target,
+ const AggClauseCosts *agg_costs)
{
Query *parse = root->parse;
return;
/*
- * All that's left to check now is to make sure all aggregate functions
- * support partial mode. If there's no aggregates then we can skip
- * checking that.
+ * If we have any non-partial-capable aggregates, or if any of them can't
+ * be serialized, we can't go parallel.
*/
- if (!parse->hasAggs)
- grouped_rel->consider_parallel = true;
- else if (aggregates_allow_partial((Node *) target->exprs) == PAT_ANY &&
- aggregates_allow_partial(root->parse->havingQual) == PAT_ANY)
- grouped_rel->consider_parallel = true;
+ if (agg_costs->hasNonPartial || agg_costs->hasNonSerial)
+ return;
+
+ /* OK, consider parallelization */
+ grouped_rel->consider_parallel = true;
}
/*
* require based on the agg_costs, path width and dNumGroups.
*/
static Size
-estimate_hashagg_tablesize(Path *path, AggClauseCosts *agg_costs,
+estimate_hashagg_tablesize(Path *path, const AggClauseCosts *agg_costs,
double dNumGroups)
{
Size hashentrysize;
* going to be safe to do so.
*/
if (input_rel->partial_pathlist != NIL)
- set_grouped_rel_consider_parallel(root, grouped_rel, target);
+ set_grouped_rel_consider_parallel(root, grouped_rel,
+ target, &agg_costs);
/*
* Determine whether it's possible to perform sort-based implementations
#include "utils/syscache.h"
#include "utils/typcache.h"
-typedef struct
-{
- PartialAggType allowedtype;
-} partial_agg_context;
typedef struct
{
bool allow_restricted;
} has_parallel_hazard_arg;
-static bool aggregates_allow_partial_walker(Node *node,
- partial_agg_context *context);
static bool contain_agg_clause_walker(Node *node, void *context);
static bool get_agg_clause_costs_walker(Node *node,
get_agg_clause_costs_context *context);
* Aggregate-function clause manipulation
*****************************************************************************/
-/*
- * aggregates_allow_partial
- * Recursively search for Aggref clauses and determine the maximum
- * level of partial aggregation which can be supported.
- */
-PartialAggType
-aggregates_allow_partial(Node *clause)
-{
- partial_agg_context context;
-
- /* initially any type is okay, until we find Aggrefs which say otherwise */
- context.allowedtype = PAT_ANY;
-
- if (!aggregates_allow_partial_walker(clause, &context))
- return context.allowedtype;
- return context.allowedtype;
-}
-
-static bool
-aggregates_allow_partial_walker(Node *node, partial_agg_context *context)
-{
- if (node == NULL)
- return false;
- if (IsA(node, Aggref))
- {
- Aggref *aggref = (Aggref *) node;
- HeapTuple aggTuple;
- Form_pg_aggregate aggform;
-
- Assert(aggref->agglevelsup == 0);
-
- /*
- * We can't perform partial aggregation with Aggrefs containing a
- * DISTINCT or ORDER BY clause.
- */
- if (aggref->aggdistinct || aggref->aggorder)
- {
- context->allowedtype = PAT_DISABLED;
- return true; /* abort search */
- }
- aggTuple = SearchSysCache1(AGGFNOID,
- ObjectIdGetDatum(aggref->aggfnoid));
- if (!HeapTupleIsValid(aggTuple))
- elog(ERROR, "cache lookup failed for aggregate %u",
- aggref->aggfnoid);
- aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple);
-
- /*
- * If there is no combine function, then partial aggregation is not
- * possible.
- */
- if (!OidIsValid(aggform->aggcombinefn))
- {
- ReleaseSysCache(aggTuple);
- context->allowedtype = PAT_DISABLED;
- return true; /* abort search */
- }
-
- /*
- * If we find any aggs with an internal transtype then we must check
- * whether these have serialization/deserialization functions;
- * otherwise, we set the maximum allowed type to PAT_INTERNAL_ONLY.
- */
- if (aggform->aggtranstype == INTERNALOID &&
- (!OidIsValid(aggform->aggserialfn) ||
- !OidIsValid(aggform->aggdeserialfn)))
- context->allowedtype = PAT_INTERNAL_ONLY;
-
- ReleaseSysCache(aggTuple);
- return false; /* continue searching */
- }
- return expression_tree_walker(node, aggregates_allow_partial_walker,
- (void *) context);
-}
-
/*
* contain_agg_clause
* Recursively search for Aggref/GroupingFunc nodes within a clause.
*
* We count the nodes, estimate their execution costs, and estimate the total
* space needed for their transition state values if all are evaluated in
- * parallel (as would be done in a HashAgg plan). See AggClauseCosts for
- * the exact set of statistics collected.
+ * parallel (as would be done in a HashAgg plan). Also, we check whether
+ * partial aggregation is feasible. See AggClauseCosts for the exact set
+ * of statistics collected.
*
* In addition, we mark Aggref nodes with the correct aggtranstype, so
* that that doesn't need to be done repeatedly. (That makes this function's
aggref->aggtranstype = aggtranstype;
}
- /* count it; note ordered-set aggs always have nonempty aggorder */
+ /*
+ * Count it, and check for cases requiring ordered input. Note that
+ * ordered-set aggs always have nonempty aggorder. Any ordered-input
+ * case also defeats partial aggregation.
+ */
costs->numAggs++;
if (aggref->aggorder != NIL || aggref->aggdistinct != NIL)
+ {
costs->numOrderedAggs++;
+ costs->hasNonPartial = true;
+ }
+
+ /*
+ * Check whether partial aggregation is feasible, unless we already
+ * found out that we can't do it.
+ */
+ if (!costs->hasNonPartial)
+ {
+ /*
+ * If there is no combine function, then partial aggregation is
+ * not possible.
+ */
+ if (!OidIsValid(aggcombinefn))
+ costs->hasNonPartial = true;
+
+ /*
+ * If we have any aggs with transtype INTERNAL then we must check
+ * whether they have serialization/deserialization functions; if
+ * not, we can't serialize partial-aggregation results.
+ */
+ else if (aggtranstype == INTERNALOID &&
+ (!OidIsValid(aggserialfn) || !OidIsValid(aggdeserialfn)))
+ costs->hasNonSerial = true;
+ }
/*
* Add the appropriate component function execution costs to
List **windowFuncs; /* lists of WindowFuncs for each winref */
} WindowFuncLists;
-/*
- * PartialAggType
- * PartialAggType stores whether partial aggregation is allowed and
- * which context it is allowed in. We require three states here as there are
- * two different contexts in which partial aggregation is safe. For aggregates
- * which have an 'stype' of INTERNAL, it is okay to pass a pointer to the
- * aggregate state within a single process, since the datum is just a
- * pointer. In cases where the aggregate state must be passed between
- * different processes, for example during parallel aggregation, passing
- * pointers directly is not going to work.
- */
-typedef enum
-{
- PAT_ANY = 0, /* Any type of partial aggregation is okay. */
- PAT_INTERNAL_ONLY, /* Some aggregates support only internal mode. */
- PAT_DISABLED /* Some aggregates don't support partial mode
- * at all */
-} PartialAggType;
-
extern Expr *make_opclause(Oid opno, Oid opresulttype, bool opretset,
Expr *leftop, Expr *rightop,
Oid opcollid, Oid inputcollid);
extern Expr *make_ands_explicit(List *andclauses);
extern List *make_ands_implicit(Expr *clause);
-extern PartialAggType aggregates_allow_partial(Node *clause);
extern bool contain_agg_clause(Node *clause);
extern void get_agg_clause_costs(PlannerInfo *root, Node *clause,
AggSplit aggsplit, AggClauseCosts *costs);