ChloeDB
Search and SQL

Graphs in ChloeDB

Reference, rendered from docs/chloedb/graphs.md.

These docs describe release 0.3.11. The docs of main describe the code as it is now.

On this page
  1. Rows are nodes
  2. Storing edges
  3. The SQL
  4. Planning and traversal
  5. Speed
  6. Does the graph help search?
  7. Edge tables
  8. On a cluster
  9. Limits and next steps

Rows can link to other rows. A traversal, a shortest path or personalized PageRank runs in the same SQL statement as BM25, filters and vector search, and can start from any of them. On arXiv, the papers cited by a new paper's two nearest neighbours find twice as many of its references as vector search alone. A graph built from the vectors themselves does not help.

Rows are nodes#

Graph databases keep nodes and edges in stores of their own. ChloeDB keeps them in a table: a row is a node, and an EDGES field holds the row's outgoing edges as a JSON array of targets, each a primary key of the same table, optionally with a label and a positive weight.

-- id KEYWORD PRIMARY KEY, title TEXT, body TEXT, year INT, emb VECTOR(512), cites EDGES
INSERT ... ('p1', 'Attention is all you need', ..., 2017, [...],
            '["p7", ["extends", "p9"], ["refutes", "p11", 0.5]]')

Formally, a snapshot S holds rows V with keys κ : V → K, and edges

$$ E \subseteq V \times K \times L \times \mathbb{R}_{>0}, \qquad (u, k, \ell, w):\ \text{row } u \text{ points at key } k \text{ with label } \ell \text{ and weight } w $$

An edge is resolved when a query follows it:

$$ \rho_S(k) = \text{the live row } v \in V \text{ with } \kappa(v) = k, \text{ if there is one} $$

A key has at most one live row in a snapshot, so ρS is a function. When it has none (the target was deleted, or never existed), the edge dangles and traversals skip it, as a foreign key without a constraint would.

Edges belong to their source row and point at keys, not storage positions. They are written, logged, updated, deleted, merged and snapshotted with the row, and nothing else in the engine had to change. A merge rewrites every docID and an update moves a row to a newer segment, but neither changes a key, so no edge is ever rewritten. The price is a lookup per hop, and most of the engineering below goes into making it cheap.

Storing edges#

One segment's graph section: out-edges as a CSR over local docIDs, a reverse index grouped by target key, and same-segment hints

Every segment, on disk or built from the memtable, gets one section per indexed EDGES field, read in place through mmap:

  • Out-edges. A CSR over local docIDs: offsets, then per edge its source, its target key (an i64, or offsets and bytes for string keys), a label code and a weight. Labels and weights are stored only when some edge has one.
  • Reverse index. Edges grouped by target key, the groups ordered by the key's 64-bit hash under the radix directory of the primary-key index, with each edge's source stored in group order. "Which rows point at key k" is one directory read and one key comparison per group, then a sequential read. Integer hashes are a bijection and need no comparison; colliding string keys form separate groups.
  • Same-segment hints. Per edge, the docID its target had in this segment when the segment was built. Since ρS has one answer, a hinted row that is still live is that answer, and the hop needs no lookup.

A hop from u along its edges therefore costs, per edge,

$$ c_{\text{out}} = p_{\text{hint}}\,c_{\text{live}} + (1 - p_{\text{hint}})\Bigl(p_{\text{memo}}\,c_{\text{memo}} + (1 - p_{\text{memo}}) \sum_{i \le j^*} c_{\text{probe},i}\Bigr) $$

where phint is the share of targets built into the same segment and still live, pmemo the share already resolved earlier in the query, and j* the segment where the key is found. A hop against the edges costs one reverse-index probe per segment for u's key, then a liveness check per source. The section takes about

$$ b \approx \underbrace{8}{\text{target}} + \underbrace{4}{\text{source}} + \underbrace{4 + 4}{\text{reverse}} + \underbrace{4}{\text{hint}} + \frac{8\,\lvert V \rvert + 16\,\lvert K_E \rvert}{\lvert E \rvert}\ \text{bytes per edge} $$

