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-f1d17bf9a0d58...@postgresql.org
Backpatch-through 15 (master will be fixed independently)

Branch
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/7e5d20bbd13fae7522003f7c3655c9165fb1b3e7

Modified Files
--------------
src/backend/utils/adt/int8.c         | 15 +++++++++++
src/backend/utils/adt/windowfuncs.c  |  1 +
src/test/regress/expected/window.out | 50 ++++++++++++++++++++++++------------
src/test/regress/sql/window.sql      | 25 ++++++++++++------
4 files changed, 67 insertions(+), 24 deletions(-)

Reply via email to