Where should embeddings live, and what does vector search actually cost?
This page is about storing embeddings and finding the closest ones quickly. I show you what an index like HNSW does, why its answers are approximate, what the memory bill looks like (measured), why filters are the hard part, and when pgvector is enough and when a dedicated vector database earns its place. After it you will be able to size a vector index, trade recall against speed, and choose where your vectors live.
Storage for embeddings, lists of a few hundred to a few thousand numbers, with an index that finds the nearest ones without comparing against every row.
Good at: Semantic search, retrieval for AI answers (RAG), recommendations, finding near-duplicates, agent memory.
Runs as: an extension in a database you already run (pgvector, MongoDB Atlas Vector Search, Cassandra 5, Redis), or a dedicated service (Qdrant, Milvus, Weaviate, Pinecone and others)The vectors come from an embedding model. The index decides how fast and how accurately you can search them. The source data and its rules live somewhere you already trust.
The same layers as a still diagram, with every part named:
From question to answer
| Place | What it holds | What to watch |
|---|---|---|
| The vectors | dimensions × 4 bytes each, as 32-bit floats: 384 dimensions is 1,536 bytes | Notebook 8: 1,641 bytes per row in the table, with its text and row header. |
| An HNSW index | a copy of every vector plus links to about 2 × m neighbours | Notebook 8: 2,048 bytes per vector at m = 16. Table plus index: 3,689 bytes, 2.4 times the raw floats. Fast only while it fits in memory. |
| An IVF index (ivfflat) | vectors grouped around centroids learned from the data | Build it after the data is loaded. lists and probes trade speed and recall. Recall drops if new data drifts from the centroids. |
| Quantized vectors | halfvec (2 bytes a dimension), int8, or one bit a dimension | 2 to 32 times smaller. Re-rank the top results with the full vectors to win back recall. |
| Metadata for filters | tenant, permissions, dates | Filtering after the index can return too few rows. Notebook 8: 1.3 rows out of 10. |
100,000 memories for 100 tenants, as in Notebook 8. Step through a search, then tick the box to filter by tenant.
About 95% in Notebook 8, with ef_search at 40. Approximate means it usually finds them, not always. Raising ef_search to 160 found all of them for a little more time.
| Setting | Default | What it trades |
|---|---|---|
| Distance operator | <=> cosine · <-> L2 · <#> inner product | Must match the operator class the index was built with (vector_cosine_ops and so on), or the index is not used. |
| m, ef_construction | 16, 64 | More links and more effort at build time: better recall, a bigger index, a slower build. |
| hnsw.ef_search | 40 | Recall at 10: 0.80 at ef_search 10, 0.95 at 40, 1.00 at 160, all around a millisecond; 2.6 ms at 400. |
| hnsw.iterative_scan | off | relaxed_order or strict_order keep scanning until enough rows pass the filter (0.8 and later). In Notebook 8: 10 of 10 rows, about 9 ms. |
| maintenance_work_mem | 64 MB | The build is much faster when the graph fits in it. Notebook 8 used 1 GB and built 100,000 vectors in 10.5 s. |
| What happens | What you see | What I do |
|---|---|---|
| The index no longer fits in memory | Search goes from about a millisecond to many | halfvec or other quantization, fewer dimensions, an index per large tenant, or more memory. |
| Filtered search returns too few rows | The search or the agent "forgets" things | Iterative scan, a partial index for each big tenant, or an exact search inside small tenants. |
| A new embedding model | Old and new vectors are not comparable; quality drops without an error | Store the model name on every row. Re-embed everything, build a new index, then switch. |
| Heavy updates and deletes | Deleted entries linger in the graph; speed and recall drift | Vacuum regularly, and rebuild the index with REINDEX CONCURRENTLY after a big change. |
| Recall never measured | Answers are quietly worse than they should be | Keep a set of test questions, compute the exact answers once, and measure recall after every change. |
| A separate vector store out of step | Deleted or private documents still appear in answers | One source of truth that feeds the vector store through an outbox or CDC, with deletes and permission changes first. |
The first two parts replay what Notebook 8 measured. The third estimates memory for your own numbers.
-- how big is the index, and is it used?
SELECT pg_size_pretty(pg_relation_size('items_embedding_idx'));
EXPLAIN (ANALYZE, BUFFERS)
SELECT id FROM items ORDER BY embedding <=> $1 LIMIT 10;
-- trade recall for speed, for this session
SET hnsw.ef_search = 100;
-- a filtered search that keeps going until enough rows pass (0.8 and later)
SET hnsw.iterative_scan = relaxed_order;
-- the exact answer, to measure recall against
SET enable_indexscan = off;
Versions and licences. pgvector 0.8 added iterative index scans; 0.8.2 (February 2026) fixed a security bug in parallel HNSW builds (CVE-2026-3172), so upgrade older 0.8 installs. Redis 8 added a vector set type, Cassandra 5.0 added vector search through its storage-attached indexes, and MongoDB offers vector search in Atlas. The vectors on this page were generated, not real embeddings: treat the recall numbers as the shape of the curve.
You have 20 million chunks with 1,536-dimension embeddings in pgvector, under an HNSW index. How much memory does the index want, and what would you do if the server has 128 GB?
Write your answer down first. Then open mine.
The arithmetic: 20 million × 1,536 × 4 bytes is about 123 GB of vectors, and the index adds roughly 512 bytes of links each, about 10 GB more: around 133 GB for the index alone. It does not fit in 128 GB with anything else running.
What I would try, in order: store halfvec (2 bytes a dimension), which halves the index to around 72 GB. Check whether a model or setting with fewer dimensions loses anything on your own test questions. Partition by tenant so the hot part fits. Then consider a disk-based index or a dedicated vector store.
Every step: measure recall against an exact search on a sample before and after. Saving memory is only a win if the answers stay good.
The same mechanism, vector search, elsewhere on the site: in real incidents, in measurements, and in designs.
Pick an answer. You will see whether it is right, and why.
1. What does hnsw.ef_search trade?
A bigger candidate list finds more of the true neighbours and takes a little longer.
2. Why did one tenant's search return 1.3 rows instead of 10 in Notebook 8?
The index does not know about the WHERE clause. Iterative scan, or an exact search inside the tenant, fixes it.
3. You switch to a new embedding model. What must happen to the stored vectors?
Vectors from different models live in different spaces; comparing them gives meaningless distances.
4. Going by Notebook 8, about how big is an HNSW index for 1 million 384-dimension vectors?
About 2,048 bytes per vector, so roughly 2 GB, and the table is as big again.
How do databases actually work? From one row on disk to transactions: the words you need, PostgreSQL, MySQL and MongoDB from the files up, then Redis, Cassandra and vector search, side by side, and the query and concurrency traps.