Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ab4ff28

Browse files
committed
Fix memory leak in repeated GIN index searches.
Commit d88976c removed this code from ginFreeScanKeys(): - if (entry->list) - pfree(entry->list); evidently in the belief that that ItemPointer array is allocated in the keyCtx and so would be reclaimed by the following MemoryContextReset. Unfortunately, it isn't and it won't. It'd likely be a good idea for that to become so, but as a simple and back-patchable fix in the meantime, restore this code to ginFreeScanKeys(). Also, add a similar pfree to where startScanEntry() is about to zero out entry->list. I am not sure if there are any code paths where this change prevents a leak today, but it seems like cheap future-proofing. In passing, make the initial allocation of so->entries[] use palloc not palloc0. The code doesn't depend on unused entries being zero; if it did, the array-enlargement code in ginFillScanEntry() would be wrong. So using palloc0 initially can only serve to confuse readers about what the invariant is. Per report from Felipe de Jesús Molina Bravo, via Jaime Casanova in <CAJGNTeMR1ndMU2Thpr8GPDUfiHTV7idELJRFusA5UXUGY1y-eA@mail.gmail.com>
1 parent 96adb14 commit ab4ff28

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/backend/access/gin/ginget.c

+2
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ startScanEntry(GinState *ginstate, GinScanEntry entry)
302302
entry->buffer = InvalidBuffer;
303303
ItemPointerSetMin(&entry->curItem);
304304
entry->offset = InvalidOffsetNumber;
305+
if (entry->list)
306+
pfree(entry->list);
305307
entry->list = NULL;
306308
entry->nlist = 0;
307309
entry->matchBitmap = NULL;

src/backend/access/gin/ginscan.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ ginFreeScanKeys(GinScanOpaque so)
246246

247247
if (entry->buffer != InvalidBuffer)
248248
ReleaseBuffer(entry->buffer);
249+
if (entry->list)
250+
pfree(entry->list);
249251
if (entry->matchIterator)
250252
tbm_end_iterate(entry->matchIterator);
251253
if (entry->matchBitmap)
@@ -285,7 +287,7 @@ ginNewScanKey(IndexScanDesc scan)
285287
so->totalentries = 0;
286288
so->allocentries = 32;
287289
so->entries = (GinScanEntry *)
288-
palloc0(so->allocentries * sizeof(GinScanEntry));
290+
palloc(so->allocentries * sizeof(GinScanEntry));
289291

290292
so->isVoidRes = false;
291293

0 commit comments

Comments
 (0)