with KE the distinct targets. That comes to 27 bytes per edge on arXiv, where the graph is under 5% of the file:

Sizes of the arXiv table: vectors 453.3 MB, doc store 129.4 MB, graph section 31.1 MB at 27.0 bytes per edge, full-text index 20.1 MB, file 639.0 MB

The SQL#

Two predicates, a column and a score join the existing functions. Each takes seeds: a key, a parameter, a tuple of keys, or any subquery over the same table, so a vector search, a BM25 query or a facet filter can be the way into the graph.

-- papers within two citations of p1, either way, nearest first
SELECT id, title, hops() FROM papers
WHERE REACHABLE(cites, 'p1', hops => 2, dir => 'both') ORDER BY hops(), id LIMIT 20;

-- one shortest path, only through surveys
SELECT id, hops() FROM papers
WHERE PATH(cites, 'p1', 'p9', max_hops => 6, through => kind = 'survey') ORDER BY hops();

-- the way in is a vector search: the papers its two best hits cite, by similarity
SELECT id FROM papers
WHERE REACHABLE(cites, (SELECT id FROM papers ORDER BY similarity(emb, :q) DESC LIMIT 2))
ORDER BY similarity(emb, :q) DESC LIMIT 20;

-- personalized PageRank from hybrid hits, fused with both rankings
SELECT id FROM papers ORDER BY rrf(bm25(body, :t), similarity(emb, :q),
  ppr(cites, (SELECT id FROM papers ORDER BY rrf(bm25(body, :t), similarity(emb, :q)) DESC LIMIT 2), weighted => 1)) DESC LIMIT 20;

The functions and their options are listed in the query language reference.

Planning and traversal#

Graph first, then the usual plan: seeds and options feed a traversal over the whole snapshot, whose result becomes an exact document set per segment

A traversal does not respect segment boundaries, so graph computations run first, over the whole snapshot. Seeds are resolved (a seed subquery runs as a query of its own on the same snapshot), and the traversal runs. Its result becomes, per segment, a materialised document set with exact cardinality, plus each row's distance and PageRank score. The planner then sees a graph predicate as a filter whose size it knows exactly: a vector search over a neighbourhood of a few hundred rows becomes an exact scan of those rows.

flowchart LR
    SD["seeds<br/>keys, a tuple or a subquery"] --> TR["traversal over the snapshot<br/>BFS · bidirectional path · PageRank push"]
    TR --> PS["per segment<br/>exact document set<br/>hops() and ppr() by row"]
    PS --> PL["usual plan<br/>probe or materialise filters<br/>vector, BM25, fusion, facets"]

The traversals:

  • Reachability: breadth-first search, frontier by frontier; a search that would meet more than max_nodes rows besides its seeds is an error rather than an answer that depends on the order of the edges.
  • Shortest paths: bidirectional breadth-first search, always expanding the smaller frontier, forward along the edges or backward against them. With through, each side keeps the rows it meets that fail it as dead ends, and the halves meet only at a row that passes it or at an end, so every middle row of the path passes through.
  • Personalized PageRank: forward push (Andersen, Chung and Lang, 2006). The vector π solves

$$ \pi = \alpha\, e_S + (1 - \alpha)\, \pi W, \qquad W_{uv} = \frac{w_{uv}}{\sum_{x} w_{ux}} $$

where eS is the restart distribution over the seeds. Every row has an estimate p and a residual r, with r = eS at the start, and pushing a row u does

$$ p(u) \mathrel{+}= \alpha\, r(u), \qquad r(v) \mathrel{+}= (1 - \alpha)\, r(u)\, W_{uv}\ \ \forall v, \qquad r(u) \leftarrow 0 $$

while some r(u) ≥ ε·deg(u). Each estimate is then within ε·deg(u) of π(u), and the work is bounded by 1/(αε) pushes whatever the graph's size. A row is queued only once its residual reaches ε, the least any threshold can be. Rows that merely received a little mass are never resolved, and resolving them had been most of the original cost.

