Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UD1gq-0000E7-V7 for pgsql-bugs@arkaria.postgresql.org; Tue, 05 Mar 2013 23:54:37 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UD1gq-0005Q6-8w for pgsql-bugs@arkaria.postgresql.org; Tue, 05 Mar 2013 23:54:36 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UD1gp-0005Q0-56 for pgsql-bugs@postgresql.org; Tue, 05 Mar 2013 23:54:35 +0000 Received: from mail-lb0-f170.google.com ([209.85.217.170]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UD1gm-00080w-91 for pgsql-bugs@postgresql.org; Tue, 05 Mar 2013 23:54:34 +0000 Received: by mail-lb0-f170.google.com with SMTP id ge1so5298873lbb.1 for ; Tue, 05 Mar 2013 15:54:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version:content-type; bh=CkNIYfSlABRUl+nWz0uyejxvtbxex4XFg9rtpCvoX0M=; b=XeWNnXlBWDkp61BULkhy7GEGuagevyu1b3S0StZwD20x+aCrwN7LPkZd8ILxkghyC8 3ANyQapmTVJnAlJCiOHem9c6E6s12SUewNXl+0v3qKjePoRayoCh32LwvxJjDlkP2gz0 FLwUrSd+2xuSU5oA4RCotmTCxak+gYb5xkLe5t/3inYnPCUSbOhvhcgVww9Mpr+uBLyv 3yHIS88snCio0M2LLU1pKgbEj8sSkcsthEQ95cS5vioMteo3nVE/LMjDKzPgshqThgSG gYGFjLb1GcMeiaG+eolDdej3f+kXuiPiptIZLR1AFRKEg4rlKnCUM+o0IEMuURoyheHw F2Wg== X-Received: by 10.152.110.116 with SMTP id hz20mr22903066lab.18.1362527670473; Tue, 05 Mar 2013 15:54:30 -0800 (PST) Received: from home.progtech.ru (os.progtech.ru. [80.252.153.41]) by mx.google.com with ESMTPS id mq7sm14829154lab.1.2013.03.05.15.54.28 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Tue, 05 Mar 2013 15:54:29 -0800 (PST) From: Sergey Burladyan To: Tom Lane Cc: pgsql-bugs@postgresql.org Subject: Re: BUG #7916: memory leak with array_agg References: <16353.1362519721@sss.pgh.pa.us> Date: Wed, 06 Mar 2013 03:54:26 +0400 In-Reply-To: <16353.1362519721@sss.pgh.pa.us> (Tom Lane's message of "Tue, 05 Mar 2013 16:42:01 -0500") Message-ID: <87ehftierx.fsf@home.progtech.ru> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Pg-Spam-Score: -2.6 (--) 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 Tom Lane 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