Improved logic for HNSW free space map - resolves #1074

This commit is contained in:
Andrew Kane committed 2026-12-02 20:13:17 -08:00
1 parent 0b853646a8
commit 48ebde7859
5 files changed
+24 -24

No files matched your search

+1 -1
View File
@@ -515,7 +515,7 @@ void HnswSetElementTuple(char *base, HnswElementTuple etup, HnswElement element
void HnswUpdateConnection(char *base, HnswNeighborArray * neighbors, HnswElement newElement, float distance, int lm, int *updateIdx, Relation index, HnswSupport * support);
bool HnswLoadNeighborTids(HnswElement element, ItemPointerData *indextids, Relation index, int m, int lm, int lc);
void HnswInitLockTranche(void);
void HnswRecordPageWithFreeSpace(Relation index, BlockNumber blkno, Page page);
void HnswRecordPageWithFreeSpace(Relation index, BlockNumber blkno, Page page, int m);
void HnswCheckDim(int expected, const HnswTypeInfo * typeInfo, Oid collation, Datum value);
const HnswTypeInfo *HnswGetTypeInfo(Relation index);
PGDLLEXPORT void HnswParallelBuildMain(dsm_segment *seg, shm_toc *toc);
+3 -3
View File
@@ -1162,7 +1162,7 @@ BuildGraph(HnswBuildState * buildstate)
* Record free space
*/
static void
RecordFreeSpace(Relation index)
RecordFreeSpace(Relation index, int m)
{
BlockNumber blkno = HNSW_HEAD_BLKNO;
@@ -1176,7 +1176,7 @@ RecordFreeSpace(Relation index)
page = BufferGetPage(buf);
/* Update free space map */
HnswRecordPageWithFreeSpace(index, blkno, page);
HnswRecordPageWithFreeSpace(index, blkno, page, m);
blkno = HnswPageGetOpaque(page)->nextblkno;
UnlockReleaseBuffer(buf);
@@ -1205,7 +1205,7 @@ BuildIndex(Relation heap, Relation index, IndexInfo *indexInfo,
log_newpage_range(index, forkNum, 0, RelationGetNumberOfBlocksInFork(index, forkNum), true);
if (buildstate->typeInfo->useFsm && forkNum == MAIN_FORKNUM)
RecordFreeSpace(index);
RecordFreeSpace(index, buildstate->m);
FreeBuildState(buildstate);
}
+4 -7
View File
@@ -168,7 +168,7 @@ AddElementOnDisk(Relation index, HnswElement e, int m, BlockNumber *updatedInser
uint8 tupleVersion;
char *base = NULL;
bool useFsm = !building && HnswGetTypeInfo(index)->useFsm;
bool tryFsm;
bool tryFsm = useFsm;
int fsmTries = 0;
bool fsmPage;
BlockNumber fsmHistory[HNSW_MAX_FSM_TRIES];
@@ -188,16 +188,13 @@ AddElementOnDisk(Relation index, HnswElement e, int m, BlockNumber *updatedInser
ntup = palloc0(ntupSize);
HnswSetNeighborTuple(base, ntup, e, m);
/* Ensure within MaxFSMRequestSize */
tryFsm = useFsm && combinedSize <= MaxHeapTupleSize;
/* Find a page (or two if needed) to insert the tuples */
for (;;)
{
/* Try free space map */
if (tryFsm && fsmTries < HNSW_MAX_FSM_TRIES)
{
currentPage = GetPageWithFreeSpace(index, combinedSize);
currentPage = GetPageWithFreeSpace(index, etupSize);
fsmHistory[fsmTries++] = currentPage;
/*
@@ -388,10 +385,10 @@ AddElementOnDisk(Relation index, HnswElement e, int m, BlockNumber *updatedInser
/* Update free space map */
if (useFsm)
{
HnswRecordPageWithFreeSpace(index, e->blkno, page);
HnswRecordPageWithFreeSpace(index, e->blkno, page, m);
if (e->neighborPage != e->blkno)
HnswRecordPageWithFreeSpace(index, e->neighborPage, npage);
HnswRecordPageWithFreeSpace(index, e->neighborPage, npage, m);
}
/* Commit */
+15 -12
View File
@@ -1562,7 +1562,7 @@ HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint
* Record free space
*/
void
HnswRecordPageWithFreeSpace(Relation index, BlockNumber blkno, Page page)
HnswRecordPageWithFreeSpace(Relation index, BlockNumber blkno, Page page, int m)
{
Size freeSpace = 0;
OffsetNumber maxoffno = PageGetMaxOffsetNumber(page);
@@ -1572,8 +1572,6 @@ HnswRecordPageWithFreeSpace(Relation index, BlockNumber blkno, Page page)
ItemId eitemid = PageGetItemId(page, offno);
HnswElementTuple etup = (HnswElementTuple) PageGetItem(page, eitemid);
Size itemsize;
BlockNumber neighborPage;
OffsetNumber neighborOffno;
/* Skip neighbor tuples */
if (!HnswIsElementTuple(etup))
@@ -1583,19 +1581,24 @@ HnswRecordPageWithFreeSpace(Relation index, BlockNumber blkno, Page page)
if (!etup->deleted)
continue;
/* Get neighbor page */
neighborPage = ItemPointerGetBlockNumber(&etup->neighbortid);
neighborOffno = ItemPointerGetOffsetNumber(&etup->neighbortid);
/* Keep track of largest tuple pair that will fit */
itemsize = ItemIdGetLength(eitemid);
if (neighborPage == blkno)
itemsize += ItemIdGetLength(PageGetItemId(page, neighborOffno)) + sizeof(ItemIdData);
/* Keep track of largest element tuple that will fit */
itemsize = ItemIdGetLength(eitemid) + PageGetExactFreeSpace(page);
freeSpace = Max(freeSpace, itemsize);
}
freeSpace += PageGetFreeSpace(page);
/*
* If no deleted element tuples, calculate largest element tuple that will
* fit along with neighbor tuple
*/
if (freeSpace == 0)
{
Size pageSpace = PageGetFreeSpace(page);
Size ntupSpace = HNSW_NEIGHBOR_TUPLE_SIZE(0, m) + sizeof(ItemIdData);
if (pageSpace > ntupSpace)
freeSpace = pageSpace - ntupSpace;
}
RecordPageWithFreeSpace(index, blkno, freeSpace);
}
+1 -1
View File
@@ -849,7 +849,7 @@ MarkDeleted(HnswVacuumState * vacuumstate)
if (useFsm)
{
/* Update free space map */
HnswRecordPageWithFreeSpace(index, blkno, page);
HnswRecordPageWithFreeSpace(index, blkno, page, vacuumstate->m);
/* Set to last page */
insertPage = blkno;