Window Functions with identical PARTITION BY and ORDER BY clauses evaluated separately
Christopher Inokuchi <cinokuchi@gmail.com>
From: Christopher Inokuchi <cinokuchi@gmail.com>
To: pgsql-bugs@lists.postgresql.org
Date: 2025-03-07T06:02:35Z
Lists: pgsql-bugs
Attachments
- my_table_contents.zip (application/x-zip-compressed)
Relevant documentation:
https://www.postgresql.org/docs/9.4/queries-table-expressions.html#QUERIES-WINDOW
"When multiple window functions are used, all the window functions having
syntactically equivalent PARTITION BY and ORDER BY clauses in their window
definitions are guaranteed to be evaluated in a single pass over the data."
PostgreSQL version:
"PostgreSQL 17.4 on x86_64-windows, compiled by msvc-19.42.34436, 64-bit"
Machine information:
Windows server 2016
kernel version 10.0.14393.7783
12.00 GiB memory
4 cores
Reproduction (my_table_contents.csv attached to email as zip file):
- CREATE TABLE my_table (champid SMALLINT, champmastery INT);
- COPY my_table FROM 'path\to\my_table_contents.csv' WITH (FORMAT CSV);
- CREATE INDEX my_idx ON my_table (champid, champmastery);
- SELECT
SUM(CAST(champmastery AS BIGINT)) OVER (
PARTITION BY champid
ORDER BY champmastery ASC
ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
) AS sumx,
COUNT(1) OVER (
PARTITION BY champid
ORDER BY champmastery ASC
RANGE BETWEEN 1000 PRECEDING AND 1000 FOLLOWING
) AS sampledensity
FROM my_table;
I apologize for the email spacing. It may cause issues with copy paste.
Expected result: Given both window functions in the above SELECT query have
identical PARTITION BY and ORDER BY clauses, the execution plan should have
a single "Window Aggregation" operation.
Actual result: The execution plan generated for the above query has two
"Window Aggregation" operations
[image: image.png]
Commits
-
Doc: improve description of window function processing.
- 6841118216ef 13.21 landed
- e7d29d1fac82 14.18 landed
- 2db974e40df6 15.13 landed
- 778f9dd0d2aa 16.9 landed
- 0f27bd14e480 17.5 landed
- 9f87e2593feb 18.0 landed