DEV Community

Eren
Eren

Posted on Originally published at aiandsoftwarehub.com

pgvector IVFFlat vs HNSW: what happens to recall when new data arrives (measured)

Most comparisons of pgvector's two index types quote one recall number from one dataset. I ran both on the same machine with three different data situations, because the answer flipped between them.

Setup: PostgreSQL 16.2, pgvector 0.6.2, Windows 11 laptop, 100,000 vectors of 128 dimensions, cosine distance, 100 query vectors, recall@10 measured against an exact scan of the same table. Index builds were single-threaded so the build times are comparable with each other, not with a tuned server. The data is synthetic (Gaussian clusters), so read the numbers as the shape of the behaviour, not as what your embeddings will score.

1. Easy, clustered data: IVFFlat wins on build cost

Index and setting Recall@10 Median latency
Exact scan, no index 1.000 35.0 ms
IVFFlat lists=316, probes=10 0.997 1.05 ms
HNSW, ef_search=100 0.953 1.01 ms
HNSW, ef_search=400 0.992 2.23 ms

Build: IVFFlat took 4.6 to 5.3 seconds (about 53 MiB). HNSW took 67.4 seconds (79.4 MiB). That is roughly 13 times slower to build and about 50 percent larger.

One trap: IVFFlat with probes = lists scans every list. On the 100-list index the median query took 149 ms, four times slower than the exact scan (35 ms), because you pay for the index and read everything anyway.

2. Hard data (barely clustered): close to a tie

At about 5 ms the two were close: IVFFlat 0.807 at 5.27 ms against HNSW 0.839 at 4.98 ms.

3. New data arrives: this is where they separate

I built each index on the first 100,000 rows, then inserted 50,000 more from 20 clusters that did not exist when the index was built, and queried only that new region.

State Recall@10 Median latency
IVFFlat built before the new rows, probes=10 0.473 5.01 ms
IVFFlat after REINDEX, probes=10 1.000 4.47 ms
HNSW grown by inserts, ef_search=40 0.834 0.90 ms
HNSW built fresh on all rows, ef_search=40 0.831 0.84 ms

IVFFlat picks its centroids once, at build time. The new clusters had no centroid of their own, so queries had to open many lists to find them. REINDEX recomputed the centroids and recall went from 0.473 to 1.000. HNSW had no such step. The cost was time: inserting the 50,000 rows into the HNSW-indexed table took 100 seconds.

Also worth knowing: creating an IVFFlat index on an empty table (or on 50 rows with lists=100) prints ivfflat index created with little data. Build the index after loading the data.

Choosing from these numbers

  • Data loaded once or in rare batches, rebuild is acceptable: IVFFlat. Same or better recall at the same latency, 5-second build, smaller index. Keep probes well below lists.
  • Rows keep arriving, or new rows look different from old ones: HNSW kept its recall without a rebuild. IVFFlat needs a scheduled REINDEX.
  • Insert speed matters most: HNSW inserts were slow in my test. I did not measure IVFFlat insert time.

What this does not show

Synthetic data, 128 dimensions, one machine, pgvector 0.6.2, 100 queries per setting, one run each. Small differences like 0.834 vs 0.831 are not meaningful. I did not test filtered queries, concurrent writers, parallel builds, larger tables or indexes that do not fit in memory.

Full method, all tables and the FAQ are in the original write-up: https://aiandsoftwarehub.com/blog/pgvector-ivfflat-vs-hnsw-recall-latency-build-time-new-data-measured

Top comments (0)