pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: JoeyLYZ666 (@JoeyLYZ666) <noreply+JoeyLYZ666@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #1033: `<=>` returns NaN for small nonzero vectors due to float32 squared-norm underflow
Date: Tue, 29 Sep 2026 05:19:52 +0000
Message-ID: <gh-pgvector-pgvector-1033@github.com> (raw)

## What happens?

`v <=> v` returns `NaN` for finite, nonzero `vector` values once their component magnitude becomes sufficiently small.

For example, with `vector(2)` values `[n, 0]`:

| `n` | `v <=> v` | expected |
|---:|---:|---:|
| `0.1` | `0` | `0` |
| `1e-12` | `0` | `0` |
| `1e-19` | `0` | `0` |
| `1e-20` | `0` | `0` |
| `1e-25` | **`NaN`** | `0` |
| `1e-30` | **`NaN`** | `0` |
| `1e-38` | **`NaN`** | `0` |
| `1e-45` | **`NaN`** | `0` |

These inputs are nonzero and their components are representable as single-precision values. For `[1e-25, 0]`, the mathematical norm is `1e-25`, but the squared term `1e-25 * 1e-25 = 1e-50` is below the `float4` subnormal range.

The documented `<=>` operator is cosine distance, so a nonzero vector compared with itself should have distance `0`.

The behavior appears to come from the cosine implementation accumulating the dot product and squared norms in `float`:

```c
float similarity = 0.0;
float norma = 0.0;
float normb = 0.0;
```

before converting the accumulated values to `double` for the final division. For sufficiently small values, the squared terms underflow before that conversion occurs.

## What do you expect?

For finite, nonzero vectors accepted by the `vector` type, self cosine distance should remain finite and equal to `0`, regardless of positive scaling, or the unsupported numeric range should be explicitly rejected/documented.

One possible implementation direction would be to accumulate the dot product and squared norms in a wider type. `vector_norm()` already performs its squared-norm accumulation in double precision.

## How to reproduce?

```sql
CREATE TABLE t (id bigserial PRIMARY KEY, v vector(2));

INSERT INTO t (v) VALUES
  ('[0.1,0]'),
  ('[1e-20,0]'),
  ('[1e-25,0]'),
  ('[1e-30,0]');

SELECT v <=> v FROM t ORDER BY id;

-- observed:
-- 0
-- 0
-- NaN
-- NaN
```

The attached `reproduce.py` automates the same check with `psycopg2`, records all tested magnitudes in `observed.json`, drops the temporary table, and exits non-zero on mismatch.

[reproduce.py](https://github.com/user-attachments/files/32786106/reproduce.py)

## Environment

- pgvector `0.8.6`
- PostgreSQL `18.6`
- Docker image `pgvector/pgvector:0.8.6-pg18-trixie`
- x86_64
- Python `3.12`
- psycopg2 `2.9.13`
- Windows 11 host
- `vector(2)`, no index required for the core reproducer

## Notes

As a secondary consequence of the same NaN-producing scalar distance, I also observed problematic ordering with an HNSW `vector_cosine_ops` index in the same environment.

For one test corpus, an index scan for:

```sql
ORDER BY v <=> q
LIMIT 5
```

returned distances beginning with:

```text
[NaN, 0, 0, 0, 0]
```

while the scalar distance itself already demonstrates the underlying issue independently of ANN recall.

The primary report is therefore about `<=>` producing `NaN` for finite, nonzero vectors; the index behavior is included only as an observable downstream consequence.

--

view thread (5+ messages)  latest in thread

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