Copyright (c) 2026 MindMesh Academy. All rights reserved. This content is proprietary and may not be reproduced or distributed without permission.

2.2.3. Indexing Strategies and pgvector Optimization

💡 First Principle: Every index is a prepaid query: you spend build time, memory, and write overhead now so reads are cheap later. pgvector just adds a twist — its indexes also spend accuracy. Tuning pgvector means deciding how much recall to sell for how much speed.

Standard indexing arrives first: B-tree for equality and ranges (doc_id, created_at), GIN for jsonb metadata containment (metadata @> '{"lang": "en"}'). These make the filter half of filtered vector search fast. For the vector half, pgvector offers two index types, and choosing between them is a marquee exam decision:

HNSWIVFFlat
StructureMulti-layer proximity graphk-means clusters ("lists")
Build time / memorySlow, memory-hungryFast, lighter
Query recall & speedHigher recall at low latencyGood, more tuning-sensitive
Key parametersm, ef_construction (build); ef_search (query)lists (build); probes (query)
Data prerequisiteNone — build anytimeNeeds representative data before build
Choose whenProduction RAG, evolving dataFast rebuilds, memory-constrained
CREATE INDEX ON chunks USING hnsw (embedding vector_cosine_ops);
SET hnsw.ef_search = 80;   -- higher = better recall, slower queries

The query-time knobs (ef_search, probes) are your recall dial — the "how many shelves to check" from the library model in 2.2. Raising them buys recall with latency; lowering them does the reverse. This is also the honest answer to the syllabus bullet on reducing pgvector compute overhead: right-size the dial, build indexes with adequate maintenance_work_mem so they construct efficiently, exclude vectors you never search, and consider halfvec (16-bit floats) to halve memory when your model tolerates it.

For small tables (a few thousand rows), the correct index is no index — a sequential scan is exact, fast enough, and free of build cost. Optimization means matching machinery to scale, not maximal machinery.

⚠️ Exam Trap: IVFFlat's clusters are computed from the data present at build time. Creating an IVFFlat index on an empty table, then loading millions of rows, yields garbage clusters and poor recall. Sequence matters: load data, then build IVFFlat. HNSW has no such prerequisite — a clean discriminator between the two in scenario questions.

Reflection Question: Latency is fine but users report missing obviously relevant documents. Which single parameter do you raise first on an HNSW index, and what do you trade away?

Alvin Varughese
Written byAlvin Varughese
Founder18 professional certifications