@@ -33,11 +33,19 @@ rumbeginscan(Relation rel, int nkeys, int norderbys)
3333so -> keys = NULL ;
3434so -> nkeys = 0 ;
3535so -> firstCall = true;
36+ so -> totalentries = 0 ;
37+ so -> sortedEntries = NULL ;
3638so -> tempCtx = AllocSetContextCreate (CurrentMemoryContext ,
3739"Rum scan temporary context" ,
3840ALLOCSET_DEFAULT_MINSIZE ,
3941ALLOCSET_DEFAULT_INITSIZE ,
4042ALLOCSET_DEFAULT_MAXSIZE );
43+ so -> keyCtx = AllocSetContextCreate (CurrentMemoryContext ,
44+ "Gin scan key context" ,
45+ ALLOCSET_DEFAULT_MINSIZE ,
46+ ALLOCSET_DEFAULT_INITSIZE ,
47+ ALLOCSET_DEFAULT_MAXSIZE );
48+
4149initRumState (& so -> rumstate ,scan -> indexRelation );
4250
4351scan -> opaque = so ;
@@ -249,32 +257,6 @@ freeScanKeys(RumScanOpaque so)
249257{
250258uint32 i ;
251259
252- if (so -> keys == NULL )
253- return ;
254-
255- for (i = 0 ;i < so -> nkeys ;i ++ )
256- {
257- RumScanKey key = so -> keys + i ;
258-
259- if (key -> nentries > 0 )
260- {
261- if (key -> scanEntry )
262- pfree (key -> scanEntry );
263- if (key -> entryRes )
264- pfree (key -> entryRes );
265- if (key -> addInfo )
266- pfree (key -> addInfo );
267- if (key -> addInfoIsNull )
268- pfree (key -> addInfoIsNull );
269- if (key -> queryCategories )
270- pfree (key -> queryCategories );
271- }
272- }
273-
274- pfree (so -> keys );
275- so -> keys = NULL ;
276- so -> nkeys = 0 ;
277-
278260for (i = 0 ;i < so -> totalentries ;i ++ )
279261{
280262RumScanEntry entry = so -> entries [i ];
@@ -302,7 +284,10 @@ freeScanKeys(RumScanOpaque so)
302284pfree (entry );
303285}
304286
305- pfree (so -> entries );
287+ MemoryContextReset (so -> keyCtx );
288+ so -> keys = NULL ;
289+ so -> nkeys = 0 ;
290+
306291if (so -> sortedEntries )
307292pfree (so -> sortedEntries );
308293so -> entries = NULL ;
@@ -406,6 +391,14 @@ rumNewScanKey(IndexScanDesc scan)
406391RumScanOpaque so = (RumScanOpaque )scan -> opaque ;
407392int i ;
408393bool hasNullQuery = false;
394+ MemoryContext oldCtx ;
395+
396+ /*
397+ * Allocate all the scan key information in the key context. (If
398+ * extractQuery leaks anything there, it won't be reset until the end of
399+ * scan or rescan, but that's OK.)
400+ */
401+ oldCtx = MemoryContextSwitchTo (so -> keyCtx );
409402
410403/* if no scan keys provided, allocate extra EVERYTHING RumScanKey */
411404so -> keys = (RumScanKey )
@@ -457,6 +450,8 @@ rumNewScanKey(IndexScanDesc scan)
457450NULL ,NULL ,NULL ,NULL , false);
458451}
459452
453+ MemoryContextSwitchTo (oldCtx );
454+
460455pgstat_count_index_scan (scan -> indexRelation );
461456}
462457
@@ -497,6 +492,7 @@ rumendscan(IndexScanDesc scan)
497492rum_tuplesort_end (so -> sortstate );
498493
499494MemoryContextDelete (so -> tempCtx );
495+ MemoryContextDelete (so -> keyCtx );
500496
501497pfree (so );
502498}