Sub topic 3
How the Database Evaluates a Recursive CTE
Prerequisites: You should know SELECT, WHERE, JOIN, UNION ALL, and common table expressions (CTEs). A recursive CTE is a CTE that refers to its own result while the query runs.
The two parts of a recursive CTE
A recursive CTE has two query parts:
- The anchor member runs first. It chooses the starting rows.
- The recursive member runs repeatedly. It uses rows found in the previous iteration to find more rows.
These parts are joined with UNION ALL:
WITH RECURSIVE numbers AS (
-- Anchor member
SELECT 1 AS value
UNION ALL
-- Recursive member
SELECT value + 1
FROM numbers
WHERE value < 4
)
SELECT value
FROM numbers;The result is:
| value |
|---|
| 1 |
| 2 |
| 3 |
| 4 |
The column names and data types from the anchor must be compatible with the recursive member. Here, both queries return one integer column named value.
The working table model
The database evaluates the query in iterations. It maintains a temporary working table, which contains the rows produced by the most recent iteration.
Conceptually, the process is:
- Run the anchor member.
- Put its rows into the final result and the working table.
- Run the recursive member using the current working table.
- Put the new rows into the final result and a new working table.
- Replace the old working table with the new one.
- Repeat until the recursive member returns no rows.
For the number example, the working table changes like this:
| Iteration | Working rows | New rows |
|---|---|---|
| 0 | none | `1` |
| 1 | `1` | `2` |
| 2 | `2` | `3` |
| 3 | `3` | `4` |
| 4 | `4` | none |
The final SELECT reads all rows accumulated across the iterations. The recursive member does not normally see every row produced so far. It sees the rows from the current working table.
Why termination matters
The recursive member must eventually return no rows. A condition such as WHERE value < 4 provides that stopping rule. Without it, the query may keep producing rows until the database reaches a recursion limit or cancels the statement.
A recursive query can also stop when a join finds no matching child rows. This is common when walking a hierarchy: once a manager has no employees below them, the next iteration is empty.
`UNION ALL` and iteration behavior
UNION ALL keeps every row produced by each iteration, including duplicates. This is usually the clearest choice because it preserves the recursive process and avoids the cost of duplicate removal.
Some databases allow UNION instead. UNION removes duplicate rows, which can help a graph traversal stop when it revisits the same complete row. However, duplicate removal depends on the entire row, so it may not prevent cycles when the row includes a changing depth or path column.
The exact execution strategy is database-specific. The working-table model is a reliable way to reason about results, but the database may optimize the physical operations. Do not depend on the natural output order unless you add an outer ORDER BY.
Exercise
In the number query, what happens if the condition changes from value < 4 to value < 0?
Show answer
The anchor still produces 1, but the recursive member produces no rows because 1 < 0 is false. The final result contains only 1.