The estimates add up to the share of the walk that has settled, Σ p; the rest is residuals no push spread. A fixed ε stops the walk at the seeds when their neighbours have many edges, since ε·deg(u) is then far above the residual a neighbour gets: from a user of the tests' user-item graph, whose 30 items have at least 300 edges each, ε = 10⁻⁴ settles the user's 15% and ranks no item. Without eps the walk starts at 10⁻⁴ and, while Σ p < ½, halves ε and pushes on from the residuals it has (an estimate only grows, so the walk at the lower ε continues the one at the higher), for at most 100,000 pushes; on that graph it stops at 3.1·10⁻⁶ with 72% settled, after 9,078 pushes. A walk that ends with Σ p < ½, at a given eps or out of pushes, reports ppr_unsettled in its stats.

Speed#

Two graphs, each queried from 200 random seeds on a warm cache, on one thread of an Apple M2 MacBook Air, through SQL (parsing and planning included; COUNT(*), so that no rows are fetched):

  • arXiv: 168,343 papers (ogbn-arxiv, less 1,000 held out) with 1,148,979 citations, in two segments.
  • Synthetic: a power-law graph of 1,000,000 rows and 9,998,469 edges, in four segments. Row i links to 10 earlier rows, each the target of a random earlier edge with probability ½ and a uniform earlier row otherwise.

The baselines answer the same queries, and the benchmark checks that every count and path length agrees:

  • SQLite: an edge table (src, dst) as a WITHOUT ROWID primary key, an index on (dst, src), and recursive CTEs.
  • CSR: the same traversal code on two in-memory arrays. This is the lower bound: no keys, no segments, no SQL.

Graph query latency on arXiv: ChloeDB, SQLite and an in-memory CSR, p50 and p95, for REACHABLE, PATH, ppr and vector-seeded queries

arXiv, 200 seeds per query. Source: chloedb/results/graph/latency_arxiv.json.

On the million-row graph (chloedb/results/graph/synthetic_1000000.json; loaded in 2.7 s into a 307 MB file, 27 bytes per edge):

query (200 seeds) result ChloeDB p50 / p95 ms SQLite p50 / p95 ms CSR p50 / p95 ms
REACHABLE, 1 hop out 10 rows 0.017 / 0.022 0.012 / 0.013 0.001 / 0.001
REACHABLE, 2 hops out 108 rows 0.032 / 0.045 0.061 / 0.066 0.004 / 0.005
REACHABLE, 3 hops out 890 rows 0.098 / 0.153 0.597 / 0.680 0.030 / 0.037
REACHABLE, 1 hop in 8 rows 0.014 / 0.019 0.010 / 0.017 0.001 / 0.001
REACHABLE, 2 hops both ways 1,586 rows 0.104 / 0.468 0.471 / 3.26 0.019 / 0.110
PATH either way 200 of 200 found 0.079 / 0.145 — 0.025 / 0.071
ppr, ε = 10−4 133 pushes 3.49 / 4.13 — 0.66 / 0.80
ppr, ε = 10−5 1,020 pushes 25.6 / 30.1 — 5.47 / 6.32

Against SQLite, one-hop queries cost about the same, roughly 10–15 µs, mostly parsing and planning on both sides. From two hops on, the gap opens, and the tail is where it shows most: two hops both ways on arXiv touch 3,790 rows on average and up to tens of thousands, and their p95 is 1.6 ms in ChloeDB against 14.8 ms in SQLite. The recursive CTE re-expands rows reached at several depths and joins through a B-tree per row, where ChloeDB reads a CSR slice. Shortest paths and PageRank have no practical CTE form.

Against the CSR, ChloeDB pays two costs. About 10 µs of SQL per query dominates small traversals. Key resolution dominates large ones: PageRank takes 2.4 times the CSR's time on arXiv and 5.3 times on the million-row graph, because a target outside its own segment is a hash probe into each segment's primary-key index until found, a cache miss each. Three changes found by profiling on the Xeon VM that first ran these benchmarks cut PageRank on the million-row graph from 21.4 to 5.8 ms there (3.5 ms on the M2):

  1. reverse groups by key, with the sources stored inline;
  2. same-segment hints and a per-query memo;
  3. queueing only rows that can pass the push threshold.

