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

Commit 26b4386

Browse files
committed
Only allow autovacuum to be auto-canceled by a directly blocked process.
In the original coding of the autovacuum cancel feature, commit acac68b, an autovacuum process was considered a target for cancellation if it was found to hard-block any process examined in the deadlock search. This patch tightens the test so that the autovacuum must directly hard-block the current process. This should make the behavior more predictable in general, and in particular it ensures that an autovacuum will not be canceled with less than deadlock_timeout grace period. In the old coding, it was possible for an autovacuum to be canceled almost instantly, given unfortunate timing of two or more other processes' lock attempts. This also justifies the logging methodology in the recent commit d7318d4; without this restriction, that patch isn't providing enough information to see the connection of the canceling process to the autovacuum. Like that one, patch all the way back.
1 parent d20cdd3 commit 26b4386

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

src/backend/storage/lmgr/deadlock.c

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -527,25 +527,6 @@ FindLockCycleRecurse(PGPROC *checkProc,
527527
if ((proclock->holdMask & LOCKBIT_ON(lm)) &&
528528
(conflictMask & LOCKBIT_ON(lm)))
529529
{
530-
/*
531-
* Look for a blocking autovacuum. There can be more than
532-
* one in the deadlock cycle, in which case we just pick a
533-
* random one. We stash the autovacuum worker's PGPROC so
534-
* that the caller can send a cancel signal to it, if
535-
* appropriate.
536-
*
537-
* Note we read vacuumFlags without any locking. This is
538-
* OK only for checking the PROC_IS_AUTOVACUUM flag,
539-
* because that flag is set at process start and never
540-
* reset; there is logic elsewhere to avoid canceling an
541-
* autovacuum that is working for preventing Xid
542-
* wraparound problems (which needs to read a different
543-
* vacuumFlag bit), but we don't do that here to avoid
544-
* grabbing ProcArrayLock.
545-
*/
546-
if (pgxact->vacuumFlags & PROC_IS_AUTOVACUUM)
547-
blocking_autovacuum_proc = proc;
548-
549530
/* This proc hard-blocks checkProc */
550531
if (FindLockCycleRecurse(proc, depth + 1,
551532
softEdges, nSoftEdges))
@@ -559,7 +540,34 @@ FindLockCycleRecurse(PGPROC *checkProc,
559540

560541
return true;
561542
}
562-
/* If no deadlock, we're done looking at this proclock */
543+
544+
/*
545+
* No deadlock here, but see if this proc is an autovacuum
546+
* that is directly hard-blocking our own proc. If so,
547+
* report it so that the caller can send a cancel signal
548+
* to it, if appropriate. If there's more than one such
549+
* proc, it's indeterminate which one will be reported.
550+
*
551+
* We don't touch autovacuums that are indirectly blocking
552+
* us; it's up to the direct blockee to take action. This
553+
* rule simplifies understanding the behavior and ensures
554+
* that an autovacuum won't be canceled with less than
555+
* deadlock_timeout grace period.
556+
*
557+
* Note we read vacuumFlags without any locking. This is
558+
* OK only for checking the PROC_IS_AUTOVACUUM flag,
559+
* because that flag is set at process start and never
560+
* reset. There is logic elsewhere to avoid canceling an
561+
* autovacuum that is working to prevent XID wraparound
562+
* problems (which needs to read a different vacuumFlag
563+
* bit), but we don't do that here to avoid grabbing
564+
* ProcArrayLock.
565+
*/
566+
if (checkProc == MyProc &&
567+
pgxact->vacuumFlags & PROC_IS_AUTOVACUUM)
568+
blocking_autovacuum_proc = proc;
569+
570+
/* We're done looking at this proclock */
563571
break;
564572
}
565573
}

0 commit comments

Comments
 (0)