Vector databases and index types
Once you have embeddings you need somewhere to search them. The options range from an extension in the Postgres you already run to serverless engines that keep everything on object storage. Underneath, they all rely on a small family of approximate nearest neighbour (ANN) index types. This doc explains those index types first, because they drive most of the trade-offs, and then compares the popular databases by the shape of problem each fits best.
Why it matters
Exact nearest neighbour search compares the query with every vector. That is fine for a hundred thousand vectors and painful for a hundred million. ANN indexes give up a little recall (they may miss a true neighbour) for orders of magnitude less work. Every index exposes knobs that move you along that curve, and the database you pick determines how much memory, disk and operational effort the curve costs you. Picking badly shows up as either a large bill for RAM or a retrieval system that quietly misses relevant chunks.
How it works
The index families you will meet everywhere:
- Flat (brute force). Exact. Use it for small collections and as the ground truth when you measure recall of an ANN index.
- HNSW (Hierarchical Navigable Small World). A multi-layer graph where each vector links to nearby vectors. Search starts at the sparse top layer and greedily descends. Build-time parameters are usually
m(links per node) andef_construction; the query-time parameter isef_search(oref). HNSW gives an excellent speed-recall trade-off and handles inserts well, but it is memory-hungry because the graph and vectors want to live in RAM. - IVF (inverted file). Cluster vectors into
lists(ornlist) centroids with k-means, then at query time scan only theprobes(ornprobe) closest clusters. Faster to build and lighter on memory than HNSW, with a worse speed-recall trade-off, and it should be built after you have representative data so centroids are meaningful. - Quantization. Product quantization (PQ), scalar quantization (int8) and binary quantization compress vectors so more fit in memory. Usually paired with a rescoring step using full-precision vectors.
- Disk-based graphs (DiskANN and similar). Keep most of the graph on SSD and a compressed copy in memory, for very large collections at somewhat higher latency.
Rendering diagram…
pgvector's own README summarizes the core trade-off well: IVFFlat builds faster and uses less memory than HNSW but has lower query performance in terms of the speed-recall trade-off. Supabase's docs recommend HNSW as the default for that reason.
The databases, by fit
- pgvector (Postgres). HNSW and IVFFlat indexes, all the usual SQL, joins, transactions and row-level security. Best when your data already lives in Postgres and the corpus is in the low tens of millions or less. Filtering with selective
WHEREclauses used to be a weak spot; version 0.8 added iterative index scans to help. - Qdrant. Open-source, Rust, HNSW with strong payload filtering, quantization, named dense and sparse vectors, and a Query API with built-in fusion for hybrid search. Self-host or managed cloud.
- Weaviate. Open-source, built-in hybrid search with configurable fusion and an
alphaweight, modules for vectorizers and rerankers, multi-tenancy. Self-host or managed. - Pinecone. Fully managed and serverless; you trade control for zero operations. Supports namespaces, metadata filters and sparse-dense search.
- Milvus (and Zilliz Cloud). Built for large scale, with many index types (FLAT, IVF_FLAT, IVF_SQ8, IVF_PQ, HNSW, DISKANN, SCANN, GPU variants and sparse inverted indexes) and a distributed architecture.
- LanceDB. Built on the open Lance columnar format; runs embedded in your process or as a managed service, stores vectors alongside multimodal data on local disk or object storage, and supports vector, BM25 full-text and hybrid search.
- turbopuffer. Serverless search on object storage: writes go to a write-ahead log on object storage and are indexed asynchronously, with hot namespaces cached on NVMe and memory. Very cheap at rest, with higher latency on cold queries. Supports vector, BM25 full-text and filtering, and fits many-namespace (one per tenant) designs.
Reference architecture
A decision path I use when someone asks "which vector database".
Rendering diagram…
Treat the thresholds as rough guides. The real deciding factors are usually where your data already lives, your team's operational appetite and how much filtering and hybrid search you need.
Code
pgvector from Python: create a table, an HNSW index with cosine distance, and run a filtered query. The pgvector package registers the vector type with psycopg.
import numpy as np
import psycopg
from pgvector.psycopg import register_vector
conn = psycopg.connect("postgresql://localhost/rag", autocommit=True)
conn.execute("CREATE EXTENSION IF NOT EXISTS vector")
register_vector(conn)
conn.execute("""
CREATE TABLE IF NOT EXISTS chunks (
id text PRIMARY KEY,
tenant_id text NOT NULL,
content text NOT NULL,
embedding vector(1536) NOT NULL
)
""")
conn.execute("""
CREATE INDEX IF NOT EXISTS chunks_embedding_hnsw
ON chunks USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64)
""")
def search(query_vec: np.ndarray, tenant_id: str, k: int = 10):
with conn.transaction():
conn.execute("SET LOCAL hnsw.ef_search = 100")
return conn.execute(
"""
SELECT id, content, embedding <=> %s AS distance
FROM chunks
WHERE tenant_id = %s
ORDER BY embedding <=> %s
LIMIT %s
""",
(query_vec, tenant_id, query_vec, k),
).fetchall()
The operator must match the index's operator class: <=> with vector_cosine_ops, <-> with vector_l2_ops, <#> with vector_ip_ops. A mismatch means the index is not used and you get a sequential scan.
Trade-offs and pitfalls
- Measure recall, not just latency. Compare ANN results to a flat search on a sample of queries. A fast index that misses 15 percent of true neighbours is a retrieval bug.
- Filtering interacts with ANN. With a selective filter, a graph search can return too few results. Check how your database handles it (pre-filtering, filterable HNSW, iterative scans) and test with your most selective tenant.
- Memory is the bill. HNSW wants vectors and graph in RAM. Quantization, smaller dimensions or disk-based indexes are the levers.
- IVF needs representative data. Building IVF on a tiny sample yields bad centroids. Rebuild as the corpus grows.
- Deletes and updates are not free. Graph indexes accumulate tombstones; plan compaction or rebuilds.
- Cold starts on object-storage engines. Serverless designs are cheap but the first query on a cold namespace is slower. Warm critical namespaces.
- Do not over-index on benchmarks. Vendor benchmarks pick favourable settings. Run your data, your filters and your QPS.
Checklist
- Start with flat search on a sample to get ground-truth neighbours for recall tests.
- Default to HNSW; choose IVF or disk-based indexes when memory or scale demands it.
- Tune
ef_search(orprobes) against a recall target, then check latency. - Make sure the query distance operator matches the index metric.
- Test filtered queries with your most selective filters.
- Estimate memory at full corpus size, including index overhead, before choosing.
- Plan for deletes, re-embedding and index rebuilds.