The rows here are nodes with text, vectors and edges at once. The question the engine cannot answer by itself is whether the edges add anything to a search that already has the text and the vectors, and whether edges made from the vectors (each row linked to its nearest rows) add anything.

The task: for a new paper, find the papers it cites from its title and abstract. This is citation recommendation, and the same shape as finding the records a new question depends on.

  • 1,000 papers of OGB's test split (809 from 2019, 191 from 2020, each citing at least five papers) are held out of the table.
  • Each held-out paper's title and abstract is the query. Its references in the table (15.3 on average) are the relevant rows.
  • Vectors come from a static embedder (Model2Vec potion-retrieval-32M, 512 dimensions), the same one ChloeCtx uses.
  • Each method returns 20 papers. Methods that start from vector seeds have one parameter, the number of seeds S, swept from 1 to 50. Papers with even ids choose S by nDCG@20 (516 queries) and papers with odd ids report it (484 queries), so no reported number was tuned on its own queries.
  • Comparisons are paired over the same queries, with a 95% bootstrap interval and a two-sided sign-flip test.

$$ \mathrm{recall@}k = \frac{\lvert R_k \cap T \rvert}{\lvert T \rvert}, \qquad \mathrm{nDCG@}k = \frac{\sum_{i \le k} \mathbb{1}[r_i \in T] / \log_2(i + 1)}{\sum_{i \le \min(k, \lvert T \rvert)} 1 / \log_2(i + 1)} $$

Finding a paper's references on the 484 report papers: following the citations of the two nearest papers reaches nDCG@20 0.253, against 0.123 for vector search, 0.161 for BM25 and 0.160 for hybrid search

Source: chloedb/results/graph/quality_stats.json. Times are from the quality runs on the M2, after one untimed pass per method.

The graph doubles what vector search finds. The best method is also the simplest: take the two papers nearest the query, follow their citations, and rank what they cite by similarity to the query. It reaches nDCG@20 0.253 against 0.123 for vector search (+0.130, 95% interval 0.111–0.149, p < 0.0001), 0.161 for BM25 and 0.160 for hybrid search, in 0.2 ms. Papers on the same topic cite the same work, and the nearest papers' reference lists are a better candidate set than the nearest papers themselves.

PageRank from the same seeds reaches 0.211 at its best (from one seed), below the plain one-hop expansion: on this graph the papers two or three hops away dilute the direct citations. Fusing PageRank with the vector ranking lowers it to 0.179, because the vector ranking's own top results are mostly not references. Fusing BM25, vectors and PageRank from hybrid seeds also reaches 0.211, at the cost of BM25 over long queries (48 ms).

nDCG@20 by number of vector seeds: the citation expansion peaks at two or three seeds and falls below vector search beyond 20

Fewer seeds are better. With more seeds, the citations of papers less similar to the query crowd out those of the nearest ones. In a first version of the experiment the seeds themselves stayed in the candidate set; ranked by similarity they took the top places, and from 20 seeds on the "graph" method returned exactly the vector search's results. Every graph method above removes the seeds with NOT REACHABLE(cites, seeds, hops => 0, min_hops => 0).

A graph made of vectors adds nothing. A VECTOR field can be traversed as a graph, each row's 10 most similar rows being its neighbours. On this task it returns what vector search already found when the seeds are kept (recall@20 0.112 against 0.111 at 10 seeds, on all queries), and is worse when they are removed (nDCG@20 0.079). A similarity graph only re-expresses the vector space, while citations carry information the text does not. Rows with vectors are useful as the way into a graph of real relations, not as a replacement for one. The implicit similarity graph stays in ChloeDB for exploratory queries, at 0.12 ms per hop of 10 neighbours.

Edge tables#

