pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] PR #1023: Fix zero random value when choosing HNSW level
3+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] PR #1023: Fix zero random value when choosing HNSW level
@ 2026-09-07 12:10  "cuishuang (@cuishuang)" <noreply+cuishuang@github.com>
  0 siblings, 0 replies; 3+ messages in thread

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



`HnswInitElement()` currently chooses the HNSW level with:

```c
int level = (int) (-log(RandomDouble()) * ml);
```

`RandomDouble()` can return zero on supported PostgreSQL versions:

- PostgreSQL 15+: `pg_prng_double(&pg_global_prng_state)` returns values in `[0, 1)`.
- PostgreSQL 13/14: `random() / MAX_RANDOM_VALUE` can produce zero.

When this happens, the calculation becomes:

```text
log(0)       -> -Infinity
-log(0)      -> +Infinity
(int) +Inf   -> implementation-defined / non-portable result
```

The resulting level may then be converted to `uint8`, so this is a correctness and portability issue.



Retry the random-number generation when it returns exactly zero before calling `log()`:

```c
do
    uniform = RandomDouble();
while (uniform == 0.0);
```

This preserves the existing distribution and PostgreSQL 13/14 endpoint behavior. In particular, it avoids using `1.0 - RandomDouble()`, which would change the distribution and endpoint semantics.

inline patch 1: src/hnswutils.c (+9/-1 lines) download inline-1.patchdiff --git a/src/hnswutils.c b/src/hnswutils.c index c4372b143..929465cc6 100644 --- a/src/hnswutils.c +++ b/src/hnswutils.c @@ -246,7 +246,15 @@ HnswInitElement(char *base, ItemPointer heaptid, int m, double ml, int maxLevel, { HnswElement element = HnswAlloc(allocator, sizeof(HnswElementData)); - int level = (int) (-log(RandomDouble()) * ml); + double uniform; + int level; + + /* RandomDouble() can return zero on supported PostgreSQL versions. */ + do + uniform = RandomDouble(); + while (uniform == 0.0); + + level = (int) (-log(uniform) * ml); /* Cap level */ if (level > maxLevel)
^ permalink raw [nested|flat] 3+ messages in thread

* Re: [pgvector/pgvector] PR #1023: Fix zero random value when choosing HNSW level
@ 2026-09-08 14:26  "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

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

@ankane closed this.

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

* Re: [pgvector/pgvector] PR #1023: Fix zero random value when choosing HNSW level
@ 2026-09-08 14:27  "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

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

Hi @cuishuang, thanks for reporting and the PR! Merged a version of this in the commit above that avoids looping.

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


end of thread, other threads:[~2026-09-08 14:27 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-09-07 12:10 [pgvector/pgvector] PR #1023: Fix zero random value when choosing HNSW level "cuishuang (@cuishuang)" <noreply+cuishuang@github.com>
2026-09-08 14:26 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-09-08 14:27 ` "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