Skip to content

Commit 642cd0a

Browse files
committed
Repair memory leakage introduced into the non-hashed aggregate case by
7.4 rewrite for hashed aggregate support. If the transition data type is pass-by-reference, the transValue must be pfreed when starting a new group boundary, else we have a one-value-per-group leakage. Thanks to Rae Steining for providing a reproducible test case.
1 parent 58e3511 commit 642cd0a

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/backend/executor/nodeAgg.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
* Portions Copyright (c) 1994, Regents of the University of California
4646
*
4747
* IDENTIFICATION
48-
* $PostgreSQL: pgsql/src/backend/executor/nodeAgg.c,v 1.118 2004/02/03 17:34:02 tgl Exp $
48+
* $PostgreSQL: pgsql/src/backend/executor/nodeAgg.c,v 1.119 2004/03/13 00:54:10 tgl Exp $
4949
*
5050
*-------------------------------------------------------------------------
5151
*/
@@ -251,6 +251,18 @@ initialize_aggregates(AggState *aggstate,
251251
work_mem, false);
252252
}
253253

254+
/*
255+
* If we are reinitializing after a group boundary, we have to free
256+
* any prior transValue to avoid memory leakage. We must check not
257+
* only the isnull flag but whether the pointer is NULL; since
258+
* pergroupstate is initialized with palloc0, the initial condition
259+
* has isnull = 0 and null pointer.
260+
*/
261+
if (!peraggstate->transtypeByVal &&
262+
!pergroupstate->transValueIsNull &&
263+
DatumGetPointer(pergroupstate->transValue) != NULL)
264+
pfree(DatumGetPointer(pergroupstate->transValue));
265+
254266
/*
255267
* (Re)set transValue to the initial value.
256268
*
@@ -1472,6 +1484,12 @@ ExecReScanAgg(AggState *node, ExprContext *exprCtxt)
14721484
build_hash_table(node);
14731485
node->table_filled = false;
14741486
}
1487+
else
1488+
{
1489+
/* Reset the per-group state (in particular, mark transvalues null) */
1490+
MemSet(node->pergroup, 0,
1491+
sizeof(AggStatePerGroupData) * node->numaggs);
1492+
}
14751493

14761494
/*
14771495
* if chgParam of subnode is not null then plan will be re-scanned by

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy