pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] PR #988: Fix HNSW index corruption during high INSERT and VACUUM concurrency
7+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] PR #988: Fix HNSW index corruption during high INSERT and VACUUM concurrency
@ 2026-06-15 14:44  "bhagyesh-c (@bhagyesh-c)" <noreply+bhagyesh-c@github.com>
  0 siblings, 0 replies; 7+ messages in thread

From: bhagyesh-c (@bhagyesh-c) @ 2026-06-15 14:44 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

## Summary

Under high concurrent DML load, HNSW indexes can become corrupted during `VACUUM`. This leads to persistent failures (`different vector dimensions` errors) in subsequent vacuums and and queries which scan the corrupted element. 

The corruption happens when a concurrent `INSERT` promotes a new element to the global entry point during vacuum's first phase (`RemoveHeapTids`). Because `VACUUM` caches a stale view of the entry point, it incorrectly assigns this newly promoted entry point as the `highestPoint` (which should strictly be a non-entry point). This ultimately causes vacuum to skip repairing the entry point, leaving behind a dangling edge.

## Reproduction

This race condition can be reliably reproduced using a GDB script that pauses the vacuum worker during the critical window while running concurrent inserts to force a promotion. 

* **Reproduction Script:** [reproduction.sh](https://gist.github.com/bhagyesh-c/81141580b22261d7eacb20050d4fee77)

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

## Root Cause

The corruption occurs in three steps:

1. **Phase 1 (`RemoveHeapTids`):** Vacuum scans pages to find a `highestPoint` candidate. It evaluates elements using a cached, stale `entryPoint`. If a concurrent insert promotes a new element to the entry point, `blkno == entryPoint->blkno` evaluates to `false`. Vacuum mistakenly selects this new entry point as the `highestPoint`.
2. **Phase 2 (`RepairGraphEntryPoint`):** Vacuum fetches the true latest entry point (the new element) and calls:
   `RepairGraphElement(vacuumstate, entryPoint, highestPoint);`
   Because `entryPoint` and `highestPoint` are now the exact same element, the safety check inside `RepairGraphElement` triggers and exits immediately. The entry point is left unrepaired and still contains edges pointing to deleted elements.
3. **Phase 3 (`MarkDeleted`):** When these elements are physically zeroed out, the index is left with a dangling edge pointing to a 0-dimension vector.

## Fix

1. **Add a Fallback:** Introduce a `fallbackPoint` inside `RemoveHeapTids` to track a secondary candidate for the highest point.
2. **Shift Synchronization:** Move the `HNSW_UPDATE_LOCK` unlock sequence earlier, from the start of `RepairGraph` to the end of `RemoveHeapTids`. This ensures all in-flight concurrent inserts commit before we verify the entry point.
3. **Safe Demotion:** . We then check the latest `entryPoint`. If it matches our scanned `highestPoint`, we demote `highestPoint` to the `fallbackPoint` so that Phase 2 operates on a valid non-entry point.

## Verification
* The provided reproduction script completes successfully and reports no index corruption.

inline patch 1: src/hnswvacuum.c (+36/-8 lines) download inline-1.patchdiff --git a/src/hnswvacuum.c b/src/hnswvacuum.c index 5de9b43b5..2e8844544 100644 --- a/src/hnswvacuum.c +++ b/src/hnswvacuum.c @@ -37,6 +37,8 @@ RemoveHeapTids(HnswVacuumState * vacuumstate) { BlockNumber blkno = HNSW_HEAD_BLKNO; HnswElement highestPoint = &vacuumstate->highestPoint; + HnswElementData fallbackPointData; + HnswElement fallbackPoint = &fallbackPointData; Relation index = vacuumstate->index; BufferAccessStrategy bas = vacuumstate->bas; HnswElement entryPoint = HnswGetEntryPoint(vacuumstate->index); @@ -44,10 +46,13 @@ RemoveHeapTids(HnswVacuumState * vacuumstate) /* Store separately since highestPoint.level is uint8 */ int highestLevel = -1; + int fallbackLevel = -1; - /* Initialize highest point */ + /* Initialize highest point and fallback point */ highestPoint->blkno = InvalidBlockNumber; highestPoint->offno = InvalidOffsetNumber; + fallbackPoint->blkno = InvalidBlockNumber; + fallbackPoint->offno = InvalidOffsetNumber; while (BlockNumberIsValid(blkno)) { @@ -121,12 +126,26 @@ RemoveHeapTids(HnswVacuumState * vacuumstate) } else if (etup->level > highestLevel && !(entryPoint != NULL && blkno == entryPoint->blkno && offno == entryPoint->offno)) { + /* Current highest becomes fallback */ + fallbackPoint->blkno = highestPoint->blkno; + fallbackPoint->offno = highestPoint->offno; + fallbackPoint->level = highestPoint->level; + fallbackLevel = highestLevel; + /* Keep track of highest non-entry point */ highestPoint->blkno = blkno; highestPoint->offno = offno; highestPoint->level = etup->level; highestLevel = etup->level; } + else if (etup->level > fallbackLevel && !(entryPoint != NULL && blkno == entryPoint->blkno && offno == entryPoint->offno)) + { + /* Keep track of second highest non-entry point */ + fallbackPoint->blkno = blkno; + fallbackPoint->offno = offno; + fallbackPoint->level = etup->level; + fallbackLevel = etup->level; + } } blkno = HnswPageGetOpaque(page)->nextblkno; @@ -138,6 +157,22 @@ RemoveHeapTids(HnswVacuumState * vacuumstate) UnlockReleaseBuffer(buf); } + + /* + * Wait for inserts to complete. Inserts before this point may have + * neighbors about to be deleted. Inserts after this point will not. + */ + LockPage(index, HNSW_UPDATE_LOCK, ExclusiveLock); + UnlockPage(index, HNSW_UPDATE_LOCK, ExclusiveLock); + + /* Check if highest point is actually the new entry point */ + entryPoint = HnswGetEntryPoint(index); + if (entryPoint != NULL && highestPoint->blkno == entryPoint->blkno && highestPoint->offno == entryPoint->offno) + { + highestPoint->blkno = fallbackPoint->blkno; + highestPoint->offno = fallbackPoint->offno; + highestPoint->level = fallbackPoint->level; + } } /* @@ -339,13 +374,6 @@ RepairGraph(HnswVacuumState * vacuumstate) BufferAccessStrategy bas = vacuumstate->bas; BlockNumber blkno = HNSW_HEAD_BLKNO; - /* - * Wait for inserts to complete. Inserts before this point may have - * neighbors about to be deleted. Inserts after this point will not. - */ - LockPage(index, HNSW_UPDATE_LOCK, ExclusiveLock); - UnlockPage(index, HNSW_UPDATE_LOCK, ExclusiveLock); - /* Repair entry point first */ RepairGraphEntryPoint(vacuumstate);
^ permalink raw [nested|flat] 7+ messages in thread

