Skip to content

Commit 0307b98

Browse files
committed
Detect the deadlocks between backends and the startup process.
The deadlocks that the recovery conflict on lock is involved in can happen between hot-standby backends and the startup process. If a backend takes an access exclusive lock on the table and which finally triggers the deadlock, that deadlock can be detected as expected. On the other hand, previously, if the startup process took an access exclusive lock and which finally triggered the deadlock, that deadlock could not be detected and could remain even after deadlock_timeout passed. This is a bug. The cause of this bug was that the code for handling the recovery conflict on lock didn't take care of deadlock case at all. It assumed that deadlocks involving the startup process and backends were able to be detected by the deadlock detector invoked within backends. But this assumption was incorrect. The startup process also should have invoked the deadlock detector if necessary. To fix this bug, this commit makes the startup process invoke the deadlock detector if deadlock_timeout is reached while handling the recovery conflict on lock. Specifically, in that case, the startup process requests all the backends holding the conflicting locks to check themselves for deadlocks. Back-patch to v9.6. v9.5 has also this bug, but per discussion we decided not to back-patch the fix to v9.5. Because v9.5 doesn't have some infrastructure codes (e.g., 37c5486) that this bug fix patch depends on. We can apply those codes for the back-patch, but since the next minor version release is the final one for v9.5, it's risky to do that. If we unexpectedly introduce new bug to v9.5 by the back-patch, there is no chance to fix that. We determined that the back-patch to v9.5 would give more risk than gain. Author: Fujii Masao Reviewed-by: Bertrand Drouvot, Masahiko Sawada, Kyotaro Horiguchi Discussion: https://postgr.es/m/4041d6b6-cf24-a120-36fa-1294220f8243@oss.nttdata.com
1 parent 72c2187 commit 0307b98

File tree

5 files changed

+141
-32
lines changed

5 files changed

+141
-32
lines changed

