Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 3D5421FA2A4C for ; Thu, 26 Jul 2012 10:37:39 -0300 (ADT) Received: from mx2.mail.bg ([193.201.172.118]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SuOFz-0001Ce-41 for pgsql-bugs@postgresql.org; Thu, 26 Jul 2012 13:37:38 +0000 Received: from mail.bg (unknown [10.0.0.19]) by mx2.mail.bg (Postfix) with ESMTPA id 66C2E60042DF; Thu, 26 Jul 2012 16:37:21 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=mail.bg; s=default; t=1343309841; bh=riGzysbwSJwTMFjceFIZYVgu0zVi/aWtiUdLKGlrEG0=; h=Message-ID:Date:Subject:From:To:Cc:MIME-Version:Content-Type; b=hXQEIdsiRVjFqqoS0cCA5HRdkA9iy7VZM6wY0BsVYQzmRtTLFloAtbao9I93N3/lK 3m46+fRMpLtFMtU47yTJ5NcIfjPrxxCJ9I1i7Xl9sbr/eopqP4BAN0N4TI3TzpUOsm sFETKyGzJhm+f6QSiMAGKbEkGYzBp8WKp8+TemtY= Message-ID: Date: Thu, 26 Jul 2012 16:37:21 +0300 Subject: Re: BUG #6763: Severe memory leak with arrays and hstore From: karavelov@mail.bg To: Craig Ringer Cc: pgsql-bugs@postgresql.org User-Agent: Mail.BG Webmail MIME-Version: 1.0 Content-Type: multipart/alternative;boundary="----=_20120726163720_15909" X-Priority: 3 (Normal) Importance: Normal X-Composed: 2012-07-26T16:37:20+03:00 X-Originating-IP: [193.201.172.14] X-Checking: 2012-07-26T16:37:20+03:00 X-Checked: 2012-07-26T16:37:21+03:00 X-Pg-Spam-Score: -2.0 (--) X-Archive-Number: 201207/206 X-Sequence-Number: 33682 ------=_20120726163720_15909 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable ----- Craig Ringer (ringerc@ringerc.id.au), =D0=BD=D0=B0 26.07.2012 =D0=B2 = 11:17 -----=0A> On 07/26/2012 09:32 AM, karavelov@mail.bg wrote: >> Finally= I have=0Amanaged to migrate it in batches of 100-200k user ids and >> disc= onnecting=0Aafter each query in order to free the backend and leaked >> mem= ory. > If=0Ayou do it in batches, but you do NOT disconnect and reconnect, = does > the=0Abackend continue to grow? > > What's the output of: > > SELECT= =0Acount(sub.user_id), to_char(AVG(sub.n_prefs), '99999.99') FROM ( > SELEC= T=0Auser_id, count(name) AS n_prefs FROM old_prefs GROUP BY user_id) > AS s= ub;=0A> > and > > SELECT pg_size_pretty(pg_total_relation_size('old_prefs')= ); > >=0A? > > -- > Craig Ringer > Ok, I will do the procedure again with t= aking=0Anotes on each step. First, here are the results of the queries you = asked:=0Apg=3D> SELECT count(sub.user_id), to_char(AVG(sub.n_prefs), '99999= .99') FROM=0A( SELECT user_id, count(name) AS n_prefs FROM old_prefs GROUP = BY user_id)=0AAS sub; count | to_char ---------+----------- 1257262 | 2.26 = (1 row) pg=3D>=0ASELECT pg_size_pretty(pg_total_relation_size('old_prefs'))= ; pg_size_pretty=0A---------------- 264 MB (1 row) pg=3D> d old_prefs Table= "public.old_prefs"=0AColumn | Type | Modifiers ---------+-----------------= --+----------- user_id=0A| integer | not null name | character varying | no= t null value | character=0Avarying | not null Indexes: "old_prefs_user_id_i= ds" btree (user_id) Also=0Athere are max of 34 rows per user_id in old_pref= s here is the new table I=0Ajust created: pg=3D> d new_preferences Table "p= ublic.new_preferences" Column=0A| Type | Modifiers ---------+---------+----= ------- user_id | integer | not=0Anull prefs | hstore | Indexes: "new_prefe= rences_pkey" PRIMARY KEY, btree=0A(user_id) Foreign-key constraints: "new_p= references_user_id_fkey" FOREIGN=0AKEY (user_id) REFERENCES users(user_id) = ON DELETE CASCADE Here is a newly=0Aconnected the backend: root@pg:/var/log= # ps axu | egrep '10.0.2.71|USER' |=0Agrep -v grep USER PID %CPU %MEM VSZ R= SS TTY STAT START TIME COMMAND=0Apostgres 19121 0.0 0.0 2266944 3448 ? Ss 1= 5:23 0:00 postgres: pg pg=0A10.0.2.71(51734) idle Migrating the first 200k = of the users to the new=0Ascheme: pg=3D> select count(*) from old_prefs whe= re user_id INSERT INTO=0Anew_preferences SELECT user_id,hstore(array_agg(na= me), array_agg(value))=0AFROM old_prefs WHERE user_id commit; COMMIT Here i= s the backend: USER PID=0A%CPU %MEM VSZ RSS TTY STAT START TIME COMMAND pos= tgres 19121 0.8 7.1=0A3081772 582712 ? Ss 15:23 0:02 postgres: pg pg 10.0.2= .71(51734) idle=0AMigrating another batch of users: pg =3D> select count(*)= from old_prefs=0Awhere user_id>=3D200000 and user_id INSERT INTO new_prefe= rences SELECT=0Auser_id,hstore(array_agg(name), array_agg(value)) FROM old_= prefs WHERE=0Auser_id>=3D200000 AND user_id commit; COMMIT USER PID %CPU %M= EM VSZ RSS TTY=0ASTAT START TIME COMMAND postgres 19121 1.1 8.5 3176164 697= 444 ? Ss 15:23=0A0:05 postgres: pg pg 10.0.2.71(51734) idle Another batch: = pg=3D> select=0Acount(*) from old_prefs where user_id>=3D600000 and user_id= INSERT INTO=0Anew_preferences SELECT user_id,hstore(array_agg(name), array= _agg(value))=0AFROM old_prefs WHERE user_id>=3D600000 AND user_id commit; C= OMMIT USER PID=0A%CPU %MEM VSZ RSS TTY STAT START TIME COMMAND postgres 191= 21 0.7 9.6=0A3210224 791404 ? Ss 15:23 0:08 postgres: pg pg 10.0.2.71(51734= ) idle=0AAnother batch: pg=3D> select count(*) from old_prefs where user_id= >=3D1100000=0Aand user_id INSERT INTO new_preferences SELECT=0Auser_id,hsto= re(array_agg(name), array_agg(value)) FROM old_prefs WHERE=0Auser_id>=3D110= 0000 AND user_id commit; COMMIT USER PID %CPU %MEM VSZ RSS TTY=0ASTAT START= TIME COMMAND postgres 19121 0.9 10.8 3277412 889860 ? Ss 15:23=0A0:11 post= gres: pg pg 10.0.2.71(51734) idle So Pg backeng keep growing with=0A100M pe= r 200k row from old table that became 50-60k rows in the new table=0AProcee= ding with another batch: pg=3D> select count(*) from old_prefs where=0Auser= _id>=3D1600000 and user_id INSERT INTO new_preferences SELECT=0Auser_id,hst= ore(array_agg(name), array_agg(value)) FROM old_prefs WHERE=0Auser_id>=3D16= 00000 AND user_id commit; COMMIT USER PID %CPU %MEM VSZ RSS TTY=0ASTAT STAR= T TIME COMMAND postgres 19121 0.9 11.5 3277412 945560 ? Ss 15:23=0A0:15 pos= tgres: pg pg 10.0.2.71(51734) idle Another batch: pg=3D> select=0Acount(*) = from old_prefs where user_id>=3D2400000 and user_id INSERT INTO=0Anew_prefe= rences SELECT user_id,hstore(array_agg(name), array_agg(value))=0AFROM old_= prefs WHERE user_id>=3D2400000 AND user_id commit; COMMIT USER PID=0A%CPU %= MEM VSZ RSS TTY STAT START TIME COMMAND postgres 19121 1.2 16.2=0A3736968 1= 331796 ? Ss 15:23 0:20 postgres: pg pg 10.0.2.71(51734) idle=0AAnother batc= h: pg =3D> select count(*) from old_prefs where user_id>=3D3400000=0Aand us= er_id INSERT INTO new_preferences SELECT=0Auser_id,hstore(array_agg(name), = array_agg(value)) FROM old_prefs WHERE=0Auser_id>=3D3400000 AND user_id rol= lback; ROLLBACK Ops.. have to cleanup the=0Aold_prefs, some users were dele= ted in the meantime: pg=3D> delete from=0Aold_prefs where user_id not in (s= elect user_id from users); DELETE 7 pg=3D>=0Acommit; COMMIT USER PID %CPU %= MEM VSZ RSS TTY STAT START TIME COMMAND=0Apostgres 19121 1.4 26.4 4469520 2= 157588 ? Ss 15:23 0:29 postgres: pg pg=0A10.0.2.71(51734) idle Near 1G grow= on rolled back transaction.... pg=3D>=0AINSERT INTO new_preferences SELECT= user_id,hstore(array_agg(name),=0Aarray_agg(value)) FROM old_prefs WHERE u= ser_id>=3D3400000 AND user_id commit;=0ACOMMIT USER PID %CPU %MEM VSZ RSS T= TY STAT START TIME COMMAND postgres=0A19121 1.7 26.6 4479944 2180536 ? Ss 1= 5:23 0:35 postgres: pg pg=0A10.0.2.71(51734) idle Another batch, bigger thi= s time: pg=3D> select count(*)=0Afrom old_prefs where user_id>=3D3800000 an= d user_id INSERT INTO=0Anew_preferences SELECT user_id,hstore(array_agg(nam= e), array_agg(value))=0AFROM old_prefs WHERE user_id>=3D3800000 AND user_id= commit; COMMIT USER PID=0A%CPU %MEM VSZ RSS TTY STAT START TIME COMMAND po= stgres 19121 1.9 33.1=0A5238968 2710756 ? Ss 15:23 0:45 postgres: pg pg 10.= 0.2.71(51734) idle=0AAnother big batch: pg=3D> select count(*) from old_pre= fs where=0Auser_id>=3D4200000 and user_id INSERT INTO new_preferences SELEC= T=0Auser_id,hstore(array_agg(name), array_agg(value)) FROM old_prefs WHERE= =0Auser_id>=3D4200000 AND user_id commit; COMMIT USER PID %CPU %MEM VSZ RSS= TTY=0ASTAT START TIME COMMAND postgres 19121 2.2 35.7 5438412 2918720 ? Ss= 15:23=0A0:55 postgres: pg pg 10.0.2.71(51734) idle Now a smaller batch: pg= =3D> select=0Acount(*) from old_prefs where user_id>=3D4400000 and user_id = INSERT INTO=0Anew_preferences SELECT user_id,hstore(array_agg(name), array_= agg(value))=0AFROM old_prefs WHERE user_id>=3D4400000 AND user_id commit; C= OMMIT RSS keeps=0Agrowing: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME C= OMMAND postgres=0A19121 2.2 36.0 5438412 2943784 ? Ss 15:23 1:00 postgres: = pg pg=0A10.0.2.71(51734) idle Lets see if a bigger batch will pass: pg=3D> = select=0Acount(*) from old_prefs where user_id>=3D4500000; count -------- 6= 31911 (1=0Arow) pg=3D> INSERT INTO new_preferences SELECT=0Auser_id,hstore(= array_agg(name), array_agg(value)) FROM old_prefs WHERE=0Auser_id>=3D450000= 0 GROUP BY user_id; INSERT 0 296541 pg=3D> commit; COMMIT Ok,=0Athis time i= t passed, but the backend is over 4G USER PID %CPU %MEM VSZ RSS=0ATTY STAT = START TIME COMMAND postgres 19121 2.2 50.0 7227968 4088928 ? Ss=0A15:23 1:1= 7 postgres: pg pg 10.0.2.71(51734) idle Some observations: 1.=0ABackend doe= s not free allocated memory between transactions. 2. Rolled back=0Atransact= ions also leak memory. 3. Leaked memory is not linear to work done=0A- 2 tr= ansactions with 200k keys will leak less than 1 transaction with 400k=0Akey= s Regarding Tom's question: The old_prefs does not fit in work_mem but=0Ais= quite small regarding the total RAM. Isn't the "work_mem" a limit of the= =0Amemory each backend could allocate for sorting, grouping and aggregation= ?=0AMy understanding is that bigger allocation will overflow to disk and wi= ll=0Anot kill the server. I could be wrong though. Thanks in advance and be= st=0Aregards -- Luben Karavelov ------=_20120726163720_15909 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable ----- Craig Ringer (ringerc@ringerc.id.au), =D0=BD=D0=B0 26.07.2012 =D0=B2= 11:17 -----=0A=0A> On 07/26/2012 09:32 AM, karavelov@mail.bg wrote:=0A>> F= inally I have managed to migrate it in batches of 100-200k user ids and=0A>= > disconnecting after each query in order to free the backend and leaked=0A= >> memory.=0A> If you do it in batches, but you do NOT disconnect and recon= nect, does =0A> the backend continue to grow?=0A> =0A> What's the output of= :=0A> =0A> SELECT count(sub.user_id), to_char(AVG(sub.n_prefs), '99999.99')= FROM (=0A> SELECT user_id, count(name) AS n_prefs FROM old_prefs GROUP BY = user_id) =0A> AS sub;=0A> =0A> and=0A> =0A> SELECT pg_size_pretty(pg_total_= relation_size('old_prefs'));=0A> =0A> ?=0A> =0A> --=0A> Craig Ringer=0A> = =0A=0AOk, I will do the procedure again with taking notes on each step.=0AF= irst, here are the results of the queries you asked:=0A=0Apg=3D> SELECT cou= nt(sub.user_id), to_char(AVG(sub.n_prefs), '99999.99') FROM (=0A S= ELECT user_id, count(name) AS n_prefs FROM old_prefs GROUP BY user_id) AS = sub;=0A=0A count | to_char =0A---------+-----------=0A 1257262 | 2= .26=0A(1 row)=0A=0Apg=3D> SELECT pg_size_pretty(pg_total_relation_size('old= _prefs'));=0A pg_size_pretty =0A----------------=0A 264 MB=0A(1 row)=0A=0Ap= g=3D> d old_prefs=0A Table "public.old_prefs"=0A Column | Typ= e | Modifiers =0A---------+-------------------+-----------=0A user_i= d | integer | not null=0A name | character varying | not null= =0A value | character varying | not null=0AIndexes:=0A "old_prefs_user= _id_ids" btree (user_id)=0A=0AAlso there are max of 34 rows per user_id in = old_prefs=0A=0A=0Ahere is the new table I just created:=0A=0Apg=3D> d new_p= references=0ATable "public.new_preferences"=0A Column | Type | Modifier= s =0A---------+---------+-----------=0A user_id | integer | not null=0A pre= fs | hstore | =0AIndexes:=0A "new_preferences_pkey" PRIMARY KEY, btre= e (user_id)=0AForeign-key constraints:=0A "new_preferences_user_id_fkey"= FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE=0A=0A=0A= Here is a newly connected the backend:=0A=0Aroot@pg:/var/log# ps axu | egre= p '10.0.2.71|USER' | grep -v grep=0AUSER PID %CPU %MEM VSZ RSS T= TY STAT START TIME COMMAND=0Apostgres 19121 0.0 0.0 2266944 3448 ?= Ss 15:23 0:00 postgres: pg pg 10.0.2.71(51734) idle = =0A=0A=0AMigrating the first 200k of the users to the new scheme:=0A=0Apg= =3D> select count(*) from old_prefs where user_id<200000;=0A count =0A----= ----=0A 174767=0A(1 row)=0A=0Apg=3D> INSERT INTO new_preferences SELECT use= r_id,hstore(array_agg(name), array_agg(value)) FROM old_prefs WHERE user_id= <200000 GROUP BY user_id;=0AINSERT 0 48993=0Apg=3D> commit;=0ACOMMIT=0A=0AH= ere is the backend:=0AUSER PID %CPU %MEM VSZ RSS TTY STAT S= TART TIME COMMAND=0Apostgres 19121 0.8 7.1 3081772 582712 ? Ss 1= 5:23 0:02 postgres: pg pg 10.0.2.71(51734) idle =0A=0AMigratin= g another batch of users:=0A=0Apg =3D> select count(*) from old_prefs where= user_id>=3D200000 and user_id<600000;=0A count =0A--------=0A 193824=0A(1= row)=0A=0A=3D> INSERT INTO new_preferences SELECT user_id,hstore(array_agg= (name), array_agg(value)) FROM old_prefs WHERE user_id>=3D200000 AND user_i= d<600000 GROUP BY user_id;=0AINSERT 0 54157=0A=3D> commit;=0ACOMMIT=0A=0AUS= ER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND=0Apo= stgres 19121 1.1 8.5 3176164 697444 ? Ss 15:23 0:05 postgres: pg= pg 10.0.2.71(51734) idle=0A=0AAnother batch:=0Apg=3D> select count(*) from= old_prefs where user_id>=3D600000 and user_id<1100000;=0A count =0A------= --=0A 190504=0A(1 row)=0A=0Apg=3D> INSERT INTO new_preferences SELECT user_= id,hstore(array_agg(name), array_agg(value)) FROM old_prefs WHERE user_id>= =3D600000 AND user_id<1100000 GROUP BY user_id;=0AINSERT 0 56199=0Apg=3D> c= ommit;=0ACOMMIT=0A=0AUSER PID %CPU %MEM VSZ RSS TTY STAT ST= ART TIME COMMAND=0Apostgres 19121 0.7 9.6 3210224 791404 ? Ss 15= :23 0:08 postgres: pg pg 10.0.2.71(51734) idle=0A=0AAnother batch:=0A=0Ap= g=3D> select count(*) from old_prefs where user_id>=3D1100000 and user_id<1= 600000;=0A count =0A--------=0A 194965=0A(1 row)=0A=0Apg=3D> INSERT INTO n= ew_preferences SELECT user_id,hstore(array_agg(name), array_agg(value)) FRO= M old_prefs WHERE user_id>=3D1100000 AND user_id<1600000 GROUP BY user_id;= =0AINSERT 0 60257=0Apg=3D> commit;=0ACOMMIT=0A=0AUSER PID %CPU %MEM = VSZ RSS TTY STAT START TIME COMMAND=0Apostgres 19121 0.9 10.8 3= 277412 889860 ? Ss 15:23 0:11 postgres: pg pg 10.0.2.71(51734) idl= e=0A=0A=0ASo Pg backeng keep growing with 100M per 200k row from old table = that became 50-60k rows in the new table=0AProceeding with another batch:= =0A=0Apg=3D> select count(*) from old_prefs where user_id>=3D1600000 and us= er_id<2400000;=0A count =0A--------=0A 170858=0A(1 row)=0A=0ATime: 83,994 = ms=0Apg=3D> INSERT INTO new_preferences SELECT user_id,hstore(array_agg(nam= e), array_agg(value)) FROM old_prefs WHERE user_id>=3D1600000 AND user_id<2= 400000 GROUP BY user_id;=0AINSERT 0 55447=0Apg=3D> commit;=0ACOMMIT=0A=0AUS= ER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND=0Apo= stgres 19121 0.9 11.5 3277412 945560 ? Ss 15:23 0:15 postgres: pg= pg 10.0.2.71(51734) idle=0A=0AAnother batch:=0A=0Apg=3D> select count(*) f= rom old_prefs where user_id>=3D2400000 and user_id<3400000;=0A count =0A--= ------=0A 200614=0A(1 row)=0A=0ATime: 83,409 ms=0Apg=3D> INSERT INTO new_pr= eferences SELECT user_id,hstore(array_agg(name), array_agg(value)) FROM old= _prefs WHERE user_id>=3D2400000 AND user_id<3400000 GROUP BY user_id;=0AINS= ERT 0 87940=0Apg=3D> commit;=0ACOMMIT=0A=0AUSER PID %CPU %MEM VSZ = RSS TTY STAT START TIME COMMAND=0Apostgres 19121 1.2 16.2 3736968= 1331796 ? Ss 15:23 0:20 postgres: pg pg 10.0.2.71(51734) idle=0A= =0AAnother batch:=0A=0Apg =3D> select count(*) from old_prefs where user_id= >=3D3400000 and user_id<3800000;=0A count =0A--------=0A 161390=0A(1 row)= =0A=0Apg=3D> INSERT INTO new_preferences SELECT user_id,hstore(array_agg(na= me), array_agg(value)) FROM old_prefs WHERE user_id>=3D3400000 AND user_id<= 3800000 GROUP BY user_id;=0AERROR: insert or update on table "new_preferen= ces" violates foreign key constraint "new_preferences_user_id_fkey"=0ADETAI= L: Key (user_id)=3D(3615131) is not present in table "users".=0Apg=3D> rol= lback;=0AROLLBACK=0A=0AOps.. have to cleanup the old_prefs, some users were= deleted in the meantime:=0Apg=3D> delete from old_prefs where user_id not = in (select user_id from users);=0ADELETE 7=0Apg=3D> commit;=0ACOMMIT=0A=0AU= SER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND=0Ap= ostgres 19121 1.4 26.4 4469520 2157588 ? Ss 15:23 0:29 postgres: p= g pg 10.0.2.71(51734) idle=0A=0ANear 1G grow on rolled back transaction....= =0A=0Apg=3D> INSERT INTO new_preferences SELECT user_id,hstore(array_agg(na= me), array_agg(value)) FROM old_prefs WHERE user_id>=3D3400000 AND user_id<= 3800000 GROUP BY user_id;=0AINSERT 0 131803=0Apg=3D> commit;=0ACOMMIT=0A=0A= USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND=0A= postgres 19121 1.7 26.6 4479944 2180536 ? Ss 15:23 0:35 postgres: = pg pg 10.0.2.71(51734) idle=0A=0AAnother batch, bigger this time:=0A=0Apg= =3D> select count(*) from old_prefs where user_id>=3D3800000 and user_id<42= 00000;=0A count =0A--------=0A 327374=0A(1 row)=0A=0Apg=3D> INSERT INTO ne= w_preferences SELECT user_id,hstore(array_agg(name), array_agg(value)) FROM= old_prefs WHERE user_id>=3D3800000 AND user_id<4200000 GROUP BY user_id;= =0AINSERT 0 177044=0Apg=3D> commit;=0ACOMMIT=0A=0AUSER PID %CPU %MEM = VSZ RSS TTY STAT START TIME COMMAND=0Apostgres 19121 1.9 33.1 = 5238968 2710756 ? Ss 15:23 0:45 postgres: pg pg 10.0.2.71(51734) id= le=0A=0AAnother big batch:=0A=0Apg=3D> select count(*) from old_prefs where= user_id>=3D4200000 and user_id<4400000;=0A count =0A--------=0A 375352=0A= (1 row)=0A=0Apg=3D> INSERT INTO new_preferences SELECT user_id,hstore(array= _agg(name), array_agg(value)) FROM old_prefs WHERE user_id>=3D4200000 AND u= ser_id<4400000 GROUP BY user_id;=0AINSERT 0 189095=0Apg=3D> commit;=0ACOMMI= T=0A=0AUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COM= MAND=0Apostgres 19121 2.2 35.7 5438412 2918720 ? Ss 15:23 0:55 pos= tgres: pg pg 10.0.2.71(51734) idle=0A=0ANow a smaller batch:=0A=0Apg=3D> se= lect count(*) from old_prefs where user_id>=3D4400000 and user_id<4500000;= =0A count =0A--------=0A 219249=0A(1 row)=0A=0Apg=3D> INSERT INTO new_pref= erences SELECT user_id,hstore(array_agg(name), array_agg(value)) FROM old_p= refs WHERE user_id>=3D4400000 AND user_id<4500000 GROUP BY user_id;=0AINSER= T 0 99782=0Apg=3D> commit;=0ACOMMIT=0A=0ARSS keeps growing:=0AUSER PI= D %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND=0Apostgres 1912= 1 2.2 36.0 5438412 2943784 ? Ss 15:23 1:00 postgres: pg pg 10.0.2.= 71(51734) idle=0A=0ALets see if a bigger batch will pass:=0A=0Apg=3D> selec= t count(*) from old_prefs where user_id>=3D4500000;=0A count =0A--------= =0A 631911=0A(1 row)=0A=0Apg=3D> INSERT INTO new_preferences SELECT user_id= ,hstore(array_agg(name), array_agg(value)) FROM old_prefs WHERE user_id>=3D= 4500000 GROUP BY user_id;=0AINSERT 0 296541=0Apg=3D> commit;=0ACOMMIT=0A= =0AOk, this time it passed, but the backend is over 4G=0AUSER PID %CP= U %MEM VSZ RSS TTY STAT START TIME COMMAND=0Apostgres 19121 2.= 2 50.0 7227968 4088928 ? Ss 15:23 1:17 postgres: pg pg 10.0.2.71(51= 734) idle=0A=0ASome observations:=0A=0A1. Backend does not free allocated m= emory between transactions.=0A2. Rolled back transactions also leak memory.= =0A3. Leaked memory is not linear to work done - 2 transactions with 200k k= eys will leak less than 1 transaction with 400k keys=0A=0A=0ARegarding Tom'= s question:=0AThe old_prefs does not fit in work_mem but is quite small reg= arding the total RAM. Isn't the "work_mem" a limit of the memory each backe= nd could allocate for =0Asorting, grouping and aggregation? My understandin= g is that bigger allocation will overflow to disk and will not kill the ser= ver. I could be wrong though.=0A=0AThanks in advance and best regards=0A=0A= --=0ALuben Karavelov=0A=09=09=09=09=09=09=09=09=09 ------=_20120726163720_15909--