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 */
223220if (so -> normprocinfo != NULL )
221+ {
222+ MemoryContext oldCtx = MemoryContextSwitchTo (so -> tmpCtx );
223+
224224value = IvfflatNormValue (so -> typeInfo ,so -> collation ,value );
225+
226+ MemoryContextSwitchTo (oldCtx );
227+ }
225228}
226229
227230return value ;
@@ -253,6 +256,7 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
253256int dimensions ;
254257int probes = ivfflat_probes ;
255258int maxProbes ;
259+ MemoryContext oldCtx ;
256260
257261scan = RelationGetIndexScan (index ,nkeys ,norderbys );
258262
@@ -292,6 +296,12 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
292296so -> normprocinfo = IvfflatOptionalProcInfo (index ,IVFFLAT_NORM_PROC );
293297so -> 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 */
296306so -> tupdesc = CreateTemplateTupleDesc (2 );
297307TupleDescInitEntry (so -> tupdesc , (AttrNumber )1 ,"distance" ,FLOAT8OID ,-1 ,0 );
@@ -316,6 +326,8 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
316326so -> listIndex = 0 ;
317327so -> lists = palloc (maxProbes * sizeof (IvfflatScanList ));
318328
329+ MemoryContextSwitchTo (oldCtx );
330+
319331scan -> opaque = so ;
320332
321333return scan ;
@@ -377,8 +389,6 @@ ivfflatgettuple(IndexScanDesc scan, ScanDirection dir)
377389IvfflatBench ("GetScanItems" ,GetScanItems (scan ,value ));
378390so -> first = false;
379391so -> value = value ;
380-
381- /* TODO clean up if we allocated a new value */
382392}
383393
384394while (!tuplesort_gettupleslot (so -> sortstate , true, false,so -> mslot ,NULL ))
@@ -405,14 +415,10 @@ ivfflatendscan(IndexScanDesc scan)
405415{
406416IvfflatScanOpaque so = (IvfflatScanOpaque )scan -> opaque ;
407417
408- pairingheap_free (so -> listQueue );
409- pfree (so -> listPages );
418+ /* Free any temporary files */
410419tuplesort_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
417423pfree (so );
418424scan -> opaque = NULL ;