src/backend/storage/ipc/procarray.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -2651,6 +2651,13 @@ GetConflictingVirtualXIDs(TransactionId limitXmin, Oid dbOid)
26512651
*/
26522652
pid_t
26532653
CancelVirtualTransaction(VirtualTransactionId vxid, ProcSignalReason sigmode)
2654+
{
2655+
return SignalVirtualTransaction(vxid, sigmode, true);
2656+
}
2657+
2658+
pid_t
2659+
SignalVirtualTransaction(VirtualTransactionId vxid, ProcSignalReason sigmode,
2660+
bool conflictPending)
26542661
{
26552662
ProcArrayStruct *arrayP = procArray;
26562663
int index;
@@ -2669,7 +2676,7 @@ CancelVirtualTransaction(VirtualTransactionId vxid, ProcSignalReason sigmode)
26692676
if (procvxid.backendId == vxid.backendId &&
26702677
procvxid.localTransactionId == vxid.localTransactionId)
26712678
{
2672-
proc->recoveryConflictPending = true;
2679+
proc->recoveryConflictPending = conflictPending;
26732680
pid = proc->pid;
26742681
if (pid != 0)
26752682
{

src/backend/storage/ipc/standby.c

+114-29
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ int max_standby_streaming_delay = 30 * 1000;
4141

4242
static HTAB *RecoveryLockLists;
4343

44+
/* Flags set by timeout handlers */
45+
static volatile sig_atomic_t got_standby_deadlock_timeout = false;
46+
static volatile sig_atomic_t got_standby_lock_timeout = false;
47+
4448
static void ResolveRecoveryConflictWithVirtualXIDs(VirtualTransactionId *waitlist,
4549
ProcSignalReason reason, bool report_waiting);
4650
static void SendRecoveryConflictWithBufferPin(ProcSignalReason reason);
@@ -388,8 +392,10 @@ ResolveRecoveryConflictWithDatabase(Oid dbid)
388392
* lock. As we are already queued to be granted the lock, no new lock
389393
* requests conflicting with ours will be granted in the meantime.
390394
*
391-
* Deadlocks involving the Startup process and an ordinary backend process
392-
* will be detected by the deadlock detector within the ordinary backend.
395+
* We also must check for deadlocks involving the Startup process and
396+
* hot-standby backend processes. If deadlock_timeout is reached in
397+
* this function, all the backends holding the conflicting locks are
398+
* requested to check themselves for deadlocks.
393399
*/
394400
void
395401
ResolveRecoveryConflictWithLock(LOCKTAG locktag)
@@ -400,7 +406,7 @@ ResolveRecoveryConflictWithLock(LOCKTAG locktag)
400406

401407
ltime = GetStandbyLimitTime();
402408

403-
if (GetCurrentTimestamp() >= ltime)
409+
if (GetCurrentTimestamp() >= ltime && ltime != 0)
404410
{
405411
/*
406412
* We're already behind, so clear a path as quickly as possible.
@@ -421,26 +427,85 @@ ResolveRecoveryConflictWithLock(LOCKTAG locktag)
421427
else
422428
{
423429
/*
424-
* Wait (or wait again) until ltime
430+
* Wait (or wait again) until ltime, and check for deadlocks as well
431+
* if we will be waiting longer than deadlock_timeout
425432
*/
426-
EnableTimeoutParams timeouts[1];
433+
EnableTimeoutParams timeouts[2];
434+
int cnt = 0;
435+
436+
if (ltime != 0)
437+
{
438+
got_standby_lock_timeout = false;
439+
timeouts[cnt].id = STANDBY_LOCK_TIMEOUT;
440+
timeouts[cnt].type = TMPARAM_AT;
441+
timeouts[cnt].fin_time = ltime;
442+
cnt++;
443+
}
427444

428-
timeouts[0].id = STANDBY_LOCK_TIMEOUT;
429-
timeouts[0].type = TMPARAM_AT;
430-
timeouts[0].fin_time = ltime;
431-
enable_timeouts(timeouts, 1);
445+
got_standby_deadlock_timeout = false;
446+
timeouts[cnt].id = STANDBY_DEADLOCK_TIMEOUT;
447+
timeouts[cnt].type = TMPARAM_AFTER;
448+
timeouts[cnt].delay_ms = DeadlockTimeout;
449+
cnt++;
450+
451+
enable_timeouts(timeouts, cnt);
432452
}
433453

434454
/* Wait to be signaled by the release of the Relation Lock */
435455
ProcWaitForSignal();
436456

457+
/*
458+
* Exit if ltime is reached. Then all the backends holding conflicting
459+
* locks will be canceled in the next ResolveRecoveryConflictWithLock()
460+
* call.
461+
*/
462+
if (got_standby_lock_timeout)
463+
goto cleanup;
464+
465+
if (got_standby_deadlock_timeout)
466+
{
467+
VirtualTransactionId *backends;
468+
469+
backends = GetLockConflicts(&locktag, AccessExclusiveLock);
470+
471+
/* Quick exit if there's no work to be done */
472+
if (!VirtualTransactionIdIsValid(*backends))
473+
goto cleanup;
474+
475+
/*
476+
* Send signals to all the backends holding the conflicting locks, to
477+
* ask them to check themselves for deadlocks.
478+
*/
479+
while (VirtualTransactionIdIsValid(*backends))
480+
{
481+
SignalVirtualTransaction(*backends,
482+
PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK,
483+
false);
484+
backends++;
485+
}
486+
487+
/*
488+
* Wait again here to be signaled by the release of the Relation Lock,
489+
* to prevent the subsequent RecoveryConflictWithLock() from causing
490+
* deadlock_timeout and sending a request for deadlocks check again.
491+
* Otherwise the request continues to be sent every deadlock_timeout
492+
* until the relation locks are released or ltime is reached.
493+
*/
494+
got_standby_deadlock_timeout = false;
495+
ProcWaitForSignal();
496+
}
497+
498+
cleanup:
499+
437500
/*
438501
* Clear any timeout requests established above. We assume here that the
439502
* Startup process doesn't have any other outstanding timeouts than those
440503
* used by this function. If that stops being true, we could cancel the
441504
* timeouts individually, but that'd be slower.
442505
*/
443506
disable_all_timeouts(false);
507+
got_standby_lock_timeout = false;
508+
got_standby_deadlock_timeout = false;
444509
}
445510

446511
/*
@@ -479,15 +544,7 @@ ResolveRecoveryConflictWithBufferPin(void)
479544

480545
ltime = GetStandbyLimitTime();
481546

482-
if (ltime == 0)
483-
{
484-
/*
485-
* We're willing to wait forever for conflicts, so set timeout for
486-
* deadlock check only
487-
*/
488-
enable_timeout_after(STANDBY_DEADLOCK_TIMEOUT, DeadlockTimeout);
489-
}
490-
else if (GetCurrentTimestamp() >= ltime)
547+
if (GetCurrentTimestamp() >= ltime && ltime != 0)
491548
{
492549
/*
493550
* We're already behind, so clear a path as quickly as possible.
@@ -501,26 +558,55 @@ ResolveRecoveryConflictWithBufferPin(void)
501558
* waiting longer than deadlock_timeout
502559
*/
503560
EnableTimeoutParams timeouts[2];
561+
int cnt = 0;
504562

505-
timeouts[0].id = STANDBY_TIMEOUT;
506-
timeouts[0].type = TMPARAM_AT;
507-
timeouts[0].fin_time = ltime;
508-
timeouts[1].id = STANDBY_DEADLOCK_TIMEOUT;
509-
timeouts[1].type = TMPARAM_AFTER;
510-
timeouts[1].delay_ms = DeadlockTimeout;
511-
enable_timeouts(timeouts, 2);
563+
if (ltime != 0)
564+
{
565+
timeouts[cnt].id = STANDBY_TIMEOUT;
566+
timeouts[cnt].type = TMPARAM_AT;
567+
timeouts[cnt].fin_time = ltime;
568+
cnt++;
569+
}
570+
571+
got_standby_deadlock_timeout = false;
572+
timeouts[cnt].id = STANDBY_DEADLOCK_TIMEOUT;
573+
timeouts[cnt].type = TMPARAM_AFTER;
574+
timeouts[cnt].delay_ms = DeadlockTimeout;
575+
cnt++;
576+
577+
enable_timeouts(timeouts, cnt);
512578
}
513579

514580
/* Wait to be signaled by UnpinBuffer() */
515581
ProcWaitForSignal();
516582

583+
if (got_standby_deadlock_timeout)
584+
{
585+
/*
586+
* Send out a request for hot-standby backends to check themselves for
587+
* deadlocks.
588+
*
589+
* XXX The subsequent ResolveRecoveryConflictWithBufferPin() will wait
590+
* to be signaled by UnpinBuffer() again and send a request for
591+
* deadlocks check if deadlock_timeout happens. This causes the
592+
* request to continue to be sent every deadlock_timeout until the
593+
* buffer is unpinned or ltime is reached. This would increase the
594+
* workload in the startup process and backends. In practice it may
595+
* not be so harmful because the period that the buffer is kept pinned
596+
* is basically no so long. But we should fix this?
597+
*/
598+
SendRecoveryConflictWithBufferPin(
599+
PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK);
600+
}
601+
517602
/*
518603
* Clear any timeout requests established above. We assume here that the
519604
* Startup process doesn't have any other timeouts than what this function
520605
* uses. If that stops being true, we could cancel the timeouts
521606
* individually, but that'd be slower.
522607
*/
523608
disable_all_timeouts(false);
609+
got_standby_deadlock_timeout = false;
524610
}
525611

