pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #1033: `<=>` returns NaN for small nonzero vectors due to float32 squared-norm underflow
5+ messages / 2 participants
[nested] [flat]
* [pgvector/pgvector] issue #1033: `<=>` returns NaN for small nonzero vectors due to float32 squared-norm underflow
@ 2026-09-29 05:19 "JoeyLYZ666 (@JoeyLYZ666)" <noreply+JoeyLYZ666@github.com>
0 siblings, 0 replies; 5+ messages in thread
From: JoeyLYZ666 (@JoeyLYZ666) @ 2026-09-29 05:19 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
## 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.
--
^ permalink raw [nested|flat] 5+ messages in thread
* Re: [pgvector/pgvector] issue #1033: `<=>` returns NaN for small nonzero vectors due to float32 squared-norm underflow
@ 2026-09-29 05:30 "ankane (@ankane)" <noreply+ankane@github.com>
3 siblings, 0 replies; 5+ messages in thread
From: ankane (@ankane) @ 2026-09-29 05:30 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @JoeyLYZ666, like #1027, accumulation happens at single precision for performance. There's no plan to change this.
^ permalink raw [nested|flat] 5+ messages in thread
* Re: [pgvector/pgvector] issue #1033: `<=>` returns NaN for small nonzero vectors due to float32 squared-norm underflow
@ 2026-09-29 05:30 "ankane (@ankane)" <noreply+ankane@github.com>
3 siblings, 0 replies; 5+ messages in thread
From: ankane (@ankane) @ 2026-09-29 05:30 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 5+ messages in thread
* Re: [pgvector/pgvector] issue #1033: `<=>` returns NaN for small nonzero vectors due to float32 squared-norm underflow
@ 2026-10-26 02:44 "JoeyLYZ666 (@JoeyLYZ666)" <noreply+JoeyLYZ666@github.com>
3 siblings, 0 replies; 5+ messages in thread
From: JoeyLYZ666 (@JoeyLYZ666) @ 2026-10-26 02:44 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane hi,
I found a related issue in pgvector v0.8.6 involving the negative inner product operator (`<#>`), in addition to the L2 overflow described here.
Minimal reproduction:
```sql
SELECT '[1e20,1e20]'::vector <#> '[1e20,1e20]'::vector;
-- Actual: -Infinity
-- Expected: approximately -2e40 (finite in float64)
```
Both input vectors contain finite float32 values, and the mathematical result is representable in double precision. The L1 control with the same magnitude of inputs also returns a finite result.
The v0.8.6 implementation of `VectorInnerProduct()` uses a single-precision accumulator, which appears to cause the intermediate overflow before the result is widened to `double`.
I understand that single-precision accumulation may be an intentional performance trade-off for L2 distance. Does the same decision apply to the negative inner product operator, or would this be considered a separate issue worth addressing?
[reproduce.py](https://github.com/user-attachments/files/33944319/reproduce.py)
^ permalink raw [nested|flat] 5+ messages in thread
* Re: [pgvector/pgvector] issue #1033: `<=>` returns NaN for small nonzero vectors due to float32 squared-norm underflow
@ 2026-10-26 03:05 "ankane (@ankane)" <noreply+ankane@github.com>
3 siblings, 0 replies; 5+ messages in thread
From: ankane (@ankane) @ 2026-10-26 03:05 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Yes, it's the same situation.
^ permalink raw [nested|flat] 5+ messages in thread
end of thread, other threads:[~2026-10-26 03:05 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-09-29 05:19 [pgvector/pgvector] issue #1033: `<=>` returns NaN for small nonzero vectors due to float32 squared-norm underflow "JoeyLYZ666 (@JoeyLYZ666)" <noreply+JoeyLYZ666@github.com>
2026-09-29 05:30 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-09-29 05:30 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-10-26 02:44 ` "JoeyLYZ666 (@JoeyLYZ666)" <noreply+JoeyLYZ666@github.com>
2026-10-26 03:05 ` "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