Re: CYCLE over calculates windows functions

From: Kirill Reshke <reshkekirill(at)gmail(dot)com>
To: Andrey Borodin <x4mmm(at)yandex-team(dot)ru>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: CYCLE over calculates windows functions
Date: 2026-11-28 11:20:28
Message-ID: CALdSSPhu_vPMBjhrmVEFENdwYiDY7wavNypEjRpCwBJy+UU=QQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, 27 Nov 2026 at 13:55, Andrey Borodin <x4mmm(at)yandex-team(dot)ru> wrote:
>
> Hi Kirill,
>
> On 25 Nov 2026, Kirill Reshke wrote:
> > There are more cases with wrong results (all because of subquery-style
> > query rewrite, all cures with 0001).
>
> The SQL draft I checked specifies this subquery rewrite: 7.18
> "<search or cycle clause>", Syntax Rule 8, puts the cycle-mark filter
> in the outer WHERE, as our current implementation does.

OK, should we follow this? Can we rewrite the query this way
preserving window func semantics?

> Also, 7.17 "<query expression>", SR 3)j)ix)5)C)II, prohibits window
> functions in the recursive SELECT. Supporting them is our extension,
> so the standard does not prescribe the window results in your example.

(§7.17, Syntax Rule 5(c)(II)) prohibits windows functions in WITH
RECURSIVE even without CYCLE (at least from my reading). Also here was
previous diction about similar problems [0]. At time, postgres didn't
forbid this case just because SQL standard says its disallowed.

....

Another issue found while luring SQL draft copy

WITH RECURSIVE t(n) AS (
VALUES (1)
UNION ALL
SELECT n + 1 FROM t WHERE n < 2
)
CYCLE n SET mark TO TRUE DEFAULT TRUE USING path
SELECT n, mark FROM t;

§7.18, Syntax Rule 6(c): ..CMV and CMD shall be literals and CMV shall
not be equal to CMD... - but postgres allows TO TRUE DEFAULT TRUE. We
can add this here or fix [1]

[0] https://www.postgresql.org/message-id/17581.1230482868%40sss.pgh.pa.us
[1] https://wiki.postgresql.org/wiki/PostgreSQL_vs_SQL_Standard

--
Best regards,
Kirill Reshke

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Zsolt Parragi 2026-11-28 12:00:26 Re: pg_rewind reports "no rewind required" due to stale minRecoveryPoint
Previous Message Zhijie Hou 2026-11-28 11:18:43 Re: logical decoding: removing change_useless_for_repack()