From: Tomas Vondra <tv@fuzzy.cz>
To: Frank van Vugt <ftm.van.vugt@foxi.nl>
Cc: pgsql-bugs@postgresql.org
Cc: Tomas Vondra <tv@fuzzy.cz>
Subject: Re: array_agg() on a set larger than some arbitrary(?) limit causes runaway memory usage and eventually memory exhaustion
Date: Sun, 20 Oct 2013 12:57:43 +0200
Message-ID: <c1b68cb9c6007d7fd7bcdbce3e8a9983.squirrel@sq.gransy.com> (raw)
In-Reply-To: <1615181.fDHOPIPOzk@techfox.foxi>
References: <3839201.Nfa2RvcheX@techfox.foxi>
<52634461.2020002@fuzzy.cz>
<1615181.fDHOPIPOzk@techfox.foxi>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
On 20 Říjen 2013, 10:26, Frank van Vugt wrote:
>
> I never ran into this earlier (and I've been using PostgreSQL for quite
> some
> time now) and even now, this is not about a 'production query', so I'm
> happy
> with any and all benefits for future versions that come from this ;)
>
> Having said that, should you want me to check the effects of some patch,
> just
> let me know, I'd be happy to do that.
Attached is a quick patch removing the local memory context and using
aggcontext instead. I've also tuned down the preallocation. It's against
9.2 stable, so it should apply fine against your 9.2.4. But be careful,
it's not really tested.
Tomas
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugsAttachments:array_agg.patch (text/x-diff, 1.5K) downloaddiff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.cindex 5582a06..6d8bead 100644--- a/src/backend/utils/adt/arrayfuncs.c+++ b/src/backend/utils/adt/arrayfuncs.c@@ -4552,23 +4552,15 @@ accumArrayResult(ArrayBuildState *astate,
Oid element_type,
MemoryContext rcontext)
{
- MemoryContext arr_context,- oldcontext;+ MemoryContext oldcontext;
if (astate == NULL)
{
/* First time through --- initialize */
-- /* Make a temporary context to hold all the junk */- arr_context = AllocSetContextCreate(rcontext,- "accumArrayResult",- ALLOCSET_DEFAULT_MINSIZE,- ALLOCSET_DEFAULT_INITSIZE,- ALLOCSET_DEFAULT_MAXSIZE);- oldcontext = MemoryContextSwitchTo(arr_context);+ oldcontext = MemoryContextSwitchTo(rcontext);
astate = (ArrayBuildState *) palloc(sizeof(ArrayBuildState));
- astate->mcontext = arr_context;- astate->alen = 64; /* arbitrary starting array size */+ astate->mcontext = rcontext;+ astate->alen = 4; /* arbitrary starting array size */
astate->dvalues = (Datum *) palloc(astate->alen * sizeof(Datum));
astate->dnulls = (bool *) palloc(astate->alen * sizeof(bool));
astate->nelems = 0;
@@ -4585,7 +4577,7 @@ accumArrayResult(ArrayBuildState *astate,
/* enlarge dvalues[]/dnulls[] if needed */
if (astate->nelems >= astate->alen)
{
- astate->alen *= 2;+ astate->alen += 4;
astate->dvalues = (Datum *)
repalloc(astate->dvalues, astate->alen * sizeof(Datum));
astate->dnulls = (bool *)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tv@fuzzy.cz, ftm.van.vugt@foxi.nl
Subject: Re: array_agg() on a set larger than some arbitrary(?) limit causes runaway memory usage and eventually memory exhaustion
In-Reply-To: <c1b68cb9c6007d7fd7bcdbce3e8a9983.squirrel@sq.gransy.com>
References: <3839201.Nfa2RvcheX@techfox.foxi> <52634461.2020002@fuzzy.cz> <1615181.fDHOPIPOzk@techfox.foxi> <c1b68cb9c6007d7fd7bcdbce3e8a9983.squirrel@sq.gransy.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox