pgsql: Make truncate_useless_pathkeys() consider WindowFuncs

From: David Rowley <drowley(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Make truncate_useless_pathkeys() consider WindowFuncs
Date: 2025-10-08 23:39:09
Message-ID: E1v6dkT-000kqc-0r@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Make truncate_useless_pathkeys() consider WindowFuncs

truncate_useless_pathkeys() seems to have neglected to account for
PathKeys that might be useful for WindowClause evaluation. Modify it so
that it properly accounts for that.

Making this work required adjusting two things:

1. Change from checking query_pathkeys to check sort_pathkeys instead.
2. Add explicit check for window_pathkeys

For #1, query_pathkeys gets set in standard_qp_callback() according to the
sort order requirements for the first operation to be applied after the
join planner is finished, so this changes depending on which upper
planner operations a particular query needs. If the query has window
functions and no GROUP BY, then query_pathkeys gets set to
window_pathkeys. Before this change, this meant PathKeys useful for the
ORDER BY were not accounted for in queries with window functions.

Because of #1, #2 is now required so that we explicitly check to ensure
we don't truncate away PathKeys useful for window functions.

Author: David Rowley <dgrowleyml(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAApHDvrj3HTKmXoLMbUjTO=_MNMxM=cnuCSyBKidAVibmYPnrg@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/a5a68dd6d5159626360f75ffde96eca879e6cc30

Modified Files
--------------
src/backend/optimizer/path/pathkeys.c | 24 ++++++++++++++++++++++--
src/test/regress/expected/window.out | 16 ++++++++++++++++
src/test/regress/sql/window.sql | 13 +++++++++++++
3 files changed, 51 insertions(+), 2 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Amit Langote 2025-10-09 01:21:06 pgsql: Fix internal error from CollateExpr in SQL/JSON DEFAULT expressi
Previous Message Andres Freund 2025-10-08 21:30:55 pgsql: bufmgr: Don't lock buffer header in StrategyGetBuffer()