Make autovacuum more selective about temp tables to keep
authorAlvaro Herrera
Thu, 27 Dec 2018 19:00:39 +0000 (16:00 -0300)
committerAlvaro Herrera
Thu, 27 Dec 2018 19:00:39 +0000 (16:00 -0300)
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 943576bddcb5 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[email protected]
Reviewed-by: Takayuki Tsunakawa, Michaël Paquier
src/backend/postmaster/autovacuum.c

index 70e08b552a1566c000bf337050bd2cba7beeb581..0a8fb3d58f07da45daca2a42dfdc7f58c188543f 100644 (file)
@@ -2084,13 +2084,18 @@ do_autovacuum(void)
        if (classForm->relpersistence == RELPERSISTENCE_TEMP)
        {
            int         backendID;
+           PGPROC     *proc;
 
            backendID = GetTempNamespaceBackendId(classForm->relnamespace);
 
-           /* We just ignore it if the owning backend is still active */
+           /*
+            * We just ignore it if the owning backend is still active in the
+            * same database.
+            */
            if (backendID != InvalidBackendId &&
                (backendID == MyBackendId ||
-                BackendIdGetProc(backendID) == NULL))
+                (proc = BackendIdGetProc(backendID)) == NULL ||
+                proc->databaseId != MyDatabaseId))
            {
                /*
                 * The table seems to be orphaned -- although it might be that