Skip to content

Fix HNSW page reuse after vacuum for large sparse vectors - #1074

Open
Waterpine wants to merge 1 commit into
pgvector:masterfrom
Waterpine:fix/f007-sparse-page-reuse
Open

Waterpine wants to merge 1 commit into
pgvector:masterfrom
Waterpine:fix/f007-sparse-page-reuse

Conversation

@Waterpine

Copy link
Copy Markdown
Contributor

Repeatedly deleting, vacuuming, and reinserting a single large sparsevec can cause an HNSW index to grow on every cycle, even without concurrent activity or an increase in the number of live rows.

When the free space map is enabled, VACUUM currently sets the insertion hint to the last page. Large elements whose element and neighbor tuples require separate pages cannot satisfy the combined-size free-space request. Starting from the last page then misses reusable space on earlier pages and causes unnecessary index extension.

Preserve the first page containing a deleted element as the insertion fallback, while continuing to maintain the free space map. Use the last page only when no deleted element was found.

Example

The regression test maintains one live row containing a sparse vector with 1,000 nonzero elements. Each
cycle deletes the row, runs VACUUM (INDEX_CLEANUP ON), and reinserts the same vector.

With 8 KiB pages and default HNSW index parameters, the observed index sizes are:

Completed cycles Before the fix After the fix
0 3 pages 3 pages
1 5 pages 3 pages
5 13 pages 3 pages
20 43 pages 3 pages

After every cycle, the table still contains only one live row. The fix allows the index to reuse the pages holding deleted elements.

The complete reproducer is included in test/t/049_f007_regression.pl.

Validation

  • Added a 20-cycle regression test checking stable index size and that queries return only the current row.
  • Confirmed that the new regression test fails on the unmodified baseline and passes with this fix.
  • Passed 14 SQL regression tests and 3 TAP test files, totaling 46 TAP assertions, on PostgreSQL 17.
  • Included existing sparse-vector vacuum and concurrent vacuum/insert tests.

Retain the first page with deleted elements when using the free space
map, and use the tail page only as a fallback. This preserves reusable
space for insertions whose element and neighbors need separate pages.
Cover repeated delete, vacuum, and insert cycles with large sparsevecs.

Audit finding: F007
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant