Vector databases
A database that stores embeddings with metadata and answers "the k nearest vectors to this one, where tenant = X" in milliseconds. Choose on scale, filtering, hybrid support and ops burden; often pgvector is enough.
Analogy: A warehouse that shelves products by what they resemble, not by name: you hand it a sample and ask for the ten most similar items, but only from aisle 7.
How it works
- Inside is an ANN index, usually HNSW, plus database duties: upserts, deletes, filters, replication, access control.
- Engines differ less on raw speed than on filtering, hybrid search, multi-tenancy and ops burden.
- Filter during index traversal: post-filtering an ANN top 100 by tenant can leave you just 2 rows.
- HNSW wants vectors in RAM: 1M at 1536 dims ≈ 6 GB of float32, ~1.5 GB as int8.
- Already on Postgres? pgvector is enough under tens of millions of vectors; go dedicated beyond that.
Check yourself
Answer to mark it learned; your answers aren't scored.
In your multi-tenant SaaS, every document is indexed, yet queries for small tenants often return 2–3 chunks instead of 10, while large tenants always get 10. No errors. Most likely cause?
Go deeper
Full explanation
A vector database stores vectors + metadata + an id and answers one question fast: the k vectors nearest to this query vector, among rows matching this filter. Under the hood it is an approximate nearest-neighbor index (ANN indexes: HNSW, IVF, PQ), usually HNSW, wrapped in what a database owes you: upserts and deletes, metadata filters, persistence, replication, backups and access control.
Engines differ less on raw speed than on:
- Filtered search:
tenant_id = 42 AND lang = 'es'. Naive post-filtering (ANN top 100, then filter) can return 2 rows; good engines filter during index traversal. - Hybrid: keyword scoring (Lexical search & BM25) or sparse vectors next to dense ones.
- Multi-tenancy: namespaces or per-tenant shards, so one customer never sees another's data.
- Ops model: an extension (pgvector), an embedded library (LanceDB), a server you run (Qdrant, Weaviate, Milvus) or a managed service (Pinecone, turbopuffer).
Storage math: 1M × 1536 dims × 4 bytes ≈ 6 GB of raw float32, and HNSW wants it in RAM. int8 quantization cuts it to ~1.5 GB, binary to ~0.2 GB.
My default: pgvector if you already run Postgres and stay under tens of millions of vectors; Elasticsearch/OpenSearch if that is already your search stack; a dedicated engine when scale, filtering or tenancy outgrow those.
Code pgvector: HNSW index and a filtered query
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE chunks (
id bigserial PRIMARY KEY,
tenant_id int NOT NULL,
content text NOT NULL,
embedding vector(1536) -- must match the embedding model's dimensions
);
-- Build after the bulk load: much faster than indexing row by row
CREATE INDEX ON chunks USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64);
CREATE INDEX ON chunks (tenant_id); -- the filter needs its own index
-- Query time: a wider candidate list buys recall with latency
SET hnsw.ef_search = 100;
-- pgvector 0.8+: keep scanning when the filter discards candidates
SET hnsw.iterative_scan = relaxed_order;
SELECT id, content, 1 - (embedding <=> $1) AS cosine_similarity
FROM chunks
WHERE tenant_id = $2
ORDER BY embedding <=> $1 -- <=> is cosine distance
LIMIT 10;Real example
A B2B SaaS on Postgres has 3M chunks at 1536 dims: 3M × 6 KB ≈ 18 GB raw, and pgvector's HNSW index keeps its own copy that should stay in memory. A 64 GB instance serves it; the tenant check is a SQL WHERE, and deleting a document deletes its chunks in the same transaction. A year later a consumer product needs 300M vectors (~1.8 TB of float32). That no longer fits one node's RAM, so that workload moves to a sharded or object-storage engine (Milvus, Pinecone, turbopuffer) with int8 quantization (~460 GB), while the B2B side stays on pgvector.
Trade-offs +3 · −3
+ You gain
- One query does k-NN plus metadata filters in milliseconds over millions of vectors.
- pgvector keeps vectors next to your data: joins, transactions and ACLs stay in SQL.
- Managed options (Pinecone, turbopuffer) remove index tuning and capacity planning.
− You pay
- RAM-hungry: a 1536-dim float32 vector is ~6 KB before index overhead, and HNSW wants it in memory.
- Selective filters can silently starve results if the engine post-filters.
- A separate store is another copy of your data to sync, secure and delete from.
Use it when
Semantic retrieval over more than ~100k chunks with metadata or permission filters. Start with pgvector or your existing search engine; move to a dedicated engine when scale, filtered recall or tenant count outgrows it.
Avoid it when
Under ~100k vectors, a NumPy matrix with brute-force dot products is exact and fast enough. And never as a replacement for keyword search on IDs and error codes.
Numbers to know 4
- 1M × 1536 dims × 4 B ≈ 6 GB of raw float32; int8 ≈ 1.5 GB; binary ≈ 0.2 GB.
- pgvector indexes up to 2,000 dims for
vectorand 4,000 forhalfvec. - pgvector HNSW defaults: m = 16, ef_construction = 64, hnsw.ef_search = 40.
- HNSW links cost ~2·M × 4 B per vector (~130 B at M = 16): small next to a 6 KB vector.
Common pitfalls 4
- Adding a new database before doing the math: a few million chunks fit comfortably in pgvector or the search engine you already run.
- Benchmarking without filters: a selective tenant filter applied after ANN can return 2 results instead of 10. Test the filtered query you actually run.
- Letting the index drift from the source: deleted or permission-revoked documents keep being retrieved.
- Mixing vectors from two embedding models or versions in one index: their distances are not comparable. Re-embed everything on a model change.
Interview questions 2
Answer out loud first, then reveal.
pgvector, Pinecone or Qdrant: how do you choose?Show answer
I start from what we already operate. If we run Postgres and the corpus stays under tens of millions of vectors, pgvector wins: one system, transactional deletes, permissions as SQL joins. I move to a dedicated engine when one of four things bites: vectors outgrow a node's RAM, selective filters hurt recall, thousands of tenants need isolation, or nobody can own index tuning, which points to a managed service. I decide with a benchmark on our data: filtered recall@10, p95 latency at peak QPS and monthly cost, not vendor charts.
How much memory do 50M 1536-dim embeddings need, and what would you do about it?Show answer
Back of the envelope: 50M × 1536 × 4 bytes ≈ 307 GB of float32. HNSW links add only ~130 bytes per vector at M = 16, so the vectors dominate. That is too much for one comfortable node, so I pull three levers: fewer dimensions (Matryoshka-trained models like text-embedding-3 can be shortened to 512), quantization (int8 is 4× smaller, binary 32× with full-precision rescoring), or a disk-based index like DiskANN. Then I check that recall@10 on real queries stays within a point or two of the baseline.