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 1gf9l0-0000AL-JU for pgsql-general@arkaria.postgresql.org; Thu, 03 Jan 2019 20:38:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gf9kz-0005tP-5V for pgsql-general@arkaria.postgresql.org; Thu, 03 Jan 2019 20:38:21 +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 1gf9ky-0005pr-RJ for pgsql-general@lists.postgresql.org; Thu, 03 Jan 2019 20:38:20 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gf9kw-0006Gn-4V for pgsql-general@postgresql.org; Thu, 03 Jan 2019 20:38:20 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x03KcETf005063; Thu, 3 Jan 2019 15:38:14 -0500 From: Tom Lane To: Thomas Carroll cc: "pgsql-general@postgresql.org" Subject: Re: Memory exhaustion due to temporary tables? In-reply-to: <116743157.12309338.1546516533422@mail.yahoo.com> 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> <116743157.12309338.1546516533422@mail.yahoo.com> Comments: In-reply-to Thomas Carroll message dated "Thu, 03 Jan 2019 11:55:33 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <5061.1546547894.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 03 Jan 2019 15:38:14 -0500 Message-ID: <5062.1546547894@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk [ please don't re-quote the entire thread when replying ] Thomas Carroll writes: > On Monday, December 10, 2018, 7:45:07 PM EST, Tom Lane wrote: = >> You said you'd been able to reproduce this problem outside production. >> Any chance you could boil that down to a sharable test case? > The test case is simple - so simple that I am surprised no one has encou= ntered > this before, and therefore I am really more fearing that I am doing some= thing > stupid in my effort to make it shareable and simple. In creating this e= xample > 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). Hmm. So what this test case is doing is repeatedly creating a temp table with ON COMMIT DROP, querying that table, and then closing the transaction (allowing the temp table to go away). It does leak memory in CacheMemoryContext, and as far as I can tell, the leakage consists entirely of negative catcache entries for pg_statistic rows. That happens because while querying the temp table, the planner tries to look up statistics for the table; but there are none, since you haven't ANALYZEd it. So negative catcache entries get made in hopes of saving the cost of probing pg_statistic again later. But, when we drop the table, those catcache entries don't get cleared because they do not match any catalog rows that get deleted during the drop. In a lot of use-cases, this wouldn't matter too much, either because the session isn't long-lived enough to accumulate huge numbers of negative entries, or because other catalog activity causes the entries to get flushed anyway. But if you don't have much DDL going on other than this temp table activity, then yeah it could build up. Not sure about good ways to fix this. I can think of various more-or-less klugy fixes that are specific to the pg_statistic case. There's been some ongoing discussion about trying to limit accumulation of negative catcache entries more generally, but that's not very close to being committable I think. In the meantime, you might think about switching over to some process that doesn't create and drop the same table constantly. Perhaps along the lines of create temp table if not exists tt_preTally (...) on commit delete rows; if (tt_preTally contains no rows) then insert into tt_preTally select ... end if; This'd have the advantage of reducing catalog churn in other catalogs besides pg_statistic, too. regards, tom lane