pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] PR #990: Fix concurrent HNSW insert and vacuum race condition
7+ messages / 2 participants
[nested] [flat]
* [pgvector/pgvector] PR #990: Fix concurrent HNSW insert and vacuum race condition
@ 2026-06-23 06:23 "bhagyesh-c (@bhagyesh-c)" <noreply+bhagyesh-c@github.com>
0 siblings, 0 replies; 7+ messages in thread
From: bhagyesh-c (@bhagyesh-c) @ 2026-06-23 06:23 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
## 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.patch
diff --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;
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] PR #990: Fix concurrent HNSW insert and vacuum race condition
@ 2026-06-23 17:18 ` "ankane (@ankane)" <noreply+ankane@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: ankane (@ankane) @ 2026-06-23 17:18 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] PR #990: Fix concurrent HNSW insert and vacuum race condition
@ 2026-06-23 17:18 ` "ankane (@ankane)" <noreply+ankane@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: ankane (@ankane) @ 2026-06-23 17:18 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane merged this (commit ffe28bb954cb).
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] PR #990: Fix concurrent HNSW insert and vacuum race condition
@ 2026-06-23 17:41 ` "ankane (@ankane)" <noreply+ankane@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: ankane (@ankane) @ 2026-06-23 17:41 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Thanks @bhagyesh-c, another good find.
For clarity, it's a handled, transient error rather than a crash.
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] PR #990: Fix concurrent HNSW insert and vacuum race condition
@ 2026-06-23 18:31 ` "ankane (@ankane)" <noreply+ankane@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: ankane (@ankane) @ 2026-06-23 18:31 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
> It can also be reproduced by heavy concurrent insert workloads running alongside vacuum.
Also, I'm a bit skeptical this will occur in practice. If you have a reproduction script that doesn't use GDB, please share.
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] PR #990: Fix concurrent HNSW insert and vacuum race condition
@ 2026-06-24 06:10 ` "bhagyesh-c (@bhagyesh-c)" <noreply+bhagyesh-c@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: bhagyesh-c (@bhagyesh-c) @ 2026-06-24 06:10 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Thanks @ankane for the quick review.
> Also, I'm a bit skeptical this will occur in practice. If you have a reproduction script that doesn't use GDB, please share.
We discovered this through a stress testing workload which does frequent bulk inserts over a long period of time. It is definitely rare but it can occur under very high load.
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] PR #990: Fix concurrent HNSW insert and vacuum race condition
@ 2026-06-24 18:43 ` "ankane (@ankane)" <noreply+ankane@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: ankane (@ankane) @ 2026-06-24 18:43 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Thanks for the additional context.
^ permalink raw [nested|flat] 7+ messages in thread
end of thread, other threads:[~2026-06-24 18:43 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-06-23 06:23 [pgvector/pgvector] PR #990: Fix concurrent HNSW insert and vacuum race condition "bhagyesh-c (@bhagyesh-c)" <noreply+bhagyesh-c@github.com>
2026-06-23 17:18 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-06-23 17:18 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-06-23 17:41 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-06-23 18:31 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-06-24 06:10 ` "bhagyesh-c (@bhagyesh-c)" <noreply+bhagyesh-c@github.com>
2026-06-24 18:43 ` "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