Skip to main content

πŸ—“οΈ 31082025 1113
πŸ“Ž

sql_recursion

How Recursive CTEs Work​

Recursive CTEs (WITH RECURSIVE) are commonly used for hierarchies and graphs.
Think of them like a controlled loop.

StepWhat happensExample in concept
Seed / AnchorStart with a base set of rows.SELECT id, parent_id FROM table_edges WHERE depth=0
Recursive stepJoin previous results back to the table to get next-level rows.JOIN table_edges e ON e.id = a.parent_id
TerminationStops when no new rows are returned.Implicit
MaterializationThe engine stores all β€œvisited” rows in a working table to avoid infinite loops.Stored in memory or spilled to disk if big

Why Memory Usage Spikes​

Recursive CTEs can be expensive because each iteration accumulates rows.
Memory pressure builds up due to these factors:

CauseWhy it’s heavyExample scenario
Large recursion frontierEach iteration multiplies rows.Wide org chart, many ancestors.
Wide carried rowsMore columns = larger working table.Carrying name, description, group_name inside recursion unnecessarily.
Early ORDER BYSorting intermediate results per iteration is expensive.Sorting inside the recursion instead of at the end.
Late filteringInclude irrelevant rows β†’ filter later.Join everything, then WHERE group_name IS NOT NULL.
No deduplicationSame (id, parent_id) processed multiple times.No DISTINCT until very late.
Missing indexesEach recursive step does a full table scan.No index on id or parent_id.

References