* Re: [pgvector/pgvector] PR #988: Fix HNSW index corruption during high INSERT and VACUUM concurrency
@ 2026-06-16 01:59  "ankane (@ankane)" <noreply+ankane@github.com>
  5 siblings, 0 replies; 7+ messages in thread

From: ankane (@ankane) @ 2026-06-16 01:59 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @bhagyesh-c, thanks for the report and PR. I've confirmed the issue, and the overall approach looks good. I've created [a branch](https://github.com/pgvector/pgvector/compare/hnsw-vacuum-fix) that shuffles the logic a bit. Let me know your thoughts.

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

* Re: [pgvector/pgvector] PR #988: Fix HNSW index corruption during high INSERT and VACUUM concurrency
@ 2026-06-16 09:12  "bhagyesh-c (@bhagyesh-c)" <noreply+bhagyesh-c@github.com>
  5 siblings, 0 replies; 7+ messages in thread

From: bhagyesh-c (@bhagyesh-c) @ 2026-06-16 09:12 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @ankane, thanks for the quick response. I've reviewed the changes in your branch, the fix looks good and cleaner than the original. I've also verified it against the reproduction test. 

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

* Re: [pgvector/pgvector] PR #988: Fix HNSW index corruption during high INSERT and VACUUM concurrency
@ 2026-06-16 16:48  "ankane (@ankane)" <noreply+ankane@github.com>
  5 siblings, 0 replies; 7+ messages in thread

From: ankane (@ankane) @ 2026-06-16 16:48 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 #988: Fix HNSW index corruption during high INSERT and VACUUM concurrency
@ 2026-06-16 16:51  "ankane (@ankane)" <noreply+ankane@github.com>
  5 siblings, 0 replies; 7+ messages in thread

From: ankane (@ankane) @ 2026-06-16 16:51 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thanks for the quick review. Applied the changes in the commit above. Also evaluating ways to make indexes more robust to this. Once that's complete and more testing has been done, will release 0.8.3.

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

* Re: [pgvector/pgvector] PR #988: Fix HNSW index corruption during high INSERT and VACUUM concurrency
@ 2026-06-16 18:39  "ankane (@ankane)" <noreply+ankane@github.com>
  5 siblings, 0 replies; 7+ messages in thread

From: ankane (@ankane) @ 2026-06-16 18:39 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Added two more commits around this:

1. 98dc4aa6d0ebc39c2160b35b509fb68f7d67e28d - a vacuum pass to confirm the graph was repaired before marking tuples as deleted (very quick to perform)
2. 7b72aeeff3b5355a00214fc6112dfb7e4dc0f796 - a check when trying to an load element

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

* Re: [pgvector/pgvector] PR #988: Fix HNSW index corruption during high INSERT and VACUUM concurrency
@ 2026-06-18 05:38  "ankane (@ankane)" <noreply+ankane@github.com>
  5 siblings, 0 replies; 7+ messages in thread

From: ankane (@ankane) @ 2026-06-18 05:38 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Just released 0.8.3 with the fix. For completeness, this has been present since the initial HNSW release.

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


end of thread, other threads:[~2026-06-18 05:38 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-06-15 14:44 [pgvector/pgvector] PR #988: Fix HNSW index corruption during high INSERT and VACUUM concurrency "bhagyesh-c (@bhagyesh-c)" <noreply+bhagyesh-c@github.com>
2026-06-16 01:59 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-06-16 09:12 ` "bhagyesh-c (@bhagyesh-c)" <noreply+bhagyesh-c@github.com>
2026-06-16 16:48 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-06-16 16:51 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-06-16 18:39 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-06-18 05:38 ` "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