Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gf1hy-0000YP-FZ for pgsql-general@arkaria.postgresql.org; Thu, 03 Jan 2019 12:02:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gf1hx-0003Xd-3k for pgsql-general@arkaria.postgresql.org; Thu, 03 Jan 2019 12:02:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gf1hw-0003XW-8j for pgsql-general@lists.postgresql.org; Thu, 03 Jan 2019 12:02:40 +0000 Received: from sonic304-11.consmr.mail.bf2.yahoo.com ([74.6.128.34]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gf1ho-0001SJ-RJ for pgsql-general@postgresql.org; Thu, 03 Jan 2019 12:02:39 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1546516950; bh=fIGyyb3jJ3PauaoW1/psCpHU1A8h7PDm+0xolBXwC2E=; h=Date:From:To:Cc:In-Reply-To:References:Subject:From:Subject; b=Nz6YB8DPHObl9ZhalMZN89xuMSS5aBy2owhDkl5WJU7hauEeb4LCGekOyEgiYJAHR1i/Kjuinf1zgT+IJGgCdXezsOWHJFRHnz0oh03GRcCLH/kWG2KXq9a1r48uSwtMerdYToel+nS5vbq6v+0z770ESWm311xwIWq5H84GlSHYi7P0iAYmGCcJ6gY7mDzL4uFfBvk/EJTshAUfPrv8g+H08tZ0AlhjHX7cs1HOxyNwupU6CgbtdTlqP1PSNZNQrBpb9JqJV9WcMhJo91XjmfCXEbZ3Ikeq4EUnuxX0Fv8cgdBULo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NADsnzVKFU8TAm4e3FYE6QeqjH PN6ZTXQR_lfkCB1x3RS53l.wa84JCV_9TNArWOtqAtDObZ6q1TASjphPlCQ1ogJpmNCZOEn3Dln8 - Received: from sonic.gate.mail.ne1.yahoo.com by sonic304.consmr.mail.bf2.yahoo.com with HTTP; Thu, 3 Jan 2019 12:02:30 +0000 Date: Thu, 3 Jan 2019 11:55:33 +0000 (UTC) From: Thomas Carroll To: Tom Lane Cc: "pgsql-general@postgresql.org" Message-ID: <116743157.12309338.1546516533422@mail.yahoo.com> In-Reply-To: <403.1544489101@sss.pgh.pa.us> References: <2114009259.1866365.1544469996900.ref@mail.yahoo.com> <2114009259.1866365.1544469996900@mail.yahoo.com> <2584.1544474727@sss.pgh.pa.us> <1497611735.1978595.1544479049695@mail.yahoo.com> <7049.1544482214@sss.pgh.pa.us> <1761071676.2037719.1544486470070@mail.yahoo.com> <403.1544489101@sss.pgh.pa.us> Subject: Re: Memory exhaustion due to temporary tables? MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_12309337_2139612328.1546516533419" X-Mailer: WebService/1.1.12857 YMailNorrin Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0 Content-Length: 12204 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------=_Part_12309337_2139612328.1546516533419 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable =20 On Monday, December 10, 2018, 7:45:07 PM EST, Tom Lane wrote: =20 =20 Thomas Carroll writes: >=C2=A0 On Monday, December 10, 2018, 5:50:22 PM EST, Tom Lane wrote:=C2=A0=20 >> Also, as mentioned upthread, it'd be interesting to see if there's >> a memory context dump showing up in your server log. > There are many memory context dumps, and they are long (well over 100 lin= es apiece).=C2=A0 To me these are unfamiliar and hard to read, but I tried = to pick one that referenced all the same tables used in the function.=C2=A0= I put one such dump (188 lines) here: > https://drive.google.com/file/d/1QI4nffdZByIehb_-GULOagI_dKpKElg_/view Thanks.=C2=A0 Here's the smoking gun: =C2=A0 CacheMemoryContext: 221241400 total in 38 blocks; 152192 free (90 ch= unks); 221089208 used This looks like something is leaking memory directly in CacheMemoryContext (not in any of its child contexts).=C2=A0 Can you confirm that all the memo= ry context dumps in your logs are showing similarly very-large total space values for CacheMemoryContext? This doesn't seem to be related to temp table usage, at least not directly.=C2=A0 (As a cross-check, I created and dropped 10000 temp tables = in a session, and the process's VM footprint didn't budge.)=C2=A0 My guess is th= at something else you are doing is tickling the leak. You said you'd been able to reproduce this problem outside production. Any chance you could boil that down to a sharable test case?=C2=A0 I'd bet long odds that it's not related to your data particularly, but rather to your schema or something your function is doing, so you probably could reproduce it with some dummy data (and, perhaps, not even very much of that; repeating some DDL action many times is probably what is making the leak grow to something noticeable). If I had to guess at this point, I'd wonder if it's something to do with partitioned tables.=C2=A0 That code is still relatively wet behind the ears, and memory leaks are the kind of bug I'd be expecting to still be shaking out of it now.=C2=A0 But you might not be using partitione= d tables at all.=C2=A0 Anyway, anything you're doing with new-in-v10 features= , particularly schema-related features, would be a good bet for the source of the problem. =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 regards, tom lane Thanks for your response and sorry for the delay in getting back to this.= =C2=A0=20 Because holidays. To refresh your memory, the issue I encountered is that a long-persistent connection that periodically issues function calls consumes more and more memory - eventually causing memory exhaustion.=C2=A0 Admittedly, it does ta= ke weeks. This email is about creating a shareable test case. The test case is simple - so simple that I am surprised no one has encounte= red this before, and therefore I am really more fearing that I am doing somethi= ng stupid in my effort to make it shareable and simple.=C2=A0 In creating this= example I have not run the code till it crashed the server; instead I used the measuring methodology you will see below (monitoring the smaps file). I have put needed files here: https://drive.google.com/open?id=3D1ff074Qxn7Bx9Ud9GRaegDfuJn_Tf_NTi Table of contents: testbed.public.backup # database dump, not compressed, includes "create dat= abase" pg_hba.conf # self explanatory; allows local postgres to log in as trusted postgresql.conf # self explanatory. nothing weird loop_query.bash # sends a query via named pipe to Postgres once per second To recreate the issue, load the "testbed" database into a Postgres server, configured so that psql does not need to send a password for user postgres. I created the server with this:initdb $PGDATA --locale=3D"en_US.UTF-8" -E "= UTF-8" -k Then run as postgres: cd /my/directory mkfifo qpipe # named pipe conduit to persistent Postgres connection cat > qpipe & # keep pipe from closing and terminating the PG connection psql -h localhost -d testbed -U postgres < qpipe & # listener connection ./query_loop.bash # Send function call over and over once per second # Observe output; should be 78 rows per second # Switch to a new terminal session # Identify the PID of the psql process # Monitor with this command, but plug in the real PID for the 1234 grep "^Private" /proc/1234/smaps | awk '{a+=3D$2}END{print a/1024.0}' Give it a few minutes, and you will observe memory usage climbing steadily= =20 (if my measurement technique is good).=C2=A0 From last night to this mornin= g I=20 observed memory consumption grow from 3.5 to 46.6 MB. Tested on Postgres version 10.5. Again, thanks for any help. Tom Carroll =20 ------=_Part_12309337_2139612328.1546516533419 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

=20
=20
On Monday, December 10, 2018, 7:45:07 PM EST, Tom Lane = <tgl@sss.pgh.pa.us> wrote:


Thomas Carroll <t= omfecarroll@yahoo.com> writes:

>  On Monday, De= cember 10, 2018, 5:50:22 PM EST, Tom Lane <tgl@sss.pgh.pa.us= > wrote: 
>> Also, as mentioned upthre= ad, it'd be interesting to see if there's
>> a memo= ry context dump showing up in your server log.

> There are many memory context dumps, and they are long (well= over 100 lines apiece).  To me these are unfamiliar and hard to read,= but I tried to pick one that referenced all the same tables used in the fu= nction.  I put one such dump (188 lines) here:

> https://dri= ve.google.com/file/d/1QI4nffdZByIehb_-GULOagI_dKpKElg_/view

Thanks.  Here's the smoking gun:

  CacheMemoryContext: 221241400 total in 38 = blocks; 152192 free (90 chunks); 221089208 used

This looks like something is leaking memory directly in CacheMemo= ryContext
(not in any of its child contexts).  Can y= ou confirm that all the memory
context dumps in your logs= are showing similarly very-large total space
values for = CacheMemoryContext?

This doesn't seem = to be related to temp table usage, at least not
directly.=   (As a cross-check, I created and dropped 10000 temp tables in a
session, and the process's VM footprint didn't budge.)  = My guess is that
something else you are doing is tickling= the leak.

You said you'd been able to= reproduce this problem outside production.
Any chance yo= u could boil that down to a sharable test case?  I'd bet
long odds that it's not related to your data particularly, but rather<= br clear=3D"none">to your schema or something your function is doing, so yo= u probably
could reproduce it with some dummy data (and, = perhaps, not even very
much of that; repeating some DDL a= ction many times is probably what
is making the leak grow= to something noticeable).

If I had to= guess at this point, I'd wonder if it's something to do
= with partitioned tables.  That code is still relatively wet behind
the ears, and memory leaks are the kind of bug I'd be expect= ing to
still be shaking out of it now.  But you migh= t not be using partitioned
tables at all.  Anyway, a= nything you're doing with new-in-v10 features,
particular= ly schema-related features, would be a good bet for the
s= ource of the problem.

  = ;          regards, tom lane
<= br>


Thanks for your resp= onse and sorry for the delay in getting back to this. 
Because holidays.

To refresh your memory= , the issue I encountered is that a long-persistent
connection that peri= odically issues function calls consumes more and more
memory - eventuall= y causing memory exhaustion.  Admittedly, it does take
weeks.
This email is about creating a shareable test case.

The test case = is simple - so simple that I am surprised no one has encountered
this be= fore, and therefore I am really more fearing that I am doing something
s= tupid in my effort to make it shareable and simple.  In creating this = example
I have not run the code till it crashed the server; instead I us= ed the
measuring methodology you will see below (monitoring the smaps fi= le).

I have put needed files here:

https://drive.google.com/o= pen?id=3D1ff074Qxn7Bx9Ud9GRaegDfuJn_Tf_NTi

Table of contents:
testbed.public.backup # database dump, not compressed, includes "create da= tabase"
pg_hba.conf # self explanatory; allows local postgres to log in = as trusted
postgresql.conf # self explanatory. nothing weird
loop_que= ry.bash # sends a query via named pipe to Postgres once per second

T= o recreate the issue, load the "testbed" database into a Postgres server,
configured so that psql does not need to send a passwor= d for user postgres.

I created= the server with this:
initdb $PGDATA --locale=3D"en_US.UTF= -8" -E "UTF-8" -k

Then run as postgres:

cd= /my/directory
mkfifo qpipe # named pipe conduit to persistent Postgres = connection
cat > qpipe & # keep pipe from closing and terminating= the PG connection
psql -h localhost -d testbed -U postgres < qpipe &= amp; # listener connection
./query_loop.bash # Send function call over a= nd over once per second
# Observe output; should be 78 rows per second
# Switch to a new terminal session
# Identify the PID of the psql = process
# Monitor with this command, but plug in the real PID for the 12= 34
grep "^Private" /proc/1234/smaps | awk '{a+=3D$2}END{print a/1024.0}'=

Give it a few minutes, and you will observe memor= y usage climbing steadily
(if my measurement te= chnique is good).  From last night to this morning I
=
observed memory consumption grow from 3.5 to 46.6 MB.
=

Tested on Postgres version 10.5.

Again, thanks for any help.

Tom Carroll
------=_Part_12309337_2139612328.1546516533419--