All engineering notes
RAG22 November 2026·8 min read

Agentic RAG & Hybrid Search in Postgres with pgvector

Why pure vector similarity misses exact SKUs and names—and how Reciprocal Rank Fusion + bounded retrieval loops fix accuracy.

Agentic RAG & Hybrid Search in Postgres with pgvector isometric architecture diagram

Short version

Pure vector search struggles with exact part numbers, error codes, and proper nouns. Combining Postgres full-text search (BM25/tsvector) with pgvector semantic search via Reciprocal Rank Fusion (RRF)—wrapped in a bounded agentic retrieval loop—delivers cited, high-recall answers without standing up a separate vector database.

Retrieval Method

BM25 + Vector

Merged via Reciprocal Rank Fusion (RRF)

Loop Guard

Max 3 Hops

Bounded query rewrite prevents runaway cost

Citations

100% Grounded

Every claim linked to source chunk ID

Why vector similarity alone fails on enterprise data

Embeddings capture semantic meaning, not exact tokens. When a customer searches for invoice #INV-4092 or a specific medical code, cosine similarity often ranks conceptually similar documents higher than the exact match.

Running full-text search and pgvector HNSW search side by side inside PostgreSQL—and merging both ranked lists with Reciprocal Rank Fusion—gives you exact keyword precision plus semantic recall in a single ACID database.

WITH semantic AS (
  SELECT id, ROW_NUMBER() OVER (ORDER BY embedding <=> $1) AS rank
  FROM documents WHERE workspace_id = $2 LIMIT 40
),
keyword AS (
  SELECT id, ROW_NUMBER() OVER (ORDER BY ts_rank_cd(tsv, plainto_tsquery($3)) DESC) AS rank
  FROM documents WHERE workspace_id = $2 AND tsv @@ plainto_tsquery($3) LIMIT 40
)
SELECT COALESCE(s.id, k.id) AS id,
  COALESCE(1.0 / (60 + s.rank), 0.0) + COALESCE(1.0 / (60 + k.rank), 0.0) AS rrf_score
FROM semantic s FULL OUTER JOIN keyword k ON s.id = k.id
ORDER BY rrf_score DESC LIMIT 8;

Frequently asked questions

When should I use Agentic RAG instead of basic single-shot RAG?

Use basic hybrid RAG for single-fact lookups; add a bounded agentic loop (max 2–3 retrieval steps) when questions require comparing multiple documents or synthesizing across policies.

Is pgvector fast enough without Pinecone or Qdrant?

For most SaaS and enterprise datasets up to millions of chunks, Postgres + pgvector HNSW keeps transactional metadata, tenant permissions, and vectors in one place with sub-20ms queries.

Building something in this space and want it shipped fast?

Message me on WhatsApp or send a quick project brief — fixed quotes in USD, EUR, GBP, AED, SGD, or INR within 24 hours.

Related notes