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 1gXrEy-0007nc-W0 for pgsql-hackers@arkaria.postgresql.org; Fri, 14 Dec 2018 17:27: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 1gXrEv-0008Vz-OY for pgsql-hackers@arkaria.postgresql.org; Fri, 14 Dec 2018 17:27:05 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gXrEv-0008Vs-E4 for pgsql-hackers@lists.postgresql.org; Fri, 14 Dec 2018 17:27:05 +0000 Received: from mail-qt1-x844.google.com ([2607:f8b0:4864:20::844]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gXrEr-0008U9-V2 for pgsql-hackers@lists.postgresql.org; Fri, 14 Dec 2018 17:27:03 +0000 Received: by mail-qt1-x844.google.com with SMTP id e5so6979829qtr.12 for ; Fri, 14 Dec 2018 09:27: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=ywefeuJ1A5Q9s82wotBziCHfngvJZdnhmhYYCH9Y6lk=; b=HcFKcE8NBONFxA8QAvW/eY1LTlssOZ9ljf2U8UIGqYU18gxH181p9xJxyUPtG8xgbW Y58MBW9BMX2Jc6q1SXaWqu9sQ/q2al2mUEKGcXiCnEJJjYAG9gzR6yEGIVWQ/nlcw405 +dQOPvntcdB+89H9F7qsmQmtaa15jA9VtqEy6N4kDh7Xwj5bf0F5vwAqHBrMW+hM/qx7 7RULDnA1o/rpdihtjAuZvRRjzkpn3Kqi/GzA2uC8nUksnx/kLLBQFC0QlusiyEs+Z3HV 5AcXNG5k6mHrkb8ssB+jmIG8zmwSW3Taxx+NATcURYvaQ48o42xp2SuwWCU4pYYdtNcf MQRw== 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=ywefeuJ1A5Q9s82wotBziCHfngvJZdnhmhYYCH9Y6lk=; b=ABYegY8mrzYU1SiaCZdst8b2I2L9kTJx7JTqo+w/0/unkgnPxy+YiIBA/aDoQCG5nv p37ldcb6SikhsDxJokVejEaIABVR3o4LtZ07TECjmpGOHlCy5rhDXO3WtLJKI/BAaO0L dMcyyNfLkITHJUZu7KLUUcNY1hs7vxDnTObr6KQ3PC97Wis+LncKbQNJs4zgWvnZ5Smn 0VNw307ClwTLq1yuzB03+VZ/AssLQVLlC78iWUAkI0TIzB+pKveGDXVBxtDAVYNGLCUr b6gAE+vfX3/GurMlhba82+Akz7ejWyVX2ICmZ20zZCGeZwxIuNZpl53sJg5jHg9UgIar mLig== X-Gm-Message-State: AA+aEWbEFjJGZt2ijLJHcX3uJeZ8qpboSIRR/93E3bhUp9zb9VTR4kBI NnhwDEwROOVZ44XQL8C4i/NnhIZuukNK2w== X-Google-Smtp-Source: AFSGD/UPzLo+Rj3mTk2z6zN3kA3kf42SRrjo2kESf5hP97Jr+cb99G29++2nCrftg+QFxBfpWR7C+Q== X-Received: by 2002:ac8:7644:: with SMTP id i4mr3767505qtr.293.1544808421147; Fri, 14 Dec 2018 09:27:01 -0800 (PST) Received: from alvin.alvh.no-ip.org ([190.121.29.3]) by smtp.gmail.com with ESMTPSA id n72sm4000223qki.26.2018.12.14.09.27.00 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 14 Dec 2018 09:27:00 -0800 (PST) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id 1DA94928; Fri, 14 Dec 2018 14:26:58 -0300 (-03) Date: Fri, 14 Dec 2018 14:26:58 -0300 From: Alvaro Herrera To: Robert Haas Cc: PostgreSQL Hackers Subject: Re: removal of dangling temp tables Message-ID: <20181214172658.3b7esbo3dm3gputq@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="z7adugs2f544eilm" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: NeoMutt/20180716 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --z7adugs2f544eilm Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit On 2018-Dec-14, Robert Haas wrote: > On Fri, Dec 14, 2018 at 11:29 AM Alvaro Herrera > wrote: > > I think the best way to fix this is to call RemoveTempRelations() > > unconditionally at session start (without doing the rest of the temp > > table setup, just the removal.) > > That would certainly simplify things. I think I thought about that as > far back as a734fd5d1c309cc553b7c8c79fba96218af090f7 but it seemed > like a significant behavior change and I wasn't sure that everyone > would like it. In particular, it adds overhead to backend startup > that, in the case of a large temp schema, could be fairly long. Hmm, I think in the case covered by your commit, that is a session that crashes with a few thousands of temp tables, this new patch might cause a failure to open a new session altogether. Maybe it'd be better to change temp table removal to always drop max_locks_per_transaction objects at a time (ie. commit/start a new transaction every so many objects). -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --z7adugs2f544eilm Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="clean-temp-ns-startup.patch" diff --git a/src/backend/catalog/namespace.c b/src/backend/catalog/namespace.c index 13a24631fc..003bbabf1f 100644 --- a/src/backend/catalog/namespace.c +++ b/src/backend/catalog/namespace.c @@ -4118,6 +4118,26 @@ RemoveTempRelations(Oid tempNamespaceId) } /* + * Remove temp tables, without relying on myTempNamespace being set, and + * without setting it. This is used at session start. + */ +void +InitRemoveTempRelations(void) +{ + Oid namespaceOid; + char namespaceName[NAMEDATALEN]; + + Assert(MyBackendId != InvalidBackendId); + + snprintf(namespaceName, sizeof(namespaceName), "pg_temp_%d", MyBackendId); + + namespaceOid = get_namespace_oid(namespaceName, true); + + if (OidIsValid(namespaceOid)) + RemoveTempRelations(namespaceOid); +} + +/* * Callback to remove temp relations at backend exit. */ static void diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index b636b1e262..b41b47222c 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -1074,6 +1074,10 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username, if (!bootstrap) pgstat_bestart(); + /* Remove any temp tables that might have leaked previously */ + if (!bootstrap) + InitRemoveTempRelations(); + /* close the transaction we started above */ if (!bootstrap) CommitTransactionCommand(); diff --git a/src/include/catalog/namespace.h b/src/include/catalog/namespace.h index 0e202372d5..f90eeff1b7 100644 --- a/src/include/catalog/namespace.h +++ b/src/include/catalog/namespace.h @@ -144,6 +144,7 @@ extern void GetTempNamespaceState(Oid *tempNamespaceId, Oid *tempToastNamespaceId); extern void SetTempNamespaceState(Oid tempNamespaceId, Oid tempToastNamespaceId); +extern void InitRemoveTempRelations(void); extern void ResetTempTableNamespace(void); extern OverrideSearchPath *GetOverrideSearchPath(MemoryContext context); --z7adugs2f544eilm--