]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Disable run condition optimization for some WindowFuncs
authorDavid Rowley <drowley@postgresql.org>
Wed, 1 May 2024 04:35:37 +0000 (16:35 +1200)
committerDavid Rowley <drowley@postgresql.org>
Wed, 1 May 2024 04:35:37 +0000 (16:35 +1200)
commit7e5d20bbd13fae7522003f7c3655c9165fb1b3e7
tree5dbcb8a1c05ad73ddbecdb4274c8fd83f9cae9be
parentfaba2f8f35df8cd07cd2205b0bb4815a10c38b14
Disable run condition optimization for some WindowFuncs

94985c210 added code to detect when WindowFuncs were monotonic and
allowed additional quals to be "pushed down" into the subquery to be
used as WindowClause runConditions in order to short-circuit execution
in nodeWindowAgg.c.

The Node representation of runConditions wasn't well selected and
because we do qual pushdown before planning the subquery, the planning
of the subquery could perform subquery pull-up of nested subqueries.
For WindowFuncs with args, the arguments could be changed after pushing
the qual down to the subquery.

This was made more difficult by the fact that the code duplicated the
WindowFunc inside an OpExpr to include in the WindowClauses runCondition
field.  This could result in duplication of subqueries and a pull-up of
such a subquery could result in another initplan parameter being issued
for the 2nd version of the subplan.  This could result in errors such as:

ERROR:  WindowFunc not found in subplan target lists

Here in the backbranches, we don't have the flexibility to improve the
Node representation to resolve this, so instead we just disable the
runCondition optimization for ntile() unless the argument is a Const,
(v16 only) and likewise for count(expr) (both v15 and v16).  count(*) is
unaffected.  All other window functions which support this optimization
all take zero arguments and therefore are unaffected.

Bug: #18170
Reported-by: Zuming Jiang
Discussion: https://postgr.es/m/18170-f1d17bf9a0d58b24@postgresql.org
Backpatch-through 15 (master will be fixed independently)
src/backend/utils/adt/int8.c
src/backend/utils/adt/windowfuncs.c
src/test/regress/expected/window.out
src/test/regress/sql/window.sql