Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VcOt1-0004r9-Px for pgsql-bugs@arkaria.postgresql.org; Sat, 02 Nov 2013 00:16:19 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VcOt1-0003LA-5J for pgsql-bugs@arkaria.postgresql.org; Sat, 02 Nov 2013 00:16:19 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VcOsz-0003L1-73 for pgsql-bugs@postgresql.org; Sat, 02 Nov 2013 00:16:17 +0000 Received: from mail.gransy.com ([2001:1528:137::f1]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VcOsv-0007Jo-Rw for pgsql-bugs@postgresql.org; Sat, 02 Nov 2013 00:16:16 +0000 Received: from antispam.gransy.com (antispam.gransy.com [77.78.104.124]) by mail.gransy.com (Postfix) with ESMTP id 43773508C44; Sat, 2 Nov 2013 01:16:10 +0100 (CET) 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 mWObyAw22UwI; Sat, 2 Nov 2013 01:16:10 +0100 (CET) Received: by mail.gransy.com (Postfix, from userid 5000) id DA243508C47; Sat, 2 Nov 2013 01:16:09 +0100 (CET) Received: from sq.gransy.com (elizabeth.gransy.com [89.187.132.199]) by mail.gransy.com (Postfix) with ESMTPA id 38F80508C44; Sat, 2 Nov 2013 01:16:09 +0100 (CET) Received: from 86.189.11.138 (SquirrelMail authenticated user tv@fuzzy.cz) by sq.gransy.com with HTTP; Sat, 2 Nov 2013 01:16:09 +0100 Message-ID: <83f4b150295c82d1687562e59e569b4c.squirrel@sq.gransy.com> In-Reply-To: <22340.1383228316@sss.pgh.pa.us> References: <3839201.Nfa2RvcheX@techfox.foxi> <1615181.fDHOPIPOzk@techfox.foxi> <3014614.ujIC87ofob@techfox.foxi> <22340.1383228316@sss.pgh.pa.us> Date: Sat, 2 Nov 2013 01:16:09 +0100 Subject: Re: array_agg() on a set larger than some arbitrary(?) limit causes runaway memory usage and eventually memory exhaustion From: "Tomas Vondra" To: "Tom Lane" Cc: "Frank van Vugt" , "Tomas Vondra" , pgsql-bugs@postgresql.org, "Valentine Gogichashvili" User-Agent: SquirrelMail/1.4.22 MIME-Version: 1.0 Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: 8bit X-Priority: 3 (Normal) Importance: Normal X-Pg-Spam-Score: -1.9 (-) 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 On 31 Říjen 2013, 15:05, Tom Lane wrote: > Frank van Vugt writes: >> Op zondag 20 oktober 2013 12:57:43 schreef Tomas Vondra: >>> Attached is a quick patch removing the local memory context and using >>> aggcontext instead. > >> It looks like this didn't go into git yet. Will it be in v9.2.6/v9.3.1? > > Certainly not. We can consider it for 9.4, if Tomas submits it to the > commitfest process, but it needs review and testing. In particular > I'm skeptical that it probably outright breaks some cases (look at > makeMdArrayResult) and results in substantial added bloat, not savings, > in others. Yes, I'll submit it to the next commitfest. And you're right that the makeMdArrayResult is faulty - it shouldn't really do the MemoryContextDelete as I reused the context of the aggregation function. Actually, I'm quite surprised it did not fail, even though my tests were only really sketchy. As for the 9.2 / 9.3 branches, I think we should discuss whether the current state is actually correct. I'm inclined to say that a code that manages to crash a server with 64GB of virtual memory, although with very light tweaking can happily complete with ~3.5GB of RAM, is not exactly right. It seems to me that the code somehow assumes the accumulated arrays are going to be quite large - in that case preallocating > 1kB per group is probably fine. I'm fine with "reasonable" preallocation, but 1kB seems way too aggressive to me. But it's not possible to use smaller preallocation because this is determined by the block size check in aset.c. So I see three options for <9.4 versions: (a) keeping the current implementation, that crashes with cases like this (and I don't really like this option, because the code seems broken to me) (b) keep the local memory context but tune it somehow - this however means changes in aset.c, which however impacts significant part of the codebase (not sure how much internal / external code relies on this behavior) (c) remove the local memory context (I'm really wondering what other benefit it might have in this particular case) and maybe tune the other parameters (e.g. the initial size / growth rate of the array etc.). I'd certainly vote for (c) in this case. Tomas -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs