pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Sergey Burladyan <eshkinkot@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #7916: memory leak with array_agg
Date: Wed, 06 Mar 2013 03:54:26 +0400
Message-ID: <87ehftierx.fsf@home.progtech.ru> (raw)
In-Reply-To: <16353.1362519721@sss.pgh.pa.us>
References: <E1UCeEU-0004hY-Pq@wrigleys.postgresql.org>
	<16353.1362519721@sss.pgh.pa.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

Tom Lane <tgl@sss.pgh.pa.us> writes:

> AFAICT there's no actual leak here; array_agg is just optimized for
> speed rather than space.  It eats about 8K per hashtable entry.
> While the planner knows that, it's got no good idea how many groups
> will be produced by the query, so it underestimates the space needed
> --- and the HashAggregate code is not currently capable of spilling
> the hashtable to disk, so the table balloons well past the intended
> work_mem limit.

Aha, I found this "8K per hashtable entry" with ltrace and now understand
what is going on here:
  === array_agg malloc calls count ===  === myagg malloc calls count===
      3 malloc(1024)                        3 malloc(1024)     
      3 malloc(1048576)                     3 malloc(1048576)  
      3 malloc(131072)                      3 malloc(131072)   
      1 malloc(16056)                       4 malloc(16384)    
      5 malloc(16384)                       2 malloc(16440)    
      2 malloc(16440)                       1 malloc(2048)     
      1 malloc(2048)                        3 malloc(2097152)  
      3 malloc(2097152)                     3 malloc(262144)   
      3 malloc(262144)                      3 malloc(32768)    
      3 malloc(32768)                       1 malloc(32824)    
      1 malloc(32824)                       1 malloc(4096)     
      1 malloc(4096)                        3 malloc(4194304)  
      3 malloc(4194304)                     3 malloc(524288)   
      3 malloc(524288)                      3 malloc(65536)    
      3 malloc(65536)                      12 malloc(8192)     
 724151 malloc(8192)                        1 malloc(8296)     
      1 malloc(8296)                       29 malloc(8360)     
     44 malloc(8360)                       16 malloc(8388608)  
      8 malloc(8388608)

Thank you for answer Tom!

> Although no real fix for this is within easy reach, it strikes me
> that we could possibly ameliorate things a bit by tweaking the
> memory context size parameters used by accumArrayResult().
> It would likely be reasonable to set the min size to 1K or so not 8K.
> This would make things a trifle slower when the actual space requirement
> exceeds 1K, but probably not by enough to notice.

Looks good.

> BTW, I don't believe your assertion that the handmade aggregate does
> this in 7MB.  Even a very optimistic calculation puts the space needed
> for 700000 10-element integer arrays at forty-some MB, and when I try
> it I see more like 100MB consumed thanks to hashtable overhead.

Yes you are right, Tom. My mistake.

-- 
Sergey Burladyan


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (3+ messages)

Message-ID: <87ehftierx.fsf@home.progtech.ru>
Permalink:  ../87ehftierx.fsf@home.progtech.ru/
Also on:    postgresql.org/message-id/87ehftierx.fsf@home.progtech.ru

 · 

reply

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: eshkinkot@gmail.com, tgl@sss.pgh.pa.us
  Subject: Re: BUG #7916: memory leak with array_agg
  In-Reply-To: <87ehftierx.fsf@home.progtech.ru>
  References: <E1UCeEU-0004hY-Pq@wrigleys.postgresql.org> <16353.1362519721@sss.pgh.pa.us> <87ehftierx.fsf@home.progtech.ru>

* 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