From 55dc735e1acd6d1246f6c0d058f63f1f7d48ab1c Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sat, 21 Sep 2024 19:10:25 -0700 Subject: [PATCH] Moved allocations out of GetScanItems [skip ci] --- src/ivfflat.h | 5 +++-- src/ivfscan.c | 30 ++++++++++++++++-------------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/ivfflat.h b/src/ivfflat.h index 10b4d91..8518317 100644 --- a/src/ivfflat.h +++ b/src/ivfflat.h @@ -253,8 +253,9 @@ typedef struct IvfflatScanOpaqueData /* Sorting */ Tuplesortstate *sortstate; TupleDesc tupdesc; - TupleTableSlot *slot; - bool isnull; + TupleTableSlot *vslot; + TupleTableSlot *mslot; + BufferAccessStrategy bas; /* Support functions */ FmgrInfo *procinfo; diff --git a/src/ivfscan.c b/src/ivfscan.c index 98731f4..cb6233e 100644 --- a/src/ivfscan.c +++ b/src/ivfscan.c @@ -113,14 +113,7 @@ GetScanItems(IndexScanDesc scan, Datum value) IvfflatScanOpaque so = (IvfflatScanOpaque) scan->opaque; TupleDesc tupdesc = RelationGetDescr(scan->indexRelation); double tuples = 0; - TupleTableSlot *slot = MakeSingleTupleTableSlot(so->tupdesc, &TTSOpsVirtual); - - /* - * Reuse same set of shared buffers for scan - * - * See postgres/src/backend/storage/buffer/README for description - */ - BufferAccessStrategy bas = GetAccessStrategy(BAS_BULKREAD); + TupleTableSlot *slot = so->vslot; /* Search closest probes lists */ while (!pairingheap_is_empty(so->listQueue)) @@ -134,7 +127,7 @@ GetScanItems(IndexScanDesc scan, Datum value) Page page; OffsetNumber maxoffno; - buf = ReadBufferExtended(scan->indexRelation, MAIN_FORKNUM, searchPage, RBM_NORMAL, bas); + buf = ReadBufferExtended(scan->indexRelation, MAIN_FORKNUM, searchPage, RBM_NORMAL, so->bas); LockBuffer(buf, BUFFER_LOCK_SHARE); page = BufferGetPage(buf); maxoffno = PageGetMaxOffsetNumber(page); @@ -173,8 +166,6 @@ GetScanItems(IndexScanDesc scan, Datum value) } } - FreeAccessStrategy(bas); - if (tuples < 100) ereport(DEBUG1, (errmsg("index scan found few tuples"), @@ -277,7 +268,16 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys) /* Prep sort */ so->sortstate = InitScanSortState(so->tupdesc); - so->slot = MakeSingleTupleTableSlot(so->tupdesc, &TTSOpsMinimalTuple); + /* Need separate slots for puttuple and gettuple */ + so->vslot = MakeSingleTupleTableSlot(so->tupdesc, &TTSOpsVirtual); + so->mslot = MakeSingleTupleTableSlot(so->tupdesc, &TTSOpsMinimalTuple); + + /* + * Reuse same set of shared buffers for scan + * + * See postgres/src/backend/storage/buffer/README for description + */ + so->bas = GetAccessStrategy(BAS_BULKREAD); so->listQueue = pairingheap_allocate(CompareLists, scan); @@ -351,9 +351,10 @@ ivfflatgettuple(IndexScanDesc scan, ScanDirection dir) pfree(DatumGetPointer(value)); } - if (tuplesort_gettupleslot(so->sortstate, true, false, so->slot, NULL)) + if (tuplesort_gettupleslot(so->sortstate, true, false, so->mslot, NULL)) { - ItemPointer heaptid = (ItemPointer) DatumGetPointer(slot_getattr(so->slot, 2, &so->isnull)); + bool isnull; + ItemPointer heaptid = (ItemPointer) DatumGetPointer(slot_getattr(so->mslot, 2, &isnull)); scan->xs_heaptid = *heaptid; scan->xs_recheck = false; @@ -374,6 +375,7 @@ ivfflatendscan(IndexScanDesc scan) pairingheap_free(so->listQueue); tuplesort_end(so->sortstate); + FreeAccessStrategy(so->bas); pfree(so); scan->opaque = NULL;