Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VXoLj-00072t-Ub for pgsql-bugs@arkaria.postgresql.org; Sun, 20 Oct 2013 08:27:00 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VXoLj-0002p5-8r for pgsql-bugs@arkaria.postgresql.org; Sun, 20 Oct 2013 08:26:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VXoLh-0002nc-Pg for pgsql-bugs@postgresql.org; Sun, 20 Oct 2013 08:26:57 +0000 Received: from d4b26f12.static.ziggozakelijk.nl ([212.178.111.18] helo=storagefox.foxi.nl) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VXoLf-0002Mg-H0 for pgsql-bugs@postgresql.org; Sun, 20 Oct 2013 08:26:57 +0000 Received: from techfox.foxi (techfox.foxi [192.168.0.31]) by storagefox.foxi.nl (8.14.5/8.14.5) with ESMTP id r9K8Qr3P030059; Sun, 20 Oct 2013 10:26:53 +0200 From: Frank van Vugt To: pgsql-bugs@postgresql.org Cc: Tomas Vondra 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 10:26:53 +0200 Message-ID: <1615181.fDHOPIPOzk@techfox.foxi> Organization: Foxi Group User-Agent: KMail/4.10.5 (Linux/3.7.10-1.16-desktop; KDE/4.10.5; x86_64; ; ) In-Reply-To: <52634461.2020002@fuzzy.cz> References: <3839201.Nfa2RvcheX@techfox.foxi> <52634461.2020002@fuzzy.cz> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" 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 Hi, Interesting read so far! Op zondag 20 oktober 2013 04:48:01 schreef Tomas Vondra: > because the example is constructed so that groups with multiple elements > are very unlikely). yep, the original intention of the 'experiment' was to investigate how often random() would 'clash' on various machines with/without hardware number generators, etc > the report says that it consumed ~32GB RAM and swap (not sure how > much, but probably not a small amount). On my machine it easily ate 8GB > of RAM and 4GB of swap (and then got shot by OOM). amount of swap is 32GB as well (see output of 'free' on top of report ;) ) > Anyway, I disabled the preallocation (i.e. 1 element initially, +1 for > each iteration) which should be ~80MB of data, but even then I was > unable to execute that query. > > The effect on smaller queries (say, 1e6 rows) was negligible too - it > consumed more or less the same amount of memory, irrespectedly of the > preallocation. on my setup, running the array_agg() over 1e6 records uses up ~5GB > With 1e6 groups that's ~8GB (not really far from what I see here), and > with 1e7 groups it's ~80GB. Not the most efficient approach for 80MB of > values. exactly, the 'scaling' of memory hunger was not what I'd expect from such a recordset, which made me write up the report > The failing query is slightly artificial, but pretty much any array_agg > query with large number of groups is going to fail exactly the same. So > if we could improve that somehow, that'd be nice. exactly > But let's say it's a minor issue, and by switching to a shared memory > context we've already saved ~50% memory for such these cases (single > element in a group, 1kB chunk - 64*8B = 512B). 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. -- Best, Frank. -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs