Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6a0865e

Browse files
committed
In a non-hashed Agg node, reset the "aggcontext" at group boundaries, instead
of individually pfree'ing pass-by-reference transition values. This shouldbe at least as fast as the prior coding, and it has the major advantage ofclearing out any working data an aggregate function may have stored in orunderneath the aggcontext. This avoids memory leakage when an aggregatesuch as array_agg() is used in GROUP BY mode. Per report from Chris Spotts.Back-patch to 8.4. In principle the problem could arise in prior versions,but since they didn't have array_agg the issue seems not critical.
1 parent1ca695d commit6a0865e

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

‎src/backend/executor/nodeAgg.c

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@
5555
* in either case its value need not be preserved. See int8inc() for an
5656
* example.Notice that advance_transition_function() is coded to avoid a
5757
* data copy step when the previous transition value pointer is returned.
58+
* Also, some transition functions make use of the aggcontext to store
59+
* working state.
5860
*
5961
*
6062
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
6163
* Portions Copyright (c) 1994, Regents of the University of California
6264
*
6365
* IDENTIFICATION
64-
* $PostgreSQL: pgsql/src/backend/executor/nodeAgg.c,v 1.167 2009/06/17 16:05:34 tgl Exp $
66+
* $PostgreSQL: pgsql/src/backend/executor/nodeAgg.c,v 1.168 2009/07/23 20:45:27 tgl Exp $
6567
*
6668
*-------------------------------------------------------------------------
6769
*/
@@ -272,18 +274,6 @@ initialize_aggregates(AggState *aggstate,
272274
work_mem, false);
273275
}
274276

275-
/*
276-
* If we are reinitializing after a group boundary, we have to free
277-
* any prior transValue to avoid memory leakage. We must check not
278-
* only the isnull flag but whether the pointer is NULL; since
279-
* pergroupstate is initialized with palloc0, the initial condition
280-
* has isnull = 0 and null pointer.
281-
*/
282-
if (!peraggstate->transtypeByVal&&
283-
!pergroupstate->transValueIsNull&&
284-
DatumGetPointer(pergroupstate->transValue)!=NULL)
285-
pfree(DatumGetPointer(pergroupstate->transValue));
286-
287277
/*
288278
* (Re)set transValue to the initial value.
289279
*
@@ -911,10 +901,15 @@ agg_retrieve_direct(AggState *aggstate)
911901
}
912902

913903
/*
914-
* Clear the per-output-tuple context for each group
904+
* Clear the per-output-tuple context for each group, as well as
905+
* aggcontext (which contains any pass-by-ref transvalues of the
906+
* old group). We also clear any child contexts of the aggcontext;
907+
* some aggregate functions store working state in such contexts.
915908
*/
916909
ResetExprContext(econtext);
917910

911+
MemoryContextResetAndDeleteChildren(aggstate->aggcontext);
912+
918913
/*
919914
* Initialize working state for a new input tuple group
920915
*/
@@ -1234,7 +1229,8 @@ ExecInitAgg(Agg *node, EState *estate, int eflags)
12341229
* structures and transition values. NOTE: the details of what is stored
12351230
* in aggcontext and what is stored in the regular per-query memory
12361231
* context are driven by a simple decision: we want to reset the
1237-
* aggcontext in ExecReScanAgg to recover no-longer-wanted space.
1232+
* aggcontext at group boundaries (if not hashing) and in ExecReScanAgg
1233+
* to recover no-longer-wanted space.
12381234
*/
12391235
aggstate->aggcontext=
12401236
AllocSetContextCreate(CurrentMemoryContext,

‎src/backend/utils/adt/array_userfuncs.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Copyright (c) 2003-2009, PostgreSQL Global Development Group
77
*
88
* IDENTIFICATION
9-
* $PostgreSQL: pgsql/src/backend/utils/adt/array_userfuncs.c,v 1.31 2009/06/20 18:45:28 tgl Exp $
9+
* $PostgreSQL: pgsql/src/backend/utils/adt/array_userfuncs.c,v 1.32 2009/07/23 20:45:27 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -539,7 +539,9 @@ array_agg_finalfn(PG_FUNCTION_ARGS)
539539

540540
/*
541541
* Make the result. We cannot release the ArrayBuildState because
542-
* sometimes aggregate final functions are re-executed.
542+
* sometimes aggregate final functions are re-executed. Rather, it
543+
* is nodeAgg.c's responsibility to reset the aggcontext when it's
544+
* safe to do so.
543545
*/
544546
result=makeMdArrayResult(state,1,dims,lbs,
545547
CurrentMemoryContext,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp