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 1gcIy8-0003iK-P9 for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Dec 2018 23:52:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gcIy7-0000az-11 for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Dec 2018 23:52:07 +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 1gcIy6-0000as-Ls for pgsql-hackers@lists.postgresql.org; Wed, 26 Dec 2018 23:52:06 +0000 Received: from mail-qt1-x843.google.com ([2607:f8b0:4864:20::843]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gcIy1-0006Le-JS for pgsql-hackers@lists.postgresql.org; Wed, 26 Dec 2018 23:52:05 +0000 Received: by mail-qt1-x843.google.com with SMTP id n32so18649052qte.11 for ; Wed, 26 Dec 2018 15:52:01 -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=ranDl+qD5CMsz8C+eUU9ioQj41Dm9K2I0JWEOoPdGWI=; b=NF/Dd4yk3wwVGwcs9WfnqVZML2dsHQhgup3q9BNHEa5bzKYl8VILh3q3GKh2YdLH7b jcYw6Q6O85pZDMy4wLwpu1nBfwsnEVnbTFG04yDE9tUhx4BiasgoF/br3CJfhsgANP1T e0vSOLsgI4fdm5fQ5iMJUpTc9j73z2P2flBbFqSv0uVs+vlSijMAx2IZkKkwtzA4nqIq UqqncYeSUcvumq5nmq3TEW4QUsYVlt61PEn5QdytLkBvUkuPqXQFZBEO+W6i6k6QECoe uIlhXnwVsUluPSIeoz6eXxE75avUychBWMK6CZeBb9BRwO1oVmVf4TkpSzGyo+DzIWQ+ zCcg== 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=ranDl+qD5CMsz8C+eUU9ioQj41Dm9K2I0JWEOoPdGWI=; b=HgKzKcWGglUZvRx63xmmem8lbQNm/tFUe7OHtLr4vb6Qyr4LeTUh/tyA3LKkp83RVa Dt4xrBtPL7VxCBNnr++TgrCw+YWeevkifZUBcJgod5M9MJrMLndQsCpzyUcybJymjZdT xg1kuXGIewY5UxkC3R53iN6OKGijqcOfqTsiViOkRv8kt4KEErmfI1FY9nN4sfH9rCGu 2WdlOHH5AwjC/Rl119C9PcfzkvHO8NJGkKqkdvEhD+7EZdXVE9KwiUNh37hJNDdobMCh wru3XRgV3pCeZ0BiPRTTJSIvJwcloKfAQIDW43cm/aB0iZfpJgJzjBaPLSr1Gd6/Rg/S A77A== X-Gm-Message-State: AJcUukfOqn9mRDD54Gw8XPRF/nZq9ZBQVkekkqikLe35rF9gxU3AbHz5 ry4BG2Q3sTP5KBza79VRhv568w== X-Google-Smtp-Source: ALg8bN51ZLwByzIytZfhojo6etkTfhyEeVCO2O1u2+/2Snf75TabRaJSf1smBjagY7uKkT2VMnq+Rw== X-Received: by 2002:a0c:81c4:: with SMTP id 4mr21165152qve.156.1545868319529; Wed, 26 Dec 2018 15:51:59 -0800 (PST) Received: from alvin.alvh.no-ip.org ([201.186.80.67]) by smtp.gmail.com with ESMTPSA id w34sm15510694qtj.27.2018.12.26.15.51.58 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 26 Dec 2018 15:51:58 -0800 (PST) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id D6F796B9; Wed, 26 Dec 2018 20:51:56 -0300 (-03) Date: Wed, 26 Dec 2018 20:51:56 -0300 From: Alvaro Herrera To: "Tsunakawa, Takayuki" Cc: Michael Paquier , Tom Lane , Robert Haas , PostgreSQL Hackers Subject: Re: removal of dangling temp tables Message-ID: <20181226235156.sqeoyp6gbfavqolp@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="d7434mtzlytuzog5" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <0A3221C70F24FB45833433255569204D1FB5DC66@G01JPEXMBYT05> User-Agent: NeoMutt/20180716 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --d7434mtzlytuzog5 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit On 2018-Dec-26, Tsunakawa, Takayuki wrote: > From: Alvaro Herrera [mailto:alvherre@2ndquadrant.com] > > 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. > > +1 > I think this is a bug from a user's perspective that garbage is left. > I want to believe that fixing bugs of PostgreSQL itself are > prioritized over the ABI compatibility for extensions, if we have to > choose one of them. Having been victim of ABI incompatibility myself, I loathe giving too much priority to other issues that can be resolved in other ways, so I don't necessarily support your view on bugs. That said, I think in this case it shouldn't be a problem, so I'm going to work on that next. I haven't got around to creating the abidiff reporting system yet ... > > 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. > > That sounds good. Thanks. I just tested that the attached patch does the intended. (I named it "autovac" but obviously the DISCARD part is not about autovacuum). I intend to push this patch first, and later backpatch the other one. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --d7434mtzlytuzog5 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="autovac-temp-REL9_4_STABLE.patch" diff --git a/src/backend/catalog/namespace.c b/src/backend/catalog/namespace.c index 89df585b87..d6f3fd5436 100644 --- a/src/backend/catalog/namespace.c +++ b/src/backend/catalog/namespace.c @@ -3835,12 +3835,31 @@ RemoveTempRelationsCallback(int code, Datum arg) /* * Remove all temp tables from the temporary namespace. + * + * If we haven't set up one yet, but one exists from a previous crashed + * backend, clean that one; but only do this once in a session's life. */ void ResetTempTableNamespace(void) { + static bool TempNamespaceCleaned = false; + if (OidIsValid(myTempNamespace)) RemoveTempRelations(myTempNamespace); + else if (MyBackendId != InvalidBackendId && !RecoveryInProgress() && + !TempNamespaceCleaned) + { + char namespaceName[NAMEDATALEN]; + Oid namespaceId; + + snprintf(namespaceName, sizeof(namespaceName), "pg_temp_%d", + MyBackendId); + namespaceId = get_namespace_oid(namespaceName, true); + if (OidIsValid(namespaceId)) + RemoveTempRelations(namespaceId); + } + + TempNamespaceCleaned = true; } diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.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 == RELPERSISTENCE_TEMP) { int backendID; + PGPROC *proc; backendID = GetTempNamespaceBackendId(classForm->relnamespace); - /* We just ignore it if the owning backend is still active */ - if (backendID == MyBackendId || BackendIdGetProc(backendID) == NULL) + /* + * We just ignore it if the owning backend is still active in the + * same database. + */ + if (backendID != InvalidBackendId && + (backendID == MyBackendId || + (proc = BackendIdGetProc(backendID)) == NULL || + proc->databaseId != MyDatabaseId)) { /* * We found an orphan temp table (which was probably left --d7434mtzlytuzog5--