Sub topic 17
Index Build Time, Memory, and Maintenance
Assume you know PostgreSQL indexes, transactions, and the difference between exact and approximate vector search. This chapter focuses on operating HNSW and IVFFlat indexes after your table contains real data.
Why vector indexes need operational care
A vector index is built from your stored embeddings. Its build time and memory use depend on the number of rows, vector dimensions, index type, and tuning parameters.
| Index | Build behavior | Main memory control |
|---|---|---|
| HNSW | Builds a graph of nearby vectors | `maintenance_work_mem` |
| IVFFlat | Finds centroids and assigns vectors to lists | `maintenance_work_mem` and `lists` |
HNSW usually gives better recall and does not require training data before creation. It often takes longer to build and uses more memory. IVFFlat usually builds faster and uses less memory, but its quality depends on the number and quality of its lists.
Building an index without blocking writes
A normal CREATE INDEX can block some table operations while it runs. For a production table, use CREATE INDEX CONCURRENTLY when the extra build time and resource use are acceptable.
CREATE INDEX CONCURRENTLY items_embedding_hnsw
ON items USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64);Concurrent index creation runs in multiple transactions. It cannot run inside an explicit transaction, and a failed build can leave an invalid index. Check for invalid indexes before retrying:
SELECT indexrelid::regclass AS index_name,
indisvalid,
indisready
FROM pg_index
WHERE indexrelid::regclass::text = 'items_embedding_hnsw';If the index is invalid, remove it only after confirming that no other process is using it, then start the concurrent build again.
Controlling memory and build time
maintenance_work_mem controls memory available to many maintenance operations, including index creation. A larger value can make a build faster, especially for HNSW, but it consumes server memory. Set it for one session rather than changing the whole server without measurement.
SET maintenance_work_mem = '2GB';
CREATE INDEX items_embedding_ivfflat
ON items USING ivfflat (embedding vector_l2_ops)
WITH (lists = 100);Do not choose 2GB blindly. Leave enough memory for connections, queries, autovacuum workers, and the operating system. If several indexes build at once, each operation can consume substantial memory.
For IVFFlat, create the index after loading a representative amount of data. Its lists are based on the data present during the build. Building it on an almost empty table can produce poor partitions.
Maintaining indexes as data changes
Updates and deletes create dead index entries. PostgreSQL's VACUUM can reclaim space and keep scans healthy. Autovacuum should normally handle routine maintenance, but large embedding tables may need table-specific settings.
ALTER TABLE items SET (
autovacuum_vacuum_scale_factor = 0.02,
autovacuum_analyze_scale_factor = 0.01
);
VACUUM (ANALYZE) items;HNSW cleanup can be expensive. If many rows were deleted, rebuilding the index with REINDEX can be faster than repeatedly cleaning a heavily changed graph. Rebuilding also gives you a chance to change index parameters, but it requires planning for additional disk space and build resources.
A practical maintenance cycle
Exercise
Why should an IVFFlat index usually be built after loading representative data?
Show answer
IVFFlat creates its lists from the data available during the build. An unrepresentative sample can produce poor partitions and lower search quality.