Skip to content

Commit c556b29

Browse files
author
Neil Conway
committed
Fix a gradual memory leak in ExecReScanAgg(). Because the aggregation
hash table is allocated in a child context of the agg node's memory context, MemoryContextReset() will reset but *not* delete the child context. Since ExecReScanAgg() proceeds to build a new hash table from scratch (in a new sub-context), this results in leaking the header for the previous memory context. Therefore, use MemoryContextResetAndDeleteChildren() instead. Credit: My colleague Sailesh Krishnamurthy at Truviso for isolating the cause of the leak.
1 parent af1022d commit c556b29

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/backend/executor/nodeAgg.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
* Portions Copyright (c) 1994, Regents of the University of California
6262
*
6363
* IDENTIFICATION
64-
* $PostgreSQL: pgsql/src/backend/executor/nodeAgg.c,v 1.152 2007/04/02 03:49:38 tgl Exp $
64+
* $PostgreSQL: pgsql/src/backend/executor/nodeAgg.c,v 1.153 2007/08/08 18:07:05 neilc Exp $
6565
*
6666
*-------------------------------------------------------------------------
6767
*/
@@ -1646,8 +1646,14 @@ ExecReScanAgg(AggState *node, ExprContext *exprCtxt)
16461646
MemSet(econtext->ecxt_aggvalues, 0, sizeof(Datum) * node->numaggs);
16471647
MemSet(econtext->ecxt_aggnulls, 0, sizeof(bool) * node->numaggs);
16481648

1649-
/* Release all temp storage */
1650-
MemoryContextReset(node->aggcontext);
1649+
/*
1650+
* Release all temp storage. Note that with AGG_HASHED, the hash table
1651+
* is allocated in a sub-context of the aggcontext. We're going to
1652+
* rebuild the hash table from scratch, so we need to use
1653+
* MemoryContextResetAndDeleteChildren() to avoid leaking the old hash
1654+
* table's memory context header.
1655+
*/
1656+
MemoryContextResetAndDeleteChildren(node->aggcontext);
16511657

16521658
if (((Agg *) node->ss.ps.plan)->aggstrategy == AGG_HASHED)
16531659
{

0 commit comments

Comments
 (0)