pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] PR #868: Increase sparsevec HNSW index limit to 1200 non-zero elements
3+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] PR #868: Increase sparsevec HNSW index limit to 1200 non-zero elements
@ 2025-07-02 20:59 "ysys143 (@ysys143)" <noreply+ysys143@github.com>
  0 siblings, 0 replies; 3+ messages in thread

From: ysys143 (@ysys143) @ 2025-07-02 20:59 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

This PR increases the maximum number of non-zero elements allowed in sparsevec for HNSW indexes from 1000 to 1200.

Users working with SPLADE reported encountering vectors with ~1100 non-zero elements. The current limit of 1000 prevents them from creating HNSW indexes on their data, forcing them to use workarounds like splitting vectors across multiple columns.

The change is straightforward - just updating `HNSW_MAX_NNZ` from 1000 to 1200 in `src/hnsw.h`. I've updated the tests to verify that vectors with 1200 elements can be indexed successfully, while 1201 elements still fail with the expected error message.

This is a small change that unblocks real-world usage without significant impact on memory or performance.

Fixes #749

inline patch 1: README.md, src/hnsw.h, test/expected/hnsw_sparsevec.out and 1 more (+24/-9 lines) download inline-1.patchdiff --git a/README.md b/README.md index 43c0430d2..828b14a74 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,8 @@ You can also install it with [Docker](#docker), [Homebrew](#homebrew), [PGXN](#p ### Windows +> **Note:** On Windows, `maintenance_work_mem` is currently limited to 2,024MB at most (up to and including PostgreSQL 16). It might therefore take a prohibitively long time to build indices for large vector stores. + Ensure [C++ support in Visual Studio](https://learn.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=msvc-170#download-and-...) is installed and run `x64 Native Tools Command Prompt for VS [version]` as administrator. Then use `nmake` to build: ```cmd diff --git a/src/hnsw.h b/src/hnsw.h index 5102bfb5a..9306e8530 100644 --- a/src/hnsw.h +++ b/src/hnsw.h @@ -13,7 +13,7 @@ #include "vector.h" #define HNSW_MAX_DIM 2000 -#define HNSW_MAX_NNZ 1000 +#define HNSW_MAX_NNZ 1200 /* Support functions */ #define HNSW_DISTANCE_PROC 1 diff --git a/test/expected/hnsw_sparsevec.out b/test/expected/hnsw_sparsevec.out index 2a6fe7c90..a9b5042d9 100644 --- a/test/expected/hnsw_sparsevec.out +++ b/test/expected/hnsw_sparsevec.out @@ -101,12 +101,18 @@ SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <+> (SELECT NULL::sparsevec)) DROP TABLE t; -- non-zero elements -CREATE TABLE t (val sparsevec(1001)); -INSERT INTO t (val) VALUES (array_fill(1, ARRAY[1001])::vector::sparsevec); +-- Test at the new limit (1200 elements should work) +CREATE TABLE t (val sparsevec(1200)); +INSERT INTO t (val) VALUES (array_fill(1, ARRAY[1200])::vector::sparsevec); CREATE INDEX ON t USING hnsw (val sparsevec_l2_ops); -ERROR: sparsevec cannot have more than 1000 non-zero elements for hnsw index +DROP TABLE t; +-- Test over the new limit (1201 elements should fail) +CREATE TABLE t (val sparsevec(1201)); +INSERT INTO t (val) VALUES (array_fill(1, ARRAY[1201])::vector::sparsevec); +CREATE INDEX ON t USING hnsw (val sparsevec_l2_ops); +ERROR: sparsevec cannot have more than 1200 non-zero elements for hnsw index TRUNCATE t; CREATE INDEX ON t USING hnsw (val sparsevec_l2_ops); -INSERT INTO t (val) VALUES (array_fill(1, ARRAY[1001])::vector::sparsevec); -ERROR: sparsevec cannot have more than 1000 non-zero elements for hnsw index +INSERT INTO t (val) VALUES (array_fill(1, ARRAY[1201])::vector::sparsevec); +ERROR: sparsevec cannot have more than 1200 non-zero elements for hnsw index DROP TABLE t; diff --git a/test/sql/hnsw_sparsevec.sql b/test/sql/hnsw_sparsevec.sql index 4ec07025f..562fe7df4 100644 --- a/test/sql/hnsw_sparsevec.sql +++ b/test/sql/hnsw_sparsevec.sql @@ -59,10 +59,17 @@ DROP TABLE t; -- non-zero elements -CREATE TABLE t (val sparsevec(1001)); -INSERT INTO t (val) VALUES (array_fill(1, ARRAY[1001])::vector::sparsevec); +-- Test at the new limit (1200 elements should work) +CREATE TABLE t (val sparsevec(1200)); +INSERT INTO t (val) VALUES (array_fill(1, ARRAY[1200])::vector::sparsevec); +CREATE INDEX ON t USING hnsw (val sparsevec_l2_ops); +DROP TABLE t; + +-- Test over the new limit (1201 elements should fail) +CREATE TABLE t (val sparsevec(1201)); +INSERT INTO t (val) VALUES (array_fill(1, ARRAY[1201])::vector::sparsevec); CREATE INDEX ON t USING hnsw (val sparsevec_l2_ops); TRUNCATE t; CREATE INDEX ON t USING hnsw (val sparsevec_l2_ops); -INSERT INTO t (val) VALUES (array_fill(1, ARRAY[1001])::vector::sparsevec); +INSERT INTO t (val) VALUES (array_fill(1, ARRAY[1201])::vector::sparsevec); DROP TABLE t;
^ permalink raw [nested|flat] 3+ messages in thread

* Re: [pgvector/pgvector] PR #868: Increase sparsevec HNSW index limit to 1200 non-zero elements
@ 2025-07-04 16:57 ` "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

From: ankane (@ankane) @ 2025-07-04 16:57 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @ysys143, thanks for the PR, but this won't work (please test changes before submitting a PR).

^ permalink  raw  [nested|flat] 3+ messages in thread

* Re: [pgvector/pgvector] PR #868: Increase sparsevec HNSW index limit to 1200 non-zero elements
@ 2025-07-04 16:57 ` "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

From: ankane (@ankane) @ 2025-07-04 16:57 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

^ permalink  raw  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2025-07-04 16:57 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-07-02 20:59 [pgvector/pgvector] PR #868: Increase sparsevec HNSW index limit to 1200 non-zero elements "ysys143 (@ysys143)" <noreply+ysys143@github.com>
2025-07-04 16:57 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-07-04 16:57 ` "ankane (@ankane)" <noreply+ankane@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox