10
10
#include "miscadmin.h"
11
11
#include "pgstat.h"
12
12
#include "storage/bufmgr.h"
13
-
14
- #ifdef IVFFLAT_MEMORY
15
13
#include "utils/memutils.h"
16
- #endif
17
14
18
15
#define GetScanList (ptr ) pairingheap_container(IvfflatScanList, ph_node, ptr)
19
16
#define GetScanListConst (ptr ) pairingheap_const_container(IvfflatScanList, ph_node, ptr)
@@ -221,7 +218,13 @@ GetScanValue(IndexScanDesc scan)
221
218
222
219
/* Normalize if needed */
223
220
if (so -> normprocinfo != NULL )
221
+ {
222
+ MemoryContext oldCtx = MemoryContextSwitchTo (so -> tmpCtx );
223
+
224
224
value = IvfflatNormValue (so -> typeInfo ,so -> collation ,value );
225
+
226
+ MemoryContextSwitchTo (oldCtx );
227
+ }
225
228
}
226
229
227
230
return value ;
@@ -253,6 +256,7 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
253
256
int dimensions ;
254
257
int probes = ivfflat_probes ;
255
258
int maxProbes ;
259
+ MemoryContext oldCtx ;
256
260
257
261
scan = RelationGetIndexScan (index ,nkeys ,norderbys );
258
262
@@ -292,6 +296,12 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
292
296
so -> normprocinfo = IvfflatOptionalProcInfo (index ,IVFFLAT_NORM_PROC );
293
297
so -> collation = index -> rd_indcollation [0 ];
294
298
299
+ so -> tmpCtx = AllocSetContextCreate (CurrentMemoryContext ,
300
+ "Ivfflat scan temporary context" ,
301
+ ALLOCSET_DEFAULT_SIZES );
302
+
303
+ oldCtx = MemoryContextSwitchTo (so -> tmpCtx );
304
+
295
305
/* Create tuple description for sorting */
296
306
so -> tupdesc = CreateTemplateTupleDesc (2 );
297
307
TupleDescInitEntry (so -> tupdesc , (AttrNumber )1 ,"distance" ,FLOAT8OID ,-1 ,0 );
@@ -316,6 +326,8 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
316
326
so -> listIndex = 0 ;
317
327
so -> lists = palloc (maxProbes * sizeof (IvfflatScanList ));
318
328
329
+ MemoryContextSwitchTo (oldCtx );
330
+
319
331
scan -> opaque = so ;
320
332
321
333
return scan ;
@@ -377,8 +389,6 @@ ivfflatgettuple(IndexScanDesc scan, ScanDirection dir)
377
389
IvfflatBench ("GetScanItems" ,GetScanItems (scan ,value ));
378
390
so -> first = false;
379
391
so -> value = value ;
380
-
381
- /* TODO clean up if we allocated a new value */
382
392
}
383
393
384
394
while (!tuplesort_gettupleslot (so -> sortstate , true, false,so -> mslot ,NULL ))
@@ -405,14 +415,10 @@ ivfflatendscan(IndexScanDesc scan)
405
415
{
406
416
IvfflatScanOpaque so = (IvfflatScanOpaque )scan -> opaque ;
407
417
408
- pairingheap_free (so -> listQueue );
409
- pfree (so -> listPages );
418
+ /* Free any temporary files */
410
419
tuplesort_end (so -> sortstate );
411
- FreeAccessStrategy (so -> bas );
412
- FreeTupleDesc (so -> tupdesc );
413
- pfree (so -> lists );
414
420
415
- /* TODO Free vslot and mslot without freeing TupleDesc */
421
+ MemoryContextDelete ( so -> tmpCtx );
416
422
417
423
pfree (so );
418
424
scan -> opaque = NULL ;