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

Commit 4cad253

Browse files
committed
Use CP_SMALL_TLIST for hash aggregate
Commit 1f39bce 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
1 parent 9b60c4b commit 4cad253

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

contrib/postgres_fdw/expected/postgres_fdw.out

+2-2
Original file line numberDiff line numberDiff line change
@@ -2715,7 +2715,7 @@ select sum(c1) from ft1 group by c2 having avg(c1 * (random() <= 1)::int) > 100
27152715
Group Key: ft1.c2
27162716
Filter: (avg((ft1.c1 * ((random() <= '1'::double precision))::integer)) > '100'::numeric)
27172717
-> Foreign Scan on public.ft1
2718-
Output: c1, c2
2718+
Output: c2, c1
27192719
Remote SQL: SELECT "C 1", c2 FROM "S 1"."T 1"
27202720
(10 rows)
27212721

@@ -2964,7 +2964,7 @@ select sum(c1) filter (where (c1 / c1) * random() <= 1) from ft1 group by c2 ord
29642964
Output: sum(c1) FILTER (WHERE ((((c1 / c1))::double precision * random()) <= '1'::double precision)), c2
29652965
Group Key: ft1.c2
29662966
-> Foreign Scan on public.ft1
2967-
Output: c1, c2
2967+
Output: c2, c1
29682968
Remote SQL: SELECT "C 1", c2 FROM "S 1"."T 1"
29692969
(9 rows)
29702970

src/backend/optimizer/plan/createplan.c

+24-4
Original file line numberDiff line numberDiff line change
@@ -2113,12 +2113,22 @@ create_agg_plan(PlannerInfo *root, AggPath *best_path)
21132113
Plan *subplan;
21142114
List *tlist;
21152115
List *quals;
2116+
int flags;
21162117

21172118
/*
21182119
* Agg can project, so no need to be terribly picky about child tlist, but
2119-
* we do need grouping columns to be available
2120+
* we do need grouping columns to be available. We are a bit more careful
2121+
* with hash aggregate, where we explicitly request small tlist to minimize
2122+
* I/O needed for spilling (we can't be sure spilling won't be necessary,
2123+
* so we just do it every time).
21202124
*/
2121-
subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2125+
flags = CP_LABEL_TLIST;
2126+
2127+
/* ensure small tlist for hash aggregate */
2128+
if (best_path->aggstrategy == AGG_HASHED)
2129+
flags |= CP_SMALL_TLIST;
2130+
2131+
subplan = create_plan_recurse(root, best_path->subpath, flags);
21222132

21232133
tlist = build_path_tlist(root, &best_path->path);
21242134

@@ -2200,16 +2210,26 @@ create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path)
22002210
int maxref;
22012211
List *chain;
22022212
ListCell *lc;
2213+
int flags;
22032214

22042215
/* Shouldn't get here without grouping sets */
22052216
Assert(root->parse->groupingSets);
22062217
Assert(rollups != NIL);
22072218

22082219
/*
22092220
* Agg can project, so no need to be terribly picky about child tlist, but
2210-
* we do need grouping columns to be available
2221+
* we do need grouping columns to be available. We are a bit more careful
2222+
* with hash aggregate, where we explicitly request small tlist to minimize
2223+
* I/O needed for spilling (we can't be sure spilling won't be necessary,
2224+
* so we just do it every time).
22112225
*/
2212-
subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2226+
flags = CP_LABEL_TLIST;
2227+
2228+
/* ensure small tlist for hash aggregate */
2229+
if (best_path->aggstrategy == AGG_HASHED)
2230+
flags |= CP_SMALL_TLIST;
2231+
2232+
subplan = create_plan_recurse(root, best_path->subpath, flags);
22132233

22142234
/*
22152235
* Compute the mapping from tleSortGroupRef to column index in the child's

0 commit comments

Comments
 (0)