From: Wwwing301 (@Wwwing301) <noreply+Wwwing301@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #1068: [BUG] Iterative HNSW scan during vacuum fails with `ERROR: cannot load deleted element`
Date: Wed, 18 Nov 2026 06:40:02 +0000
Message-ID: <gh-pgvector-pgvector-1068@github.com> (raw)
**Describe the bug**
On pgvector 0.8.7, an HNSW iterative scan (`hnsw.iterative_scan = strict_order`) running concurrently with `VACUUM` on the same index fails with `ERROR: cannot load deleted element`. During vacuum, deleted element tuples are removed before being repaired, so a resumed iterative scan can still visit one that vacuum is about to delete. The upstream fix `2f7507eb7aab` (2026-10-31, "Fixed possible error with iterative index scans during HNSW vacuuming") adds the missing repair path in `src/hnswvacuum.c`; 0.8.7 predates it and does not contain the fix.
**Environment**
- pgvector 0.8.7, docker image `pgvector/pgvector:pg17` (PostgreSQL 17.12 Debian), Linux x86_64
- Date: 2026-11-17; GUC `hnsw.iterative_scan = strict_order`; table autovacuum disabled
- Version check: 0.8.7 lacks `2f7507eb7aab` — `src/hnswvacuum.c` has no `RepairDeleting`/`RepairDeletingNeighbors`
**To Reproduce**
1. Setup, mirroring upstream `test/t/046_hnsw_vacuum_scan.pl`:
```sql
CREATE TABLE tst2 (i serial, v vector(3));
ALTER TABLE tst2 SET (autovacuum_enabled = false);
INSERT INTO tst2 (v) SELECT ARRAY[random(),random(),random()] FROM generate_series(1,1000);
CREATE INDEX ON tst2 USING hnsw (v vector_l2_ops);
DELETE FROM tst2;
```
2. `sel.sql`:
```sql
SET hnsw.iterative_scan = strict_order;
SELECT i FROM tst2 ORDER BY v <-> '[0,0,0]' LIMIT 10;
```
`vac.sql`: `VACUUM tst2;`
3. Run five concurrent clients, 1000 transactions each, one vacuum per 1000 selects:
```
pgbench -U postgres -d postgres -n --no-vacuum -c 5 -t 1000 \
-f sel.sql@1000 -f vac.sql@1
```
4. Observe the `pgbench` exit status and the server log.
**Expected behavior**
Iterative scans and vacuum must run concurrently without error: each scan returns up to 10 rows (here none — the table is empty after `DELETE`), `VACUUM` completes, and `pgbench` processes all 5000 transactions; a scan must never abort the client with an internal error.
**Actual behavior**
`pgbench` aborted mid-run — "number of transactions actually processed: 1984/5000", "Run was aborted; the above results are incomplete." The server log shows four errors within 1 ms from distinct backends (PIDs 17304-17307) at 08:33:05 UTC: `ERROR: cannot load deleted element`. Failures are non-deterministic and vanish once vacuum finishes, so retries mask the defect.