From: Manuelreyesbravo (@Manuelreyesbravo) <noreply+Manuelreyesbravo@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] PR #1020: Prefetch unvisited neighbors during HNSW search
Date: Wed, 26 Aug 2026 22:54:13 +0000
Message-ID: <gh-pgvector-pgvector-1020-c5432041146@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-1020@github.com>
References: <gh-pgvector-pgvector-1020@github.com>
I should have read your branches before opening this. You already wrote this
patch: `hnsw-prefetch` (May 2025) is the same loop in the same function, and mine
differs only in guarding on `!inMemory` instead of `USE_PREFETCH`. You then moved
on to `hnsw-read-stream` and `hnsw-read-stream3`, which is clearly the better
shape. So the interesting question is not the one this PR asks — it is how the
two compare, and I could not find that measured. Here it is, including the part
that argues against my own patch.
Setup: 38,352 `vector(768)` rows, 145 MB HNSW index, PG19beta2. Three binaries
from one tree with identical flags, swapped between runs, A/B order alternated,
page cache dropped identically, PostgreSQL in a 200 MB cgroup, 300 searches per
run with a different query vector each time.
One correction was needed first: `hnsw-read-stream3` sits on an older master
whose Makefile predates `-ffp-contract=fast`, so comparing it against current
master would have measured two compilers rather than two algorithms, in distance
code. I cherry-picked `90aaf21` onto current master instead — it applies cleanly.
| | latency | vs master |
|---|---:|---:|
| master | 12.85 ms | |
| `hnsw-read-stream3` | 11.54 ms | 1.11x |
| this PR | 7.57 ms | 1.67x |
That gap is mostly not about read streams. It is the ramp-up. A stream starts at
`readahead_distance = 1` and doubles only when it has to wait for I/O, and it
decays on hits — and in HNSW two thirds of the accesses are hits (585 hits vs 278
reads per query), which arrive right before the misses do. Adding
`READ_STREAM_FULL` to your flags skips the ramp-up:
| | latency | |
|---|---:|---:|
| `hnsw-read-stream3` as written | 14.28 ms | |
| **+ `READ_STREAM_FULL`** | 10.78 ms | **1.32x** |
| this PR | | 1.14x over the above |
*(absolute numbers in that pair are inflated — something else was using the disk.
The A/B is internal and alternated, so the ratio holds, but do not compare those
milliseconds against the first table.)*
**So with a one-line change your branch lands within 14% of this patch**, and at
that distance I would keep the read stream: it gets I/O accounting, buffer
management and AIO integration from core, and this PR gets none of that. I think
that weakens the case for merging this, and it seemed worth saying plainly rather
than leaving it for you to find.
I also re-ran the head-to-head under `io_method=io_uring` rather than `worker`,
since testing something called "async I/O" on the less favourable backend would
not have been fair. It made no difference (1.58x vs 1.49x, noise ~1 ms).
All versions read the identical set of pages — 585 hits, 278 reads, to the block —
so none of this changes what the search visits.
One thing I got wrong along the way, in case it saves you time: I assumed the
per-node `read_stream_resume` restarted the look-ahead each neighborhood. It does
not — `resume` restores the saved distance, and its comment says it exists for
exactly this case ("streams of self-referential blocks"). The ramp-up happens once
per stream, not once per node.
Correction to an earlier version of this comment: I first described
`READ_STREAM_FULL` as a flag I was abusing, on the assumption that it meant "I am
reading the whole relation". It does not. Its documented meaning is exactly this
case — "this flag disables [the ramp-up], declaring ahead of time that we'll be
reading all available buffers". So it is not a workaround, it is the intended
mechanism, and an HNSW neighborhood does consume every block the callback
produces. That makes the fix a plain one-line flag addition rather than anything
core needs to grow.
Caveats: one machine, one dataset, one index size, one `m` and `ef_search`, and
your commit measured rebased rather than as you left it. Close this PR if the read
stream is where you want it to go — the `READ_STREAM_FULL` result stands on its
own and is yours to use.