@@ -33,11 +33,19 @@ rumbeginscan(Relation rel, int nkeys, int norderbys)
33
33
so -> keys = NULL ;
34
34
so -> nkeys = 0 ;
35
35
so -> firstCall = true;
36
+ so -> totalentries = 0 ;
37
+ so -> sortedEntries = NULL ;
36
38
so -> tempCtx = AllocSetContextCreate (CurrentMemoryContext ,
37
39
"Rum scan temporary context" ,
38
40
ALLOCSET_DEFAULT_MINSIZE ,
39
41
ALLOCSET_DEFAULT_INITSIZE ,
40
42
ALLOCSET_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
+
41
49
initRumState (& so -> rumstate , scan -> indexRelation );
42
50
43
51
scan -> opaque = so ;
@@ -249,32 +257,6 @@ freeScanKeys(RumScanOpaque so)
249
257
{
250
258
uint32 i ;
251
259
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
-
278
260
for (i = 0 ; i < so -> totalentries ; i ++ )
279
261
{
280
262
RumScanEntry entry = so -> entries [i ];
@@ -302,7 +284,10 @@ freeScanKeys(RumScanOpaque so)
302
284
pfree (entry );
303
285
}
304
286
305
- pfree (so -> entries );
287
+ MemoryContextReset (so -> keyCtx );
288
+ so -> keys = NULL ;
289
+ so -> nkeys = 0 ;
290
+
306
291
if (so -> sortedEntries )
307
292
pfree (so -> sortedEntries );
308
293
so -> entries = NULL ;
@@ -406,6 +391,14 @@ rumNewScanKey(IndexScanDesc scan)
406
391
RumScanOpaque so = (RumScanOpaque ) scan -> opaque ;
407
392
int i ;
408
393
bool 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 );
409
402
410
403
/* if no scan keys provided, allocate extra EVERYTHING RumScanKey */
411
404
so -> keys = (RumScanKey )
@@ -457,6 +450,8 @@ rumNewScanKey(IndexScanDesc scan)
457
450
NULL , NULL , NULL , NULL , false);
458
451
}
459
452
453
+ MemoryContextSwitchTo (oldCtx );
454
+
460
455
pgstat_count_index_scan (scan -> indexRelation );
461
456
}
462
457
@@ -497,6 +492,7 @@ rumendscan(IndexScanDesc scan)
497
492
rum_tuplesort_end (so -> sortstate );
498
493
499
494
MemoryContextDelete (so -> tempCtx );
495
+ MemoryContextDelete (so -> keyCtx );
500
496
501
497
pfree (so );
502
498
}
0 commit comments