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: Fix query pullup issue with WindowClause runCondition
Date: Sun, 05 May 2024 00:55:12 +0000
Message-ID: <E1s3Q9m-001Z37-Uu@gemulon.postgresql.org> (raw)

Fix query pullup issue with WindowClause runCondition

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

To fix this, we change the node representation of these run conditions
and instead of storing an OpExpr containing the WindowFunc in a list
inside WindowClause, we now store a new node type named
WindowFuncRunCondition within a new field in the WindowFunc.  These get
transformed into OpExprs later in planning once subquery pull-up has been
performed.

This problem did exist in v15 and v16, but that was fixed by 9d36b883b
and e5d20bbd.

Cat version bump due to new node type and modifying WindowFunc struct.

Bug: #18305
Reported-by: Zuming Jiang
Discussion: https://postgr.es/m/18305-33c49b4c830b37b3%40postgresql.org

Branch
------
master

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

Modified Files
--------------
src/backend/nodes/nodeFuncs.c             | 26 +++++++++++----
src/backend/optimizer/path/allpaths.c     | 36 +++++++--------------
src/backend/optimizer/plan/createplan.c   |  2 +-
src/backend/optimizer/plan/planner.c      | 54 +++++++++++++++++++++++++------
src/backend/optimizer/prep/prepjointree.c |  8 -----
src/backend/optimizer/util/clauses.c      |  1 +
src/backend/optimizer/util/pathnode.c     |  3 ++
src/backend/parser/parse_clause.c         |  1 -
src/backend/parser/parse_expr.c           |  1 +
src/backend/parser/parse_func.c           |  1 +
src/include/catalog/catversion.h          |  2 +-
src/include/nodes/parsenodes.h            |  2 --
src/include/nodes/pathnodes.h             |  1 +
src/include/nodes/primnodes.h             | 30 +++++++++++++++++
src/include/optimizer/pathnode.h          |  1 +
src/test/regress/expected/window.out      | 18 +++++++++++
src/test/regress/sql/window.sql           |  8 +++++
src/tools/pgindent/typedefs.list          |  1 +
18 files changed, 143 insertions(+), 53 deletions(-)



Message-ID: <E1s3Q9m-001Z37-Uu@gemulon.postgresql.org>
Permalink:  ../E1s3Q9m-001Z37-Uu@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1s3Q9m-001Z37-Uu@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: Fix query pullup issue with WindowClause runCondition
  In-Reply-To: <E1s3Q9m-001Z37-Uu@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