Home›Learn›Vector databases

Vector databases

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.

Vector search: in one line

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 architecture: a model, an index, and the data behind it

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:

Vector search: architecture

From question to answer

Embedding modelturns text or images into a vector of, say, 384, 768 or 1,536 numbers · the same model must embed both the stored data and the questions
→
Index, in memoryHNSW: a layered graph of neighbours, fast and accurate, hungry for memory · IVF: clusters of vectors, cheaper to build, needs tuning · quantized copies to save space
→
Storagethe vectors and their metadata, plus the source documents · in pgvector, an ordinary table and an index
Who does what: An exact search compares the question with every vector: always right, slow at scale. An approximate index checks a small part of the data and is usually right; how often is called recall, and you trade it against speed with settings like ef_search.
Where the data lives

What takes the space, and where it goes

PlaceWhat it holdsWhat to watch
The vectorsdimensions × 4 bytes each, as 32-bit floats: 384 dimensions is 1,536 bytesNotebook 8: 1,641 bytes per row in the table, with its text and row header.
An HNSW indexa copy of every vector plus links to about 2 × m neighboursNotebook 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 dataBuild it after the data is loaded. lists and probes trade speed and recall. Recall drops if new data drifts from the centroids.
Quantized vectorshalfvec (2 bytes a dimension), int8, or one bit a dimension2 to 32 times smaller. Re-rank the top results with the full vectors to win back recall.
Metadata for filterstenant, permissions, datesFiltering after the index can return too few rows. Notebook 8: 1.3 rows out of 10.
Follow it

Follow one search, and one filter

100,000 memories for 100 tenants, as in Notebook 8. Step through a search, then tick the box to filter by tenant.

An HNSW index over 100,000 vectors returns its 10 nearest in under a millisecond. At the default settings, what share of the true 10 nearest does it find?

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.

The settings that matter

pgvector's settings, with what they did in Notebook 8

SettingDefaultWhat it trades
Distance operator<=> cosine · <-> L2 · <#> inner productMust match the operator class the index was built with (vector_cosine_ops and so on), or the index is not used.
m, ef_construction16, 64More links and more effort at build time: better recall, a bigger index, a slower build.
hnsw.ef_search40Recall 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_scanoffrelaxed_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_mem64 MBThe 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 breaks first

The failures I would plan for

What happensWhat you seeWhat I do
The index no longer fits in memorySearch goes from about a millisecond to manyhalfvec or other quantization, fewer dimensions, an index per large tenant, or more memory.
Filtered search returns too few rowsThe search or the agent "forgets" thingsIterative scan, a partial index for each big tenant, or an exact search inside small tenants.
A new embedding modelOld and new vectors are not comparable; quality drops without an errorStore the model name on every row. Re-embed everything, build a new index, then switch.
Heavy updates and deletesDeleted entries linger in the graph; speed and recall driftVacuum regularly, and rebuild the index with REINDEX CONCURRENTLY after a big change.
Recall never measuredAnswers are quietly worse than they should beKeep a set of test questions, compute the exact answers once, and measure recall after every change.
A separate vector store out of stepDeleted or private documents still appear in answersOne source of truth that feeds the vector store through an outbox or CDC, with deletes and permission changes first.
Where it fits in a design

Start with the database you have

Use it for

  • pgvector, when the index fits in one machine's memory and you want filters, joins, permissions and deletes in one transaction: Search and AI retrieval, Agent memory
  • The vector search in the database you already run, MongoDB, Redis or Cassandra 5, before adding a new system
  • A dedicated vector database when the index is far larger than one machine, query rates are very high, or you need its hybrid search and multi-tenancy at scale

Not for

  • The source of truth for documents, permissions or deletes
  • Exact lookups by id or keyword: a normal index is faster and exact
  • A search nobody has measured recall for
Try it

Trade recall for speed, filter a tenant, size an index

The first two parts replay what Notebook 8 measured. The third estimates memory for your own numbers.

Check it yourself

The commands I run first

-- 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;
Measured on this site

Notebook 8, PostgreSQL 18.7 with pgvector 0.8.7, 100,000 generated 384-dimension vectors

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.

Your turn

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.

Show my answer

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.

Connected

Where this shows up

The same mechanism, vector search, elsewhere on the site: in real incidents, in measurements, and in designs.

Check yourself · 4 questions

Check yourself

Pick an answer. You will see whether it is right, and why.

1. What does hnsw.ef_search trade?

2. Why did one tenant's search return 1.3 rows instead of 10 in Notebook 8?

3. You switch to a new embedding model. What must happen to the stored vectors?

4. Going by Notebook 8, about how big is an HNSW index for 1 million 384-dimension vectors?

Part 1 of 4 · Learn · 23 pagesAll pages

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.