SQLite in 2014: reading cyclic dependencies with a recursive CTE
Explore the WITH RECURSIVE capability introduced in SQLite 3.8.3 using a small dependency graph with a cycle. The example checks deduplication, root selection and the expected result.
Archive retrospective · Topic year 2014
In this article
Connect the 2014 feature to one question
SQLite 3.8.3 added common table expressions and WITH support on February 3, 2014. This December 2026 retrospective explains that milestone through a small dependency graph; it does not pretend to be a contemporary 2014 publication. The question is which components can be reached from catalog. Real dependencies need not form a tree: a node can be referenced from several places, and a cycle can lead back to an earlier node. We want each reachable node once. We are not counting paths, finding shortest distances or computing a deployment order. Defining that output determines what the recursive expression should carry. In this example, carrying only the node identifier is the essential choice.
Put the cycle into the fixture
The fixture contains six nodes. Catalog points to backend and ui. Backend points to api, api to auth, and auth back to backend. That last edge deliberately creates a cycle. The isolated node has no connection to this group, so it must remain outside the result when catalog is the root. Keeping edges in a separate table permits several outgoing dependencies. A composite primary key prevents duplicate edges, but does not prevent cycles. Foreign keys reject references to nonexistent nodes; they do not establish that the graph is acyclic. These are different rules. The setup below belongs in a temporary example database and requires no changes to production tables or real user records.
PRAGMA foreign_keys = ON;
CREATE TABLE nodes(id INTEGER PRIMARY KEY, label TEXT NOT NULL);
CREATE TABLE edges(
src INTEGER NOT NULL REFERENCES nodes(id),
dst INTEGER NOT NULL REFERENCES nodes(id),
PRIMARY KEY(src, dst)
);
INSERT INTO nodes VALUES
(1,'catalog'),(2,'backend'),(3,'api'),
(4,'auth'),(5,'ui'),(6,'isolated');
INSERT INTO edges VALUES (1,2),(1,5),(2,3),(3,4),(4,2);
Deduplication depends on the complete row
The initial SELECT requires the supplied root to exist in nodes. The recursive part follows outgoing edges. UNION prevents an identical identifier from entering the working queue again, so returning from auth to backend does not expand backend indefinitely. Equality concerns the complete row. Adding an increasing depth column would make the same identifier at different depths a different row, invalidating this termination argument. The example deliberately carries no depth. Replacing UNION with UNION ALL would preserve repetitions and allow this cyclic graph to keep generating nodes. If the requirement were to report every path instead, visited-node tracking and explicit bounds would need a separate design. Choose the result before choosing the operator.
WITH RECURSIVE reachable(id) AS (
SELECT id FROM nodes WHERE id = :root
UNION
SELECT e.dst
FROM edges e JOIN reachable r ON e.src = r.id
)
SELECT n.id, n.label
FROM reachable r JOIN nodes n ON n.id = r.id
ORDER BY n.id;
Check outputs that catch different mistakes
For root 1, the expected identifiers are 1, 2, 3, 4 and 5. The final ORDER BY sorts the displayed result; it does not claim that traversal happened in that order. Node 6 must be absent. Root 99 should return no rows because the initial SELECT finds no node. Root 6 should return just the isolated node, since the root itself counts as reachable. These cases check cycle termination, root validation and a valid disconnected root separately. We ran them locally using the current SQLite engine available on the computer. We did not execute the 2014 binary; the release record establishes when the feature appeared.
-- :root = 1 -> 1 catalog, 2 backend, 3 api, 4 auth, 5 ui
-- :root = 99 -> no rows
-- :root = 6 -> 6 isolated
Reachability does not establish startup order
The resulting set cannot tell an application which component to start first. Our fixture contains a cycle, so sorting identifiers cannot resolve the dependency problem. Deployment planning would require a separate treatment that reports cycles. Nor can this output reveal how many routes reach a node: it intentionally merges that information. A product label should respect the distinction. Reachable components describes this result, while safe deployment plan promises more than it computes. Error states also deserve separation. An empty set from an unknown root is different from a query that timed out. Rendering both as an ordinary empty list could make incomplete work look like a verified absence of data.
Revisit the tradeoffs at a larger scale
Correctness on six nodes does not establish acceptable performance on millions of edges. The composite key begins with src, matching the adjacency lookup, but the actual query plan and cost still need evaluation on representative data. UNION retains information needed to recognize repeated results. That cost supports termination here and belongs in a larger memory and latency assessment. A product may not need to draw the entire reachable graph immediately; a clearly bounded view or background operation might fit the requirement better. These are design options, not benchmark findings from this article. The useful outcome is an explainable stopping condition based on the chosen identity, supported by an original fixture and diagram. Another design question concerns the version of the graph being read. If edges change between requests, a later result may differ without any defect in cycle handling. An audit that needs reproducibility would need to identify the edge snapshot. Labels may also change while identifiers remain stable. Carrying only the identifier keeps a display-name change separate from the identity used for deduplication. Our fixed fixture does not measure concurrent modifications.
Reader discussion
Which bottleneck would you expect as this solution scales?
No comments yet. Be the first to contribute.
Write a comment