Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 412C311F9D7B for ; Thu, 26 Jul 2012 22:00:58 -0300 (ADT) Received: from mail-yw0-f46.google.com ([209.85.213.46]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SuYvG-000754-As for pgsql-bugs@postgresql.org; Fri, 27 Jul 2012 01:00:57 +0000 Received: by yhmm54 with SMTP id m54so2660146yhm.19 for ; Thu, 26 Jul 2012 18:00:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding :x-gm-message-state; bh=sI8PlKzrkAO9Qo0Da727BDdg3dZYo3U19+tyGebqlxE=; b=LGQcL0BfV3mkvXZ9QHiSALummGSlraC8dK87UdwH0T7d+/6i3ii2eixsJkMbRBpz9j 4BL79B/EC8gdU0s2AzCqZYviuwqb0PSX3qxbeB9oNInXtQBIL+5h7rAfr1hGuGvXyKuV JxwzWBaRiuZvfyD7lyuN+ZWVd9KmnCAALxSJSIu3kKPdYx2nA9bBXmVkODOGfw3Tt74B tJcRiF3Fq5Tn1Xb/lVqGb7SeURw0A0lsrGKQ9BFrPAczYfgSxmI0D5Ayr6tUKrtUCuXg x03Yp5SeDgBT8L1fioceRJuwfkp0x2uke0B4s2lLDGlal+SjWulKS+r8SsOSMnNWfb3S /UUQ== Received: by 10.50.95.231 with SMTP id dn7mr503174igb.18.1343350841678; Thu, 26 Jul 2012 18:00:41 -0700 (PDT) Received: from ayaki.localdomain (124-169-186-146.dyn.iinet.net.au. [124.169.186.146]) by mx.google.com with ESMTPS id z8sm4768889igi.5.2012.07.26.18.00.39 (version=SSLv3 cipher=OTHER); Thu, 26 Jul 2012 18:00:41 -0700 (PDT) Message-ID: <5011E834.4020805@ringerc.id.au> Date: Fri, 27 Jul 2012 09:00:36 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120717 Thunderbird/14.0 MIME-Version: 1.0 To: karavelov@mail.bg, pgsql-bugs Subject: Re: BUG #6763: Severe memory leak with arrays and hstore References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Gm-Message-State: ALoCoQkJ9b8izi24Z/4ltC32WKkJzG7LoEt9/iB1+wRW2226mFgfz+3XIOG5WLVtTqG6VAQeQix5 X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201207/211 X-Sequence-Number: 33687 Woah. Your email client did something insane, and I cannot read your message. See below: On 07/26/2012 09:37 PM, karavelov@mail.bg wrote: > ----- Craig Ringer (ringerc@ringerc.id.au), на 26.07.2012 в 11:17 ----- >> On 07/26/2012 09:32 AM, karavelov@mail.bg wrote: >> Finally I have > managed to migrate it in batches of 100-200k user ids and >> disconnecting > after each query in order to free the backend and leaked >> memory. > If > you do it in batches, but you do NOT disconnect and reconnect, does > the > backend continue to grow? > > What's the output of: > > SELECT > count(sub.user_id), to_char(AVG(sub.n_prefs), '99999.99') FROM ( > SELECT > user_id, count(name) AS n_prefs FROM old_prefs GROUP BY user_id) > AS sub; >>> and > > SELECT pg_size_pretty(pg_total_relation_size('old_prefs')); > > > ? > > -- > Craig Ringer > Ok, I will do the procedure again with taking > notes on each step. First, here are the results of the queries you asked: > pg=> SELECT count(sub.user_id), to_char(AVG(sub.n_prefs), '99999.99') FROM > ( SELECT user_id, count(name) AS n_prefs FROM old_prefs GROUP BY user_id) > AS sub; count | to_char ---------+----------- 1257262 | 2.26 (1 row) pg=> > SELECT pg_size_pretty(pg_total_relation_size('old_prefs')); pg_size_pretty > ---------------- 264 MB (1 row) pg=> d old_prefs Table "public.old_prefs" > Column | Type | Modifiers ---------+-------------------+----------- user_id > | integer | not null name | character varying | not null value | character > varying | not null Indexes: "old_prefs_user_id_ids" btree (user_id) Also > there are max of 34 rows per user_id in old_prefs here is the new table I > just created: pg=> d new_preferences Table "public.new_preferences" Column > | Type | Modifiers ---------+---------+----------- user_id | integer | not > null prefs | hstore | Indexes: "new_preferences_pkey" PRIMARY KEY, btree > (user_id) Foreign-key constraints: "new_preferences_user_id_fkey" FOREIGN > KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE Here is a newly > connected the backend: root@pg:/var/log# ps axu | egrep '10.0.2.71|USER' | > grep -v grep USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND > postgres 19121 0.0 0.0 2266944 3448 ? Ss 15:23 0:00 postgres: pg pg > 10.0.2.71(51734) idle Migrating the first 200k of the users to the new > scheme: pg=> select count(*) from old_prefs where user_id INSERT INTO > new_preferences SELECT user_id,hstore(array_agg(name), array_agg(value)) > FROM old_prefs WHERE user_id commit; COMMIT Here is the backend: USER PID > %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND postgres 19121 0.8 7.1 > 3081772 582712 ? Ss 15:23 0:02 postgres: pg pg 10.0.2.71(51734) idle > Migrating another batch of users: pg => select count(*) from old_prefs > where user_id>=200000 and user_id INSERT INTO new_preferences SELECT > user_id,hstore(array_agg(name), array_agg(value)) FROM old_prefs WHERE > user_id>=200000 AND user_id commit; COMMIT USER PID %CPU %MEM VSZ RSS TTY > STAT START TIME COMMAND postgres 19121 1.1 8.5 3176164 697444 ? Ss 15:23 > 0:05 postgres: pg pg 10.0.2.71(51734) idle Another batch: pg=> select > count(*) from old_prefs where user_id>=600000 and user_id INSERT INTO > new_preferences SELECT user_id,hstore(array_agg(name), array_agg(value)) > FROM old_prefs WHERE user_id>=600000 AND user_id commit; COMMIT USER PID > %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND postgres 19121 0.7 9.6 > 3210224 791404 ? Ss 15:23 0:08 postgres: pg pg 10.0.2.71(51734) idle > Another batch: pg=> select count(*) from old_prefs where user_id>=1100000 > and user_id INSERT INTO new_preferences SELECT > user_id,hstore(array_agg(name), array_agg(value)) FROM old_prefs WHERE > user_id>=1100000 AND user_id commit; COMMIT USER PID %CPU %MEM VSZ RSS TTY > STAT START TIME COMMAND postgres 19121 0.9 10.8 3277412 889860 ? Ss 15:23 > 0:11 postgres: pg pg 10.0.2.71(51734) idle So Pg backeng keep growing with > 100M per 200k row from old table that became 50-60k rows in the new table > Proceeding with another batch: pg=> select count(*) from old_prefs where > user_id>=1600000 and user_id INSERT INTO new_preferences SELECT > user_id,hstore(array_agg(name), array_agg(value)) FROM old_prefs WHERE > user_id>=1600000 AND user_id commit; COMMIT USER PID %CPU %MEM VSZ RSS TTY > STAT START TIME COMMAND postgres 19121 0.9 11.5 3277412 945560 ? Ss 15:23 > 0:15 postgres: pg pg 10.0.2.71(51734) idle Another batch: pg=> select > count(*) from old_prefs where user_id>=2400000 and user_id INSERT INTO > new_preferences SELECT user_id,hstore(array_agg(name), array_agg(value)) > FROM old_prefs WHERE user_id>=2400000 AND user_id commit; COMMIT USER PID > %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND postgres 19121 1.2 16.2 > 3736968 1331796 ? Ss 15:23 0:20 postgres: pg pg 10.0.2.71(51734) idle > Another batch: pg => select count(*) from old_prefs where user_id>=3400000 > and user_id INSERT INTO new_preferences SELECT > user_id,hstore(array_agg(name), array_agg(value)) FROM old_prefs WHERE > user_id>=3400000 AND user_id rollback; ROLLBACK Ops.. have to cleanup the > old_prefs, some users were deleted in the meantime: pg=> delete from > old_prefs where user_id not in (select user_id from users); DELETE 7 pg=> > commit; COMMIT USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND > postgres 19121 1.4 26.4 4469520 2157588 ? Ss 15:23 0:29 postgres: pg pg > 10.0.2.71(51734) idle Near 1G grow on rolled back transaction.... pg=> > INSERT INTO new_preferences SELECT user_id,hstore(array_agg(name), > array_agg(value)) FROM old_prefs WHERE user_id>=3400000 AND user_id commit; > COMMIT USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND postgres > 19121 1.7 26.6 4479944 2180536 ? Ss 15:23 0:35 postgres: pg pg > 10.0.2.71(51734) idle Another batch, bigger this time: pg=> select count(*) > from old_prefs where user_id>=3800000 and user_id INSERT INTO > new_preferences SELECT user_id,hstore(array_agg(name), array_agg(value)) > FROM old_prefs WHERE user_id>=3800000 AND user_id commit; COMMIT USER PID > %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND postgres 19121 1.9 33.1 > 5238968 2710756 ? Ss 15:23 0:45 postgres: pg pg 10.0.2.71(51734) idle > Another big batch: pg=> select count(*) from old_prefs where > user_id>=4200000 and user_id INSERT INTO new_preferences SELECT > user_id,hstore(array_agg(name), array_agg(value)) FROM old_prefs WHERE > user_id>=4200000 AND user_id commit; COMMIT USER PID %CPU %MEM VSZ RSS TTY > STAT START TIME COMMAND postgres 19121 2.2 35.7 5438412 2918720 ? Ss 15:23 > 0:55 postgres: pg pg 10.0.2.71(51734) idle Now a smaller batch: pg=> select > count(*) from old_prefs where user_id>=4400000 and user_id INSERT INTO > new_preferences SELECT user_id,hstore(array_agg(name), array_agg(value)) > FROM old_prefs WHERE user_id>=4400000 AND user_id commit; COMMIT RSS keeps > growing: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND postgres > 19121 2.2 36.0 5438412 2943784 ? Ss 15:23 1:00 postgres: pg pg > 10.0.2.71(51734) idle Lets see if a bigger batch will pass: pg=> select > count(*) from old_prefs where user_id>=4500000; count -------- 631911 (1 > row) pg=> INSERT INTO new_preferences SELECT > user_id,hstore(array_agg(name), array_agg(value)) FROM old_prefs WHERE > user_id>=4500000 GROUP BY user_id; INSERT 0 296541 pg=> commit; COMMIT Ok, > this time it passed, but the backend is over 4G USER PID %CPU %MEM VSZ RSS > TTY STAT START TIME COMMAND postgres 19121 2.2 50.0 7227968 4088928 ? Ss > 15:23 1:17 postgres: pg pg 10.0.2.71(51734) idle Some observations: 1. > Backend does not free allocated memory between transactions. 2. Rolled back > transactions also leak memory. 3. Leaked memory is not linear to work done > - 2 transactions with 200k keys will leak less than 1 transaction with 400k > keys Regarding Tom's question: The old_prefs does not fit in work_mem but > is quite small regarding the total RAM. Isn't the "work_mem" a limit of the > memory each backend could allocate for sorting, grouping and aggregation? > My understanding is that bigger allocation will overflow to disk and will > not kill the server. I could be wrong though. Thanks in advance and best > regards -- Luben Karavelov >