Re: Window Functions with identical PARTITION BY and ORDER BY clauses evaluated separately
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Christopher Inokuchi <cinokuchi@gmail.com>
Cc: Greg Sabino Mullane <htamfids@gmail.com>, pgsql-bugs@lists.postgresql.org
Date: 2025-03-08T00:05:51Z
Lists: pgsql-bugs
Christopher Inokuchi <cinokuchi@gmail.com> writes: > Was it really not intentional that the docs explicitly name PARTITION BY > and ORDER BY rather than the entire window_definition? If I understand > correctly, only those two clauses control which records are hit and in what > order. Yeah, it's intentional, and in fact required by the SQL standard. However, you're misinterpreting what the guarantee is. The spec requirement is that window functions sharing PARTITION BY and ORDER BY all be evaluated on the same concrete ordering of the data, ie there can't be any re-sorting between them. And that's what we implement. We do use a separate WindowAgg node for each distinguishable window specification, but you'll notice there is not a Sort step between them unless the query involves entirely-incompatible PARTITION/ORDER BY specs. Perhaps the wording in section 7.2.5 could be improved; I agree that "evaluated in one pass" is capable of being read in more than one way, and it's not clear that it's referring to sorts. Do you have any suggestions for clearer wording? It's possible that we could restructure things so that window functions having distinct frame clauses were nonetheless done in one WindowAgg node. But it would complicate the code and it's far from obvious to me that it'd buy much in speed. Optimizing the sort steps is where most of the potential win lies. regards, tom lane
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