aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2018-10-31 17:04:42 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2018-10-31 17:04:42 -0400
commit2493e2c2d167ce7ad7d034c7c7de17d79e806c7a (patch)
tree5b0dfab4f336d1f94561270cd86737b420f8cf94
parentf107c33461e5372fa2257c09bcdb173c9d47dc03 (diff)
downloadpostgresql-2493e2c2d167ce7ad7d034c7c7de17d79e806c7a.tar.gz
postgresql-2493e2c2d167ce7ad7d034c7c7de17d79e806c7a.zip
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 2a6368343 (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
-rw-r--r--doc/src/sgml/indexam.sgml3
-rw-r--r--src/backend/access/spgist/spgscan.c8
2 files changed, 10 insertions, 1 deletions
diff --git a/doc/src/sgml/indexam.sgml b/doc/src/sgml/indexam.sgml
index beb99d1831a..d758a4987dc 100644
--- a/doc/src/sgml/indexam.sgml
+++ b/doc/src/sgml/indexam.sgml
@@ -614,7 +614,8 @@ amendscan (IndexScanDesc scan);
</programlisting>
End a scan and release resources. The <literal>scan</literal> struct itself
should not be freed, but any locks or pins taken internally by the
- access method must be released.
+ access method must be released, as well as any other memory allocated
+ by <function>ambeginscan</function> and other scan-related functions.
</para>
<para>
diff --git a/src/backend/access/spgist/spgscan.c b/src/backend/access/spgist/spgscan.c
index 5260d5017d1..aede1a76cbf 100644
--- a/src/backend/access/spgist/spgscan.c
+++ b/src/backend/access/spgist/spgscan.c
@@ -240,6 +240,14 @@ spgendscan(IndexScanDesc scan)
MemoryContextDelete(so->tempCxt);
MemoryContextDelete(so->traversalCxt);
+
+ if (so->keyData)
+ pfree(so->keyData);
+
+ if (so->state.deadTupleStorage)
+ pfree(so->state.deadTupleStorage);
+
+ pfree(so);
}
/*