From 456fa635a909ee36f73ca84d340521bd730f265f Mon Sep 17 00:00:00 2001 From: David Rowley Date: Fri, 27 Jan 2023 16:08:41 +1300 Subject: Teach planner about more monotonic window functions 9d9c02ccd introduced runConditions for window functions to allow monotonic window function evaluation to be made more efficient when the window function value went beyond some value that it would never go back from due to its monotonic nature. That commit added prosupport functions to inform the planner that row_number(), rank(), dense_rank() and some forms of count(*) were monotonic. Here we add support for ntile(), cume_dist() and percent_rank(). Reviewed-by: Melanie Plageman Discussion: https://postgr.es/m/CAApHDvqR+VqB8s+xR-24bzJbU8xyFrBszJ17qKgECf7cWxLCaA@mail.gmail.com --- src/backend/utils/adt/windowfuncs.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/backend/utils/adt/windowfuncs.c') diff --git a/src/backend/utils/adt/windowfuncs.c b/src/backend/utils/adt/windowfuncs.c index af13b8e53d5..b87a624fb2f 100644 --- a/src/backend/utils/adt/windowfuncs.c +++ b/src/backend/utils/adt/windowfuncs.c @@ -288,6 +288,15 @@ window_percent_rank_support(PG_FUNCTION_ARGS) { Node *rawreq = (Node *) PG_GETARG_POINTER(0); + if (IsA(rawreq, SupportRequestWFuncMonotonic)) + { + SupportRequestWFuncMonotonic *req = (SupportRequestWFuncMonotonic *) rawreq; + + /* percent_rank() is monotonically increasing */ + req->monotonic = MONOTONICFUNC_INCREASING; + PG_RETURN_POINTER(req); + } + if (IsA(rawreq, SupportRequestOptimizeWindowClause)) { SupportRequestOptimizeWindowClause *req = (SupportRequestOptimizeWindowClause *) rawreq; @@ -362,6 +371,15 @@ window_cume_dist_support(PG_FUNCTION_ARGS) { Node *rawreq = (Node *) PG_GETARG_POINTER(0); + if (IsA(rawreq, SupportRequestWFuncMonotonic)) + { + SupportRequestWFuncMonotonic *req = (SupportRequestWFuncMonotonic *) rawreq; + + /* cume_dist() is monotonically increasing */ + req->monotonic = MONOTONICFUNC_INCREASING; + PG_RETURN_POINTER(req); + } + if (IsA(rawreq, SupportRequestOptimizeWindowClause)) { SupportRequestOptimizeWindowClause *req = (SupportRequestOptimizeWindowClause *) rawreq; @@ -465,6 +483,18 @@ window_ntile_support(PG_FUNCTION_ARGS) { Node *rawreq = (Node *) PG_GETARG_POINTER(0); + if (IsA(rawreq, SupportRequestWFuncMonotonic)) + { + SupportRequestWFuncMonotonic *req = (SupportRequestWFuncMonotonic *) rawreq; + + /* + * ntile() is monotonically increasing as the number of buckets cannot + * change after the first call + */ + req->monotonic = MONOTONICFUNC_INCREASING; + PG_RETURN_POINTER(req); + } + if (IsA(rawreq, SupportRequestOptimizeWindowClause)) { SupportRequestOptimizeWindowClause *req = (SupportRequestOptimizeWindowClause *) rawreq; -- cgit v1.2.3