pgsql: Allow window functions to adjust their frameOptions

From: David Rowley <drowley(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Allow window functions to adjust their frameOptions
Date: 2022-12-22 23:44:19
Message-ID: E1p8VEY-004wmR-En@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Allow window functions to adjust their frameOptions

WindowFuncs such as row_number() don't care if it's called with ROWS
UNBOUNDED PRECEDING AND CURRENT ROW or with RANGE UNBOUNDED PRECEDING AND
CURRENT ROW. The latter is less efficient as the RANGE option requires
that the executor check for peer rows, so using the ROW option instead
would cause less overhead. Because RANGE is part of the default frame
options for WindowClauses, it means WindowAgg is, by default, working much
harder than it needs to for window functions where the ROWS / RANGE option
has no effect on the window function's result.

On a test query from the discussion thread, a performance improvement of
344% was seen by using ROWS instead of RANGE.

Here we add a new support function node type to allow support functions to
be called for window functions so that the most optimal version of the
frame options can be set. The planner has been adjusted so that the frame
options are changed only if all window functions sharing the same window
clause agree on what the optimized frame options are.

Here we give the ability for row_number(), rank(), dense_rank(),
percent_rank(), cume_dist() and ntile() to alter their WindowClause's
frameOptions.

Reviewed-by: Vik Fearing, Erwin Brandstetter, Zhihong Yu
Discussion: https://postgr.es/m/CAGHENJ7LBBszxS+SkWWFVnBmOT2oVsBhDMB1DFrgerCeYa_DyA@mail.gmail.com
Discussion: https://postgr.es/m/CAApHDvohAKEtTXxq7Pc-ic2dKT8oZfbRKeEJP64M0B6+S88z+A@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/ed1a88ddaccfe883e4cf74d30319accfeae6cfe5

Modified Files
--------------
src/backend/optimizer/plan/planner.c | 156 +++++++++++++++++++++++++++++++++++
src/backend/parser/parse_agg.c | 4 +
src/backend/utils/adt/windowfuncs.c | 148 +++++++++++++++++++++++++++++++++
src/include/catalog/catversion.h | 2 +-
src/include/catalog/pg_proc.dat | 15 +++-
src/include/nodes/supportnodes.h | 44 ++++++++++
src/test/regress/expected/window.out | 73 ++++++++++++++++
src/test/regress/sql/window.sql | 42 ++++++++++
8 files changed, 480 insertions(+), 4 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Michael Paquier 2022-12-23 00:20:37 pgsql: Rename pg_dissect_walfile_name() to pg_split_walfile_name()
Previous Message Thomas Munro 2022-12-22 22:06:42 pgsql: Improve notation of cacheinfo table in syscache.c.