Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VXqhl-0003qu-Eh for pgsql-bugs@arkaria.postgresql.org; Sun, 20 Oct 2013 10:57:53 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VXqhk-0004QJ-Sw for pgsql-bugs@arkaria.postgresql.org; Sun, 20 Oct 2013 10:57:52 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VXqhj-0004OJ-GR for pgsql-bugs@postgresql.org; Sun, 20 Oct 2013 10:57:51 +0000 Received: from mail.gransy.com ([2001:1528:137::f1]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VXqhb-0005cV-9h for pgsql-bugs@postgresql.org; Sun, 20 Oct 2013 10:57:50 +0000 Received: from antispam.gransy.com (antispam.gransy.com [77.78.104.124]) by mail.gransy.com (Postfix) with ESMTP id 6B925508C17; Sun, 20 Oct 2013 12:57:40 +0200 (CEST) X-Virus-Scanned: G-Hosting.cz + Station.cz + Klenot.cz + Subreg.cz amavisd-new at antispam.gransy.com Received: from mail.gransy.com ([82.208.44.189]) by antispam.gransy.com (antispam.gransy.com [77.78.104.124]) (amavisd-new, port 10024) with ESMTP id jBDq0HPhry6t; Sun, 20 Oct 2013 12:57:47 +0200 (CEST) Received: by mail.gransy.com (Postfix, from userid 5000) id 30E4F508C1F; Sun, 20 Oct 2013 12:57:40 +0200 (CEST) Received: from sq.gransy.com (elizabeth.gransy.com [89.187.132.199]) by mail.gransy.com (Postfix) with ESMTPA id 7DA0D508C17; Sun, 20 Oct 2013 12:57:39 +0200 (CEST) Received: from 80.250.29.169 (SquirrelMail authenticated user tv@fuzzy.cz) by sq.gransy.com with HTTP; Sun, 20 Oct 2013 12:57:43 +0200 Message-ID: In-Reply-To: <1615181.fDHOPIPOzk@techfox.foxi> References: <3839201.Nfa2RvcheX@techfox.foxi> <52634461.2020002@fuzzy.cz> <1615181.fDHOPIPOzk@techfox.foxi> Date: Sun, 20 Oct 2013 12:57:43 +0200 Subject: Re: array_agg() on a set larger than some arbitrary(?) limit causes runaway memory usage and eventually memory exhaustion From: "Tomas Vondra" To: "Frank van Vugt" Cc: pgsql-bugs@postgresql.org, "Tomas Vondra" User-Agent: SquirrelMail/1.4.22 MIME-Version: 1.0 Content-Type: multipart/mixed;boundary="----=_20131020125742_48374" X-Priority: 3 (Normal) Importance: Normal X-Pg-Spam-Score: -0.0 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org ------=_20131020125742_48374 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit 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 ------=_20131020125742_48374 Content-Type: text/x-diff; name="array_agg.patch" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename="array_agg.patch" diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c index 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 *) ------=_20131020125742_48374 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ------=_20131020125742_48374--