Skip to content

Commit bf379ee

Browse files
committed
Use a memory context for IVFFlat index scans
1 parent e1bc929 commit bf379ee

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/ivfflat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ typedef struct IvfflatScanOpaqueData
260260
int dimensions;
261261
bool first;
262262
Datum value;
263+
MemoryContext tmpCtx;
263264

264265
/* Sorting */
265266
Tuplesortstate *sortstate;

src/ivfscan.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
#include "miscadmin.h"
1111
#include "pgstat.h"
1212
#include "storage/bufmgr.h"
13-
14-
#ifdef IVFFLAT_MEMORY
1513
#include "utils/memutils.h"
16-
#endif
1714

1815
#define GetScanList(ptr) pairingheap_container(IvfflatScanList, ph_node, ptr)
1916
#define GetScanListConst(ptr) pairingheap_const_container(IvfflatScanList, ph_node, ptr)
@@ -221,7 +218,13 @@ GetScanValue(IndexScanDesc scan)
221218

222219
/* Normalize if needed */
223220
if (so->normprocinfo != NULL)
221+
{
222+
MemoryContext oldCtx = MemoryContextSwitchTo(so->tmpCtx);
223+
224224
value = IvfflatNormValue(so->typeInfo, so->collation, value);
225+
226+
MemoryContextSwitchTo(oldCtx);
227+
}
225228
}
226229

227230
return value;
@@ -253,6 +256,7 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
253256
int dimensions;
254257
int probes = ivfflat_probes;
255258
int maxProbes;
259+
MemoryContext oldCtx;
256260

257261
scan = RelationGetIndexScan(index, nkeys, norderbys);
258262

@@ -292,6 +296,12 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
292296
so->normprocinfo = IvfflatOptionalProcInfo(index, IVFFLAT_NORM_PROC);
293297
so->collation = index->rd_indcollation[0];
294298

299+
so->tmpCtx = AllocSetContextCreate(CurrentMemoryContext,
300+
"Ivfflat scan temporary context",
301+
ALLOCSET_DEFAULT_SIZES);
302+
303+
oldCtx = MemoryContextSwitchTo(so->tmpCtx);
304+
295305
/* Create tuple description for sorting */
296306
so->tupdesc = CreateTemplateTupleDesc(2);
297307
TupleDescInitEntry(so->tupdesc, (AttrNumber) 1, "distance", FLOAT8OID, -1, 0);
@@ -316,6 +326,8 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
316326
so->listIndex = 0;
317327
so->lists = palloc(maxProbes * sizeof(IvfflatScanList));
318328

329+
MemoryContextSwitchTo(oldCtx);
330+
319331
scan->opaque = so;
320332

321333
return scan;
@@ -377,8 +389,6 @@ ivfflatgettuple(IndexScanDesc scan, ScanDirection dir)
377389
IvfflatBench("GetScanItems", GetScanItems(scan, value));
378390
so->first = false;
379391
so->value = value;
380-
381-
/* TODO clean up if we allocated a new value */
382392
}
383393

384394
while (!tuplesort_gettupleslot(so->sortstate, true, false, so->mslot, NULL))
@@ -405,14 +415,10 @@ ivfflatendscan(IndexScanDesc scan)
405415
{
406416
IvfflatScanOpaque so = (IvfflatScanOpaque) scan->opaque;
407417

408-
pairingheap_free(so->listQueue);
409-
pfree(so->listPages);
418+
/* Free any temporary files */
410419
tuplesort_end(so->sortstate);
411-
FreeAccessStrategy(so->bas);
412-
FreeTupleDesc(so->tupdesc);
413-
pfree(so->lists);
414420

415-
/* TODO Free vslot and mslot without freeing TupleDesc */
421+
MemoryContextDelete(so->tmpCtx);
416422

417423
pfree(so);
418424
scan->opaque = NULL;

0 commit comments

Comments
 (0)