]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Avoid deadlock during orphan temp table removal.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 2 Apr 2024 18:59:04 +0000 (14:59 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 2 Apr 2024 18:59:04 +0000 (14:59 -0400)
If temp tables have dependencies (such as sequences) then it's
possible for autovacuum's cleanup of orphan temp tables to deadlock
against an incoming backend that's trying to clean out the temp
namespace for its own use.  That can happen because RemoveTempRelations'
performDeletion call can visit objects within the namespace in
an order different from the order in which a per-table deletion
will visit them.

To fix, observe that performDeletion will begin by taking an exclusive
lock on the temp namespace (even though it won't actually delete it).
So, if we can get a shared lock on the namespace, we can be sure we're
not running concurrently with RemoveTempRelations, while also not
conflicting with ordinary use of the namespace.  This requires
introducing a conditional version of LockDatabaseObject, but that's no
big deal.  (It's surprising we've got along without that this long.)

Report and patch by Mikhail Zhilin.  Back-patch to all supported
branches.

Discussion: https://postgr.es/m/c43ce028-2bc2-4865-9b89-3f706246eed5@postgrespro.ru

src/backend/postmaster/autovacuum.c
src/backend/storage/lmgr/lmgr.c
src/include/storage/lmgr.h

index 72a5c4aefe99a17d3de7751404c773220ac7dca7..356678b0304514f4f44a6743c19d8e08031679a9 100644 (file)
@@ -76,6 +76,7 @@
 #include "catalog/dependency.h"
 #include "catalog/namespace.h"
 #include "catalog/pg_database.h"
+#include "catalog/pg_namespace.h"
 #include "commands/dbcommands.h"
 #include "commands/vacuum.h"
 #include "lib/ilist.h"
@@ -2272,6 +2273,24 @@ do_autovacuum(void)
                        continue;
                }
 
+               /*
+                * Try to lock the temp namespace, too.  Even though we have lock on
+                * the table itself, there's a risk of deadlock against an incoming
+                * backend trying to clean out the temp namespace, in case this table
+                * has dependencies (such as sequences) that the backend's
+                * performDeletion call might visit in a different order.  If we can
+                * get AccessShareLock on the namespace, that's sufficient to ensure
+                * we're not running concurrently with RemoveTempRelations.  If we
+                * can't, back off and let RemoveTempRelations do its thing.
+                */
+               if (!ConditionalLockDatabaseObject(NamespaceRelationId,
+                                                                                  classForm->relnamespace, 0,
+                                                                                  AccessShareLock))
+               {
+                       UnlockRelationOid(relid, AccessExclusiveLock);
+                       continue;
+               }
+
                /* OK, let's delete it */
                ereport(LOG,
                                (errmsg("autovacuum: dropping orphan temp table \"%s.%s.%s\"",
@@ -2289,7 +2308,7 @@ do_autovacuum(void)
 
                /*
                 * To commit the deletion, end current transaction and start a new
-                * one.  Note this also releases the lock we took.
+                * one.  Note this also releases the locks we took.
                 */
                CommitTransactionCommand();
                StartTransactionCommand();
index 1543da61620db2071546cb30d8852cf8a02269dd..2acba3934b9599b52b52b968830a1adf6787fb17 100644 (file)
@@ -1019,6 +1019,44 @@ LockDatabaseObject(Oid classid, Oid objid, uint16 objsubid,
        AcceptInvalidationMessages();
 }
 
+/*
+ *             ConditionalLockDatabaseObject
+ *
+ * As above, but only lock if we can get the lock without blocking.
+ * Returns true iff the lock was acquired.
+ */
+bool
+ConditionalLockDatabaseObject(Oid classid, Oid objid, uint16 objsubid,
+                                                         LOCKMODE lockmode)
+{
+       LOCKTAG         tag;
+       LOCALLOCK  *locallock;
+       LockAcquireResult res;
+
+       SET_LOCKTAG_OBJECT(tag,
+                                          MyDatabaseId,
+                                          classid,
+                                          objid,
+                                          objsubid);
+
+       res = LockAcquireExtended(&tag, lockmode, false, true, true, &locallock);
+
+       if (res == LOCKACQUIRE_NOT_AVAIL)
+               return false;
+
+       /*
+        * Now that we have the lock, check for invalidation messages; see notes
+        * in LockRelationOid.
+        */
+       if (res != LOCKACQUIRE_ALREADY_CLEAR)
+       {
+               AcceptInvalidationMessages();
+               MarkLockClear(locallock);
+       }
+
+       return true;
+}
+
 /*
  *             UnlockDatabaseObject
  */
index be1d2c99a95055d10021d7a252d3ac0981f99afa..2c0c1f346fa51b349d6be1558affe7cf491d94bb 100644 (file)
@@ -93,6 +93,8 @@ extern void SpeculativeInsertionWait(TransactionId xid, uint32 token);
 /* Lock a general object (other than a relation) of the current database */
 extern void LockDatabaseObject(Oid classid, Oid objid, uint16 objsubid,
                                                           LOCKMODE lockmode);
+extern bool ConditionalLockDatabaseObject(Oid classid, Oid objid,
+                                                                                 uint16 objsubid, LOCKMODE lockmode);
 extern void UnlockDatabaseObject(Oid classid, Oid objid, uint16 objsubid,
                                                                 LOCKMODE lockmode);