Sub topic 9
What a vector database adds beyond an index
This chapter assumes you know what embeddings are, how cosine and Euclidean distance work, and why approximate nearest neighbor (ANN) indexes such as HNSW exist. It covers what a vector database provides on top of an index.
An index is not a database
An ANN index, such as the FAISS HNSW index you built earlier, answers one question: given a query vector, which stored vectors are closest? It returns integer positions. It does not know what those positions mean.
A real application needs more. It must store the original text, attach metadata, survive a restart, accept writes while serving queries, and let several clients connect. A vector database wraps an index with those features.
The data model
Most vector databases store records with three parts:
- ID. A unique key for the record.
- Vector. The embedding, with a fixed dimension per collection.
- Payload. Metadata and often the source text, such as
category,url, orcreated_at.
A group of records with the same vector dimension and distance metric is called a collection (or table, or index, depending on the product). The metric is fixed when the collection is created, because the index structure depends on it.
The pgvector example from the plan shows this model in SQL:
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE items (
id bigserial PRIMARY KEY,
category text,
embedding vector(3)
);
INSERT INTO items (category, embedding) VALUES
('a', '[1, 0, 0]'),
('b', '[0, 1, 0]'),
('a', '[0.9, 0.1, 0]');
CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops);
SELECT id, embedding <=> '[1, 0, 0]' AS cosine_distance
FROM items
WHERE category = 'a'
ORDER BY embedding <=> '[1, 0, 0]'
LIMIT 2;The id and category columns are the ID and payload. The embedding column is the vector. vector_cosine_ops fixes the metric, and the <=> operator must match it. If you query with a different operator than the index was built for, the planner cannot use the index and falls back to a full scan.
Components beyond the index
The next diagram shows how a typical system is layered.
Each layer solves a separate problem:
| Component | What it does | Why it matters |
|---|---|---|
| Persistence | Writes vectors and index data to disk | Data survives restarts without rebuilding |
| Write-ahead log | Records changes before applying them | Recovers recent writes after a crash |
| Query planner | Combines vector search with filters | Decides whether to filter before or after the search |
| Payload store | Holds metadata and source text | Returns usable results, not bare IDs |
| Access control | Authenticates clients and limits access | Needed when several teams or tenants share data |
| Replication and sharding | Copies or splits data across machines | Provides availability and capacity |
Why this matters in practice
A bare index forces you to build these parts yourself. A common failure is keeping vectors in FAISS and metadata in a separate store, then joining them by position. The two drift apart after deletes, because the index positions shift or are reused.
A database keeps the vector and its payload under one ID and updates them together. It also lets you change data without rebuilding everything. Many indexes handle inserts well but handle deletes poorly, so databases often mark records as deleted and clean them up in the background. Chapter 14 returns to this.
Consistency and freshness
Writes are not always searchable immediately. Some systems add new vectors to a small in-memory buffer that is searched by brute force, then merge it into the main index later. Others refresh on a schedule. Check whether the system you use gives read-your-writes behavior, because a RAG pipeline that ingests a document and queries it a second later can miss it.
Exercise
A team stores embeddings in a FAISS index and their metadata in a separate table, linked by row number. After they delete 5% of the documents, search results return the wrong titles. What caused this, and which database feature prevents it?
Show answer
Deleting records shifted or reused the positions in the index, so the row numbers no longer matched the metadata table. A vector database stores the vector and payload under one stable ID and updates both together, which keeps them consistent.
Exercise
Why must the distance operator in a pgvector query match the operator class used to create the index?
Show answer
The index is built for one metric, such as cosine distance with vector_cosine_ops. A query using a different operator cannot use that index, so the planner falls back to a slower full scan.