HNSW & Token Tuning
pgvector IVFFlat vs HNSW: Build Time, Recall & RSS Memory
Understand why pgvector HNSW outperforms IVFFlat on streaming agent writes and how to cap maintenance_work_mem build spikes.
HNSW: 1.35x Build RSS · Streaming Safe · No Re-Clustering
Why IVFFlat Fails Dynamic Agent Memory
IVFFlat partitions space around initial k-means centroids. Because agent memory grows continuously as users converse, early centroids go stale and recall drops silently unless you rebuild indexes offline.
Preventing maintenance_work_mem Spill
If maintenance_work_mem is too small when creating an HNSW index, PostgreSQL falls back to disk-backed graph construction that runs 10x slower. Size maintenance_work_mem to at least 1-2 GB on 8-16 GB nodes.
Measured Thresholds & Vector Stack Parameters
| pgvector Index Type | Streaming Inserts | Build RAM & Recall Profile |
|---|---|---|
| IVFFlat (lists=1000) | Degrades as data shifts | Low build RAM, requires periodic REINDEX |
| HNSW (m=16, ef=64) | Handles continuous add() | Needs 1.35x index size in maintenance_work_mem |
| HNSW halfvec(1536) | Handles continuous add() | 50% smaller index & build RSS than fp32 |