A vector database stores embedding vectors and an approximate-nearest-neighbor (ANN) index over them, so you can query by semantic similarity at scale. Examples include pgvector, Pinecone, Weaviate, Qdrant and Milvus.
Embedding, index, query: the retrieval path
An embedding model turns each data chunk into a high-dimensional vector that captures its meaning. The database builds an ANN index over those vectors, often with HNSW graphs or IVF clusters. The index finds the top-k nearest vectors to a query by distance metrics such as cosine similarity or dot product, in sub-linear time.
At query time, the query is embedded with the same model used for the chunks, so both land in one vector space. The index returns the closest chunks, and metadata filters such as tenant or date range narrow the result. The top-k chunks are then prepended to the prompt so the model answers from retrieved data instead of its training memory.
A query for "best Italian restaurants" can return chunks about pasta places or pizza spots, even without the exact keywords.
- Embed the input chunks.
- Build the ANN index with HNSW or IVF.
- Embed the query and fetch the top-k neighbors.
- Apply metadata filters.
- Return the results for the prompt.
Geometric distance versus keyword overlap
Keyword search in an engine such as Elasticsearch is the nearest analogy. It ranks documents by matching tokens. A vector database indexes dense float vectors and ranks by geometric distance, which reflects meaning. That finds relevant results without shared terms, and it adds a recall and precision trade-off that keyword search does not have in the same form.
Brute-force cosine similarity in Postgres
Computing cosine similarity across every row in Postgres gives exact results, but each query costs O(N). From 10,000 vectors to 10 million, that degrades quickly and breaks real-time use. An ANN index gives up a little recall for much higher speed by searching a small subset. Dedicated systems also provide sharding, filtering and hybrid scoring, which would take custom work to build on a general database.
pgvector or a dedicated vector database
| Situation | Choice |
|---|---|
| Few thousand documents | An exact scan is fast enough; skip the ANN index and the separate database |
| Small corpus, already on Postgres | pgvector, with one system to run |
| Millions of vectors, high QPS, multi-tenant | Dedicated vector database |
Example scenario: average similarity of 0.45
Suppose queries return an average similarity score of 0.45 where 0.75 was expected (illustrative figures). Likely causes are a query embedded with a different model than the chunks, chunks that are poorly cut, or an ANN index returning approximate neighbors that miss the closest vectors.
- Confirm embedding dimensionality is consistent across the dataset.
- Compare ANN results with an exact scan on a sample of queries to measure recall.
- Log similarity scores for a sample of queries and look at outliers.
ef_search for HNSW or nprobe for IVF) or rebuild the index, which costs more latency or build time. If exact search scores are also low, fix the embedding model or the chunking. Monitor average similarity and recall.