An edge table keeps a graph whose edges change one at a time, such as items seen together, apart from the rows it links: each of its rows is an edge, and adding one, changing its weight or deleting it writes that row alone. Each of its segments indexes its rows by the keys they link, in a section of its own (edge_table):

  • the distinct keys its rows name, numbered in the order of their hashes, with the same radix directory as the primary-key index;
  • each row's source and target as key numbers, and its weight when the segment was built;
  • for each key, the rows that leave it and the rows that reach it, in row order.

A hop from row u looks up u's key in each segment of the edge table, then reads the rows that leave it (or reach it, or both, by direction). For each row it checks that the row is live and reads its weight as it is now, in-place writes included, so a decayed or zeroed weight counts at once. The neighbour is the row's other end, a key number, which the walk resolves to a row of the walked table once per segment. Two shortcuts keep a long walk cheap without costing a short one anything. A segment's key numbers get a dense memo once the walk has resolved a thirty-second of them. A segment whose weights were written in place gets a flat copy of its weights once the walk has read a sixteenth as many as its in-place layers hold.

The section is smaller than an EDGES field's graph for the same edges, because a key is stored once per segment rather than once per edge. A hop costs more, because an EDGES field's out-edges sit with their row and most targets resolve through a same-segment hint. chloedb-edges-bench (in chloedb-server) measures both:

  • Writes. It streams 300,000 click events of 10,000 users on 50,000 items (Zipf 1.0) through /v1/stream. Each event adds 1 to the weight of the edges between its item and the user's last five items, both ways, with add operations whose doc creates an edge's row on its first event. That is 9.0 edge operations per event and 1,896,100 edges in the end.
  • Walks. ppr is seeded with a user's last 20 items, for 1,000 users, over the edge table and over an EDGES field of items holding the same edges and weights. Each query runs once to warm up, then once timed.
edge table EDGES field
bytes per edge (index) 20.6 33.2
ppr p50 ms 21.9 15.3
ppr p90 ms 30.1 26.6
ppr p99 ms 38.7 37.4
  • Walks cost about 1.4 times as much at the median, and about the same at p99. Both rank the same 200 rows in the same order in all 1,000 walks. The edge table had 10 segments here. In a first run whose merges had left it 15, its p50 was 28.5 ms against 17.6, since a hop looks up its key in every segment.
  • Writes. The events alone take 14.1 µs each (71,155 per second). With the edges, they take 190.2 µs (5,257 per second): 19.6 µs per edge operation, most of which create a row.
  • Weights as counts. 54 of the 1,000 walks found nothing over either graph, when ppr pushed a row only once its residual reached eps times its weighted degree: at the default eps of 10⁻⁴, a seed whose edges' weights added up to more than 500 was never pushed. It now compares the residual with eps times the row's number of edges, so that scaling every weight alike (counts against frequencies) changes nothing; the weights still divide what a push spreads. Those numbers were measured before the change. A walk that stops at max_pushes says so in its plan line (stopped at max_pushes) and in the stats (max_pushes_reached: true).

Cloud VM: 4 cores of an Intel Xeon at 2.10 GHz, 16.9 GB, Linux 6.18. Source: chloedb-server/results/edges/vm.json.

On a cluster#

