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 1gcEXs-0005bE-0M for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Dec 2018 19:08:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gcEXq-00031m-Iv for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Dec 2018 19:08:42 +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 1gcEXq-00031f-Bt for pgsql-hackers@lists.postgresql.org; Wed, 26 Dec 2018 19:08:42 +0000 Received: from mail-qk1-x742.google.com ([2607:f8b0:4864:20::742]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gcEXn-0008LU-Hg for pgsql-hackers@lists.postgresql.org; Wed, 26 Dec 2018 19:08:41 +0000 Received: by mail-qk1-x742.google.com with SMTP id g125so9773196qke.4 for ; Wed, 26 Dec 2018 11:08:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=KblyiYuWGoLMik3FKNzGqODqjf5G3L/bEB+C4e3of20=; b=0jXeI7H28l8/NelZYcMJeMoGb9VAWo7BMRd4QbiMjZ9Uwkl18oJNCt03elTtkqfeL9 WGJV4BkTSFaUlENsxnqLp/FAxmOKgjuui0+yL3snvTem6jfcmZGA+Xo+e5kXF0dy4r21 aAPkfryQmw6mwUXr4/KkbSXruWYX/wtqbnO9mzkQbLcbAQU/GQCr/ecxa6DZ2UVFLrhb ThitXl8IQ8zBfwCihUEG9D/xQKf3uipkOkRCRovYKkWtsU6PaZutC2qJF2ILTUIWRL1Y R7X393P8LRL/u3xsWn4DjY+skkAUElGYDePbKUmoBrP/3vpQoo5PplF4rC8BhKp6okBr GxjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=KblyiYuWGoLMik3FKNzGqODqjf5G3L/bEB+C4e3of20=; b=TMeOIkLZ/x01nuknXSokCtLCV0ev22kvAxTITwGK247NNVPTpTzJ4g5DG8nlqhaeF1 1BgsO34SBcQSBeWtTDG4Sd44qqAwrlMGNrVGkT8jBSarnbuY7XuhOcesUducgUrSKzut dyTYsr0XWv6z3ClcrW8vGfgdkk8pG/2GpNlyYPmR21ZzkMT+ZHolJrnNzHSeqNU//BCe X19ykvo6OTl5ffyo5Pua5+x5loLPU9ph6z9/iNtWB1ikO0SM1x7wotFGS1l+stArC6T2 jFSrX7lHuO5TmZNVnIsPwBOasLA5Ll+/hJYWU8wtSL+S7gQkVoFy9IUU0k6S+wmXywLJ Au3Q== X-Gm-Message-State: AJcUukchhmnvdLvhiVYsHOkgT2AkrnS7WZWSuIEa+LIlVVSQ/KPOjSFe c8udBs/DyA/nqtYy6v5k6tmYrA== X-Google-Smtp-Source: ALg8bN6492/h/nDVMmP3Fcui2xDcUmrrq1kMlcC1vmk9xo86cEgJcn/S74sXLis27JAMrapXaw3AeQ== X-Received: by 2002:ae9:ee02:: with SMTP id i2mr18506998qkg.179.1545851317352; Wed, 26 Dec 2018 11:08:37 -0800 (PST) Received: from alvin.alvh.no-ip.org ([201.186.80.67]) by smtp.gmail.com with ESMTPSA id m14sm11044583qka.21.2018.12.26.11.08.36 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 26 Dec 2018 11:08:36 -0800 (PST) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id BB06F253; Wed, 26 Dec 2018 16:08:34 -0300 (-03) Date: Wed, 26 Dec 2018 16:08:34 -0300 From: Alvaro Herrera To: Michael Paquier Cc: Tom Lane , Robert Haas , PostgreSQL Hackers Subject: Re: removal of dangling temp tables Message-ID: <20181226190834.wsk2wzott5yzrjiq@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="2g2g5wvzs6ruvdiz" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20181216065847.GJ5012@paquier.xyz> User-Agent: NeoMutt/20180716 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --2g2g5wvzs6ruvdiz Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit On 2018-Dec-16, Michael Paquier wrote: > On Sat, Dec 15, 2018 at 09:51:31AM -0500, Tom Lane wrote: > > Alvaro Herrera writes: > >> Oh, we already have it! Sorry, I overlooked it. With that, it seems > >> the patch is fairly simple ... I wonder about the locking implications > >> in autovacuum, though -- the value is set in backends without acquiring > >> a lock. > > > > I was wondering about that too. But I think it's probably OK. If > > autovacuum observes that (a) a table is old enough to pose a wraparound > > hazard and (b) its putatively owning backend hasn't yet set > > tempNamespaceId, then I think it's safe to conclude that that table is > > removable, despite the theoretical race condition. > > This relies on the fact that the flag gets set by a backend within a > transaction context, and autovacuum would not see yet temp relations > associated to it at the moment of the scan of pg_class if the backend > has not committed yet its namespace creation via the creation of the > first temp table it uses. Makes sense, thanks. I think there are two possible ways forward. The conservative one is to just apply the attached patch to branches 9.4-10. That will let autovacuum drop tables when the backend is in another database. It may not solve the problem for the bunch of users that have only one database that takes the majority of connections, but I think it's worth doing nonetheless. I tested the 9.4 instance and it works fine; tables are deleted as soon as I make the session connection to another database. The more aggressive action is to backpatch 943576bddcb5 ("Make autovacuum more aggressive to remove orphaned temp tables") which is currently only in pg11. We would put the new PGPROC member at the end of the struct, to avoid ABI incompatibilities, but it'd bring trouble for extensions that put PGPROC in arrays. I checked the code of some known extensions; found that pglogical uses PGPROC, but only pointers to it, so it wouldn't be damaged by the proposed change AFAICS. Another possibly useful change is to make DISCARD ALL and DISCARD TEMP delete everything in what would be the backend's temp namespace, even if it hasn't been initialized yet. This would cover the case where a connection pooler keeps the connection open for a very long time, which I think is a common case. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --2g2g5wvzs6ruvdiz Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="autovac-temp-REL9_4_STABLE.patch" Content-Transfer-Encoding: quoted-printable commit 5aa717be9fe7288cbec043cdea0fb757433f3462=1B[m Author: Alvaro Herrera AuthorDate: Wed Dec 26 15:12:44 2018 -0300 CommitDate: Wed Dec 26 15:12:45 2018 -0300 Improve autovacuum logic about temp tables nearing XID wraparound diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/a= utovacuum.c index c18285d690..62a06add56 100644 --- a/src/backend/postmaster/autovacuum.c +++ b/src/backend/postmaster/autovacuum.c @@ -2108,11 +2108,18 @@ do_autovacuum(void) if (classForm->relpersistence =3D=3D RELPERSISTENCE_TEMP) { int backendID; + PGPROC *proc; =20 backendID =3D GetTempNamespaceBackendId(classForm->relnamespace); =20 - /* We just ignore it if the owning backend is still active */ - if (backendID =3D=3D MyBackendId || BackendIdGetProc(backendID) =3D=3D = NULL) + /* + * We just ignore it if the owning backend is still active in the + * same database. + */ + if (backendID !=3D InvalidBackendId && + (backendID =3D=3D MyBackendId || + (proc =3D BackendIdGetProc(backendID)) =3D=3D NULL || + proc->databaseId !=3D MyDatabaseId)) { /* * We found an orphan temp table (which was probably left --2g2g5wvzs6ruvdiz--