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

Commit 6faca9a

Browse files
committed
Avoid deadlock during orphan temp table removal.
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
1 parent 4133c1f commit 6faca9a

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

src/backend/postmaster/autovacuum.c

+20-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
#include "catalog/dependency.h"
7777
#include "catalog/namespace.h"
7878
#include "catalog/pg_database.h"
79+
#include "catalog/pg_namespace.h"
7980
#include "commands/dbcommands.h"
8081
#include "commands/vacuum.h"
8182
#include "common/int.h"
@@ -2175,6 +2176,24 @@ do_autovacuum(void)
21752176
continue;
21762177
}
21772178

2179+
/*
2180+
* Try to lock the temp namespace, too. Even though we have lock on
2181+
* the table itself, there's a risk of deadlock against an incoming
2182+
* backend trying to clean out the temp namespace, in case this table
2183+
* has dependencies (such as sequences) that the backend's
2184+
* performDeletion call might visit in a different order. If we can
2185+
* get AccessShareLock on the namespace, that's sufficient to ensure
2186+
* we're not running concurrently with RemoveTempRelations. If we
2187+
* can't, back off and let RemoveTempRelations do its thing.
2188+
*/
2189+
if (!ConditionalLockDatabaseObject(NamespaceRelationId,
2190+
classForm->relnamespace, 0,
2191+
AccessShareLock))
2192+
{
2193+
UnlockRelationOid(relid, AccessExclusiveLock);
2194+
continue;
2195+
}
2196+
21782197
/* OK, let's delete it */
21792198
ereport(LOG,
21802199
(errmsg("autovacuum: dropping orphan temp table \"%s.%s.%s\"",
@@ -2192,7 +2211,7 @@ do_autovacuum(void)
21922211

21932212
/*
21942213
* To commit the deletion, end current transaction and start a new
2195-
* one. Note this also releases the lock we took.
2214+
* one. Note this also releases the locks we took.
21962215
*/
21972216
CommitTransactionCommand();
21982217
StartTransactionCommand();

src/backend/storage/lmgr/lmgr.c

+38
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,44 @@ LockDatabaseObject(Oid classid, Oid objid, uint16 objsubid,
10181018
AcceptInvalidationMessages();
10191019
}
10201020

1021+
/*
1022+
* ConditionalLockDatabaseObject
1023+
*
1024+
* As above, but only lock if we can get the lock without blocking.
1025+
* Returns true iff the lock was acquired.
1026+
*/
1027+
bool
1028+
ConditionalLockDatabaseObject(Oid classid, Oid objid, uint16 objsubid,
1029+
LOCKMODE lockmode)
1030+
{
1031+
LOCKTAG tag;
1032+
LOCALLOCK *locallock;
1033+
LockAcquireResult res;
1034+
1035+
SET_LOCKTAG_OBJECT(tag,
1036+
MyDatabaseId,
1037+
classid,
1038+
objid,
1039+
objsubid);
1040+
1041+
res = LockAcquireExtended(&tag, lockmode, false, true, true, &locallock);
1042+
1043+
if (res == LOCKACQUIRE_NOT_AVAIL)
1044+
return false;
1045+
1046+
/*
1047+
* Now that we have the lock, check for invalidation messages; see notes
1048+
* in LockRelationOid.
1049+
*/
1050+
if (res != LOCKACQUIRE_ALREADY_CLEAR)
1051+
{
1052+
AcceptInvalidationMessages();
1053+
MarkLockClear(locallock);
1054+
}
1055+
1056+
return true;
1057+
}
1058+
10211059
/*
10221060
* UnlockDatabaseObject
10231061
*/

src/include/storage/lmgr.h

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ extern void SpeculativeInsertionWait(TransactionId xid, uint32 token);
9393
/* Lock a general object (other than a relation) of the current database */
9494
extern void LockDatabaseObject(Oid classid, Oid objid, uint16 objsubid,
9595
LOCKMODE lockmode);
96+
extern bool ConditionalLockDatabaseObject(Oid classid, Oid objid,
97+
uint16 objsubid, LOCKMODE lockmode);
9698
extern void UnlockDatabaseObject(Oid classid, Oid objid, uint16 objsubid,
9799
LOCKMODE lockmode);
98100

0 commit comments

Comments
 (0)