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

Commitca392df

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 parent0de5274 commitca392df

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"
@@ -2293,6 +2294,24 @@ do_autovacuum(void)
22932294
continue;
22942295
}
22952296

2297+
/*
2298+
* Try to lock the temp namespace, too. Even though we have lock on
2299+
* the table itself, there's a risk of deadlock against an incoming
2300+
* backend trying to clean out the temp namespace, in case this table
2301+
* has dependencies (such as sequences) that the backend's
2302+
* performDeletion call might visit in a different order. If we can
2303+
* get AccessShareLock on the namespace, that's sufficient to ensure
2304+
* we're not running concurrently with RemoveTempRelations. If we
2305+
* can't, back off and let RemoveTempRelations do its thing.
2306+
*/
2307+
if (!ConditionalLockDatabaseObject(NamespaceRelationId,
2308+
classForm->relnamespace,0,
2309+
AccessShareLock))
2310+
{
2311+
UnlockRelationOid(relid,AccessExclusiveLock);
2312+
continue;
2313+
}
2314+
22962315
/* OK, let's delete it */
22972316
ereport(LOG,
22982317
(errmsg("autovacuum: dropping orphan temp table \"%s.%s.%s\"",
@@ -2310,7 +2329,7 @@ do_autovacuum(void)
23102329

23112330
/*
23122331
* To commit the deletion, end current transaction and start a new
2313-
* one. Note this also releases thelock we took.
2332+
* one. Note this also releases thelocks we took.
23142333
*/
23152334
CommitTransactionCommand();
23162335
StartTransactionCommand();

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

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

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

‎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