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

Commitf5d9212

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 parent8f8511d commitf5d9212

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
@@ -75,6 +75,7 @@
7575
#include"catalog/dependency.h"
7676
#include"catalog/namespace.h"
7777
#include"catalog/pg_database.h"
78+
#include"catalog/pg_namespace.h"
7879
#include"commands/dbcommands.h"
7980
#include"commands/vacuum.h"
8081
#include"lib/ilist.h"
@@ -2270,6 +2271,24 @@ do_autovacuum(void)
22702271
continue;
22712272
}
22722273

2274+
/*
2275+
* Try to lock the temp namespace, too. Even though we have lock on
2276+
* the table itself, there's a risk of deadlock against an incoming
2277+
* backend trying to clean out the temp namespace, in case this table
2278+
* has dependencies (such as sequences) that the backend's
2279+
* performDeletion call might visit in a different order. If we can
2280+
* get AccessShareLock on the namespace, that's sufficient to ensure
2281+
* we're not running concurrently with RemoveTempRelations. If we
2282+
* can't, back off and let RemoveTempRelations do its thing.
2283+
*/
2284+
if (!ConditionalLockDatabaseObject(NamespaceRelationId,
2285+
classForm->relnamespace,0,
2286+
AccessShareLock))
2287+
{
2288+
UnlockRelationOid(relid,AccessExclusiveLock);
2289+
continue;
2290+
}
2291+
22732292
/* OK, let's delete it */
22742293
ereport(LOG,
22752294
(errmsg("autovacuum: dropping orphan temp table \"%s.%s.%s\"",
@@ -2287,7 +2306,7 @@ do_autovacuum(void)
22872306

22882307
/*
22892308
* To commit the deletion, end current transaction and start a new
2290-
* one. Note this also releases thelock we took.
2309+
* one. Note this also releases thelocks we took.
22912310
*/
22922311
CommitTransactionCommand();
22932312
StartTransactionCommand();

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,44 @@ LockDatabaseObject(Oid classid, Oid objid, uint16 objsubid,
990990
AcceptInvalidationMessages();
991991
}
992992

993+
/*
994+
*ConditionalLockDatabaseObject
995+
*
996+
* As above, but only lock if we can get the lock without blocking.
997+
* Returns true iff the lock was acquired.
998+
*/
999+
bool
1000+
ConditionalLockDatabaseObject(Oidclassid,Oidobjid,uint16objsubid,
1001+
LOCKMODElockmode)
1002+
{
1003+
LOCKTAGtag;
1004+
LOCALLOCK*locallock;
1005+
LockAcquireResultres;
1006+
1007+
SET_LOCKTAG_OBJECT(tag,
1008+
MyDatabaseId,
1009+
classid,
1010+
objid,
1011+
objsubid);
1012+
1013+
res=LockAcquireExtended(&tag,lockmode, false, true, true,&locallock);
1014+
1015+
if (res==LOCKACQUIRE_NOT_AVAIL)
1016+
return false;
1017+
1018+
/*
1019+
* Now that we have the lock, check for invalidation messages; see notes
1020+
* in LockRelationOid.
1021+
*/
1022+
if (res!=LOCKACQUIRE_ALREADY_CLEAR)
1023+
{
1024+
AcceptInvalidationMessages();
1025+
MarkLockClear(locallock);
1026+
}
1027+
1028+
return true;
1029+
}
1030+
9931031
/*
9941032
*UnlockDatabaseObject
9951033
*/

‎src/include/storage/lmgr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ extern void SpeculativeInsertionWait(TransactionId xid, uint32 token);
9292
/* Lock a general object (other than a relation) of the current database */
9393
externvoidLockDatabaseObject(Oidclassid,Oidobjid,uint16objsubid,
9494
LOCKMODElockmode);
95+
externboolConditionalLockDatabaseObject(Oidclassid,Oidobjid,
96+
uint16objsubid,LOCKMODElockmode);
9597
externvoidUnlockDatabaseObject(Oidclassid,Oidobjid,uint16objsubid,
9698
LOCKMODElockmode);
9799

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp