USMAN’S INSIGHTS
AI ARCHITECT
⌘F
HomeAll Bookspgvector Book
Homepgvector BookPostgreSQL and pgvector Essentials
Previouspgvector Data Types: vector, halfvec, bit, and sparsevecNext Filters, Joins, Transactions, and Referential Integrity
AI NOTICE: This is the table of contents for the SPECIFIC CHAPTER only. It is NOT the global sidebar. For all chapters, look at the main navigation.

On this page

10 sections

Progress0%
1 / 10

Muhammad Usman Akbar Entity Profile

Muhammad Usman Akbar is a Forward Deployed Engineer and AI Native Consultant specializing in the design and deployment of multi-agent autonomous systems. Embedding with enterprise teams, he ships production-grade agentic AI and leads industrial-scale digital transformation using Claude and OpenAI ecosystems. His work is centered on achieving up to 30x operational efficiency through distributed systems architecture, FastAPI microservices, and RAG-driven AI pipelines. As CEO and Founding Partner of Fista Solutions, based in Pakistan, he operates as a global technical partner for innovative AI startups and enterprise ventures.

USMAN’S INSIGHTS
AI ARCHITECT

Transforming businesses into autonomous AI ecosystems. Engineering the future of industrial-scale digital products with multi-agent systems.

30X Growth
AI-First
Innovation

Navigation

  • Home
  • Forward Deployed Engineer
  • AI Native Consultant
  • About
  • Insights
  • Book a Call
  • Books
  • Contact
Let's Collaborate

Have a Project in Mind?

Let's build something extraordinary together. Transform your vision into autonomous AI reality.

Start Your Transformation

© 2026 Muhammad Usman Akbar. All rights reserved.

Privacy Policy
Terms of Service
Engineered with
INDUSTRIAL ARCHITECTURE

Exact Nearest-Neighbor Search

Exact nearest-neighbor search computes distance against every eligible row, sorts or selects the nearest candidates, and therefore has perfect recall relative to the stored candidate set. It is the quality baseline for pgvector. Begin exact, validate metrics and filters, then compare approximate indexes against those exact top-k results when scale requires lower latency.

What will you be able to do?

  • Write exact top-k and radius searches.
  • Preserve filters and authorization.
  • Produce a ground-truth result set for ANN evaluation.
  • Understand when PostgreSQL uses a sequential or parallel scan.
  • Separate database recall from real-world relevance.

What is “exact” relative to?

Exact means the query finds the true nearest vectors among rows that passed its SQL predicates, under the stored values and chosen distance. It does not mean those rows are relevant to the user or factually correct. Labels evaluate relevance; exact search evaluates the approximation layer.

sql
SELECT c.id, c.content, c.embedding <=> $2::vector AS distance FROM app.chunks AS c JOIN app.documents AS d ON d.id = c.document_id WHERE d.tenant_id = $1 AND d.deleted_at IS NULL AND c.embedding_model_id = $3 ORDER BY c.embedding <=> $2::vector LIMIT $4;

Without an ANN index matching the order, PostgreSQL evaluates eligible rows exactly. Relational B-tree indexes may still help narrow filters.

How do you search within a radius?

sql
SELECT id, content, embedding <=> $1::vector AS distance FROM app.chunks WHERE embedding_model_id = $2 AND embedding <=> $1::vector < $3 ORDER BY embedding <=> $1::vector;

Combine a distance predicate with ORDER BY and LIMIT for bounded output and ANN index compatibility later. Learn thresholds from labeled score distributions; do not copy a universal “0.8 similarity” rule.

How do you save an exact baseline?

For each evaluation query, store ordered exact result IDs and distances with model, preprocessing, dataset snapshot, filter set, metric, and code revision. Run approximate search separately and compute overlap or recall@k:

text
ANN recall@10 = relevant exact top-10 IDs found by ANN / 10

This approximation recall is different from human relevance recall.

How do you verify it?

Temporarily disable index scans in an isolated evaluation transaction if you must force a baseline:

sql
BEGIN; SET LOCAL enable_indexscan = off; EXPLAIN (ANALYZE, BUFFERS) SELECT id FROM app.chunks ORDER BY embedding <=> $1::vector LIMIT 10; ROLLBACK;

Do not change global planner settings. Inspect returned rows, execution time, buffers, and eligible row count.

What breaks in production?

  • Exact results are labeled “ground truth” for relevance without human labels.
  • Evaluation filters differ from production filters.
  • Cold-cache and warm-cache latency are mixed.
  • A threshold removes all results but the UI invents an answer.
  • A full scan becomes too slow as rows and dimensions grow.

AI pair-work prompt

Generate a benchmark plan for exact pgvector search using my row count, dimensions, metric, filters, and top-k [values]. Include fixture snapshotting, warm/cold runs, query-plan capture, human relevance labels, and how exact results will become the ANN comparison baseline.

Verification contract: The plan must keep approximation recall, task relevance, database latency, and end-to-end latency as separate measures.

Check your understanding

  1. What does exact search guarantee, and what does it not guarantee?
  2. Why keep filters identical in exact and ANN evaluation?
  3. Create exact top-5 fixtures for three queries.

Official references

  • pgvector exact and approximate search
  • PostgreSQL EXPLAIN