Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VXWju-0004te-Qj for pgsql-bugs@arkaria.postgresql.org; Sat, 19 Oct 2013 13:38:47 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VXWju-0001o9-4X for pgsql-bugs@arkaria.postgresql.org; Sat, 19 Oct 2013 13:38:46 +0000 Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VXWjt-0001o3-EX for pgsql-bugs@postgresql.org; Sat, 19 Oct 2013 13:38:45 +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 1VXWjr-0007vk-48 for pgsql-bugs@postgresql.org; Sat, 19 Oct 2013 13:38:45 +0000 Received: from techfox.foxi (techfox.foxi [192.168.0.31]) by storagefox.foxi.nl (8.14.5/8.14.5) with ESMTP id r9JDcfbn020267; Sat, 19 Oct 2013 15:38:41 +0200 From: Frank van Vugt To: pgsql-bugs@postgresql.org Cc: ftm.van.vugt@foxi.nl Subject: array_agg() on a set larger than some arbitrary(?) limit causes runaway memory usage and eventually memory exhaustion Date: Sat, 19 Oct 2013 15:38:40 +0200 Message-ID: <3839201.Nfa2RvcheX@techfox.foxi> Organization: Foxi Group User-Agent: KMail/4.10.5 (Linux/3.7.10-1.16-desktop; KDE/4.10.5; x86_64; ; ) 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 L.S. Something seems wrong here.... when applying arrag_agg() on a large recordset, above a certain size 'things fall over' and memory usage races off until the system runs out of it: # select version(); version ----------------------------------------------------------------------------------- PostgreSQL 9.2.4 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.7.1, 64-bit # \! free -g total used free shared buffers cached Mem: 31 1 30 0 0 0 -/+ buffers/cache: 0 30 Swap: 31 0 31 ==================== Create concatenate function and aggregate: CREATE OR REPLACE FUNCTION comma_cat(text, text) RETURNS text LANGUAGE 'plpgsql' IMMUTABLE STRICT SECURITY INVOKER AS ' BEGIN IF $1 = '''' THEN RETURN $2; ELSIF $2 = '''' THEN RETURN $1; ELSE RETURN $1 || '', '' || $2; END IF; END;'; CREATE AGGREGATE comcat(text) ( SFUNC = comma_cat, STYPE = text ); Activate timing: \timing Timing is on. Create demo data: create temp table f as select id, random() as value from generate_series(1, 1e7::int) as f(id); Time: 7036,917 ms ==================== Don't mind the 'usefulness' of the exact query below, I ran into this issue when experimenting a bit using random(). On my system, using the comcat() aggregate is no problem regardless of the size of the recordset: with g as (select * from f limit 1e5) select comcat(id::text), min(value) from g group by g.value having count(1) > 1; Time: 189,835 ms with g as (select * from f limit 1e6) select comcat(id::text), min(value) from g group by g.value having count(1) > 1; Time: 1815,756 ms with g as (select * from f) select comcat(id::text), min(value) from g group by g.value having count(1) > 1; Time: 18660,326 ms ==================== However, using the array_agg() this breaks (on my system ) on the largest set: with g as (select * from f limit 1e5) select array_agg(id), min(value) from g group by g.value having count(1) > 1; Time: 361,242 ms with g as (select * from f limit 1e6) select array_agg(id), min(value) from g group by g.value having count(1) > 1; Time: 3310,347 ms with g as (select * from f) select array_agg(id), min(value) from g group by g.value having count(1) > 1; Time: => the last query eats up all 32GB main memory in seconds, then starts on the 32GB swap (which obviously takes a bit longer to digest) until eventually the child process gets killed by the oom-killer and postgresql restarts.... -- 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