pgsql: Allow window functions to adjust their frameOptions - Mailing list pgsql-committers

From David Rowley
Subject pgsql: Allow window functions to adjust their frameOptions
Date
Msg-id E1p8VEY-004wmR-En@gemulon.postgresql.org
Whole thread Raw
List 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(-)


pgsql-committers by date:

Previous
From: Thomas Munro
Date:
Subject: pgsql: Improve notation of cacheinfo table in syscache.c.
Next
From: Michael Paquier
Date:
Subject: pgsql: Rename pg_dissect_walfile_name() to pg_split_walfile_name()