Re: Fix a reference error for window functions: In the function 'find_window_functions', the deduplication logic should be removed

Tender Wang <tndrwang@gmail.com>

From: Tender Wang <tndrwang@gmail.com>
To: David Rowley <dgrowleyml@gmail.com>
Cc: Meng Zhang <mza117jc@gmail.com>, pgsql-hackers@lists.postgresql.org
Date: 2026-01-25T07:20:48Z
Lists: pgsql-hackers
Hi,all

David Rowley <dgrowleyml@gmail.com> 于2026年1月25日周日 15:05写道:
>
> On Sun, 25 Jan 2026 at 17:09, Meng Zhang <mza117jc@gmail.com> wrote:
> > The deduplication logic won't cause an error when the result of this function is only used in `select_active_windows`.
> > But when the result is used in `optimize_window_clauses`, it will cause the `winref` field of a certain window function to not be modified in the new window.
>
> Thanks for the report. I'll have a look.
>
> David
>
>
I did some analysis, and I found this issue was introduced by this
commit ed1a88d:
Allow window functions to adjust their frameOptions

In optimize_window_clauses(), the list wc->winref = 2 was set to NIL,
so "window 2" would not be in the activewindow lists.
We only have one windowagg node, in ExecEndWindowAgg(), when we
process the second "ROW_NUMBER() OVER window2"
which its winref was still 2 because in
find_window_functions_walker(), it was skipped due to duplicates.

The attached patch seems workable. But I have one question.  If we
implement the attached patch, would it introduce redundant
computation?
Maybe optimize_window_clauses() would optimize the repeated
computation. I don't understand this much.

-- 
Thanks,
Tender Wang



Commits

  1. Fix possible issue of a WindowFunc being in the wrong WindowClause

  2. Remove deduplication logic from find_window_functions

  3. Allow window functions to adjust their frameOptions