Skip to content

Commit 7fc2363

Browse files
committed
Make autovacuum more selective about temp tables to keep
When temp tables are in danger of XID wraparound, autovacuum drops them; however, it preserves those that are owned by a working session. This is desirable, except when the session is connected to a different database (because the temp tables cannot be from that session), so make it only keep the temp tables only if the backend is in the same database as the temp tables. This is not bulletproof: it fails to detect temp tables left by a session whose backend ID is reused in the same database but the new session does not use temp tables. Commit 943576b fixes that case too, for branches 11 and up (which is why we don't apply this fix to those branches), but back-patching that one is not universally agreed on. Discussion: https://postgr.es/m/20181214162843.37g6h3txto43akrb@alvherre.pgsql Reviewed-by: Takayuki Tsunakawa, Michaël Paquier
1 parent 285abc8 commit 7fc2363

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/backend/postmaster/autovacuum.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,11 +2046,18 @@ do_autovacuum(void)
20462046
if (classForm->relpersistence == RELPERSISTENCE_TEMP)
20472047
{
20482048
int backendID;
2049+
PGPROC *proc;
20492050

20502051
backendID = GetTempNamespaceBackendId(classForm->relnamespace);
20512052

2052-
/* We just ignore it if the owning backend is still active */
2053-
if (backendID == MyBackendId || BackendIdGetProc(backendID) == NULL)
2053+
/*
2054+
* We just ignore it if the owning backend is still active in the
2055+
* same database.
2056+
*/
2057+
if (backendID != InvalidBackendId &&
2058+
(backendID == MyBackendId ||
2059+
(proc = BackendIdGetProc(backendID)) == NULL ||
2060+
proc->databaseId != MyDatabaseId))
20542061
{
20552062
/*
20562063
* We found an orphan temp table (which was probably left

0 commit comments

Comments
 (0)