Skip to content

Commit 88a4c4c

Browse files
author
Alexander Korotkov
committed
Separate memory context for keys.
1 parent a63521e commit 88a4c4c

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

rum.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ typedef struct
632632
typedef struct RumScanOpaqueData
633633
{
634634
MemoryContext tempCtx;
635+
MemoryContext keyCtx; /* used to hold key and entry data */
635636
RumState rumstate;
636637

637638
RumScanKey keys; /* one per scan qualifier expr */

rumget.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,15 +602,18 @@ scan_entry_cmp(const void *p1, const void *p2)
602602
static void
603603
startScan(IndexScanDesc scan)
604604
{
605+
MemoryContext oldCtx = CurrentMemoryContext;
605606
RumScanOpaque so = (RumScanOpaque) scan->opaque;
606607
RumState *rumstate = &so->rumstate;
607608
uint32 i;
608609
bool useFastScan = false;
609610

611+
MemoryContextSwitchTo(so->keyCtx);
610612
for (i = 0; i < so->totalentries; i++)
611613
{
612614
startScanEntry(rumstate, so->entries[i]);
613615
}
616+
MemoryContextSwitchTo(oldCtx);
614617

615618
if (RumFuzzySearchLimit > 0)
616619
{

rumscan.c

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,19 @@ rumbeginscan(Relation rel, int nkeys, int norderbys)
3333
so->keys = NULL;
3434
so->nkeys = 0;
3535
so->firstCall = true;
36+
so->totalentries = 0;
37+
so->sortedEntries = NULL;
3638
so->tempCtx = AllocSetContextCreate(CurrentMemoryContext,
3739
"Rum scan temporary context",
3840
ALLOCSET_DEFAULT_MINSIZE,
3941
ALLOCSET_DEFAULT_INITSIZE,
4042
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+
4149
initRumState(&so->rumstate, scan->indexRelation);
4250

4351
scan->opaque = so;
@@ -249,32 +257,6 @@ freeScanKeys(RumScanOpaque so)
249257
{
250258
uint32 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-
278260
for (i = 0; i < so->totalentries; i++)
279261
{
280262
RumScanEntry entry = so->entries[i];
@@ -302,7 +284,10 @@ freeScanKeys(RumScanOpaque so)
302284
pfree(entry);
303285
}
304286

305-
pfree(so->entries);
287+
MemoryContextReset(so->keyCtx);
288+
so->keys = NULL;
289+
so->nkeys = 0;
290+
306291
if (so->sortedEntries)
307292
pfree(so->sortedEntries);
308293
so->entries = NULL;
@@ -406,6 +391,14 @@ rumNewScanKey(IndexScanDesc scan)
406391
RumScanOpaque so = (RumScanOpaque) scan->opaque;
407392
int i;
408393
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);
409402

410403
/* if no scan keys provided, allocate extra EVERYTHING RumScanKey */
411404
so->keys = (RumScanKey)
@@ -457,6 +450,8 @@ rumNewScanKey(IndexScanDesc scan)
457450
NULL, NULL, NULL, NULL, false);
458451
}
459452

453+
MemoryContextSwitchTo(oldCtx);
454+
460455
pgstat_count_index_scan(scan->indexRelation);
461456
}
462457

@@ -497,6 +492,7 @@ rumendscan(IndexScanDesc scan)
497492
rum_tuplesort_end(so->sortstate);
498493

499494
MemoryContextDelete(so->tempCtx);
495+
MemoryContextDelete(so->keyCtx);
500496

501497
pfree(so);
502498
}

0 commit comments

Comments
 (0)