Indexes: Overview and Index Choice

This page maps the index code in the snapshot (main @ 8a689c5c, 2026-11-30) to what SQL actually uses. There are two separate index layers. One is wired to SQL and lives in shared registries on the storage engine. The other is a set of standalone index structures in src/index/ that no SQL path populates. The operator summary is in How it works.

Pages in this chapter:

Page Covers
B-tree and hash secondary indexes Equality, IN and range acceleration for btree/hash indexes, plus the unwired in-memory and on-disk B-trees
GIN: JSON, arrays and full-text postings USING gin on JSON/array columns and on tsvector
HNSW and pgvector VECTOR(n), distance operators, the HNSW graph, the SQL KNN path, and the cross-wire vector service
Full-text search tsvector/tsquery functions, the @@ index path, match_text, and the unwired Tantivy index
Graph adjacency index GraphAdjacencyIndex and why nothing uses it
Unwired index structures DiskANN, IVF-PQ, the tiered vector index and the IndexManager
Embeddings Embedders used by vector collections and the /v1/embeddings HTTP endpoint

The two index layers

Layer 1: engine-wide registries (wired)

The storage engine owns two registries that every QueryEngine sharing that storage sees: VectorIndexRegistry and SecondaryIndexRegistry. Entries are keyed by db\x1ftable\x1fcolumn (vector entries also append the metric), lowercased. (source: src/storage/mod.rs, VectorIndexEntry, SecondaryIndexEntry) Tests hnsw_registry_shared_across_engines and secidx_registry_shared_across_engines check that a second engine on the same storage sees the same entries. (source: src/query/engine.rs, those tests)

Neither registry is persisted. The storage module documents its "v1 persistence model" as none: an entry is rebuilt from a table scan the first time a query needs it after a start or after it is marked dirty. (source: src/storage/mod.rs, VectorIndexEntry doc comment) Only the index definition is durable, as catalog keys written by CREATE INDEX. (source: src/query/engine.rs, execute_create_index) The full-text test fts_index_rebuilt_after_restart covers a rebuild after reopening storage. (source: src/query/engine.rs, fts_index_rebuilt_after_restart)

All registry paths follow the same model: the index proposes candidate row keys, the engine refetches those rows, and the original predicate or ORDER BY runs unchanged on them. An index can therefore return extra candidates without changing results; it can only lose rows if it misses one. (source: src/query/engine.rs, try_hnsw_candidate_keys, try_secondary_index_candidates, try_gin_candidates, try_fts_candidates)

Layer 2: src/index/ structures (not wired)

src/index/mod.rs defines the traits Index, VectorIndex, GraphIndex and FullTextIndex, and an IndexManager holding maps for B-tree, persistent B-tree, vector, graph, full-text (Tantivy) and GIN indexes. (source: src/index/mod.rs, IndexManager, IndexKind)

Not wired: no code outside src/index/ calls any IndexManager::create_* method (create_btree_index, create_persistent_btree_index, create_vector_index, create_tiered_vector_index, create_fulltext_index, create_gin_index, create_graph_index). The engine holds an index_manager field built with IndexManager::new(), and its insert/delete hooks (update_indexes_for_insert, update_indexes_for_delete) and execute_full_text_search consult it, but it is always empty, so those paths do nothing. (source: src/query/engine.rs, index_manager field, update_indexes_for_insert, update_indexes_for_delete, execute_full_text_search) LogicalPlan::FullTextSearch, the only plan node that reaches execute_full_text_search, is not produced by any parser. (source: src/plan/logical.rs, LogicalPlan::FullTextSearch)

PhysicalPlanner, which looks up GIN and persistent B-tree indexes in the IndexManager, is only constructed in its own unit tests. (source: src/plan/physical_planner.rs, PhysicalPlanner, get_gin_index, get_persistent_btree_index)

What CREATE INDEX does

execute_create_index always writes the definition __catalog__:index:{table}:{name} as a bincode tuple of key texts, the unique flag and the method. What else it writes depends on the method and the key. (source: src/query/engine.rs, execute_create_index)

