Skip to content

Commit 82dd1c2

Browse files
committed
Fix memory leak in repeated SPGIST index scans.
spgendscan neglected to pfree all the memory allocated by spgbeginscan. It's possible to get away with that in most normal queries, since the memory is allocated in the executor's per-query context which is about to get deleted anyway; but it causes severe memory leakage during creation or filling of large exclusion-constraint indexes. Also, document that amendscan is supposed to free what ambeginscan allocates. The docs' lack of clarity on that point probably caused this bug to begin with. (There is discussion of changing that API spec going forward, but I don't think it'd be appropriate for the back branches.) Per report from Bruno Wolff. It's been like this since the beginning, so back-patch to all active branches. In HEAD, also fix an independent leak caused by commit 2a63683 (allocating memory during spgrescan instead of spgbeginscan, which might be all right if it got cleaned up, but it didn't). And do a bit of code beautification on that commit, too. Discussion: https://postgr.es/m/20181024012314.GA27428@wolff.to
1 parent 7dd8b3c commit 82dd1c2

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

doc/src/sgml/indexam.sgml

+2-1
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,8 @@ amendscan (IndexScanDesc scan);
455455
</programlisting>
456456
End a scan and release resources. The <literal>scan</> struct itself
457457
should not be freed, but any locks or pins taken internally by the
458-
access method must be released.
458+
access method must be released, as well as any other memory allocated
459+
by <function>ambeginscan</function> and other scan-related functions.
459460
</para>
460461

461462
<para>

src/backend/access/spgist/spgscan.c

+8
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ spgendscan(PG_FUNCTION_ARGS)
236236

237237
MemoryContextDelete(so->tempCxt);
238238

239+
if (so->keyData)
240+
pfree(so->keyData);
241+
242+
if (so->state.deadTupleStorage)
243+
pfree(so->state.deadTupleStorage);
244+
245+
pfree(so);
246+
239247
PG_RETURN_VOID();
240248
}
241249

0 commit comments

Comments
 (0)