Graph queries run through a coordinator too, and answer as one database does. On a sharded cluster a row's edges point at rows that other shards hold, so no shard can walk its part alone. The coordinator walks instead:

  1. It runs the walk one database runs (the same breadth-first walk, bidirectional search or forward push) over the rows it has read so far, and notes each row the walk needed and did not have: its out-edges (an EDGES field's JSON text), its in-edges, whether it exists, and whether it passes through.
  2. It reads all of them in one round over every shard, keys in batches of 2,000, four reads at a time (each goes to every shard, so a round of many reads takes its turns beside other queries): SELECT id, cites FROM papers WHERE id IN (...) for out-edges, WHERE REACHABLE(cites, (...), hops => 1, dir => 'in') for in-edges (each shard answers that from its own rows' edges, by the reverse index), and SELECT src, dst, w FROM cooc WHERE src IN (...) (or dst IN) over an edge table.
  3. It walks again from the start, until a walk misses nothing. That walk is the one database's walk.
  4. Each shard is then given the walk's answer for its own rows (hops for REACHABLE, place for PATH, score for ppr, in the plan as graph_given), and the rest of the query runs as any query does: filters, bm25, vector search, GROUP BY, joins.

REACHABLE and hops() take about one round per hop, PATH one per hop from either end, and ppr one each time its pushes reach rows it has not read. The plan says walked by the coordinator in N rounds.

ppr over an edge table whose src and dst both reference the table walked (src KEYWORD REFERENCES users) counts a row's edges first (SELECT src, COUNT(*) FROM replies WHERE src IN (...) GROUP BY src) and reads them only to push the row, as one database reads a row's edges only as far as the push test needs. Most rows a walk meets are not pushed, and a well-connected row's list is long: on a reply graph of 887,000 people and 2.6 million edges over 4 shards (all on one 4-core Intel Xeon at 2.10 GHz), ppr(replies, (24 of the busiest people), max_pushes => 2000) read every list it met, 2.7 million edges in its fourth round, and was refused after 2.6 s; counting first it took 7 rounds and 0.7 s (0.25 to 0.5 s on one database), and queries beside it waited 110 ms at most where they had waited up to 850. The plan says N rows' edges counted first. The count takes REFERENCES at its word: an edge to a key that is no row counts toward its row's edges, where one database leaves it out, so keep such a table's references whole or give it no REFERENCES. An edge table that does not say so has each list read in full. Two answers can differ from one database's: PATH may pick another path of the same length, and ppr scores may differ in the last digits (by about 10⁻⁸ in the tests), since it pushes rows in the order it read them; with no eps, the eps it adapts to can differ too (the tests' top five agree within 10⁻³). A walk is refused past max_nodes, as on one database, and after 256 rounds. A walk over a VECTOR field (its nearest neighbours as edges) is refused on a cluster, since each shard's neighbour graph is its own.

On 20,000 rows of 5 links each, split over 3 shards with the coordinator in one process (gRPC on loopback, a 4-core Intel Xeon at 2.10 GHz, medians of 25 seeds), a 2-hop REACHABLE took 4.3 ms in 3 rounds against 0.23 ms on one database, 3 hops 6.3 ms in 4 rounds against 0.30 ms, PATH 9.0 ms against 0.20 ms, and ppr at eps => 1e-4 83 ms in 6 rounds against 1.8 ms; an application's own walk, a read of the frontier's links per hop with no check that its rows exist, took 2.1 ms for 2 hops (cargo test --profile ci --test all graph_bench:: -- --ignored, chloedb-server/results/graph/cluster.json). Between machines each round adds a network round trip.

A coordinator of one shard that holds every slot, with no move in progress, sends graph queries to that shard, which walks them itself. Joins along links (JOIN papers b ON b.cites = a.id) run on a cluster as well.

Limits and next steps#

  • One quality task, one embedder. With a stronger embedder, vector search would score above 0.123 and the relative gain over it would likely shrink. The gains over BM25 (+57%) and hybrid search (+58%) are the more robust numbers.
  • Cross-segment hops cost lookups, two to five times the in-memory CSR on these graphs. A snapshot-wide resolved CSR, rebuilt when the segment set changes, would close the gap for read-mostly tables; merging into fewer segments narrows it today.
  • No patterns. There is no multi-hop pattern matching with a predicate per step; through and labels cover one constraint per traversal. SQL/PGQ's MATCH would be the standard syntax for more.
  • Unweighted paths. Weights are used by PageRank only.
  • Walks across shards cost a round trip per hop (above), and a large ppr many rounds. A small edge table kept whole on every shard would let each shard walk it alone, in one round; that is not built yet.
  • ChloeCtx. Its records have natural edges the store does not keep yet: a distilled note to the turn it came from, a superseded decision to its replacement, an error to the edit that fixed it. The arXiv result suggests the pattern to try (recall's best hits as seeds, then the records linked to them), but ChloeCtx's earlier study found that widening a hit to its neighbouring records gained little, so this needs its own measurement first.

The full report, with every table, is ChloeDB graphs. Reproducing the results has the commands.