USMAN’S INSIGHTS
AI ARCHITECT
⌘F
HomeAll Bookspgvector Book
Homepgvector BookRetrieval Engineering
PreviousExact Search as the Quality BaselineNext IVFFlat Indexes from Training to Tuning
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

HNSW Indexes from Intuition to Tuning

Hierarchical Navigable Small World (HNSW) builds a multilayer proximity graph. Search enters sparse upper layers, navigates toward the query, then explores a denser neighborhood. HNSW usually offers a strong speed–recall tradeoff and needs no training step, but consumes memory, builds more slowly, increases write work, and still requires measured tuning.

What will you be able to do?

  • Create HNSW indexes with correct operator classes.
  • Explain m, ef_construction, and ef_search.
  • Tune recall against an exact baseline.
  • Build and monitor indexes safely.
  • Recognize filter and memory constraints.

How do the parameters work?

  • m is the maximum connections per layer; upstream default is 16.
  • ef_construction is the candidate-list size during graph construction; default is 64.
  • hnsw.ef_search is the candidate-list size per search; default is 40.

Higher values can improve recall but consume more build/query time and memory. Defaults and available settings are version-specific; inspect your installed pgvector documentation.

sql
CREATE INDEX CONCURRENTLY chunks_embedding_hnsw_cos ON app.chunk_embeddings USING hnsw (embedding vector_cosine_ops) WITH (m = 16, ef_construction = 64);

CONCURRENTLY reduces blocking of writes but takes longer, cannot run inside a transaction block, and has failure states you must inspect. For an empty new table, a normal build before traffic may be simpler.

How do you tune search per request?

sql
BEGIN; SET LOCAL hnsw.ef_search = 100; SELECT chunk_id FROM app.chunk_embeddings WHERE model_id = $1 ORDER BY embedding <=> $2::vector LIMIT 10; COMMIT;

Keep setting and query on the same transaction/connection. Sweep several values, recording exact recall@k, p50/p95 latency, CPU, buffers, and returned rows for each filter slice.

What matters during a build?

HNSW builds faster when its graph fits in maintenance_work_mem, but setting that too high across concurrent maintenance can exhaust server memory. Monitor the server, limit parallel build concurrency, and use progress views where available. Increase memory only from a capacity calculation.

How do you verify it?

Confirm index validity in pg_index, size via pg_relation_size, and usage with EXPLAIN (ANALYZE, BUFFERS). Compare at least five ef_search values against exact results on held-out queries. Test inserts and deletes after the build and observe write latency.

What breaks in production?

  • The query operator does not match the HNSW operator class.
  • A high global ef_search increases all query cost.
  • Memory is sized per build but several builds run together.
  • Recall is measured only on unfiltered popular queries.
  • Index bloat or write cost grows without maintenance observation.

AI pair-work prompt

Design an HNSW tuning experiment for [rows, dimensions, metric, query rate, filters, latency/recall goals]. Include build matrix, ef_search sweep, exact baseline, slice metrics, memory budget, concurrent-build procedure, write impact, and rollback.

Verification contract: Choose parameters from a plotted speed–recall curve and operational budget, not a copied setting.

Check your understanding

  1. Why can HNSW be built before rows exist?
  2. Which parameter changes per-query exploration?
  3. What proves the intended index was used?

Official references

  • pgvector HNSW
  • PostgreSQL CREATE INDEX
  • PostgreSQL create-index progress