Skip to content

Commit 3c4ca21

Browse files
committed
Have DISCARD ALL/TEMP remove leftover temp tables
Previously, it would only remove temp tables created in the same session; but if the session uses the BackendId of a previously crashed backend that left temp tables around, those would not get removed. Since autovacuum would not drop them either (because it sees that the BackendId is in use by the current session) these can cause annoying xid-wraparound warnings. Apply to branches 9.4 to 10. This is not a problem since version 11, because commit 943576b added state tracking that makes autovacuum realize that those temp tables are not ours, so it removes them. This is useful to handle in DISCARD, because even though it does not handle all situations, it does handle the common one where a connection pooler keeps the same session open for an indefinitely long time. Discussion: https://postgr.es/m/20181226190834.wsk2wzott5yzrjiq@alvherre.pgsql Reviewed-by: Takayuki Tsunakawa, Michaël Paquier
1 parent a6ca47c commit 3c4ca21

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/backend/catalog/namespace.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4042,12 +4042,31 @@ RemoveTempRelationsCallback(int code, Datum arg)
40424042

40434043
/*
40444044
* Remove all temp tables from the temporary namespace.
4045+
*
4046+
* If we haven't set up one yet, but one exists from a previous crashed
4047+
* backend, clean that one; but only do this once in a session's life.
40454048
*/
40464049
void
40474050
ResetTempTableNamespace(void)
40484051
{
4052+
static bool TempNamespaceCleaned = false;
4053+
40494054
if (OidIsValid(myTempNamespace))
40504055
RemoveTempRelations(myTempNamespace);
4056+
else if (MyBackendId != InvalidBackendId && !RecoveryInProgress() &&
4057+
!TempNamespaceCleaned)
4058+
{
4059+
char namespaceName[NAMEDATALEN];
4060+
Oid namespaceId;
4061+
4062+
snprintf(namespaceName, sizeof(namespaceName), "pg_temp_%d",
4063+
MyBackendId);
4064+
namespaceId = get_namespace_oid(namespaceName, true);
4065+
if (OidIsValid(namespaceId))
4066+
RemoveTempRelations(namespaceId);
4067+
}
4068+
4069+
TempNamespaceCleaned = true;
40514070
}
40524071

40534072

0 commit comments

Comments
 (0)