Skip to content

What is a vector database?

Mykhailo Poliarush

Definition

A vector database stores embeddings with ids and metadata and returns the items whose vectors are closest to a query vector. It builds an approximate nearest-neighbor index, such as HNSW or IVFFlat, so a search stays fast as the collection grows, and it can filter results by metadata.

What does a vector database actually do?

It answers one question: which stored items are closest to this vector? Each record holds an embedding (a list of numbers), an id and usually metadata such as language, tenant or document type. A query is itself an embedding. The database returns the top k records by a distance measure, such as cosine distance.

Checking every record gives the exact answer but gets slower as the collection grows. So vector databases build an approximate nearest-neighbor (ANN) index. pgvector's README states the baseline plainly: by default it performs exact nearest neighbor search, which gives perfect recall, and you add an approximate index when you trade some recall for speed (pgvector README).

Two common index families:

IndexHow it worksTrade-off (per the pgvector README)
HNSWA multilayer graph. A query walks from coarse layers down to close neighbors.Better speed-recall trade-off, but slower builds and more memory. Can be created on an empty table.
IVFFlatSplits vectors into lists, then searches only the lists closest to the query.Faster builds and less memory, but lower query performance. Build it after the table has some data, for good recall.

Both expose a recall knob. In pgvector it is hnsw.ef_search (default 40) for HNSW and ivfflat.probes for IVFFlat. A higher value finds more true neighbors and costs more time.

How does a nearest-neighbor query work?

The code below is exact search in plain Node, with no dependencies and hand-made 4-number vectors. It shows the two ideas that matter before you choose a product: scoring and filtering. We ran it on Node v22.9.0 on 2026-12-05.

const docs = [
  { id: "refund-policy",   lang: "en", v: [0.9, 0.1, 0.0, 0.2] },
  { id: "return-window",   lang: "en", v: [0.8, 0.2, 0.1, 0.1] },
  { id: "invoice-faq",     lang: "en", v: [0.1, 0.9, 0.2, 0.0] },
  { id: "rueckerstattung", lang: "de", v: [0.9, 0.0, 0.1, 0.3] },
  { id: "office-hours",    lang: "en", v: [0.0, 0.1, 0.9, 0.3] },
];
const dot = (a, b) => a.reduce((s, x, i) => s + x * b[i], 0);
const cosine = (a, b) => dot(a, b) / Math.sqrt(dot(a, a) * dot(b, b));

function search(query, k, filter = () => true) {
  return docs
    .filter(filter) // pre-filter: only matching rows are scored
    .map((d) => ({ id: d.id, score: +cosine(query, d.v).toFixed(4) }))
    .sort((x, y) => y.score - x.score)
    .slice(0, k);
}
const q = [0.85, 0.15, 0.05, 0.15]; // "how do I get my money back?"
console.log(search(q, 3));
console.log(search(q, 3, (d) => d.lang === "en"));
// Post-filter: take the top 2 first, then keep only German documents.
console.log(search(q, 2).filter((r) => docs.find((d) => d.id === r.id).lang === "de"));

Output (shortened to id and score):

[ refund-policy 0.9954, return-window 0.9943, rueckerstattung 0.9736 ]
[ refund-policy 0.9954, return-window 0.9943, invoice-faq 0.2826 ]
[]

The third line is the trap. The German document is the third-best match overall, so filtering after taking the top 2 leaves nothing. The second line shows the filter applied first: three English results come back, though the third is a weak match.

Why does filtering break vector search?

Real indexes face the same problem at scale. The pgvector README says that with approximate indexes the filter is applied after the index is scanned. Its example: if a condition matches 10% of rows and hnsw.ef_search is the default 40, only about 4 rows match on average. Since version 0.8.0 you can enable iterative index scans, which keep scanning until enough results are found or a limit is reached (pgvector README).

Dedicated engines handle it in the index itself. Qdrant describes a filterable HNSW index with extra filter-aware edges. It benefits from them only when the payload indexes exist before the HNSW index is built, so adding a payload index later means rebuilding the HNSW index (Qdrant indexing docs).

Tenant, language, date range and permission filters are common in business search. Test your real filters, not an unfiltered demo.

Dedicated vector database or a vector extension?

Vector extension in a database you run (for example pgvector)Dedicated vector database (for example Qdrant, Pinecone)
Data locationVectors beside your tables, same transactions and backupsSeparate system, kept in sync with your source data
Index typesPer the README: exact search, HNSW, IVFFlatEngine-specific. Qdrant's docs say it uses HNSW as its dense vector index
Limits to read firstvector columns indexed up to 2,000 dimensionsPer product docs and plan
Search modesFull-text and vector combined in SQLPinecone's current docs describe one index that can hold dense vectors, sparse vectors and full-text fields
OpsOne system to runOne more system, or a managed service

pgvector's limits come from its README, and the others from the Qdrant and Pinecone pages cited above. Both columns are valid. Our rule of thumb, not a benchmark: if your vectors and source data already live in Postgres, start with the extension. Choose a separate store when vector search is the core product, when load or scale outgrows the main database, or when you need features the extension lacks.

What should you check before choosing one?

  1. Filtering. Run your real filters and compare result counts and recall against exact search. See the section above.
  2. Hybrid search. Most business questions include names, codes and numbers that embeddings blur. Check that hybrid search with keyword scoring and rank fusion is built in, so you do not have to assemble it.
  3. Recall against speed. Measure recall by comparing the index with exact search on a sample of your queries, then tune the search parameters. Compare databases at equal recall, never at default settings.
  4. Memory and cost. Graph indexes such as HNSW use more memory than IVFFlat. Estimate vectors times dimensions times bytes, plus index overhead, and check the vendor's pricing page for current terms.
  5. Update pattern. Frequent writes and deletes affect index quality and rebuild time. Test with your real change rate.
  6. Embedding model changes. A new embedding model means re-embedding everything and a new index. Keep the model name and version with each record.
  7. Ranking on top. The nearest vectors are not always the best answers. A reranking step often adds more quality than a switch of database.

Vector search is one part of semantic search and retrieval pipelines. The index choice rarely decides quality. Chunking, the embedding model and evaluation matter more. Our AI-ready data engine work covers that data layer, and the generative AI team connects it to production features. For the wider picture, see what AI-ready data means.

Sources

  1. pgvector: open-source vector similarity search for Postgres (README), pgvector project, GitHub, 2026-11-22
  2. Indexing, Qdrant documentation, 2026-11-24
  3. Index data overview, Pinecone documentation, 2026-11-18

Frequently asked questions

Do I need a vector database for RAG?

Not always. For a small collection, exact search over a table is fast enough and needs no new system. Add an approximate index when queries get slow, and a dedicated store when your current database cannot keep up.

Is pgvector a vector database?

It is a Postgres extension that adds vector columns, distance operators and HNSW and IVFFlat indexes. You get vector search inside the database you already run, with your other tables and transactions beside it.

What is the difference between a vector database and a relational database?

A relational database answers exact questions: rows where a column equals a value. A vector database answers similarity questions: which stored items are closest to this vector. Many relational databases now offer both.

Why does a filtered vector search return too few results?

With some approximate indexes the filter runs after the index scan, so the scan can return 40 candidates and the filter can remove most of them. Look for filter-aware indexes or iterative scans, and test with your real filters.

What is HNSW?

HNSW (Hierarchical Navigable Small World) is a graph index. A query starts in a sparse top layer and walks down toward closer vectors. It trades memory and build time for fast, high-recall search.

Building with AI agents? Talk to an engineer.