pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #957: The case for including `NULL`s in pgvector indexes
5+ messages / 3 participants
[nested] [flat]

* [pgvector/pgvector] issue #957: The case for including `NULL`s in pgvector indexes
@ 2026-02-18 17:04  "Ngalstyan4 (@Ngalstyan4)" <noreply+Ngalstyan4@github.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Ngalstyan4 (@Ngalstyan4) @ 2026-02-18 17:04 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Per pgvector's readme, currently:
> NULL vectors are not indexed (as well as zero vectors for cosine distance).

This causes unexpected behaviour in at least one contrived edge case  -- the same query (`SELECT * FROM t ORDER BY val <-> '[1,1,1]';`) returns different number of rows depending on execution plan (`NULL`s missing from vector index scan, but present in sequential scan).  Would be great to get your thoughts this and whether it is worth to either include `NULL`s in the index or document the edge case (I tried searching for it but did not find it documented, sorry if it is already known). 

Fully reproducible example SQL and output below.


`pgvector_example.sql`:
```SQL
-- pgvector_example.sql
-- Demonstrates inconsistent query results when NULL vectors are present
-- depending on whether an index is used or not.

-- Setup
DROP TABLE IF EXISTS t;
CREATE TABLE t (id serial PRIMARY KEY, val vector(3));
INSERT INTO t (val) VALUES ('[1,2,3]'), ('[4,5,6]'), ('[7,8,9]'), (NULL);

-- Create HNSW index (NULL vectors are not indexed)
CREATE INDEX ON t USING hnsw (val vector_l2_ops);

-- Query 1: With index (enable indexscan, disable seqscan)
SET enable_seqscan = off;
SET enable_indexscan = on;

SELECT '=== With Index ===' AS info;
SELECT * FROM t ORDER BY val <-> '[1,1,1]';
-- Result: 3 rows (NULL row is missing because it's not in the index)

-- Query 2: Without index (enable seqscan, disable indexscan)
SET enable_seqscan = on;
SET enable_indexscan = off;

SELECT '=== Without Index (Sequential Scan) ===' AS info;
SELECT * FROM t ORDER BY val <-> '[1,1,1]';
-- Result: 4 rows (NULL row is included)

-- The same query returns different row counts depending on the execution plan.
-- This is unexpected: the index should only affect performance, not results.

-- Cleanup
DROP TABLE t;

```

`pgvector_example.out`:
```SQL
-- pgvector_example.sql
-- Demonstrates inconsistent query results when NULL vectors are present
-- depending on whether an index is used or not.
-- Setup
DROP TABLE IF EXISTS t;
psql:../pgvector_example.sql:6: NOTICE:  table "t" does not exist, skipping
DROP TABLE
CREATE TABLE t (id serial PRIMARY KEY, val vector(3));
CREATE TABLE
INSERT INTO t (val) VALUES ('[1,2,3]'), ('[4,5,6]'), ('[7,8,9]'), (NULL);
INSERT 0 4
-- Create HNSW index (NULL vectors are not indexed)
CREATE INDEX ON t USING hnsw (val vector_l2_ops);
CREATE INDEX
-- Query 1: With index (enable indexscan, disable seqscan)
SET enable_seqscan = off;
SET
SET enable_indexscan = on;
SET
SELECT '=== With Index ===' AS info;
        info        
--------------------
 === With Index ===
(1 row)

SELECT * FROM t ORDER BY val <-> '[1,1,1]';
 id |   val   
----+---------
  1 | [1,2,3]
  2 | [4,5,6]
  3 | [7,8,9]
(3 rows)

-- Result: 3 rows (NULL row is missing because it's not in the index)
-- Query 2: Without index (enable seqscan, disable indexscan)
SET enable_seqscan = on;
SET
SET enable_indexscan = off;
SET
SELECT '=== Without Index (Sequential Scan) ===' AS info;
                  info                   
-----------------------------------------
 === Without Index (Sequential Scan) ===
(1 row)

SELECT * FROM t ORDER BY val <-> '[1,1,1]';
 id |   val   
----+---------
  1 | [1,2,3]
  2 | [4,5,6]
  3 | [7,8,9]
  4 | 
(4 rows)

-- Result: 4 rows (NULL row is included)
-- The same query returns different row counts depending on the execution plan.
-- This is unexpected: the index should only affect performance, not results.
-- Cleanup
DROP TABLE t;
DROP TABLE

```

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

* Re: [pgvector/pgvector] issue #957: The case for including `NULL`s in pgvector indexes
@ 2026-02-18 23:22  "jkatz (@jkatz)" <noreply+jkatz@github.com>
  3 siblings, 0 replies; 5+ messages in thread

From: jkatz (@jkatz) @ 2026-02-18 23:22 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

This is the difference between exact nearest neighbor and approximate nearest neighbor. Even without considering NULLs, a sequential scan could return different results than an ANN index scan.

But the other aspect is that you can't calculate the distance between a vector and a NULL, so it wouldn't surface in the index except in the case you're posting, and a sequential scan would be faster at returning that.

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

* Re: [pgvector/pgvector] issue #957: The case for including `NULL`s in pgvector indexes
@ 2026-02-19 00:47  "Ngalstyan4 (@Ngalstyan4)" <noreply+Ngalstyan4@github.com>
  3 siblings, 0 replies; 5+ messages in thread

From: Ngalstyan4 (@Ngalstyan4) @ 2026-02-19 00:47 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

That's fair. I am aware of the difference.
I had previously seen cases where this kind of query would return significantly fewer results in index scan, compared to sequential scan. E.g. if I had ~1000 non-null vectors in the table, the query would return order of 100 results with default `ef_search` parameter, while sequential scan would return all 1000 results. 


But despite that, I was still thrown off when I first noticed that the exact same query (`SELECT * FROM t ORDER BY val <-> '[1,1,1]';`) can return almost the same but slightly different results in the two cases. So, decided to bring it up in here, in case it is worth addressing in any way.  The answer might be "no", in which case feel free to close the issue!


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

* Re: [pgvector/pgvector] issue #957: The case for including `NULL`s in pgvector indexes
@ 2026-02-19 03:43  "ankane (@ankane)" <noreply+ankane@github.com>
  3 siblings, 0 replies; 5+ messages in thread

From: ankane (@ankane) @ 2026-02-19 03:43 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @Ngalstyan4, it's exactly what @jkatz mentioned. I don't think there's a strong reason to change this.

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

* Re: [pgvector/pgvector] issue #957: The case for including `NULL`s in pgvector indexes
@ 2026-02-19 03:43  "ankane (@ankane)" <noreply+ankane@github.com>
  3 siblings, 0 replies; 5+ messages in thread

From: ankane (@ankane) @ 2026-02-19 03:43 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

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


end of thread, other threads:[~2026-02-19 03:43 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-02-18 17:04 [pgvector/pgvector] issue #957: The case for including `NULL`s in pgvector indexes "Ngalstyan4 (@Ngalstyan4)" <noreply+Ngalstyan4@github.com>
2026-02-18 23:22 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2026-02-19 00:47 ` "Ngalstyan4 (@Ngalstyan4)" <noreply+Ngalstyan4@github.com>
2026-02-19 03:43 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-02-19 03:43 ` "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