Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6faca9a

Browse files
committed
Avoid deadlock during orphan temp table removal.
If temp tables have dependencies (such as sequences) then it'spossible for autovacuum's cleanup of orphan temp tables to deadlockagainst an incoming backend that's trying to clean out the tempnamespace for its own use. That can happen because RemoveTempRelations'performDeletion call can visit objects within the namespace inan order different from the order in which a per-table deletionwill visit them.To fix, observe that performDeletion will begin by taking an exclusivelock 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'renot running concurrently with RemoveTempRelations, while also notconflicting with ordinary use of the namespace. This requiresintroducing a conditional version of LockDatabaseObject, but that's nobig deal. (It's surprising we've got along without that this long.)Report and patch by Mikhail Zhilin. Back-patch to all supportedbranches.Discussion:https://postgr.es/m/c43ce028-2bc2-4865-9b89-3f706246eed5@postgrespro.ru
1 parent4133c1f commit6faca9a

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

‎src/backend/postmaster/autovacuum.c

Lines changed: 20 additions & 1 deletion
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 thelock we took.
2214+
* one. Note this also releases thelocks we took.
21962215
*/
21972216
CommitTransactionCommand();
21982217
StartTransactionCommand();

‎src/backend/storage/lmgr/lmgr.c

Lines changed: 38 additions & 0 deletions
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(Oidclassid,Oidobjid,uint16objsubid,
1029+
LOCKMODElockmode)
1030+
{
1031+
LOCKTAGtag;
1032+
LOCALLOCK*locallock;
1033+
LockAcquireResultres;
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

Lines changed: 2 additions & 0 deletions
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
externvoidLockDatabaseObject(Oidclassid,Oidobjid,uint16objsubid,
9595
LOCKMODElockmode);
96+
externboolConditionalLockDatabaseObject(Oidclassid,Oidobjid,
97+
uint16objsubid,LOCKMODElockmode);
9698
externvoidUnlockDatabaseObject(Oidclassid,Oidobjid,uint16objsubid,
9799
LOCKMODElockmode);
98100

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp