Skip to the page

Retrieval-augmented generation · mid

Where to store vectors: pgvector, vector databases and filters

A team can keep vectors in PostgreSQL with pgvector, or use a dedicated vector database or a search engine. Filtered search with an approximate index has a known trap that returns too few rows.

The notes

What you must know

  • pgvector keeps vectors next to your rows: joins, transactions, row-level security and one backup. It has HNSW and IVFFlat indexes.
  • Dedicated engines (Qdrant, Weaviate, Milvus) are built for large-scale approximate search, with quantisation, sharding, filter-aware search and built-in hybrid search. They are one more system to run, secure and keep in sync.
  • Managed services need little operation but check the data region, cost at scale and lock-in. OpenSearch or Elasticsearch fit if you already run them for search.
  • Decide with the size, the filters, the team and where the data must live. A few million vectors is often fine in PostgreSQL.
  • Filter trap: an HNSW scan returns about ef_search candidates (40 by default) across the whole table and applies the filter afterwards. A selective filter leaves few rows.
  • Fixes: raise ef_search (costs latency); use iterative index scans (pgvector 0.8.0 and later, hnsw.iterative_scan) so the scan continues until enough rows pass; for a very selective filter use a B-tree index and exact search over the small set; use partial indexes or partitioning.
  • IVFFlat builds faster and is smaller. Create it after loading data, because it learns its lists from the data.
  • Storage estimate: number of chunks times dimensions times bytes per number, plus index overhead.

The red pen

Common traps: mistake → fix

  • Mistake: The filter runs before the vector search

    Fix: with an approximate index it often runs after, so rows are lost.

  • Mistake: A vector database is always better than PostgreSQL

    Fix: for a few million vectors with joins and access rules, pgvector is often simpler.

  • Mistake: Approximate search finds exactly the true nearest vectors

    Fix: it trades some recall for speed.

Easy to hard

Questions interviewers ask, easy to hard

  1. pgvector or a vector database? How do you decide?
  2. What is HNSW?
  3. A filtered query returns 3 rows instead of 10. Why?
  4. How do you fix filtered search in pgvector?
  5. Estimate the memory for 50 lakh chunks of 1,024 dimensions.

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview