From a48dfdfc496338f68a2b458afb37ca0e28595bde Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Thu, 3 Dec 2026 15:50:36 -0800 Subject: [PATCH] Enabled iterative index scans by default for HNSW [skip ci] --- CHANGELOG.md | 5 ++++- README.md | 18 ++++++++++++++++++ src/hnsw.c | 2 +- test/expected/hnsw_vector.out | 2 +- test/t/015_hnsw_vector_duplicates.pl | 1 + test/t/023_hnsw_bit_duplicates.pl | 1 + test/t/027_hnsw_halfvec_duplicates.pl | 1 + test/t/031_hnsw_sparsevec_duplicates.pl | 1 + 8 files changed, 28 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6929d28d..30f12744 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ -## 0.9.0 (unreleased) +## 1.0.0 (unreleased) +See the [upgrade notes](README.md#1-0) to avoid a performance regression with certain queries + +- Enabled iterative index scans by default for HNSW - Added asynchronous I/O for indexes - Added vacuuming progress for indexes - Added `vector_dims` function for `sparsevec` diff --git a/README.md b/README.md index 77148166..ee65e524 100644 --- a/README.md +++ b/README.md @@ -1287,6 +1287,24 @@ You can check the version in the current database with: SELECT extversion FROM pg_extension WHERE extname = 'vector'; ``` +## Upgrade Notes + +### 1.0 + +pgvector 1.0 enables iterative index scans by default for HNSW. Unfortunately, this can cause a performance regression for queries with a distance filter due to the [current behavior](https://www.postgresql.org/message-id/flat/CAOdR5yGUoMQ6j7M5hNUXrySzaqZVGf_Ne%2B8fwZMRKTFxU1nbJg%40mail.gmail.com) of the Postgres executor. + +```sql +SELECT id FROM items WHERE embedding <-> '[1,2,3]' < 5 ORDER BY embedding <-> '[1,2,3]' LIMIT 5; +``` + +Before upgrading, update these queries to use a materialized CTE and place the distance filter outside of it. + +```sql +WITH nearest_results AS MATERIALIZED ( + SELECT id, embedding <-> '[1,2,3]' AS distance FROM items ORDER BY distance LIMIT 5 +) SELECT id FROM nearest_results WHERE distance < 5 ORDER BY distance; +``` + ## Thanks Thanks to: diff --git a/src/hnsw.c b/src/hnsw.c index ae5263ad..6447c53a 100644 --- a/src/hnsw.c +++ b/src/hnsw.c @@ -106,7 +106,7 @@ HnswInit(void) DefineCustomEnumVariable("hnsw.iterative_scan", "Sets the mode for iterative scans", NULL, &hnsw_iterative_scan, - HNSW_ITERATIVE_SCAN_OFF, hnsw_iterative_scan_options, PGC_USERSET, GUC_EXPLAIN, NULL, NULL, NULL); + HNSW_ITERATIVE_SCAN_STRICT, hnsw_iterative_scan_options, PGC_USERSET, GUC_EXPLAIN, NULL, NULL, NULL); /* This is approximate and does not affect the initial scan */ DefineCustomIntVariable("hnsw.max_scan_tuples", "Sets the max number of tuples to visit for iterative scans", diff --git a/test/expected/hnsw_vector.out b/test/expected/hnsw_vector.out index 1641f040..7a1b37e6 100644 --- a/test/expected/hnsw_vector.out +++ b/test/expected/hnsw_vector.out @@ -184,7 +184,7 @@ ERROR: 1001 is outside the valid range for parameter "hnsw.ef_search" (1 .. 100 SHOW hnsw.iterative_scan; hnsw.iterative_scan --------------------- - off + strict_order (1 row) SET hnsw.iterative_scan = on; diff --git a/test/t/015_hnsw_vector_duplicates.pl b/test/t/015_hnsw_vector_duplicates.pl index c68eb030..7b474bec 100644 --- a/test/t/015_hnsw_vector_duplicates.pl +++ b/test/t/015_hnsw_vector_duplicates.pl @@ -26,6 +26,7 @@ sub test_duplicates my $res = $node->safe_psql("postgres", qq( SET enable_seqscan = off; SET hnsw.ef_search = 1; + SET hnsw.iterative_scan = off; SELECT COUNT(*) FROM (SELECT * FROM tst ORDER BY v <-> '[1,1,1]') t; )); is($res, 10); diff --git a/test/t/023_hnsw_bit_duplicates.pl b/test/t/023_hnsw_bit_duplicates.pl index f6d928c4..a4e8acde 100644 --- a/test/t/023_hnsw_bit_duplicates.pl +++ b/test/t/023_hnsw_bit_duplicates.pl @@ -26,6 +26,7 @@ sub test_duplicates my $res = $node->safe_psql("postgres", qq( SET enable_seqscan = off; SET hnsw.ef_search = 1; + SET hnsw.iterative_scan = off; SELECT COUNT(*) FROM (SELECT * FROM tst ORDER BY v <~> '111') t; )); is($res, 10); diff --git a/test/t/027_hnsw_halfvec_duplicates.pl b/test/t/027_hnsw_halfvec_duplicates.pl index 80f88530..3e4374a5 100644 --- a/test/t/027_hnsw_halfvec_duplicates.pl +++ b/test/t/027_hnsw_halfvec_duplicates.pl @@ -26,6 +26,7 @@ sub test_duplicates my $res = $node->safe_psql("postgres", qq( SET enable_seqscan = off; SET hnsw.ef_search = 1; + SET hnsw.iterative_scan = off; SELECT COUNT(*) FROM (SELECT * FROM tst ORDER BY v <-> '[1,1,1]') t; )); is($res, 10); diff --git a/test/t/031_hnsw_sparsevec_duplicates.pl b/test/t/031_hnsw_sparsevec_duplicates.pl index 09b8380b..c76d944d 100644 --- a/test/t/031_hnsw_sparsevec_duplicates.pl +++ b/test/t/031_hnsw_sparsevec_duplicates.pl @@ -26,6 +26,7 @@ sub test_duplicates my $res = $node->safe_psql("postgres", qq( SET enable_seqscan = off; SET hnsw.ef_search = 1; + SET hnsw.iterative_scan = off; SELECT COUNT(*) FROM (SELECT * FROM tst ORDER BY v <-> '{1:1,2:1,3:1}/3') t; )); is($res, 10);