postgresql/src
David Rowley 4297a35196 Fix possible issue of a WindowFunc being in the wrong WindowClause
ed1a88dda made it so WindowClauses can be merged when all window
functions belonging to the WindowClause can equally well use some
other WindowClause without any behavioral changes.  When that
optimization applies, the WindowFunc's "winref" gets adjusted to
reference the new WindowClause.

That commit does not work well with the deduplication logic in
find_window_functions(), which only added the WindowFunc to the list
when there wasn't already an identical WindowFunc in the list.  That
deduplication logic meant that the duplicate WindowFunc wouldn't get the
"winref" changed when optimize_window_clauses() was able to swap the
WindowFunc to another WindowClause.  This could lead to the following
error in the unlikely event that the deduplication code did something and
the duplicate WindowFunc happened to be moved into another WindowClause.

ERROR:  WindowFunc with winref 2 assigned to WindowAgg with winref 1

As it turns out, the deduplication logic in find_window_functions() is
pretty bogus.  It might have done something when added, as that code
predates b8d7f053c, which changed how projections work.  As it turns
out, at least now we *will* evaluate the duplicate WindowFuncs.  All
that the deduplication code seems to do today is assist in
underestimating the WindowAggPath costs due to not counting the
evaluation costs of duplicate WindowFuncs.

Ideally the fix would be to remove the deduplication code, but that
could result in changes to the plan costs, as duplicate WindowFuncs
would then be costed.  Instead, let's play it safe and shift the
deduplication code so it runs after the other processing in
optimize_window_clauses().

Backpatch only as far as v16 as there doesn't seem to be any other harm
done by the WindowFunc deduplication code before then.  This issue was
fixed in master by 7027dd499.

Reported-by: Meng Zhang <mza117jc@gmail.com>
Author: Meng Zhang <mza117jc@gmail.com>
Author: David Rowley <dgrowleyml@gmail.com>
Discussion: https://postgr.es/m/CAErYLFAuxmW0UVdgrz7iiuNrxGQnFK_OP9hBD5CUzRgjrVrz=Q@mail.gmail.com
Backpatch-through: 16
2026-01-26 23:47:37 +13:00
..
backend Fix possible issue of a WindowFunc being in the wrong WindowClause 2026-01-26 23:47:37 +13:00
bin pg_waldump: Relax LSN comparison check in TAP test 2026-01-14 16:02:39 +09:00
common With GB18030, prevent SIGSEGV from reading past end of allocation. 2025-05-05 04:52:07 -07:00
fe_utils In fmtIdEnc(), handle failure of enlargePQExpBuffer(). 2025-02-16 12:46:35 -05:00
include Fix trigger transition table capture for MERGE in CTE queries. 2026-01-24 11:30:50 +00:00
interfaces Fix printf format string warning on MinGW. 2025-12-21 21:14:54 +13:00
makefiles Use --strip-unneeded when stripping static libraries with GNU strip. 2023-04-20 18:12:32 -04:00
pl Translation updates 2025-11-10 13:04:09 +01:00
port Fix O_CLOEXEC flag handling in Windows port. 2025-12-10 09:11:19 +13:00
template Use unnamed POSIX semaphores on Cygwin. 2023-01-06 10:33:28 +13:00
test Fix trigger transition table capture for MERGE in CTE queries. 2026-01-24 11:30:50 +00:00
timezone Update time zone data files to tzdata release 2025c. 2026-01-18 14:55:01 -05:00
tools For inplace update, send nontransactional invalidations. 2025-12-16 16:13:55 -08:00
tutorial Pre-beta mechanical code beautification. 2023-05-19 17:24:48 -04:00
.gitignore
DEVELOPERS
Makefile Integrate pg_bsd_indent into our build/test infrastructure. 2023-02-12 12:22:21 -05:00
Makefile.global.in Don't put library-supplied -L/-I switches before user-supplied ones. 2025-07-29 15:17:41 -04:00
Makefile.shlib Stop using "-multiply_defined suppress" on macOS. 2023-09-26 21:06:21 -04:00
meson.build Integrate pg_bsd_indent into our build/test infrastructure. 2023-02-12 12:22:21 -05:00
nls-global.mk Fix update-po for the PGXS case 2025-10-16 20:21:05 +02:00