[PATCH] Fix quotation logic for unreserved keywords in window specifications

From: Kwangwon Seo <anchovyseo(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: [PATCH] Fix quotation logic for unreserved keywords in window specifications
Date: 2026-07-10 09:28:23
Message-ID: CAHJxwBWx_v=aWp7ZrGRFw2r_7MJdxYX2um3ZOmxTg4c_tL1qLA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,

I was testing views with pg_dump/pg_restore and found a case where an
unreserved keyword is not quoted correctly.

example:

-- Just a base table
CREATE TABLE t (v int);
INSERT INTO t VALUES (1), (2);

-- Create a view
CREATE VIEW v AS
SELECT count(*) OVER w2 FROM t
WINDOW "rows" AS (PARTITION BY v), w2 AS ("rows" ORDER BY v);

But the problem is, here, that quotation is gone.

-- Problem: Quotation is disappared
SELECT pg_get_viewdef('v');
pg_get_viewdef
-------------------------------------------------------------
SELECT count(*) OVER w2 AS count +
FROM t +
WINDOW rows AS (PARTITION BY v), w2 AS (rows ORDER BY v);
(1 row)

-- If you use this for recovering the view above, You'll face this error.
SELECT count(*) OVER w2 AS count
FROM t
WINDOW rows AS (PARTITION BY v), w2 AS (rows ORDER BY v);
ERROR: syntax error at or near "ORDER"
LINE 3: WINDOW rows AS (PARTITION BY v), w2 AS (rows ORDER BY v);

This breaks pg_dump/restore: the view is lost.
As a result, pg_dump/pg_restore cannot restore the view successfully.

Fortunately, I found this comment in the gram.y:

It says:

* If we see PARTITION, RANGE, ROWS or GROUPS as the first token after the
'('
* of a window_specification, we want the assumption to be that there is
* no existing_window_name; but those keywords are unreserved and so could
* be ColIds.

I think that assumption can be broken when inheritance is used
across multiple window definitions.
I mean this: w2 AS ("rows" ORDER BY v)

Some comments for patch file:

The attached patch quotes the window name when it is a keyword
in get_rule_windowspec().
Output for ordinary window names is unchanged. The window definition itself
is a ColId so no change is needed there; only the refname is quoted.

Maybe this is a known thing on the team's side, but I think, at least,
should not break
compatibility with current tools. That is the motivation of this patch...

I'd appreciate any feedback or suggestions...!!

Best regards...

Attachment Content-Type Size
v1-0001-Fix-quotation-logic-for-unreserved-keywords-in-wi.patch text/x-patch 4.5 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2026-07-10 09:51:51 Re: PSQL - prevent describe listing tables that are already in listed schemas
Previous Message Laurenz Albe 2026-07-10 09:26:58 Re: COALESCE patch