USMAN’S INSIGHTS
AI ARCHITECT
⌘F
HomeAll Bookspgvector Book
Homepgvector BookScale and Advanced Techniques
PreviousHalf Precision, Binary Quantization, Sparse Vectors, and SubvectorsNext Vertical Scaling, Read Replicas, Sharding, and Distributed PostgreSQL
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

9 sections

Progress0%
1 / 9

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

Two-Stage Retrieval and Expression Indexes

Two-stage retrieval uses a cheaper or approximate representation to generate a broad candidate set, then reranks candidates with the authoritative full vector or a learned model. PostgreSQL expression indexes support half-precision, binary, normalized, or subvector first stages. Candidate depth is the critical bridge: too small loses recall; too large erases latency gains.

What will you be able to do?

  • Create an expression-index candidate stage.
  • Rerank by the original vector.
  • Preserve exact final ordering.
  • Tune candidate depth and ANN effort jointly.
  • Diagnose expression mismatch.

What does the SQL pattern look like?

sql
WITH candidates AS MATERIALIZED ( SELECT id FROM app.items WHERE tenant_id = $1 ORDER BY binary_quantize(embedding)::bit(1536) <~> binary_quantize($2::vector)::bit(1536) LIMIT 200 ) SELECT i.id, i.embedding <=> $2::vector AS distance FROM candidates c JOIN app.items i USING (id) ORDER BY i.embedding <=> $2::vector LIMIT 10;

The corresponding HNSW expression index must use the same binary expression and bit_hamming_ops. Authorization must apply in the candidate stage and remain true at final access.

Why use MATERIALIZED?

It can preserve the intended stage boundary and candidate limit when the planner might inline a CTE. Always inspect the plan. In iterative relaxed scans, materializing and adding a small expression such as distance + 0 for strict reordering may be required by some PostgreSQL/pgvector versions; follow current upstream guidance.

How do you verify it?

Sweep candidate depths such as 20, 50, 100, 200, and 500 with ANN effort. Compare final top-10 against exact full-vector search. Record stage latency, total latency, recall, buffers, and result count for each filter slice.

What breaks in production?

  • The first stage uses an unauthorized global pool.
  • Reranking only reorders; it cannot recover missing candidates.
  • The planner does not use the expression index.
  • Candidate arrays create huge memory or SQL payloads.
  • A normalized expression changes after a model migration.

AI pair-work prompt

Generate and explain a two-stage pgvector query for [representation, metric, filters]. Include exact matching expression-index DDL, oversampling sweep, materialization decision, full-vector rerank, plan assertions, authorization, and held-out recall gates.

Verification contract: Compare final IDs with full exact search and reject any speed gain that violates the defined slice-level recall.

Check your understanding

  1. What does candidate depth trade?
  2. Why must authorization happen before coarse retrieval?
  3. What evidence proves expression-index usage?

Official references

  • pgvector binary quantization
  • pgvector iterative scans
  • PostgreSQL indexes on expressions