diff options
author | Tomas Vondra <tomas.vondra@postgresql.org> | 2020-05-31 14:43:13 +0200 |
---|---|---|
committer | Tomas Vondra <tomas.vondra@postgresql.org> | 2020-05-31 14:43:13 +0200 |
commit | 4cad2534da6d17067d98cf04be2dfc1bda8f2cd0 (patch) | |
tree | 5b27dc496791ec122500851a35f3f63d46147828 /src | |
parent | 9b60c4b979bce060495e2b05ba01d1cc6bffdd2d (diff) | |
download | postgresql-4cad2534da6d17067d98cf04be2dfc1bda8f2cd0.tar.gz postgresql-4cad2534da6d17067d98cf04be2dfc1bda8f2cd0.zip |
Use CP_SMALL_TLIST for hash aggregate
Commit 1f39bce021 added disk-based hash aggregation, which may spill
incoming tuples to disk. It however did not request projection to make
the tuples as narrow as possible, which may mean having to spill much
more data than necessary (increasing I/O, pushing other stuff from page
cache, etc.).
This adds CP_SMALL_TLIST in places that may use hash aggregation - we do
that only for AGG_HASHED. It's unnecessary for AGG_SORTED, because that
either uses explicit Sort (which already does projection) or pre-sorted
input (which does not need spilling to disk).
Author: Tomas Vondra
Reviewed-by: Jeff Davis
Discussion: https://postgr.es/m/20200519151202.u2p2gpiawoaznsv2%40development
Diffstat (limited to 'src')
-rw-r--r-- | src/backend/optimizer/plan/createplan.c | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c index 9941dfe65e4..744eed187d4 100644 --- a/src/backend/optimizer/plan/createplan.c +++ b/src/backend/optimizer/plan/createplan.c @@ -2113,12 +2113,22 @@ create_agg_plan(PlannerInfo *root, AggPath *best_path) Plan *subplan; List *tlist; List *quals; + int flags; /* * Agg can project, so no need to be terribly picky about child tlist, but - * we do need grouping columns to be available + * we do need grouping columns to be available. We are a bit more careful + * with hash aggregate, where we explicitly request small tlist to minimize + * I/O needed for spilling (we can't be sure spilling won't be necessary, + * so we just do it every time). */ - subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST); + flags = CP_LABEL_TLIST; + + /* ensure small tlist for hash aggregate */ + if (best_path->aggstrategy == AGG_HASHED) + flags |= CP_SMALL_TLIST; + + subplan = create_plan_recurse(root, best_path->subpath, flags); tlist = build_path_tlist(root, &best_path->path); @@ -2200,6 +2210,7 @@ create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path) int maxref; List *chain; ListCell *lc; + int flags; /* Shouldn't get here without grouping sets */ Assert(root->parse->groupingSets); @@ -2207,9 +2218,18 @@ create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path) /* * Agg can project, so no need to be terribly picky about child tlist, but - * we do need grouping columns to be available + * we do need grouping columns to be available. We are a bit more careful + * with hash aggregate, where we explicitly request small tlist to minimize + * I/O needed for spilling (we can't be sure spilling won't be necessary, + * so we just do it every time). */ - subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST); + flags = CP_LABEL_TLIST; + + /* ensure small tlist for hash aggregate */ + if (best_path->aggstrategy == AGG_HASHED) + flags |= CP_SMALL_TLIST; + + subplan = create_plan_recurse(root, best_path->subpath, flags); /* * Compute the mapping from tleSortGroupRef to column index in the child's |