Re: 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: Re: CYCLE over calculates windows functions
Date: 2026-11-25 15:38:38
Message-ID: CALdSSPjMrS=p8qGJURmFx0JKxYfV1jHCu7wTHyFuXYnukJwGvA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, 25 Nov 2026 at 14:13, Kirill Reshke <reshkekirill(at)gmail(dot)com> wrote:

> -- 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)
>

There are more cases with wrong results (all because of subquery-style
query rewrite, all cures with 0001). Sorry for AI slop, tests just
FTWW

1) GROUP BY dst OFFSET 0

-- Each query is independent. cardinality(path) = 3 selects the second
-- recursive step. Only parents whose cycle mark is false may take that step.
-- 1. GROUP BY, no HAVING. Expected one row: 1.
WITH RECURSIVE w(id) AS (
VALUES (1), (2)
UNION ALL
(SELECT dst FROM w JOIN (VALUES (1,1), (2,3), (3,1)) e(src,dst) ON src = id
GROUP BY dst OFFSET 0)
) CYCLE id SET cyc USING path
SELECT id FROM w WHERE cardinality(path) = 3;
id
----
(0 rows)

2. GROUP BY with HAVING. The extra edge to 4 is rejected by HAVING.
-- Expected one row: 1.
WITH RECURSIVE w(id) AS (
VALUES (1), (2)
UNION ALL
(SELECT dst FROM w JOIN (VALUES (1,1), (2,3), (3,1), (2,4))
e(src,dst) ON src = id
GROUP BY dst HAVING dst < 4 OFFSET 0)
) CYCLE id SET cyc USING path
SELECT id FROM w WHERE cardinality(path) = 3;
id
----
(0 rows)

3. DISTINCT. Expected one row: 1.
WITH RECURSIVE w(id) AS (
VALUES (1), (2)
UNION ALL
SELECT DISTINCT dst FROM w JOIN (VALUES (1,1), (2,3), (3,1))
e(src,dst) ON src = id
) CYCLE id SET cyc USING path
SELECT id FROM w WHERE cardinality(path) = 3;
id
----
(0 rows)

4. DISTINCT ON. ORDER BY prefers the marked parent 1 over the live
-- parent 3 if both get to compete for dst = 1. Expected one row: 1.
WITH RECURSIVE w(id) AS (
VALUES (1), (2)
UNION ALL
(SELECT DISTINCT ON (dst) dst
FROM w JOIN (VALUES (1,1), (2,3), (3,1)) e(src,dst) ON src = id
ORDER BY dst, id)
) CYCLE id SET cyc USING path
SELECT id FROM w WHERE cardinality(path) = 3;
id
----
(0 rows)

5. LIMIT. At step 1 the two lowest destinations are 1 (cycle) and 3.
-- Only 3 may continue, via 3 -> 4. Expected one row: 4.
WITH RECURSIVE w(id) AS (
VALUES (1), (2)
UNION ALL
(SELECT dst FROM w JOIN (VALUES (1,1), (1,4), (2,3), (3,4))
e(src,dst) ON src = id
ORDER BY dst, id LIMIT 2)
) CYCLE id SET cyc USING path
SELECT id FROM w WHERE cardinality(path) = 3;
id
----
(0 rows)

6. Window count. Only the live parent 3 contributes to dst = 1.
-- Expected one row: 1.
WITH RECURSIVE w(id,n) AS (
VALUES (1,0::bigint), (2,0::bigint)
UNION ALL
SELECT dst, count(*) OVER (PARTITION BY dst)
FROM w JOIN (VALUES (1,1), (2,3), (3,1)) e(src,dst) ON src = id
) CYCLE id SET cyc USING path
SELECT n FROM w WHERE cardinality(path) = 3;
n
---
2
(1 row)

--
Best regards,
Kirill Reshke

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Ivan Alekseev 2026-11-25 15:44:03 [PATCH] Allow NOTIFY without cross-transaction commit ordering
Previous Message Andrey Rachitskiy 2026-11-25 15:38:04 Fix OOM when rendering large geometry arrays