pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Rowley <drowley@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Disable run condition optimization for some WindowFuncs
Date: Wed, 01 May 2024 04:35:53 +0000
Message-ID: <E1s21hA-000tor-9r@gemulon.postgresql.org> (raw)

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)

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(-)



view thread (6+ messages)  latest in thread

Message-ID: <E1s21hA-000tor-9r@gemulon.postgresql.org>
Permalink:  ../E1s21hA-000tor-9r@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1s21hA-000tor-9r@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: drowley@postgresql.org, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Disable run condition optimization for some WindowFuncs
  In-Reply-To: <E1s21hA-000tor-9r@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox