Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

WITH RECURSIVE

WITH RECURSIVE defines queries with an anchor term and a recursive term that consumes the previous round’s frontier:

WITH RECURSIVE nums(n) AS (
    VALUES (1)
    UNION ALL
    SELECT n + 1 FROM nums WHERE n < 3
)
SELECT n FROM nums ORDER BY n;

UNION ALL keeps every row. UNION removes already-seen keys and converges on cyclic graphs. max_recursive_iterations limits recursive rounds; the default is 10000.

EXPLAIN ANALYZE reports iterations, duplicates, spills, and memory counters. CYCLE ... USING produces a native array path. The current subset supports UNION [ALL], independent CTEs, and initial mutual recursion support; window features are outside this subset.