CYCLE over calculates windows functions

From: Kirill Reshke <reshkekirill(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: CYCLE over calculates windows functions
Date: 2026-11-25 09:13:24
Message-ID: CALdSSPgtV22NRqW-jJ8vq3r56_O_5+rpWuae-7bPXWJ7Z-LXeg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

So, I was reviewing two recent bugs reports 19922/19923 [0] & [1]
and fix from [2]. And I learned how CTE with CYCLE clause is
implemented in PostgreSQL: it goes thru rewrite subsystem to make
classing CTE for cycle detection, just like doc says [3]. To better
understand how [2] and rewriteSearchAndCycle works I made this example
(from attached 0002 test):

create temp table cycle_window_edge (src int, dst int);
insert into cycle_window_edge values (1, 2), (2, 1), (2, 3), (3, 4);

with recursive walk(id, depth) as (

values (1, 0::bigint)
union all
select e.dst, w.depth + 1
from walk w join cycle_window_edge e on e.src = w.id

) cycle id set is_cycle using path
select depth, id, is_cycle from walk order by depth, id;

On master, rewriteSearchAndCycle will rewrite this to be efficiently
this (internally it will be AST not SQL, so SQL is just for
explanation)

WITH RECURSIVE walk(id, depth, n, is_cycle, path) AS (
VALUES (1, 0, 0::bigint, false, ARRAY[1])

UNION ALL

SELECT c.id, c.depth, c.n, c.is_cycle, c.path
FROM (
SELECT e.dst AS id,
w.depth + 1 AS depth,
count(*) OVER () AS n,
e.dst = ANY(w.path) AS is_cycle,
w.path || e.dst AS path,
w.is_cycle AS parent_is_cycle
FROM walk w
JOIN cycle_window_edge e ON e.src = w.id
) AS c
WHERE c.parent_is_cycle <> true
)
SELECT depth, id, n, is_cycle
FROM walk
ORDER BY depth, id;

-- not correct result (last row n == 2, counted edge 1->2 and than
filtering out)

depth | id | n | is_cycle
-------+----+---+----------
0 | 1 | 0 | f
1 | 2 | 1 | f
2 | 1 | 2 | t
2 | 3 | 2 | f
3 | 4 | 2 | f
(5 rows)

So, our recursive step goes into subquery and we then do outer
filtering by parent_is_cycle var.
But turns out, this is not always correct rewriting, cause we first
collect all rows from subquery and only then filter. If we use window
function in recursive step, parent_is_cycle=true rows will be counted
in the window function column for surviving rows.

So, dont make subquery?

After 0001, rewrite would be like ths

WITH RECURSIVE walk(id, depth, n, is_cycle, path) AS (
VALUES (1, 0, 0::bigint, false, ARRAY[1])

UNION ALL

SELECT e.dst,
w.depth + 1,
count(*) OVER () AS n,
e.dst = ANY(w.path) AS is_cycle,
w.path || e.dst AS path
FROM walk w
JOIN cycle_window_edge e ON e.src = w.id
WHERE w.is_cycle <> true
)
SELECT depth, id, n, is_cycle
FROM walk
ORDER BY depth, id;

-- correct result

depth | id | n | is_cycle
-------+----+---+----------
0 | 1 | 0 | f
1 | 2 | 1 | f
2 | 1 | 2 | t
2 | 3 | 2 | f
3 | 4 | 1 | f
(5 rows)

which will preserve window functions semantic

[0]https://www.postgresql.org/message-id/19922-a0d338245d361a4c@postgresql.org
[1] https://www.postgresql.org/message-id/19923-24cef1e958fa2370@postgresql.org
[2] https://github.com/postgres/postgres/commit/d27418375d64663c920d81b0814d389a33d621db
[3] https://www.postgresql.org/docs/current/queries-with.html

--
Best regards,
Kirill Reshke

Attachment Content-Type Size
0001-Fix-CYCLE-filtering-before-window-functions.patch application/octet-stream 2.0 KB
0002-Test-CYCLE-filtering-before-window-functions.patch application/octet-stream 2.2 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Álvaro Herrera 2026-11-25 09:40:34 Re: Adjust error message to reflect the inheritance relationship
Previous Message Daniel Gustafsson 2026-11-25 08:44:06 Re: [PATCH] Support casts from numeric to oid8