pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #991: Optimize zero-vector detection in vector_norm with early exit
3+ messages / 2 participants
[nested] [flat]
* [pgvector/pgvector] issue #991: Optimize zero-vector detection in vector_norm with early exit
@ 2026-06-24 16:35 "zthhhhh (@zthhhhh)" <noreply+zthhhhh@github.com>
0 siblings, 0 replies; 3+ messages in thread
From: zthhhhh (@zthhhhh) @ 2026-06-24 16:35 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
The `vector_norm`function computes the full Euclidean norm:
https://github.com/pgvector/pgvector/blob/master/src/vector.c#L763
In practice, `vector_norm ` is used as the NORM_PROC in HNSW and IVF operator classes (e.g. vector_cosine_ops) to detect all-zero vectors so they can be skipped during index building and insertion. For example, HnswCheckNorm and IvfflatCheckNorm both check vector_norm(...) > 0 , they only care whether the vector is zero, not the actual norm value.
Because of this, vector_norm always iterates through every element (computing squares and sqrt), even if the first element is already non-zero. This is wasteful when the only goal is zero detection.
A simple early-exit optimization can reduce the loop cost for non-zero vectors (the vast majority in practice):
```
for (int i = 0; i < a->dim; i++)
{
if (ax[i] != 0)
{
norm = 1.0;
break;
}
}
```
I'd like to propose adding a lightweight zero-check function (or optimizing the NORM_PROC usage) so that the index build/insert hot path doesn't pay the cost of a full Euclidean norm when only zero-detection is needed.
^ permalink raw [nested|flat] 3+ messages in thread
* Re: [pgvector/pgvector] issue #991: Optimize zero-vector detection in vector_norm with early exit
@ 2026-06-24 19:29 "ankane (@ankane)" <noreply+ankane@github.com>
1 sibling, 0 replies; 3+ messages in thread
From: ankane (@ankane) @ 2026-06-24 19:29 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @zthhhhh, thanks for the suggestion.
I wouldn't expect this to have a significant impact on build or insert time, so I don't think it's worth complicating the code, but someone is welcome to submit a PR with benchmarks that shows it does significantly improve performance while adding little code/complexity.
A few other thoughts:
<strike>The approach outlined would still need to square to ensure it's non-zero.</strike> Edit: this probably isn't necessary since the norm uses a wider type.
Two other approaches would be:
1. Pass the norm to the normalize function (this is how the code originally worked before multiple types)
2. Call the normalize function, then check for all zeros
^ permalink raw [nested|flat] 3+ messages in thread
* Re: [pgvector/pgvector] issue #991: Optimize zero-vector detection in vector_norm with early exit
@ 2026-06-24 19:29 "ankane (@ankane)" <noreply+ankane@github.com>
1 sibling, 0 replies; 3+ messages in thread
From: ankane (@ankane) @ 2026-06-24 19:29 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-06-24 19:29 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-06-24 16:35 [pgvector/pgvector] issue #991: Optimize zero-vector detection in vector_norm with early exit "zthhhhh (@zthhhhh)" <noreply+zthhhhh@github.com>
2026-06-24 19:29 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-06-24 19:29 ` "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