Skip to content

Commit 2602838

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 5199aba commit 2602838

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

38363836
/*
38373837
* Remove all temp tables from the temporary namespace.
3838+
*
3839+
* If we haven't set up one yet, but one exists from a previous crashed
3840+
* backend, clean that one; but only do this once in a session's life.
38383841
*/
38393842
void
38403843
ResetTempTableNamespace(void)
38413844
{
3845+
static bool TempNamespaceCleaned = false;
3846+
38423847
if (OidIsValid(myTempNamespace))
38433848
RemoveTempRelations(myTempNamespace);
3849+
else if (MyBackendId != InvalidBackendId && !RecoveryInProgress() &&
3850+
!TempNamespaceCleaned)
3851+
{
3852+
char namespaceName[NAMEDATALEN];
3853+
Oid namespaceId;
3854+
3855+
snprintf(namespaceName, sizeof(namespaceName), "pg_temp_%d",
3856+
MyBackendId);
3857+
namespaceId = get_namespace_oid(namespaceName, true);
3858+
if (OidIsValid(namespaceId))
3859+
RemoveTempRelations(namespaceId);
3860+
}
3861+
3862+
TempNamespaceCleaned = true;
38443863
}
38453864

38463865

0 commit comments

Comments
 (0)