Method and key Extra catalog key Accelerates
hnsw or ivfflat on one column __catalog__:vecindex:{table}:{name} = col\x1fmetric_tag ORDER BY col <op> const LIMIT n
btree or hash on plain columns, no WHERE __catalog__:secidx:{table}:{name} = leading column Equality, IN, ranges on the leading column
gin on a tsvector column or on to_tsvector('cfg', col) __catalog__:ftsidx:{table}:{name} = col\x1fsource @@
gin on any other plain column __catalog__:ginidx:{table}:{name} @>, ?, ?\|, ?&
UNIQUE (any method) __catalog__:uqindex:{table} (JSON list) Uniqueness enforcement and unique lookups
Any index on a materialized view none Nothing; the definition is metadata only

(source: src/query/engine.rs, execute_create_index, backfill_unique_index, append_unique_index_def)

Partial and expression UNIQUE indexes are enforced. (source: src/query/engine.rs, backfill_unique_index) ivfflat is accepted as a synonym for an HNSW-backed index; its lists parameter is ignored, as are all WITH (...) storage parameters, so m and ef_construction cannot be set from SQL. (source: src/query/sql/parser.rs, CREATE INDEX parsing; src/query/engine.rs, execute_create_index) CREATE INDEX IF NOT EXISTS on an existing name leaves the existing index alone; test create_index_if_not_exists_leaves_an_existing_index_alone. (source: src/query/engine.rs, that test)

How the engine chooses an index

Index choice is a fixed order of checks in the table-scan path, not a cost comparison. The first check that returns candidates wins. (source: src/query/engine.rs, table-scan path calling try_hnsw_candidate_keys, try_unique_index_lookup, try_secondary_index_candidates, try_secondary_range_candidates, try_gin_candidates, try_fts_candidates)

Order Check Conditions
1 HNSW restrict ORDER BY col <op> const ascending, one sort key, LIMIT, not in a transaction
2 Primary-key equality pk = const
3 Primary-key IN list pk IN (...)
4 Unique index lookup Equality on all columns of a unique index
5 Composite unique Equality on a composite unique key
6 Secondary equality / IN Allowed in a transaction only if the entry was rebuilt before the transaction's snapshot; skipped for citext, OR, float literals
7 Secondary range Not in a transaction; at most 1024 candidates, or more if they are under 1/40 of the table
8 JSON/array GIN Not in a transaction
9 Full-text GIN Not in a transaction
10 Primary-key integer range Range on an integer PK
11 Primary-key prefix ranges Range on a PK prefix
12 Full scan Otherwise

(source: src/query/engine.rs, the functions above and RANGE_INDEX_MIN_CANDIDATES)

EXPLAIN reports Index Scan using <name> when one of these paths applies. (source: src/plan/explain.rs, ScanAccess; src/query/engine.rs, explain_secondary_index_name) Tests secidx_accelerated_path_is_actually_used, hnsw_accelerated_path_is_actually_used and fts_accelerated_path_is_actually_used check that the index path is taken. (source: src/query/engine.rs, those tests)

The optimizer

The logical optimizer is on unless SYNERGYDB_OPTIMIZER is 0, false or off. It applies rule-based rewrites and a greedy join reorder that uses table statistics. (source: src/plan/optimizer.rs; src/query/engine.rs, optimizer_enabled) It does not pick among access paths; the table-scan order above does that. Test inner_join_drives_from_indexed_side checks that a join drives from the indexed side. (source: src/query/engine.rs, inner_join_drives_from_indexed_side)

Not verified: a cost-based choice between index and full scan. The only size-based rule is the range threshold in row 7.

Third-party crates

The index code uses bincode (catalog values, HNSW and persistent B-tree state), crc32fast (B-tree page checksums), tantivy 0.22 (unwired full-text index), crossbeam-channel (vector indexer queue), dashmap and parking_lot. (source: Cargo.toml, [dependencies])

Differences from other pages

  • How it works calls the optimizer cost-based, citing the README. The code applies a fixed rule order for index choice. (source: src/query/engine.rs, table-scan path)
  • The same page says src/plan/ is not in the snapshot. It is present at 8a689c5c. (source: src/plan/optimizer.rs)