pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: Ngalstyan4 (@Ngalstyan4) <noreply+Ngalstyan4@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #957: The case for including `NULL`s in pgvector indexes
Date: Wed, 18 Feb 2026 17:04:32 +0000
Message-ID: <gh-pgvector-pgvector-957@github.com> (raw)

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

```

view thread (5+ messages)  latest in thread

Message-ID: <gh-pgvector-pgvector-957@github.com>
Permalink:  ../gh-pgvector-pgvector-957@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