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

Commit 785c611

Browse files
knizhnikdmpgpro
authored andcommitted
Release deadlock detection flag in case of abnormal transaction termination
1 parent 7949cd7 commit 785c611

File tree

1 file changed

+19
-0
lines changed
  • src/backend/storage/lmgr

1 file changed

+19
-0
lines changed

src/backend/storage/lmgr/proc.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ static LOCALLOCK *lockAwaited = NULL;
8686

8787
static DeadLockState deadlock_state = DS_NOT_YET_CHECKED;
8888

89+
static bool inside_deadlock_check = false;
90+
8991
/* Is a deadlock check pending? */
9092
static volatile sig_atomic_t got_deadlock_timeout;
9193

@@ -755,6 +757,14 @@ ProcReleaseLocks(bool isCommit)
755757
{
756758
if (!MyProc)
757759
return;
760+
761+
/* Release deadlock detection flag is backend was interrupted inside deadlock check */
762+
if (inside_deadlock_check)
763+
{
764+
pg_atomic_clear_flag(&ProcGlobal->activeDeadlockCheck);
765+
inside_deadlock_check = false;
766+
}
767+
758768
/* If waiting, get off wait queue (should only be needed after error) */
759769
LockErrorCleanup();
760770
/* Release standard locks, including session-level if aborting */
@@ -1662,6 +1672,14 @@ CheckDeadLock(void)
16621672
if (!pg_atomic_test_set_flag(&ProcGlobal->activeDeadlockCheck))
16631673
return;
16641674

1675+
/*
1676+
* Ensure that only one backend is checking for deadlock.
1677+
* Otherwise under high load cascade of deadlock timeout expirations can cause stuck of Postgres.
1678+
*/
1679+
if (!pg_atomic_test_set_flag(&ProcGlobal->activeDeadlockCheck))
1680+
return;
1681+
inside_deadlock_check = true;
1682+
16651683
/*
16661684
* Acquire exclusive lock on the entire shared lock data structures. Must
16671685
* grab LWLocks in partition-number order to avoid LWLock deadlock.
@@ -1739,6 +1757,7 @@ CheckDeadLock(void)
17391757
LWLockRelease(LockHashPartitionLockByIndex(i));
17401758

17411759
pg_atomic_clear_flag(&ProcGlobal->activeDeadlockCheck);
1760+
inside_deadlock_check = false;
17421761
}
17431762

17441763
/*

0 commit comments

Comments
 (0)