Skip to content

Commit c651020

Browse files
committed
Remove redundant PARTITION BY columns from WindowClauses
Here we adjust the query planner to have it remove items from a window clause's PARTITION BY clause in cases where the pathkey for a column in the PARTITION BY clause is redundant. Doing this allows the optimization added in 9d9c02c to stop window aggregation early rather than going into "pass-through" mode to find tuples belonging to the next partition. Also, when we manage to remove all PARTITION BY columns, we now no longer needlessly check that the current tuple belongs to the same partition as the last tuple in nodeWindowAgg.c. If the pathkey was redundant then all tuples must contain the same value for the given redundant column, so there's no point in checking that during execution. Author: David Rowley Reviewed-by: Richard Guo Discussion: https://postgr.es/m/CAApHDvo2ji+hdxrxfXtRtsfSVw3to2o1nCO20qimw0dUGK8hcQ@mail.gmail.com
1 parent 4637a6a commit c651020

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

src/backend/optimizer/plan/createplan.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2623,12 +2623,7 @@ create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path)
26232623

26242624
/*
26252625
* Convert SortGroupClause lists into arrays of attr indexes and equality
2626-
* operators, as wanted by executor. (Note: in principle, it's possible
2627-
* to drop some of the sort columns, if they were proved redundant by
2628-
* pathkey logic. However, it doesn't seem worth going out of our way to
2629-
* optimize such cases. In any case, we must *not* remove the ordering
2630-
* column for RANGE OFFSET cases, as the executor needs that for in_range
2631-
* tests even if it's known to be equal to some partitioning column.)
2626+
* operators, as wanted by executor.
26322627
*/
26332628
partColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numPart);
26342629
partOperators = (Oid *) palloc(sizeof(Oid) * numPart);

src/backend/optimizer/plan/planner.c

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5999,6 +5999,9 @@ make_window_input_target(PlannerInfo *root,
59995999
* Create a pathkeys list describing the required input ordering
60006000
* for the given WindowClause.
60016001
*
6002+
* Modifies wc's partitionClause to remove any clauses which are deemed
6003+
* redundant by the pathkey logic.
6004+
*
60026005
* The required ordering is first the PARTITION keys, then the ORDER keys.
60036006
* In the future we might try to implement windowing using hashing, in which
60046007
* case the ordering could be relaxed, but for now we always sort.
@@ -6007,8 +6010,7 @@ static List *
60076010
make_pathkeys_for_window(PlannerInfo *root, WindowClause *wc,
60086011
List *tlist)
60096012
{
6010-
List *window_pathkeys;
6011-
List *window_sortclauses;
6013+
List *window_pathkeys = NIL;
60126014

60136015
/* Throw error if can't sort */
60146016
if (!grouping_is_sortable(wc->partitionClause))
@@ -6022,12 +6024,45 @@ make_pathkeys_for_window(PlannerInfo *root, WindowClause *wc,
60226024
errmsg("could not implement window ORDER BY"),
60236025
errdetail("Window ordering columns must be of sortable datatypes.")));
60246026

6025-
/* Okay, make the combined pathkeys */
6026-
window_sortclauses = list_concat_copy(wc->partitionClause, wc->orderClause);
6027-
window_pathkeys = make_pathkeys_for_sortclauses(root,
6028-
window_sortclauses,
6029-
tlist);
6030-
list_free(window_sortclauses);
6027+
/*
6028+
* First fetch the pathkeys for the PARTITION BY clause. We can safely
6029+
* remove any clauses from the wc->partitionClause for redundant pathkeys.
6030+
*/
6031+
if (wc->partitionClause != NIL)
6032+
{
6033+
bool sortable;
6034+
6035+
window_pathkeys = make_pathkeys_for_sortclauses_extended(root,
6036+
&wc->partitionClause,
6037+
tlist,
6038+
true,
6039+
&sortable);
6040+
6041+
Assert(sortable);
6042+
}
6043+
6044+
/*
6045+
* In principle, we could also consider removing redundant ORDER BY items
6046+
* too as doing so does not alter the result of peer row checks done by
6047+
* the executor. However, we must *not* remove the ordering column for
6048+
* RANGE OFFSET cases, as the executor needs that for in_range tests even
6049+
* if it's known to be equal to some partitioning column.
6050+
*/
6051+
if (wc->orderClause != NIL)
6052+
{
6053+
List *orderby_pathkeys;
6054+
6055+
orderby_pathkeys = make_pathkeys_for_sortclauses(root,
6056+
wc->orderClause,
6057+
tlist);
6058+
6059+
/* Okay, make the combined pathkeys */
6060+
if (window_pathkeys != NIL)
6061+
window_pathkeys = append_pathkeys(window_pathkeys, orderby_pathkeys);
6062+
else
6063+
window_pathkeys = orderby_pathkeys;
6064+
}
6065+
60316066
return window_pathkeys;
60326067
}
60336068

src/include/nodes/parsenodes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,8 @@ typedef struct GroupingSet
14731473
* if the clause originally came from WINDOW, and is NULL if it originally
14741474
* was an OVER clause (but note that we collapse out duplicate OVERs).
14751475
* partitionClause and orderClause are lists of SortGroupClause structs.
1476+
* partitionClause is sanitized by the query planner to remove any columns or
1477+
* expressions belonging to redundant PathKeys.
14761478
* If we have RANGE with offset PRECEDING/FOLLOWING, the semantics of that are
14771479
* specified by startInRangeFunc/inRangeColl/inRangeAsc/inRangeNullsFirst
14781480
* for the start offset, or endInRangeFunc/inRange* for the end offset.

0 commit comments

Comments
 (0)