pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #1027: [BUG] L2 distance overflows in a float intermediate and loses ordering
3+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #1027: [BUG] L2 distance overflows in a float intermediate and loses ordering
@ 2026-09-12 12:57  "leemeii (@leemeii)" <noreply+leemeii@github.com>
  0 siblings, 0 replies; 3+ messages in thread

From: leemeii (@leemeii) @ 2026-09-12 12:57 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

## Summary

In pgvector 0.8.6, the L2 distance calculation can overflow for valid finite vectors.

`VectorL2SquaredDistance()` accumulates squared differences in a single-precision `float`. For sufficiently large but valid vector components, the squared distance exceeds the maximum finite `float32` value and becomes `Infinity`.

This collapses distinct distances to the same value and can make nearest-neighbor ordering incorrect.

## Environment

* PostgreSQL 17.11
* pgvector 0.8.6
* Type: `vector(2)`
* Operator: `<->`
* No vector index
* Execution path: sequential scan followed by sort
* All input values are finite

## Minimal reproduction

Run this in a clean PostgreSQL session:

```sql
CREATE EXTENSION IF NOT EXISTS vector;

CREATE TEMP TABLE t (
    id integer,
    v  vector(2)
);

INSERT INTO t VALUES
    (2, '[1e20, 2e19]'),
    (1, '[1e20, 1e19]');

EXPLAIN (COSTS OFF)
SELECT id, v <-> '[0,0]'::vector AS distance
FROM t
ORDER BY v <-> '[0,0]'::vector
LIMIT 2;

SELECT id, v <-> '[0,0]'::vector AS distance
FROM t
ORDER BY v <-> '[0,0]'::vector
LIMIT 2;
```

## Observed result

On the tested builds, the query returns:

```text
 id | distance
----+----------
  2 | Infinity
  1 | Infinity
```

The observed order may vary because both sort keys are `Infinity`, but the important issue is that two different finite distances have been collapsed to the same value.

## Expected result

Mathematically:

```text
id=1:
sqrt((1e20)^2 + (1e19)^2) ≈ 1.0049875e20

id=2:
sqrt((1e20)^2 + (2e19)^2) ≈ 1.0198039e20
```

Therefore the expected order is:

```text
[1, 2]
```

The two distances are not equal.

## Root cause

The implementation uses:

```c
float distance = 0.0;

for (int i = 0; i < dim; i++)
{
    float diff = ax[i] - bx[i];
    distance += diff * diff;
}
```

For this input:

```text
id=1 squared distance = 1e40 + 1e38 = 1.01e40
id=2 squared distance = 1e40 + 4e38 = 1.04e40
```

The maximum finite `float32` value is approximately:

```text
3.4028235e38
```

Thus both squared distances overflow to positive infinity during the `float` calculation.

Although `l2_distance()` returns `double precision`, the conversion happens after the intermediate `float` value has already become `Infinity`:

```c
sqrt((double) VectorL2SquaredDistance(...))
```

The final mathematical distances are representable as `double precision`; the overflow is caused by the intermediate `float32` calculation.

Relevant source:

* [`[VectorL2SquaredDistance()](https://github.com/pgvector/pgvector/blob/v0.8.6/src/vector.c#L560-L588)`](https://github.com/pgvector/pgvector/blob/v0.8.6/src/vector.c#L560-L588)
* [[pgvector vector type and distance function documentation](https://github.com/pgvector/pgvector#reference)](https://github.com/pgvector/pgvector#reference)

## Scaling check

Multiplying both vectors and the query by `0.01` preserves the mathematical ordering, but avoids the intermediate overflow:

```sql
TRUNCATE t;

INSERT INTO t VALUES
    (2, '[1e18, 2e17]'),
    (1, '[1e18, 1e17]');

SELECT id, v <-> '[0,0]'::vector AS distance
FROM t
ORDER BY v <-> '[0,0]'::vector
LIMIT 2;
```

This returns the expected order:

```text
[1, 2]
```

## Why this is a bug

This is more than ordinary rounding error:

* the inputs are valid finite vectors;
* the true distances are finite and distinct;
* the true distances fit within `double precision`;
* the implementation converts both results to `Infinity`;
* nearest-neighbor ordering information is lost.

Could this be addressed by computing the subtraction, multiplication, and accumulation in `double`, or by using a numerically stable scaled sum-of-squares algorithm?

Related but different:

* [[Issue #762](https://github.com/pgvector/pgvector/issues/762)](https://github.com/pgvector/pgvector/issues/762)
* [[PR #871](https://github.com/pgvector/pgvector/pull/871)](https://github.com/pgvector/pgvector/pull/871)

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

* Re: [pgvector/pgvector] issue #1027: [BUG] L2 distance overflows in a float intermediate and loses ordering
@ 2026-09-14 17:09  "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

From: ankane (@ankane) @ 2026-09-14 17:09 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @leemeii, accumulation happens at single precision for performance. There's no plan to change this.

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

* Re: [pgvector/pgvector] issue #1027: [BUG] L2 distance overflows in a float intermediate and loses ordering
@ 2026-09-14 17:09  "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

From: ankane (@ankane) @ 2026-09-14 17:09 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:[~2026-09-14 17:09 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-09-12 12:57 [pgvector/pgvector] issue #1027: [BUG] L2 distance overflows in a float intermediate and loses ordering "leemeii (@leemeii)" <noreply+leemeii@github.com>
2026-09-14 17:09 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-09-14 17:09 ` "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