clean-temp-ns-startup.patch
text/x-diff
Filename: clean-temp-ns-startup.patch
Type: text/x-diff
Part: 0
Message:
Re: removal of dangling temp tables
Patch
Format: unified
| File | + | − |
|---|---|---|
| src/backend/catalog/namespace.c | 20 | 0 |
| src/backend/utils/init/postinit.c | 4 | 0 |
| src/include/catalog/namespace.h | 1 | 0 |
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);