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

Started by Meng Zhangabout 3 hours ago2 messages
#1Meng Zhang
mza117jc@gmail.com
1 attachment(s)

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.

Attachments:

0001-Fix-a-reference-error-for-window-functions.patchapplication/octet-stream; name=0001-Fix-a-reference-error-for-window-functions.patchDownload
From 041a75ba1559e220f43e0bda2c85494ec0d84844 Mon Sep 17 00:00:00 2001
From: Meng Zhang <mza117jc@gmail.com>
Date: Sun, 25 Jan 2026 09:30:16 +0800
Subject: [PATCH] Fix a reference error for window functions

The duplicates window function would be eliminated in
'find_window_functions'. This 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.
---
 src/backend/optimizer/util/clauses.c | 11 ++++-------
 src/test/regress/expected/window.out | 17 +++++++++++++++++
 src/test/regress/sql/window.sql      | 10 ++++++++++
 3 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c
index 39d35827c3..32204776c4 100644
--- a/src/backend/optimizer/util/clauses.c
+++ b/src/backend/optimizer/util/clauses.c
@@ -261,13 +261,10 @@ find_window_functions_walker(Node *node, WindowFuncLists *lists)
 		if (wfunc->winref > lists->maxWinRef)
 			elog(ERROR, "WindowFunc contains out-of-range winref %u",
 				 wfunc->winref);
-		/* eliminate duplicates, so that we avoid repeated computation */
-		if (!list_member(lists->windowFuncs[wfunc->winref], wfunc))
-		{
-			lists->windowFuncs[wfunc->winref] =
-				lappend(lists->windowFuncs[wfunc->winref], wfunc);
-			lists->numWindowFuncs++;
-		}
+
+		lists->windowFuncs[wfunc->winref] =
+			lappend(lists->windowFuncs[wfunc->winref], wfunc);
+		lists->numWindowFuncs++;
 
 		/*
 		 * We assume that the parser checked that there are no window
diff --git a/src/test/regress/expected/window.out b/src/test/regress/expected/window.out
index 7a04d3a7a9..1856a0eeb1 100644
--- a/src/test/regress/expected/window.out
+++ b/src/test/regress/expected/window.out
@@ -5876,3 +5876,20 @@ WINDOW w AS (ORDER BY x ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING);
 --cleanup
 DROP TABLE planets CASCADE;
 NOTICE:  drop cascades to view planets_view
+CREATE TEMPORARY TABLE optimize_window(a int, b int);
+INSERT INTO optimize_window values(1,1),(1,2),(2,1),(2,2);
+SELECT ow.a,
+       ROW_NUMBER() OVER window1,
+        ROW_NUMBER() OVER window2,
+        ROW_NUMBER() OVER window2
+FROM optimize_window ow
+WINDOW window1 as (order by ow.b),
+       window2 as (order by ow.b);
+ a | row_number | row_number | row_number 
+---+------------+------------+------------
+ 1 |          1 |          1 |          1
+ 2 |          2 |          2 |          2
+ 1 |          3 |          3 |          3
+ 2 |          4 |          4 |          4
+(4 rows)
+
diff --git a/src/test/regress/sql/window.sql b/src/test/regress/sql/window.sql
index 37d837a2f6..b82965a3a3 100644
--- a/src/test/regress/sql/window.sql
+++ b/src/test/regress/sql/window.sql
@@ -2133,3 +2133,13 @@ WINDOW w AS (ORDER BY x ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING);
 
 --cleanup
 DROP TABLE planets CASCADE;
+
+CREATE TEMPORARY TABLE optimize_window(a int, b int);
+INSERT INTO optimize_window values(1,1),(1,2),(2,1),(2,2);
+SELECT ow.a,
+       ROW_NUMBER() OVER window1,
+        ROW_NUMBER() OVER window2,
+        ROW_NUMBER() OVER window2
+FROM optimize_window ow
+WINDOW window1 as (order by ow.b),
+       window2 as (order by ow.b);
-- 
2.47.1

#2David Rowley
dgrowleyml@gmail.com
In reply to: Meng Zhang (#1)
Re: Fix a reference error for window functions: In the function 'find_window_functions', the deduplication logic should be removed

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