What if you could find every connection in a network with just one simple query?
Why Recursive CTE for graph traversal in PostgreSQL? - Purpose & Use Cases
Imagine you have a big family tree or a network of friends, and you want to find all the connections starting from one person. Doing this by hand means checking each person one by one, writing down who they know, then checking those people, and so on.
This manual way is slow and confusing. You might miss some connections or repeat checking the same person many times. It's easy to get lost or make mistakes, especially when the network is large or has loops.
Recursive CTEs let the database do this work for you automatically. You write a simple query that tells the database how to find direct connections, and then it repeats the process to find connections of connections, and so on, until it finds all related nodes.
SELECT * FROM connections WHERE person = 'Alice'; -- then manually repeat for each found person
WITH RECURSIVE network AS ( SELECT person, friend, ARRAY[person, friend] AS path FROM connections WHERE person = 'Alice' UNION ALL SELECT c.person, c.friend, n.path || c.friend FROM connections c JOIN network n ON c.person = n.friend WHERE c.friend <> ALL(n.path) ) SELECT person, friend FROM network;This lets you explore complex networks easily, like tracing all friends of friends or all parts of a family tree, with just one clear query.
For example, a social media app can use recursive CTEs to find all friends of a user up to several levels deep, helping suggest new friends or show connection paths.
Manual graph traversal is slow and error-prone.
Recursive CTEs automate repeated searching in connected data.
They make exploring networks simple and reliable with one query.
Practice
WITH RECURSIVE clause in PostgreSQL when working with graph data?Solution
Step 1: Understand the purpose of recursive CTEs
Recursive CTEs allow a query to call itself repeatedly, which is useful for exploring connected data like graph nodes.Step 2: Identify the correct use case
UsingWITH RECURSIVElets you traverse graph structures step-by-step by referencing the CTE inside itself.Final Answer:
To repeatedly execute a query that references itself to explore connected nodes -> Option CQuick Check:
Recursive CTE = self-referencing query [OK]
- Confusing recursive CTE with temporary tables
- Thinking it automatically optimizes performance
- Using it for unrelated table joins
path in PostgreSQL?Solution
Step 1: Recall the syntax for recursive CTEs
The correct syntax starts withWITH RECURSIVE cte_name AS ( ... ), where the CTE references itself inside.Step 2: Match the syntax options
WITH RECURSIVE path AS (SELECT ... UNION ALL SELECT ... FROM path ...) correctly usesWITH RECURSIVE path AS (SELECT ... UNION ALL SELECT ... FROM path ...). Other options misuse keywords or order.Final Answer:
WITH RECURSIVE path AS (SELECT ... UNION ALL SELECT ... FROM path ...) -> Option DQuick Check:
WITH RECURSIVE + CTE name + self-reference [OK]
- Swapping RECURSIVE and CTE name order
- Using CREATE instead of WITH
- Omitting UNION ALL between parts
edges(source INT, target INT) with data:(1, 2), (2, 3), (3, 4), (2, 5)What will the following query return?
WITH RECURSIVE path AS (SELECT source, target FROM edges WHERE source = 1 UNION ALL SELECT e.source, e.target FROM edges e JOIN path p ON e.source = p.target) SELECT * FROM path ORDER BY source, target;
Solution
Step 1: Identify the anchor and recursive parts
The anchor selects edges where source=1: (1,2). The recursive part joins edges where source matches previous target.Step 2: Trace recursive expansion
From (1,2), next edges with source=2 are (2,3) and (2,5). From (2,3), next edge with source=3 is (3,4). No further edges from 5 or 4.Final Answer:
Rows: (1,2), (2,3), (2,5), (3,4) -> Option AQuick Check:
Recursive join expands all reachable edges [OK]
- Ignoring multiple edges from same source
- Stopping recursion too early
- Misordering results
WITH RECURSIVE path(node) AS (SELECT 1 UNION ALL SELECT node + 1 FROM path WHERE node < 3) SELECT * FROM path;
What is wrong if this query runs forever without stopping?
Solution
Step 1: Analyze the recursive condition
The recursive part adds 1 to node while node < 3, but due to UNION ALL accumulating duplicates of rows satisfying the condition, recursion continues indefinitely.Step 2: Identify why it loops infinitely
If the termination condition is incorrect or not reached, recursion never stops. Here, the condition is present but ineffective against duplicates, causing infinite loop.Final Answer:
Missing a proper termination condition in the recursive part -> Option BQuick Check:
Infinite recursion = no stop condition [OK]
- Confusing UNION and UNION ALL effects
- Ignoring anchor query correctness
- Forgetting to select from CTE
employees(id INT, manager_id INT) representing a company hierarchy. How would you write a recursive CTE to find all employees under manager with id = 10, including indirect reports?Solution
Step 1: Identify the anchor query
The anchor selects employees directly reporting to manager 10:WHERE manager_id = 10.Step 2: Understand the recursive join
The recursive part joins employees whosemanager_idmatches any previously found employeeid, thus finding indirect reports.Final Answer:
WITH RECURSIVE subordinates AS ( SELECT id FROM employees WHERE manager_id = 10 UNION ALL SELECT e.id FROM employees e JOIN subordinates s ON e.manager_id = s.id ) SELECT * FROM subordinates; -> Option AQuick Check:
Join on e.manager_id = s.id to find subordinates [OK]
- Swapping join conditions (id vs manager_id)
- Starting anchor with wrong WHERE clause
- Selecting wrong columns in recursive part
Ready to prove it?
Pass the test and this pattern moves unknown
