pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: Manuelreyesbravo (@Manuelreyesbravo) <noreply+Manuelreyesbravo@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] PR #1020: Prefetch unvisited neighbors during HNSW search
Date: Tue, 25 Aug 2026 21:50:28 +0000
Message-ID: <gh-pgvector-pgvector-1020@github.com> (raw)

`HnswLoadUnvisitedFromDisk` fills the `unvisited` array with the tid of every neighbor the search is about to consider, so their block numbers are all known before the first one is read. The loop right after reads them one at a time, waiting on each. This asks for them up front so the reads can overlap.

The whole change is one loop, guarded to the on-disk path:

```c
if (!inMemory)
{
    for (int i = 0; i < unvisitedLength; i++)
        PrefetchBuffer(index, MAIN_FORKNUM,
                       ItemPointerGetBlockNumber(&unvisited[i].indextid));
}
```

## When it helps, and when it does not

38,352 `vector(768)` rows, 145 MB HNSW index, PostgreSQL 19beta2. Each run is 300 searches with a **different query vector every time** (pgbench), so the working set is the whole index rather than one repeated path.

| memory available | without | with | |
|---|---|---|---|
| 200 MB | 14.9 ms | 8.0 ms | **1.9x** |
| 350 MB | 5.03 ms | 4.96 ms | no difference |
| index fully resident | 0.756 ms | 0.737 ms | no difference |

It helps when I/O dominates and stays out of the way when it does not. I could not measure a cost in the resident case; HNSW visits few enough nodes that the extra calls disappear into the noise.

## Method

Getting this to measure anything real took three attempts, so the details matter:

- **Two prebuilt binaries** swapped between runs, not rebuilds. With the change already committed, `git stash` silently stashes nothing and you end up timing one binary against itself.
- **A/B order alternated** between rounds. The first run warms the cache and the second looks faster whichever binary it is.
- **Page cache dropped identically** before each run, and PostgreSQL confined to a memory cgroup. Without the cgroup a 145 MB index simply lives in the host page cache and reads land at ~3 us, which is memory. Inside it they land at ~56 us, which is the disk.

An earlier version of this measurement dropped the page cache before *every query* and reported 3.5x. That simulates "every search is the first one after a reboot" and overstates the effect by about two times. The 1.9x above is the number that survives a realistic workload.

## Correctness

- `make installcheck`: 14/14, including `hnsw_vector`, `hnsw_halfvec`, `hnsw_bit`, `hnsw_sparsevec`.
- Buffer reads and hits are identical with and without the change, down to the last buffer: the search walks the same graph and touches the same pages.
- The 50 nearest neighbors come back identical, in the same order, compared with `diff` between the two builds.

`PrefetchBuffer` is a hint and not a read: no pin, no lock, nothing to release, so it cannot change what the scan visits.

## Where this does not pay off

I tried the same change in a quantized graph index (pgvectorscale's DiskANN, ~0.7 KB per vector) and it measured as noise. With quantization many neighbors share one 8 KB page, so asking for sixteen of them is three or four reads, not sixteen. HNSW stores the full vector, each element takes close to a page of its own, and a neighborhood really is that many independent reads. That seems to be the condition for this to be worth anything.

## Caveats

One machine, one dataset, one index size. `ivfscan.c` has the same shape and is untouched here. Happy to run whatever else would make this easier to evaluate — different dimensions, `m`, `ef_search`, or a larger corpus.


inline patch 1: src/hnswutils.c (+17/-0 lines) download inline-1.patchdiff --git a/src/hnswutils.c b/src/hnswutils.c index c4372b143..547eec1d0 100644 --- a/src/hnswutils.c +++ b/src/hnswutils.c @@ -901,6 +901,23 @@ HnswSearchLayer(char *base, HnswQuery * q, List *ep, int ef, int lc, Relation in else HnswLoadUnvisitedFromDisk(cElement, unvisited, &unvisitedLength, v, index, m, lm, lc); + /* + * The block number of every unvisited neighbor is known at this point, + * but the loop below reads them one at a time, waiting on each. Issue + * the reads up front so they overlap; each HnswLoadElementImpl below + * then finds its page resident, or already in flight. + * + * This is a hint and not a read: it takes no pin and no lock, so there + * is nothing to release and no way for it to change what the search + * visits. A block already in shared buffers costs a lookup and no more. + */ + if (!inMemory) + { + for (int i = 0; i < unvisitedLength; i++) + PrefetchBuffer(index, MAIN_FORKNUM, + ItemPointerGetBlockNumber(&unvisited[i].indextid)); + } + /* OK to count elements instead of tuples */ if (tuples != NULL) (*tuples) += unvisitedLength;

view thread (15+ messages)  latest in thread

Message-ID: <gh-pgvector-pgvector-1020@github.com>
Permalink:  ../gh-pgvector-pgvector-1020@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