pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] PR #1026: HNSW: don't rewind insertPage to deleted slots that cannot fit the element
14+ messages / 2 participants
[nested] [flat]
* [pgvector/pgvector] PR #1026: HNSW: don't rewind insertPage to deleted slots that cannot fit the element
@ 2026-09-10 07:29 "jitokim (@jitokim)" <noreply+jitokim@github.com>
0 siblings, 0 replies; 14+ messages in thread
From: jitokim (@jitokim) @ 2026-09-10 07:29 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
`HnswFreeOffset()` latches `*newInsertPage` on the first deleted element it finds, before checking whether that slot can hold the element being inserted. With fixed-size types every slot fits, so this is harmless. With `sparsevec` a slot freed by a small element cannot hold a larger one, so after a `VACUUM` the metapage `insertPage` is rewound to a page that never fits, and every subsequent larger insert walks the index from there to the end — a full index scan per insert, permanently.
Fixes #1025
### Change
Latch `newInsertPage` only when the element tuple fits (`pageFree >= etupSize`). The freed neighbor tuple is always at least a level-0 neighbor tuple, so checking `etupSize` alone is sufficient. This matches the "first page where element at level 0 can fit" criterion `AddElementOnDisk()` already uses for `minCombinedSize`, so both updates of `newInsertPage` follow the same rule.
An earlier revision of this PR checked the full fit for the current element (`pageFree >= etupSize && npageFree >= ntupSize`). That over-constrains for fixed-size types: a level-N element needs a larger neighbor tuple than a level-0 slot provides, so it would skip level-0 slots without latching and the next insert would start past them. Measured on `vector(64)` m=16 (200k rows, 5% scattered delete, VACUUM, 50k inserts, 5 runs): ~4% of freed slots left unreused, +0.2% index size, recovered on the next VACUUM. The current revision leaves zero such slots in 12/12 runs (counted via `pageinspect` as `deleted=1` element tuples).
### Verification
`sparsevec(10000)`, 100k rows at nnz=50, scattered 5% delete + `VACUUM`, then nnz=500 inserts — buffers per insert:
| | stock | this PR |
|---|---|---|
| L1 — first large insert after `VACUUM` | 10,772 | 10,784 |
| L2 — repeat | **10,747** | **1,637** |
| L3 — after a small insert | **10,749** | **1,664** |
`make installcheck` 14/14 on this branch (PostgreSQL 16).
inline patch 1: src/hnswinsert.c, test/t/049_hnsw_sparsevec_insert_page.pl, test/t/050_hnsw_sparsevec_insert_page_reuse.pl (+215/-5 lines) download inline-1.patch
diff --git a/src/hnswinsert.c b/src/hnswinsert.c
index 32b9ff07e..c7d121e74 100644
--- a/src/hnswinsert.c
+++ b/src/hnswinsert.c
@@ -15,6 +15,8 @@
#include "varatt.h"
#endif
+#define HNSW_MAX_UNFIT_PAGES 3
+
/*
* Get the insert page
*/
@@ -38,14 +40,43 @@ GetInsertPage(Relation index)
return insertPage;
}
+/*
+ * Get the last page if it is initialized
+ */
+static BlockNumber
+GetLastPage(Relation index)
+{
+ BlockNumber lastPage;
+ Buffer buf;
+ bool isNew;
+
+ /* New pages are locked before the extension lock is released */
+ LockRelationForExtension(index, ExclusiveLock);
+ lastPage = RelationGetNumberOfBlocks(index) - 1;
+ UnlockRelationForExtension(index, ExclusiveLock);
+
+ /* Skip new page since it is not linked yet */
+ buf = ReadBuffer(index, lastPage);
+ LockBuffer(buf, BUFFER_LOCK_SHARE);
+ isNew = PageIsNew(BufferGetPage(buf));
+ UnlockReleaseBuffer(buf);
+
+ if (isNew)
+ return InvalidBlockNumber;
+
+ return lastPage;
+}
+
/*
* Check for a free offset
*/
static bool
-HnswFreeOffset(Relation index, Buffer buf, Page page, HnswElement element, Size etupSize, Size ntupSize, Buffer *nbuf, Page *npage, OffsetNumber *freeOffno, OffsetNumber *freeNeighborOffno, BlockNumber *newInsertPage, uint8 *tupleVersion)
+HnswFreeOffset(Relation index, Buffer buf, Page page, HnswElement element, Size etupSize, Size ntupSize, Buffer *nbuf, Page *npage, OffsetNumber *freeOffno, OffsetNumber *freeNeighborOffno, BlockNumber *newInsertPage, int *unfitPages, uint8 *tupleVersion)
{
OffsetNumber offno;
OffsetNumber maxoffno = PageGetMaxOffsetNumber(page);
+ bool fits = false;
+ bool unfit = false;
for (offno = FirstOffsetNumber; offno <= maxoffno; offno = OffsetNumberNext(offno))
{
@@ -65,9 +96,6 @@ HnswFreeOffset(Relation index, Buffer buf, Page page, HnswElement element, Size
Size pageFree;
Size npageFree;
- if (!BlockNumberIsValid(*newInsertPage))
- *newInsertPage = elementPage;
-
if (neighborPage == elementPage)
{
*nbuf = buf;
@@ -99,6 +127,16 @@ HnswFreeOffset(Relation index, Buffer buf, Page page, HnswElement element, Size
else if (pageFree >= etupSize)
npageFree += pageFree - etupSize;
+ /* Keep track of first page where element can fit */
+ if (pageFree >= etupSize)
+ {
+ if (!BlockNumberIsValid(*newInsertPage))
+ *newInsertPage = elementPage;
+ fits = true;
+ }
+ else
+ unfit = true;
+
/* Check for space */
if (pageFree >= etupSize && npageFree >= ntupSize)
{
@@ -112,6 +150,10 @@ HnswFreeOffset(Relation index, Buffer buf, Page page, HnswElement element, Size
}
}
+ /* Count pages with deleted slots too small for element */
+ if (unfit && !fits)
+ (*unfitPages)++;
+
return false;
}
@@ -160,6 +202,8 @@ AddElementOnDisk(Relation index, HnswElement e, int m, BlockNumber insertPage, B
OffsetNumber freeOffno = InvalidOffsetNumber;
OffsetNumber freeNeighborOffno = InvalidOffsetNumber;
BlockNumber newInsertPage = InvalidBlockNumber;
+ int unfitPages = 0;
+ bool jumped = false;
uint8 tupleVersion;
char *base = NULL;
@@ -210,7 +254,7 @@ AddElementOnDisk(Relation index, HnswElement e, int m, BlockNumber insertPage, B
}
/* Next, try space from a deleted element */
- if (HnswFreeOffset(index, buf, page, e, etupSize, ntupSize, &nbuf, &npage, &freeOffno, &freeNeighborOffno, &newInsertPage, &tupleVersion))
+ if (HnswFreeOffset(index, buf, page, e, etupSize, ntupSize, &nbuf, &npage, &freeOffno, &freeNeighborOffno, &newInsertPage, &unfitPages, &tupleVersion))
{
if (nbuf != buf)
{
@@ -243,6 +287,21 @@ AddElementOnDisk(Relation index, HnswElement e, int m, BlockNumber insertPage, B
if (!building)
GenericXLogAbort(state);
UnlockReleaseBuffer(buf);
+
+ /* Jump to last page after a few pages with slots too small */
+ if (!jumped && unfitPages >= HNSW_MAX_UNFIT_PAGES)
+ {
+ BlockNumber lastPage = GetLastPage(index);
+
+ if (BlockNumberIsValid(lastPage) && lastPage > currentPage)
+ currentPage = lastPage;
+
+ /* Keep insert page so smaller elements can reuse slots */
+ if (!BlockNumberIsValid(newInsertPage))
+ newInsertPage = insertPage;
+
+ jumped = true;
+ }
}
else
{
diff --git a/test/t/049_hnsw_sparsevec_insert_page.pl b/test/t/049_hnsw_sparsevec_insert_page.pl
new file mode 100644
index 000000000..a96a425d0
--- /dev/null
+++ b/test/t/049_hnsw_sparsevec_insert_page.pl
@@ -0,0 +1,82 @@
+use strict;
+use warnings FATAL => 'all';
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+my $dim = 10000;
+my $rows = 30000;
+my $small_nnz = 200;
+my $large_nnz = 1000;
+
+# Buffers read by an insert, without keeping the row
+sub insert_buffers
+{
+ my ($node, $nnz, $seed) = @_;
+ my $explain = $node->safe_psql("postgres", qq(
+ BEGIN;
+ EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF)
+ INSERT INTO tst (v) VALUES (make_sv($nnz, $seed));
+ ROLLBACK;
+ ));
+ my $buffers = 0;
+ while ($explain =~ /shared hit=(\d+)(?: read=(\d+))?/g)
+ {
+ my $total = $1 + ($2 // 0);
+ $buffers = $total if $total > $buffers;
+ }
+ return $buffers;
+}
+
+# Initialize node
+my $node = PostgreSQL::Test::Cluster->new('node');
+$node->init;
+$node->start;
+
+# Create table and index
+$node->safe_psql("postgres", "CREATE EXTENSION vector;");
+$node->safe_psql("postgres", "CREATE TABLE tst (i serial, v sparsevec($dim)) WITH (autovacuum_enabled = off);");
+
+# Generate a sparsevec with the requested number of non-zeros
+$node->safe_psql("postgres", qq(
+ CREATE FUNCTION make_sv(nnz int, seed bigint) RETURNS sparsevec
+ LANGUAGE sql IMMUTABLE AS \$\$
+ SELECT ('{' || string_agg(
+ ((g - 1) * ($dim / nnz) + 1 + (seed % ($dim / nnz)))::text || ':' ||
+ round((0.1 + ((seed * g) % 991)::numeric / 991.0), 4)::text,
+ ',' ORDER BY g) || '}/$dim')::sparsevec
+ FROM generate_series(1, nnz) g;
+ \$\$;
+));
+
+# Every row has a small number of non-zeros
+$node->safe_psql("postgres",
+ "INSERT INTO tst (v) SELECT make_sv($small_nnz, i) FROM generate_series(1, $rows) i;"
+);
+$node->safe_psql("postgres", "CREATE INDEX idx ON tst USING hnsw (v sparsevec_l2_ops);");
+
+# Cost of a large insert while no element is deleted
+my $baseline = insert_buffers($node, $large_nnz, 900001);
+
+# Delete scattered rows and vacuum
+$node->safe_psql("postgres", "DELETE FROM tst WHERE i % 20 = 0;");
+$node->safe_psql("postgres", "VACUUM tst;");
+
+my $pages = $node->safe_psql("postgres", "SELECT pg_relation_size('idx') / 8192;");
+
+# Only the first large insert should scan past freed slots
+insert_buffers($node, $large_nnz, 900002);
+my $repeat = insert_buffers($node, $large_nnz, 900003);
+
+# Insert a small row, then a large one
+insert_buffers($node, $small_nnz, 900004);
+my $after_small = insert_buffers($node, $large_nnz, 900005);
+
+note("index pages: $pages, baseline: $baseline, repeat: $repeat, after small: $after_small");
+
+# Compare to index size
+my $limit = $pages / 2;
+cmp_ok($repeat, '<', $limit, "large insert after vacuum does not scan the index");
+cmp_ok($after_small, '<', $limit, "large insert after a small insert does not scan the index");
+
+done_testing();
diff --git a/test/t/050_hnsw_sparsevec_insert_page_reuse.pl b/test/t/050_hnsw_sparsevec_insert_page_reuse.pl
new file mode 100644
index 000000000..423bd73de
--- /dev/null
+++ b/test/t/050_hnsw_sparsevec_insert_page_reuse.pl
@@ -0,0 +1,69 @@
+use strict;
+use warnings FATAL => 'all';
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+my $dim = 10000;
+my $rows = 4000;
+my $small_nnz = 200;
+my $large_nnz = 1000;
+
+sub index_pages
+{
+ my ($node) = @_;
+ return $node->safe_psql("postgres", "SELECT pg_relation_size('idx') / 8192;");
+}
+
+# Initialize node
+my $node = PostgreSQL::Test::Cluster->new('node');
+$node->init;
+$node->start;
+
+# Create table and index
+$node->safe_psql("postgres", "CREATE EXTENSION vector;");
+$node->safe_psql("postgres", "CREATE TABLE tst (i serial, v sparsevec($dim)) WITH (autovacuum_enabled = off);");
+
+# Generate a sparsevec with the requested number of non-zeros
+$node->safe_psql("postgres", qq(
+ CREATE FUNCTION make_sv(nnz int, seed bigint) RETURNS sparsevec
+ LANGUAGE sql IMMUTABLE AS \$\$
+ SELECT ('{' || string_agg(
+ ((g - 1) * ($dim / nnz) + 1 + (seed % ($dim / nnz)))::text || ':' ||
+ round((0.1 + ((seed * g) % 991)::numeric / 991.0), 4)::text,
+ ',' ORDER BY g) || '}/$dim')::sparsevec
+ FROM generate_series(1, nnz) g;
+ \$\$;
+));
+
+# Every row has a small number of non-zeros
+$node->safe_psql("postgres",
+ "INSERT INTO tst (v) SELECT make_sv($small_nnz, i) FROM generate_series(1, $rows) i;"
+);
+$node->safe_psql("postgres", "CREATE INDEX idx ON tst USING hnsw (v sparsevec_l2_ops);");
+
+my $pages = index_pages($node);
+
+# Delete scattered rows and vacuum
+$node->safe_psql("postgres", "DELETE FROM tst WHERE i % 20 = 0;");
+$node->safe_psql("postgres", "VACUUM tst;");
+my $deleted = $rows / 20;
+
+# Insert large rows that do not fit freed slots
+$node->safe_psql("postgres",
+ "INSERT INTO tst (v) SELECT make_sv($large_nnz, 900000 + i) FROM generate_series(1, 3) i;"
+);
+my $pages_before = index_pages($node);
+
+# Insert small rows that fit freed slots
+$node->safe_psql("postgres",
+ "INSERT INTO tst (v) SELECT make_sv($small_nnz, 910000 + i) FROM generate_series(1, $deleted) i;"
+);
+my $growth = index_pages($node) - $pages_before;
+
+# Compare to growth without reuse
+my $no_reuse = $pages * $deleted / $rows;
+note("index pages: $pages, growth: $growth, without reuse: $no_reuse");
+cmp_ok($growth, '<', $no_reuse / 4, "small inserts after large inserts reuse freed slots");
+
+done_testing();
^ permalink raw [nested|flat] 14+ messages in thread
* Re: [pgvector/pgvector] PR #1026: HNSW: don't rewind insertPage to deleted slots that cannot fit the element
@ 2026-09-10 13:57 "jitokim (@jitokim)" <noreply+jitokim@github.com>
12 siblings, 0 replies; 14+ messages in thread
From: jitokim (@jitokim) @ 2026-09-10 13:57 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Pushed two follow-up commits:
- `test/t/049_hnsw_sparsevec_insert_page.pl` — 30k `sparsevec` rows, 5% scattered delete, `VACUUM`, then asserts that a repeat large insert (and a large insert after a small one) reads less than half the index. The check is a ratio against `pg_relation_size`, so it does not depend on the machine. Fails on master (`9324 < 3751.5` FAILED), passes with this patch (~15 s).
- A comment above the latch explaining why it has to come after the size check.
Verified on `pgvector/pgvector:pg16` (0.8.6 / PG 16.15): `make installcheck` 14/14 and the full TAP suite (49 files, 1,256 tests) pass. Fixed-size `vector(512)` insert cost and final index sizes for append-only and delete/VACUUM/reinsert workloads are unchanged versus master. Full numbers in #1025.
^ permalink raw [nested|flat] 14+ messages in thread
* Re: [pgvector/pgvector] PR #1026: HNSW: don't rewind insertPage to deleted slots that cannot fit the element
@ 2026-09-23 18:38 "ankane (@ankane)" <noreply+ankane@github.com>
12 siblings, 0 replies; 14+ messages in thread
From: ankane (@ankane) @ 2026-09-23 18:38 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @jitokim, thanks for the PR. The proposed approach has its own drawback - if a sparsevec with a large nnz is inserted after a vacuum, the insert page will be moved to the end of the index and none of the space will be reused.
One idea to partially mitigate this is to only move the insert page a certain number of free slots.
```diff
inline patch 1: src/hnswinsert.c (+18/-5 lines) download inline-1.patch
--- a/src/hnswinsert.c
+++ b/src/hnswinsert.c
@@ -42,10 +42,11 @@ GetInsertPage(Relation index)
* Check for a free offset
*/
static bool
-HnswFreeOffset(Relation index, Buffer buf, Page page, HnswElement element, Size etupSize, Size ntupSize, Buffer *nbuf, Page *npage, OffsetNumber *freeOffno, OffsetNumber *freeNeighborOffno, BlockNumber *newInsertPage, uint8 *tupleVersion)
+HnswFreeOffset(Relation index, Buffer buf, Page page, HnswElement element, Size etupSize, Size ntupSize, Buffer *nbuf, Page *npage, OffsetNumber *freeOffno, OffsetNumber *freeNeighborOffno, BlockNumber *newInsertPage, int *newInsertPageTries, uint8 *tupleVersion)
{
OffsetNumber offno;
OffsetNumber maxoffno = PageGetMaxOffsetNumber(page);
+ int startingTries = *newInsertPageTries;
for (offno = FirstOffsetNumber; offno <= maxoffno; offno = OffsetNumberNext(offno))
{
@@ -65,9 +66,6 @@ HnswFreeOffset(Relation index, Buffer buf, Page page, HnswElement element, Size
Size pageFree;
Size npageFree;
- if (!BlockNumberIsValid(*newInsertPage))
- *newInsertPage = elementPage;
-
if (neighborPage == elementPage)
{
*nbuf = buf;
@@ -99,6 +97,20 @@ HnswFreeOffset(Relation index, Buffer buf, Page page, HnswElement element, Size
else if (pageFree >= etupSize)
npageFree += pageFree - etupSize;
+ /*
+ * Keep track of first page where element at level 0 can fit. For
+ * variable-length items (sparsevec), skip up to 3 pages to
+ * prevent large scans.
+ */
+ if (!BlockNumberIsValid(*newInsertPage))
+ {
+ if (pageFree >= etupSize || *newInsertPageTries >= 3)
+ *newInsertPage = elementPage;
+ /* Only increment once per page */
+ else if (*newInsertPageTries == startingTries)
+ (*newInsertPageTries)++;
+ }
+
/* Check for space */
if (pageFree >= etupSize && npageFree >= ntupSize)
{
@@ -160,6 +172,7 @@ AddElementOnDisk(Relation index, HnswElement e, int m, BlockNumber insertPage, B
OffsetNumber freeOffno = InvalidOffsetNumber;
OffsetNumber freeNeighborOffno = InvalidOffsetNumber;
BlockNumber newInsertPage = InvalidBlockNumber;
+ int newInsertPageTries = 0;
uint8 tupleVersion;
char *base = NULL;
@@ -210,7 +223,7 @@ AddElementOnDisk(Relation index, HnswElement e, int m, BlockNumber insertPage, B
}
/* Next, try space from a deleted element */
- if (HnswFreeOffset(index, buf, page, e, etupSize, ntupSize, &nbuf, &npage, &freeOffno, &freeNeighborOffno, &newInsertPage, &tupleVersion))
+ if (HnswFreeOffset(index, buf, page, e, etupSize, ntupSize, &nbuf, &npage, &freeOffno, &freeNeighborOffno, &newInsertPage, &newInsertPageTries, &tupleVersion))
{
if (nbuf != buf)
{
```
This would prevent it from getting stuck on sparsevec with a small nnz.
Another approach would be to use the free space map. Will spend some time trying this.
^ permalink raw [nested|flat] 14+ messages in thread
* Re: [pgvector/pgvector] PR #1026: HNSW: don't rewind insertPage to deleted slots that cannot fit the element
@ 2026-09-25 00:42 "jitokim (@jitokim)" <noreply+jitokim@github.com>
12 siblings, 0 replies; 14+ messages in thread
From: jitokim (@jitokim) @ 2026-09-25 00:42 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Thanks for the feedback, @ankane. Reproduced: after VACUUM, a few large inserts move insertPage to the tail and small inserts stop reusing the freed slots (+1,259 pages vs +10 on master, 50k rows).
The tries-bounded latch keeps reuse, but repeat large inserts still scan to the end of the index (`049` fails), since insertPage only moves ~4 pages per insert.
Pushed a version that bounds the scan instead: after 3 pages whose deleted slots are all too small, jump to the last page and leave insertPage unchanged. Reuse matches master, large inserts stay near baseline, and `050` covers your case. Numbers are in #1025.
That said, FSM sounds like the better fix. Happy to close this if you go that way.
^ permalink raw [nested|flat] 14+ messages in thread
* Re: [pgvector/pgvector] PR #1026: HNSW: don't rewind insertPage to deleted slots that cannot fit the element
@ 2026-10-06 20:47 "ankane (@ankane)" <noreply+ankane@github.com>
12 siblings, 0 replies; 14+ messages in thread
From: ankane (@ankane) @ 2026-10-06 20:47 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Still need to do a lot more testing, but pushed an initial version of the free space map to the [hnsw-free-space-map branch](https://github.com/pgvector/pgvector/compare/hnsw-free-space-map).
^ permalink raw [nested|flat] 14+ messages in thread
* Re: [pgvector/pgvector] PR #1026: HNSW: don't rewind insertPage to deleted slots that cannot fit the element
@ 2026-10-15 05:02 "ankane (@ankane)" <noreply+ankane@github.com>
12 siblings, 0 replies; 14+ messages in thread
From: ankane (@ankane) @ 2026-10-15 05:02 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
I think the branch is in a pretty good state, so planning to include it in 0.9.0.
^ permalink raw [nested|flat] 14+ messages in thread
* Re: [pgvector/pgvector] PR #1026: HNSW: don't rewind insertPage to deleted slots that cannot fit the element
@ 2026-10-15 13:49 "jitokim (@jitokim)" <noreply+jitokim@github.com>
12 siblings, 0 replies; 14+ messages in thread
From: jitokim (@jitokim) @ 2026-10-15 13:49 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Tried the branch at 9b91f2d with the tests from this PR on PG 16.10. 038, 049 and 050 pass. In 049 a repeat large insert after VACUUM is back to 1,857 buffers (1,866 after a small insert, baseline 1,849, master 9,324), and 050 shows no index growth.
The only case I could still make slow is a contrived one: 20k rows at nnz=100, delete just the level >= 1 elements, VACUUM, then insert nnz=155. None of the freed slots fit, so the insert reads close to the whole index (2,867 reads on 2,859 pages, 4 of 5 seeds). insertPage stays put now, so it doesn't get worse over time. Not sure it's worth handling.
Closing this one in favor of the branch is fine with me.
^ permalink raw [nested|flat] 14+ messages in thread
* Re: [pgvector/pgvector] PR #1026: HNSW: don't rewind insertPage to deleted slots that cannot fit the element
@ 2026-10-15 18:22 "ankane (@ankane)" <noreply+ankane@github.com>
12 siblings, 0 replies; 14+ messages in thread
From: ankane (@ankane) @ 2026-10-15 18:22 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Thanks for testing. Can you share more about the contrived test? The `insertPage` should always be the last page for new indexes that use the free space map. For previously created indexes, the `insertPage` should move to the last page after the first vacuum (but should probably have it move after the first insert that extends the index).
Note: edited to correct current behavior
^ permalink raw [nested|flat] 14+ messages in thread
* Re: [pgvector/pgvector] PR #1026: HNSW: don't rewind insertPage to deleted slots that cannot fit the element
@ 2026-10-15 23:09 "ankane (@ankane)" <noreply+ankane@github.com>
12 siblings, 0 replies; 14+ messages in thread
From: ankane (@ankane) @ 2026-10-15 23:09 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Updated the logic, so please let me know if that fixes it.
^ permalink raw [nested|flat] 14+ messages in thread
* Re: [pgvector/pgvector] PR #1026: HNSW: don't rewind insertPage to deleted slots that cannot fit the element
@ 2026-10-16 16:37 "jitokim (@jitokim)" <noreply+jitokim@github.com>
12 siblings, 0 replies; 14+ messages in thread
From: jitokim (@jitokim) @ 2026-10-16 16:37 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Sure. The setup:
```sql
CREATE TABLE tst (i serial, v sparsevec(10000));
-- 20k rows, nnz=100 each
CREATE INDEX idx ON tst USING hnsw (v sparsevec_l2_ops);
-- delete only the rows whose element is at level >= 1 (about 1,270 rows), then
VACUUM tst;
-- then insert one row with nnz=155
```
The level >= 1 delete is what makes it contrived: every freed slot ends up too small for the new row.
Results on 78647a3 (PG 16.10, 5 runs each from a fresh copy of the same database, buffers from EXPLAIN (ANALYZE, BUFFERS)):
- Index built with 78647a3: insertPage is already the last page (2858 of 2,859) before and after. Level 0 inserts still touch ~4,300 buffers (read 2,871), level 1 inserts ~1,450. Same as 9b91f2d. 8 inserts in a row: level 0 stays at ~4,280 hits each.
- Index built and vacuumed with master (insertPage 3), then 78647a3 installed: the first insert reads 2,868 and moves insertPage to 2858, like you described. After that, 8 inserts in a row are ~2,100 hits each, and insertPage moves to 2859 once the index extends. On master the same index stays at insertPage 3 and ~5,070 buffers per insert.
So the previous-index case looks fixed. The new-index case doesn't seem to be about insertPage, since it's already at the end.
^ permalink raw [nested|flat] 14+ messages in thread
* Re: [pgvector/pgvector] PR #1026: HNSW: don't rewind insertPage to deleted slots that cannot fit the element
@ 2026-10-17 02:58 "ankane (@ankane)" <noreply+ankane@github.com>
12 siblings, 0 replies; 14+ messages in thread
From: ankane (@ankane) @ 2026-10-17 02:58 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Thanks, fixed the issue if you want to test again.
^ permalink raw [nested|flat] 14+ messages in thread
* Re: [pgvector/pgvector] PR #1026: HNSW: don't rewind insertPage to deleted slots that cannot fit the element
@ 2026-10-23 04:35 "jitokim (@jitokim)" <noreply+jitokim@github.com>
12 siblings, 0 replies; 14+ messages in thread
From: jitokim (@jitokim) @ 2026-10-23 04:35 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Thanks, that fixed it. Same setup on 4e34dea (PG 16.10):
- Index built with 4e34dea: an insert that doesn't fit any freed slot is now ~1,400 buffers (read ~145), down from ~4,260 (read 2,868) on 78647a3. 8 inserts in a row stay flat at ~2,250 hits each, and insertPage moves to the new page once the index extends.
- Index built and vacuumed with master, then 4e34dea installed: the first insert still reads the whole index once (2,869) and moves insertPage to the end. After that it's ~1,550 per insert. Master stays at ~4,400 per insert.
- 038, 049 and 050 pass.
^ permalink raw [nested|flat] 14+ messages in thread
* Re: [pgvector/pgvector] PR #1026: HNSW: don't rewind insertPage to deleted slots that cannot fit the element
@ 2026-10-23 05:03 "ankane (@ankane)" <noreply+ankane@github.com>
12 siblings, 0 replies; 14+ messages in thread
From: ankane (@ankane) @ 2026-10-23 05:03 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 14+ messages in thread
* Re: [pgvector/pgvector] PR #1026: HNSW: don't rewind insertPage to deleted slots that cannot fit the element
@ 2026-10-23 05:05 "ankane (@ankane)" <noreply+ankane@github.com>
12 siblings, 0 replies; 14+ messages in thread
From: ankane (@ankane) @ 2026-10-23 05:05 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Merged in the commit above. Thanks for testing and driving this!
^ permalink raw [nested|flat] 14+ messages in thread
end of thread, other threads:[~2026-10-23 05:05 UTC | newest]
Thread overview: 14+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-09-10 07:29 [pgvector/pgvector] PR #1026: HNSW: don't rewind insertPage to deleted slots that cannot fit the element "jitokim (@jitokim)" <noreply+jitokim@github.com>
2026-09-10 13:57 ` "jitokim (@jitokim)" <noreply+jitokim@github.com>
2026-09-23 18:38 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-09-25 00:42 ` "jitokim (@jitokim)" <noreply+jitokim@github.com>
2026-10-06 20:47 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-10-15 05:02 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-10-15 13:49 ` "jitokim (@jitokim)" <noreply+jitokim@github.com>
2026-10-15 18:22 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-10-15 23:09 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-10-16 16:37 ` "jitokim (@jitokim)" <noreply+jitokim@github.com>
2026-10-17 02:58 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-10-23 04:35 ` "jitokim (@jitokim)" <noreply+jitokim@github.com>
2026-10-23 05:03 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-10-23 05:05 ` "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