pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: bhagyesh-c (@bhagyesh-c) <noreply+bhagyesh-c@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] PR #990: Fix concurrent HNSW insert and vacuum race condition
Date: Tue, 23 Jun 2026 06:23:01 +0000
Message-ID: <gh-pgvector-pgvector-990@github.com> (raw)

## Summary

HNSW indexes can experience a race condition during concurrent INSERT and VACUUM, which causes INSERT queries to fail with a `cannot load deleted element` error.

The failure happens when an in-flight INSERT caches a deleted element in its candidate list while finding neighbors and traversing the graph, and VACUUM marks the element as deleted before the INSERT has finished evaluating it.

## Reproduction
This race condition can be reproduced using a gdb script that pauses an INSERT worker while VACUUM zeroes out an element that the INSERT has already queued in its candidate list. [Reproduction Script](https://gist.github.com/bhagyesh-c/cf7d73651c3359d11cd57d646ba8d967)

It can also be reproduced by heavy concurrent insert workloads running alongside vacuum.

## Root Cause
The crash occurs because VACUUM's third phase (`MarkDeleted`) physically zeroes out deleted elements when an in-flight INSERT holds a cached copy of the same element. Chronologically:

1. Element A is deleted in the heap.
2. VACUUM completes Phase 1 (`RemoveHeapTids`), and completes `RepairGraphEntryPoint` in Phase 2. 
3. A new INSERT begins, and inside `HnswFindElementNeighbors`, it loads the element A and adds it to the in memory candidate priority queue. INSERT holds `HNSW_UPDATE_LOCK` in Shared mode. (Exception: if there is an entry point promotion during the insert, the lock is upgraded to exclusive and the failure doesn’t occur)
4. VACUUM resumes and completes the remaining phases without any lock conflict.
5. INSERT resumes and tries to load element A which now has `etup->deleted=1`, failing the check:

```c
if (unlikely(etup->deleted))
	elog(ERROR, "cannot load deleted element");
```

## Fix

Introduce an `HNSW_UPDATE_LOCK` barrier alongside the existing scan lock barrier at the start of `MarkDeleted` phase.

## Verification
Reproduction script runs successfully without the insert query failing after the fix is applied.

inline patch 1: src/hnswvacuum.c (+6/-3 lines) download inline-1.patchdiff --git a/src/hnswvacuum.c b/src/hnswvacuum.c index 8361b315c..40e8588c9 100644 --- a/src/hnswvacuum.c +++ b/src/hnswvacuum.c @@ -584,13 +584,16 @@ MarkDeleted(HnswVacuumState * vacuumstate) BufferAccessStrategy bas = vacuumstate->bas; /* - * Wait for index scans to complete. Scans before this point may contain - * tuples about to be deleted. Scans after this point will not, since the - * graph has been repaired. + * Wait for index scans and inserts to complete. Scans and inserts before + * this point may contain tuples about to be deleted. Scans and inserts + * after this point will not, since the graph has been repaired. */ LockPage(index, HNSW_SCAN_LOCK, ExclusiveLock); UnlockPage(index, HNSW_SCAN_LOCK, ExclusiveLock); + LockPage(index, HNSW_UPDATE_LOCK, ExclusiveLock); + UnlockPage(index, HNSW_UPDATE_LOCK, ExclusiveLock); + while (BlockNumberIsValid(blkno)) { Buffer buf;

view thread (7+ messages)  latest in thread

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