Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit aa2e84d

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 7fc2363 commit aa2e84d

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
@@ -3902,12 +3902,31 @@ RemoveTempRelationsCallback(int code, Datum arg)
39023902

39033903
/*
39043904
* Remove all temp tables from the temporary namespace.
3905+
*
3906+
* If we haven't set up one yet, but one exists from a previous crashed
3907+
* backend, clean that one; but only do this once in a session's life.
39053908
*/
39063909
void
39073910
ResetTempTableNamespace(void)
39083911
{
3912+
static bool TempNamespaceCleaned = false;
3913+
39093914
if (OidIsValid(myTempNamespace))
39103915
RemoveTempRelations(myTempNamespace);
3916+
else if (MyBackendId != InvalidBackendId && !RecoveryInProgress() &&
3917+
!TempNamespaceCleaned)
3918+
{
3919+
char namespaceName[NAMEDATALEN];
3920+
Oid namespaceId;
3921+
3922+
snprintf(namespaceName, sizeof(namespaceName), "pg_temp_%d",
3923+
MyBackendId);
3924+
namespaceId = get_namespace_oid(namespaceName, true);
3925+
if (OidIsValid(namespaceId))
3926+
RemoveTempRelations(namespaceId);
3927+
}
3928+
3929+
TempNamespaceCleaned = true;
39113930
}
39123931

39133932

0 commit comments

Comments
 (0)