diff options
Diffstat (limited to 'src/backend')
-rw-r--r-- | src/backend/optimizer/plan/planner.c | 156 | ||||
-rw-r--r-- | src/backend/parser/parse_agg.c | 4 | ||||
-rw-r--r-- | src/backend/utils/adt/windowfuncs.c | 148 |
3 files changed, 308 insertions, 0 deletions
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index e21e72eb870..8a41e1e6d36 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -41,6 +41,7 @@ #ifdef OPTIMIZER_DEBUG #include "nodes/print.h" #endif +#include "nodes/supportnodes.h" #include "optimizer/appendinfo.h" #include "optimizer/clauses.h" #include "optimizer/cost.h" @@ -208,6 +209,8 @@ static PathTarget *make_partial_grouping_target(PlannerInfo *root, PathTarget *grouping_target, Node *havingQual); static List *postprocess_setop_tlist(List *new_tlist, List *orig_tlist); +static void optimize_window_clauses(PlannerInfo *root, + WindowFuncLists *wflists); static List *select_active_windows(PlannerInfo *root, WindowFuncLists *wflists); static PathTarget *make_window_input_target(PlannerInfo *root, PathTarget *final_target, @@ -1430,7 +1433,16 @@ grouping_planner(PlannerInfo *root, double tuple_fraction) wflists = find_window_functions((Node *) root->processed_tlist, list_length(parse->windowClause)); if (wflists->numWindowFuncs > 0) + { + /* + * See if any modifications can be made to each WindowClause + * to allow the executor to execute the WindowFuncs more + * quickly. + */ + optimize_window_clauses(root, wflists); + activeWindows = select_active_windows(root, wflists); + } else parse->hasWindowFuncs = false; } @@ -5433,6 +5445,150 @@ postprocess_setop_tlist(List *new_tlist, List *orig_tlist) } /* + * optimize_window_clauses + * Call each WindowFunc's prosupport function to see if we're able to + * make any adjustments to any of the WindowClause's so that the executor + * can execute the window functions in a more optimal way. + * + * Currently we only allow adjustments to the WindowClause's frameOptions. We + * may allow more things to be done here in the future. + */ +static void +optimize_window_clauses(PlannerInfo *root, WindowFuncLists *wflists) +{ + List *windowClause = root->parse->windowClause; + ListCell *lc; + + foreach(lc, windowClause) + { + WindowClause *wc = lfirst_node(WindowClause, lc); + ListCell *lc2; + int optimizedFrameOptions = 0; + + Assert(wc->winref <= wflists->maxWinRef); + + /* skip any WindowClauses that have no WindowFuncs */ + if (wflists->windowFuncs[wc->winref] == NIL) + continue; + + foreach(lc2, wflists->windowFuncs[wc->winref]) + { + SupportRequestOptimizeWindowClause req; + SupportRequestOptimizeWindowClause *res; + WindowFunc *wfunc = lfirst_node(WindowFunc, lc2); + Oid prosupport; + + prosupport = get_func_support(wfunc->winfnoid); + + /* Check if there's a support function for 'wfunc' */ + if (!OidIsValid(prosupport)) + break; /* can't optimize this WindowClause */ + + req.type = T_SupportRequestOptimizeWindowClause; + req.window_clause = wc; + req.window_func = wfunc; + req.frameOptions = wc->frameOptions; + + /* call the support function */ + res = (SupportRequestOptimizeWindowClause *) + DatumGetPointer(OidFunctionCall1(prosupport, + PointerGetDatum(&req))); + + /* + * Skip to next WindowClause if the support function does not + * support this request type. + */ + if (res == NULL) + break; + + /* + * Save these frameOptions for the first WindowFunc for this + * WindowClause. + */ + if (foreach_current_index(lc2) == 0) + optimizedFrameOptions = res->frameOptions; + + /* + * On subsequent WindowFuncs, if the frameOptions are not the same + * then we're unable to optimize the frameOptions for this + * WindowClause. + */ + else if (optimizedFrameOptions != res->frameOptions) + break; /* skip to the next WindowClause, if any */ + } + + /* adjust the frameOptions if all WindowFunc's agree that it's ok */ + if (lc2 == NULL && wc->frameOptions != optimizedFrameOptions) + { + ListCell *lc3; + + /* apply the new frame options */ + wc->frameOptions = optimizedFrameOptions; + + /* + * We now check to see if changing the frameOptions has caused + * this WindowClause to be a duplicate of some other WindowClause. + * This can only happen if we have multiple WindowClauses, so + * don't bother if there's only 1. + */ + if (list_length(windowClause) == 1) + continue; + + /* + * Do the duplicate check and reuse the existing WindowClause if + * we find a duplicate. + */ + foreach(lc3, windowClause) + { + WindowClause *existing_wc = lfirst_node(WindowClause, lc3); + + /* skip over the WindowClause we're currently editing */ + if (existing_wc == wc) + continue; + + /* + * Perform the same duplicate check that is done in + * transformWindowFuncCall. + */ + if (equal(wc->partitionClause, existing_wc->partitionClause) && + equal(wc->orderClause, existing_wc->orderClause) && + wc->frameOptions == existing_wc->frameOptions && + equal(wc->startOffset, existing_wc->startOffset) && + equal(wc->endOffset, existing_wc->endOffset)) + { + ListCell *lc4; + + /* + * Now move each WindowFunc in 'wc' into 'existing_wc'. + * This required adjusting each WindowFunc's winref and + * moving the WindowFuncs in 'wc' to the list of + * WindowFuncs in 'existing_wc'. + */ + foreach(lc4, wflists->windowFuncs[wc->winref]) + { + WindowFunc *wfunc = lfirst_node(WindowFunc, lc4); + + wfunc->winref = existing_wc->winref; + } + + /* move list items */ + wflists->windowFuncs[existing_wc->winref] = list_concat(wflists->windowFuncs[existing_wc->winref], + wflists->windowFuncs[wc->winref]); + wflists->windowFuncs[wc->winref] = NIL; + + /* + * transformWindowFuncCall() should have made sure there + * are no other duplicates, so we needn't bother looking + * any further. + */ + break; + } + } + } + } +} + +/* * select_active_windows * Create a list of the "active" window clauses (ie, those referenced * by non-deleted WindowFuncs) in the order they are to be executed. diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c index 3ef9e8ee5e1..8eec2088aa3 100644 --- a/src/backend/parser/parse_agg.c +++ b/src/backend/parser/parse_agg.c @@ -1025,6 +1025,10 @@ transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc, /* matched, no refname */ ; else continue; + + /* + * Also see similar de-duplication code in optimize_window_clauses + */ if (equal(refwin->partitionClause, windef->partitionClause) && equal(refwin->orderClause, windef->orderClause) && refwin->frameOptions == windef->frameOptions && diff --git a/src/backend/utils/adt/windowfuncs.c b/src/backend/utils/adt/windowfuncs.c index 596564fa15c..db852361887 100644 --- a/src/backend/utils/adt/windowfuncs.c +++ b/src/backend/utils/adt/windowfuncs.c @@ -107,6 +107,24 @@ window_row_number_support(PG_FUNCTION_ARGS) PG_RETURN_POINTER(req); } + if (IsA(rawreq, SupportRequestOptimizeWindowClause)) + { + SupportRequestOptimizeWindowClause *req = (SupportRequestOptimizeWindowClause *) rawreq; + + /* + * The frame options can always become "ROWS BETWEEN UNBOUNDED + * PRECEDING AND CURRENT ROW". row_number() always just increments by + * 1 with each row in the partition. Using ROWS instead of RANGE + * saves effort checking peer rows during execution. + */ + req->frameOptions = (FRAMEOPTION_NONDEFAULT | + FRAMEOPTION_ROWS | + FRAMEOPTION_START_UNBOUNDED_PRECEDING | + FRAMEOPTION_END_CURRENT_ROW); + + PG_RETURN_POINTER(req); + } + PG_RETURN_POINTER(NULL); } @@ -149,6 +167,27 @@ window_rank_support(PG_FUNCTION_ARGS) PG_RETURN_POINTER(req); } + if (IsA(rawreq, SupportRequestOptimizeWindowClause)) + { + SupportRequestOptimizeWindowClause *req = (SupportRequestOptimizeWindowClause *) rawreq; + + /* + * rank() is coded in such a way that it returns "(COUNT (*) OVER + * (<opt> RANGE UNBOUNDED PRECEDING) - COUNT (*) OVER (<opt> RANGE + * CURRENT ROW) + 1)" regardless of the frame options. We'll set the + * frame options to "ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW" + * so they agree with what window_row_number_support() optimized the + * frame options to be. Using ROWS instead of RANGE saves from doing + * peer row checks during execution. + */ + req->frameOptions = (FRAMEOPTION_NONDEFAULT | + FRAMEOPTION_ROWS | + FRAMEOPTION_START_UNBOUNDED_PRECEDING | + FRAMEOPTION_END_CURRENT_ROW); + + PG_RETURN_POINTER(req); + } + PG_RETURN_POINTER(NULL); } @@ -190,6 +229,24 @@ window_dense_rank_support(PG_FUNCTION_ARGS) PG_RETURN_POINTER(req); } + if (IsA(rawreq, SupportRequestOptimizeWindowClause)) + { + SupportRequestOptimizeWindowClause *req = (SupportRequestOptimizeWindowClause *) rawreq; + + /* + * dense_rank() is unaffected by the frame options. Here we set the + * frame options to match what's done in row_number's support + * function. Using ROWS instead of RANGE (the default) saves the + * executor from having to check for peer rows. + */ + req->frameOptions = (FRAMEOPTION_NONDEFAULT | + FRAMEOPTION_ROWS | + FRAMEOPTION_START_UNBOUNDED_PRECEDING | + FRAMEOPTION_END_CURRENT_ROW); + + PG_RETURN_POINTER(req); + } + PG_RETURN_POINTER(NULL); } @@ -223,6 +280,37 @@ window_percent_rank(PG_FUNCTION_ARGS) } /* + * window_percent_rank_support + * prosupport function for window_percent_rank() + */ +Datum +window_percent_rank_support(PG_FUNCTION_ARGS) +{ + Node *rawreq = (Node *) PG_GETARG_POINTER(0); + + if (IsA(rawreq, SupportRequestOptimizeWindowClause)) + { + SupportRequestOptimizeWindowClause *req = (SupportRequestOptimizeWindowClause *) rawreq; + + /* + * percent_rank() is unaffected by the frame options. Here we set the + * frame options to match what's done in row_number's support + * function. Using ROWS instead of RANGE (the default) saves the + * executor from having to check for peer rows. + */ + req->frameOptions = (FRAMEOPTION_NONDEFAULT | + FRAMEOPTION_ROWS | + FRAMEOPTION_START_UNBOUNDED_PRECEDING | + FRAMEOPTION_END_CURRENT_ROW); + + PG_RETURN_POINTER(req); + } + + PG_RETURN_POINTER(NULL); +} + + +/* * cume_dist * return fraction between 0 and 1 inclusive, * which is described as NP / NR, where NP is the number of rows preceding or @@ -266,6 +354,36 @@ window_cume_dist(PG_FUNCTION_ARGS) } /* + * window_cume_dist_support + * prosupport function for window_cume_dist() + */ +Datum +window_cume_dist_support(PG_FUNCTION_ARGS) +{ + Node *rawreq = (Node *) PG_GETARG_POINTER(0); + + if (IsA(rawreq, SupportRequestOptimizeWindowClause)) + { + SupportRequestOptimizeWindowClause *req = (SupportRequestOptimizeWindowClause *) rawreq; + + /* + * cume_dist() is unaffected by the frame options. Here we set the + * frame options to match what's done in row_number's support + * function. Using ROWS instead of RANGE (the default) saves the + * executor from having to check for peer rows. + */ + req->frameOptions = (FRAMEOPTION_NONDEFAULT | + FRAMEOPTION_ROWS | + FRAMEOPTION_START_UNBOUNDED_PRECEDING | + FRAMEOPTION_END_CURRENT_ROW); + + PG_RETURN_POINTER(req); + } + + PG_RETURN_POINTER(NULL); +} + +/* * ntile * compute an exact numeric value with scale 0 (zero), * ranging from 1 (one) to n, per spec. @@ -339,6 +457,36 @@ window_ntile(PG_FUNCTION_ARGS) } /* + * window_ntile_support + * prosupport function for window_ntile() + */ +Datum +window_ntile_support(PG_FUNCTION_ARGS) +{ + Node *rawreq = (Node *) PG_GETARG_POINTER(0); + + if (IsA(rawreq, SupportRequestOptimizeWindowClause)) + { + SupportRequestOptimizeWindowClause *req = (SupportRequestOptimizeWindowClause *) rawreq; + + /* + * ntile() is unaffected by the frame options. Here we set the frame + * options to match what's done in row_number's support function. + * Using ROWS instead of RANGE (the default) saves the executor from + * having to check for peer rows. + */ + req->frameOptions = (FRAMEOPTION_NONDEFAULT | + FRAMEOPTION_ROWS | + FRAMEOPTION_START_UNBOUNDED_PRECEDING | + FRAMEOPTION_END_CURRENT_ROW); + + PG_RETURN_POINTER(req); + } + + PG_RETURN_POINTER(NULL); +} + +/* * leadlag_common * common operation of lead() and lag() * For lead() forward is true, whereas for lag() it is false. |