526612
static void
@@ -580,13 +666,12 @@ CheckRecoveryConflictDeadlock(void)
580666

581667
/*
582668
* StandbyDeadLockHandler() will be called if STANDBY_DEADLOCK_TIMEOUT
583-
* occurs before STANDBY_TIMEOUT. Send out a request for hot-standby
584-
* backends to check themselves for deadlocks.
669+
* occurs before STANDBY_TIMEOUT.
585670
*/
586671
void
587672
StandbyDeadLockHandler(void)
588673
{
589-
SendRecoveryConflictWithBufferPin(PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK);
674+
got_standby_deadlock_timeout = true;
590675
}
591676

592677
/*
@@ -605,11 +690,11 @@ StandbyTimeoutHandler(void)
605690

606691
/*
607692
* StandbyLockTimeoutHandler() will be called if STANDBY_LOCK_TIMEOUT is exceeded.
608-
* This doesn't need to do anything, simply waking up is enough.
609693
*/
610694
void
611695
StandbyLockTimeoutHandler(void)
612696
{
697+
got_standby_lock_timeout = true;
613698
}
614699

615700
/*

src/backend/storage/lmgr/proc.c

+3
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,9 @@ CheckDeadLockAlert(void)
17191719
* Have to set the latch again, even if handle_sig_alarm already did. Back
17201720
* then got_deadlock_timeout wasn't yet set... It's unlikely that this
17211721
* ever would be a problem, but setting a set latch again is cheap.
1722+
*
1723+
* Note that, when this function runs inside procsignal_sigusr1_handler(),
1724+
* the handler function sets the latch again after the latch is set here.
17221725
*/
17231726
SetLatch(MyLatch);
17241727
errno = save_errno;

src/backend/tcop/postgres.c

+14-2
Original file line numberDiff line numberDiff line change
@@ -2760,11 +2760,23 @@ RecoveryConflictInterrupt(ProcSignalReason reason)
27602760
case PROCSIG_RECOVERY_CONFLICT_BUFFERPIN:
27612761

27622762
/*
2763-
* If we aren't blocking the Startup process there is nothing
2764-
* more to do.
2763+
* If PROCSIG_RECOVERY_CONFLICT_BUFFERPIN is requested but we
2764+
* aren't blocking the Startup process there is nothing more
2765+
* to do.
2766+
*
2767+
* When PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK is
2768+
* requested, if we're waiting for locks and the startup
2769+
* process is not waiting for buffer pin (i.e., also waiting
2770+
* for locks), we set the flag so that ProcSleep() will check
2771+
* for deadlocks.
27652772
*/
27662773
if (!HoldingBufferPinThatDelaysRecovery())
2774+
{
2775+
if (reason == PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK &&
2776+
GetStartupBufferPinWaitBufId() < 0)
2777+
CheckDeadLockAlert();
27672778
return;
2779+
}
27682780

27692781
MyProc->recoveryConflictPending = true;
27702782

src/include/storage/procarray.h

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ extern VirtualTransactionId *GetCurrentVirtualXIDs(TransactionId limitXmin,
7070
int *nvxids);
7171
extern VirtualTransactionId *GetConflictingVirtualXIDs(TransactionId limitXmin, Oid dbOid);
7272
extern pid_t CancelVirtualTransaction(VirtualTransactionId vxid, ProcSignalReason sigmode);
73+
extern pid_t SignalVirtualTransaction(VirtualTransactionId vxid, ProcSignalReason sigmode,
74+
bool conflictPending);
7375

7476
extern bool MinimumActiveBackends(int min);
7577
extern int CountDBBackends(Oid databaseid);

0 commit comments

Comments
 (0)