From 72c0dd0ea78155daa348845fcd74b9102a404056 Mon Sep 17 00:00:00 2001
From: Kwangwon Seo <anchovyseo@gmail.com>
Date: Fri, 10 Jul 2026 16:26:33 +0900
Subject: [PATCH v1] Fix quotation logic for unreserved keywords in window
 specifications

---
 src/backend/utils/adt/ruleutils.c    | 30 +++++++++++++++++++++++++++-
 src/test/regress/expected/window.out | 12 +++++++++++
 src/test/regress/sql/window.sql      |  7 +++++++
 3 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 819631781c0..6084b48f435 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -450,6 +450,7 @@ static void get_rule_orderby(List *orderList, List *targetList,
 static void get_rule_windowclause(Query *query, deparse_context *context);
 static void get_rule_windowspec(WindowClause *wc, List *targetList,
 								deparse_context *context);
+static void appendWindowRefName(StringInfo buf, const char *refname);
 static void get_window_frame_options(int frameOptions,
 									 Node *startOffset, Node *endOffset,
 									 deparse_context *context);
@@ -7159,7 +7160,7 @@ get_rule_windowspec(WindowClause *wc, List *targetList,
 	appendStringInfoChar(buf, '(');
 	if (wc->refname)
 	{
-		appendStringInfoString(buf, quote_identifier(wc->refname));
+		appendWindowRefName(buf, wc->refname);
 		needspace = true;
 	}
 	/* partition clauses are always inherited, so only print if no refname */
@@ -7201,6 +7202,33 @@ get_rule_windowspec(WindowClause *wc, List *targetList,
 	appendStringInfoChar(buf, ')');
 }
 
+/*
+ * Emit the name of the window definition.
+ *
+ * PARTITION, RANGE, ROWS, and GROUPS have the same precedence as IDENT
+ * at the start of a window specification, preventing them from being
+ * recognized as an existing_window_name (see opt_existing_window_name
+ * in gram.y). Since these are unreserved keywords, quote_identifier()
+ * does not quote them, causing the generated SQL to fail when reparsed.
+ * Therefore, quote any keyword here rather than maintaining a list.
+ */
+static void
+appendWindowRefName(StringInfo buf, const char *refname)
+{
+	const char *quoted = quote_identifier(refname);
+
+	if (quoted == refname &&
+		ScanKeywordLookup(refname, &ScanKeywords) >= 0)
+	{
+		/* quote_identifier() left it bare, so it needs no escaping */
+		appendStringInfoChar(buf, '"');
+		appendStringInfoString(buf, refname);
+		appendStringInfoChar(buf, '"');
+	}
+	else
+		appendStringInfoString(buf, quoted);
+}
+
 /*
  * Append the description of a window's framing options to context->buf
  */
diff --git a/src/test/regress/expected/window.out b/src/test/regress/expected/window.out
index c0bde1c5eec..5080b415e0b 100644
--- a/src/test/regress/expected/window.out
+++ b/src/test/regress/expected/window.out
@@ -1361,6 +1361,18 @@ SELECT pg_get_viewdef('v_window');
     FROM generate_series(now(), (now() + '@ 100 days'::interval), '@ 1 hour'::interval) i(i);
 (1 row)
 
+-- A window name that is an unreserved keyword cannot be an existing_window_name
+CREATE TEMP VIEW v2_window_unreserved_kw AS
+	SELECT count(*) OVER w2 FROM generate_series(1, 1) s(v)
+  WINDOW "rows" AS (PARTITION BY v), w2 AS ("rows" ORDER BY v);
+SELECT pg_get_viewdef('v2_window_unreserved_kw');
+                        pg_get_viewdef                         
+---------------------------------------------------------------
+  SELECT count(*) OVER w2 AS count                            +
+    FROM generate_series(1, 1) s(v)                           +
+   WINDOW rows AS (PARTITION BY v), w2 AS ("rows" ORDER BY v);
+(1 row)
+
 -- test overflow frame specifications
 SELECT sum(unique1) over (rows between current row and 9223372036854775807 following exclude current row),
 	unique1, four
diff --git a/src/test/regress/sql/window.sql b/src/test/regress/sql/window.sql
index 8e6f92d94c7..396b95d0d39 100644
--- a/src/test/regress/sql/window.sql
+++ b/src/test/regress/sql/window.sql
@@ -330,6 +330,13 @@ CREATE TEMP VIEW v_window AS
 
 SELECT pg_get_viewdef('v_window');
 
+-- A window name that is an unreserved keyword cannot be an existing_window_name
+CREATE TEMP VIEW v2_window_unreserved_kw AS
+	SELECT count(*) OVER w2 FROM generate_series(1, 1) s(v)
+  WINDOW "rows" AS (PARTITION BY v), w2 AS ("rows" ORDER BY v);
+
+SELECT pg_get_viewdef('v2_window_unreserved_kw');
+
 -- test overflow frame specifications
 SELECT sum(unique1) over (rows between current row and 9223372036854775807 following exclude current row),
 	unique1, four
-- 
2.52.0

