Skip to content

Commit faa2cbe

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 6379156 commit faa2cbe

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
@@ -525,25 +525,6 @@ FindLockCycleRecurse(PGPROC *checkProc,
525525
if ((proclock->holdMask & LOCKBIT_ON(lm)) &&
526526
(conflictMask & LOCKBIT_ON(lm)))
527527
{
528-
/*
529-
* Look for a blocking autovacuum. There can be more than
530-
* one in the deadlock cycle, in which case we just pick a
531-
* random one. We stash the autovacuum worker's PGPROC so
532-
* that the caller can send a cancel signal to it, if
533-
* appropriate.
534-
*
535-
* Note we read vacuumFlags without any locking. This is
536-
* OK only for checking the PROC_IS_AUTOVACUUM flag,
537-
* because that flag is set at process start and never
538-
* reset; there is logic elsewhere to avoid cancelling an
539-
* autovacuum that is working for preventing Xid
540-
* wraparound problems (which needs to read a different
541-
* vacuumFlag bit), but we don't do that here to avoid
542-
* grabbing ProcArrayLock.
543-
*/
544-
if (proc->vacuumFlags & PROC_IS_AUTOVACUUM)
545-
blocking_autovacuum_proc = proc;
546-
547528
/* This proc hard-blocks checkProc */
548529
if (FindLockCycleRecurse(proc, depth + 1,
549530
softEdges, nSoftEdges))
@@ -557,7 +538,34 @@ FindLockCycleRecurse(PGPROC *checkProc,
557538

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

0 commit comments

Comments
 (0)