Sub topic 12
What the Recursive Member May Not Do
Assume you know SELECT, joins, grouping, and ordinary common table expressions (CTEs). A recursive CTE has two parts:
- The anchor member produces the first rows.
- The recursive member reads rows already found and produces the next rows.
The recursive member is deliberately restricted. These limits keep each iteration predictable and prevent one step from needing the entire unfinished result.
The recursive member's job
A recursive member should perform one expansion step. It can usually:
- Join the recursive CTE to an ordinary table.
- Filter rows with
WHERE. - Calculate new column values.
- Increase a depth counter.
- Append a value to a path.
For example, this query generates values from 1 through 5:
WITH RECURSIVE walk(node, depth) AS (
SELECT 1, 0
UNION ALL
SELECT node + 1, depth + 1
FROM walk
WHERE node < 5
)
SELECT node, depth
FROM walk
ORDER BY node;The recursive member reads one iteration of walk, creates the next iteration, and stops when node < 5 is false.
Common restrictions
Exact rules vary by database, but these restrictions are common:
| Restricted operation | Why it causes trouble | Usual alternative |
|---|---|---|
| Aggregate functions such as `SUM` | The current result is incomplete | Aggregate after recursion |
| Window functions | They need a defined complete window | Calculate after recursion |
| `GROUP BY` | Group contents change between iterations | Group in the outer query |
| Multiple references to the recursive CTE | The engine cannot define one clear working set | Join once, or split the work |
| A recursive reference inside a subquery | Iteration boundaries become ambiguous | Join directly in the recursive member |
| Some outer joins | Null-preserving behavior can conflict with expansion | Use an inner join or restructure the query |
| `ORDER BY`, `DISTINCT`, or row limits | Many engines cannot apply them per iteration | Apply them to the final result |
A recursive member may join employees to the current hierarchy, but it generally cannot calculate a complete COUNT(*) for that hierarchy during the same recursive step.
Aggregate after recursion
Suppose each recursive row represents one quantity used in a bill of materials. Let recursion produce all component uses first. Then aggregate those rows in the outer query:
WITH RECURSIVE parts(component_id, quantity) AS (
SELECT 10, 2
UNION ALL
SELECT component_id + 1, quantity * 2
FROM parts
WHERE component_id < 12
)
SELECT component_id, SUM(quantity) AS total_quantity
FROM parts
GROUP BY component_id
ORDER BY component_id;The recursive member only expands rows and calculates quantities. SUM and GROUP BY run after recursion has finished.
If you need a running value, carry it as a regular column instead of using a window function. For example, carry path_cost + item_cost through each iteration. This computes a value for each path, rather than trying to rank or summarize an incomplete set.
Why these limits matter
The database evaluates a recursive CTE in iterations. It stores rows from the previous iteration in a working table, runs the recursive member, and stores the newly produced rows for the next iteration.
An aggregate over the final result is well-defined. An aggregate inside the recursive member may depend on rows that have not been generated yet. Restrictions prevent that ambiguity.
Practical rule
Keep the recursive member narrow:
- Read the current row set.
- Join to find the next rows.
- Compute state such as depth, quantity, or path.
- Stop with a clear condition.
- Filter, sort, or aggregate the completed result outside the recursive CTE.
Exercise
Where should you place SUM, GROUP BY, and ORDER BY when a database rejects them in the recursive member?
Show answer
Place them in the outer query that reads the completed recursive CTE. Keep the recursive member focused on expanding rows and carrying state.