From: jitokim (@jitokim) <noreply+jitokim@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #1025: HNSW: insertPage rewinds to unfittable deleted slots after VACUUM, causing near-full index scan per insert with variable-size types (sparsevec) — reproduction for #975
Date: Thu, 10 Sep 2026 13:57:10 +0000
Message-ID: <gh-pgvector-pgvector-1025-c5619858526@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-1025@github.com>
References: <gh-pgvector-pgvector-1025@github.com>
Follow-up with a clean run on the current release image, `pgvector/pgvector:pg16` (pgvector **0.8.6**, PostgreSQL 16.15, arm64). `src/hnswinsert.c` is byte-identical between v0.8.5 and v0.8.6, so this is present in the current release as well.
| | stock 100k | stock 200k | patched 100k | patched 200k |
|---|---|---|---|---|
| index pages | 9,093 | 18,177 | 9,093 | 18,175 |
| C0 (before delete/vacuum) | 1,479 | 1,782 | 1,645 | 1,706 |
| L1 (first after vacuum) | 11,153 | 20,021 | 10,800 | 20,036 |
| L2 (repeat) | **10,760** | **20,020** | **1,652** | **1,823** |
| L3 (after a small insert) | **10,706** | **19,823** | **1,688** | **1,740** |
On stock, `buffers − baseline` equals the index page count to within 2% at both scales, and the cost is sustained (L2 ≈ L3 ≈ L1). With #1026 the walk happens once (L1) and later inserts return to baseline.
Two things this run clarified:
- The rewind itself comes from **VACUUM**: `hnswvacuum.c` sets `insertPage` to the first page holding a deleted element. The unconditional latch in `HnswFreeOffset()` is what re-pins it there after every insert, which is why the regression is permanent rather than a one-time cost after each vacuum.
- I also tried a stricter variant that latches only inside the `pageFree >= etupSize && npageFree >= ntupSize` branch. It measures the same (L2 1,815 / L3 1,736 at 200k), so the element-only condition in #1026 is sufficient — and neighbor tuples can live on another page, so gating the hint on same-page neighbor space would be stricter than the hint needs.
No-regression checks on the patched build: a `vector(512)` control (33,335 pages) stays flat at ~3,200 buffers before and after delete+VACUUM; an append-only 20k-row sparsevec load ends at the same 6,365 pages; 5% scattered delete + VACUUM + same-size reinsert ends at 4,547 pages on both builds, so space reuse is preserved. `make installcheck` (14/14) and the full TAP suite (49 files, 1,256 tests) pass.
#1026 now also carries a TAP test (`test/t/049_hnsw_sparsevec_insert_page.pl`) that asserts a repeat large insert after VACUUM reads less than half the index. It fails on master and passes with the patch.