Commit 450c8230 authored by Alvaro Herrera's avatar Alvaro Herrera

Don't hold ProcArrayLock longer than needed in rare cases

While cancelling an autovacuum worker, we hold ProcArrayLock while
formatting a debugging log string.  We can make this shorter by saving
the data we need to produce the message and doing the formatting outside
the locked region.

This isn't terribly critical, as it only occurs pretty rarely: when a
backend runs deadlock detection and it happens to be blocked by a
autovacuum running autovacuum.  Still, there's no need to cause a hiccup
in ProcArrayLock processing, which can be very high-traffic in some
cases.

While at it, rework code so that we only print the string when it is
really going to be used, as suggested by Michael Paquier.

Discussion: https://postgr.es/m/20201118214127.GA3179@alvherre.pgsqlReviewed-by: default avatarMichael Paquier <michael@paquier.xyz>
parent 0cc99327
...@@ -1311,14 +1311,28 @@ ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable) ...@@ -1311,14 +1311,28 @@ ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable)
{ {
PGPROC *autovac = GetBlockingAutoVacuumPgproc(); PGPROC *autovac = GetBlockingAutoVacuumPgproc();
uint8 statusFlags; uint8 statusFlags;
uint8 lockmethod_copy;
LOCKTAG locktag_copy;
/*
* Grab info we need, then release lock immediately. Note this
* coding means that there is a tiny chance that the process
* terminates its current transaction and starts a different one
* before we have a change to send the signal; the worst possible
* consequence is that a for-wraparound vacuum is cancelled. But
* that could happen in any case unless we were to do kill() with
* the lock held, which is much more undesirable.
*/
LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE); LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
statusFlags = ProcGlobal->statusFlags[autovac->pgxactoff];
lockmethod_copy = lock->tag.locktag_lockmethodid;
locktag_copy = lock->tag;
LWLockRelease(ProcArrayLock);
/* /*
* Only do it if the worker is not working to protect against Xid * Only do it if the worker is not working to protect against Xid
* wraparound. * wraparound.
*/ */
statusFlags = ProcGlobal->statusFlags[autovac->pgxactoff];
if ((statusFlags & PROC_IS_AUTOVACUUM) && if ((statusFlags & PROC_IS_AUTOVACUUM) &&
!(statusFlags & PROC_VACUUM_FOR_WRAPAROUND)) !(statusFlags & PROC_VACUUM_FOR_WRAPAROUND))
{ {
...@@ -1326,25 +1340,25 @@ ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable) ...@@ -1326,25 +1340,25 @@ ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable)
StringInfoData locktagbuf; StringInfoData locktagbuf;
StringInfoData logbuf; /* errdetail for server log */ StringInfoData logbuf; /* errdetail for server log */
/* report the case, if configured to do so */
initStringInfo(&locktagbuf); initStringInfo(&locktagbuf);
initStringInfo(&logbuf); initStringInfo(&logbuf);
DescribeLockTag(&locktagbuf, &lock->tag); DescribeLockTag(&locktagbuf, &locktag_copy);
appendStringInfo(&logbuf, appendStringInfo(&logbuf,
_("Process %d waits for %s on %s."), _("Process %d waits for %s on %s."),
MyProcPid, MyProcPid,
GetLockmodeName(lock->tag.locktag_lockmethodid, GetLockmodeName(lockmethod_copy, lockmode),
lockmode),
locktagbuf.data); locktagbuf.data);
/* release lock as quickly as possible */
LWLockRelease(ProcArrayLock);
/* send the autovacuum worker Back to Old Kent Road */
ereport(DEBUG1, ereport(DEBUG1,
(errmsg("sending cancel to blocking autovacuum PID %d", (errmsg("sending cancel to blocking autovacuum PID %d",
pid), pid),
errdetail_log("%s", logbuf.data))); errdetail_log("%s", logbuf.data)));
pfree(logbuf.data);
pfree(locktagbuf.data);
/* send the autovacuum worker Back to Old Kent Road */
if (kill(pid, SIGINT) < 0) if (kill(pid, SIGINT) < 0)
{ {
/* /*
...@@ -1362,12 +1376,7 @@ ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable) ...@@ -1362,12 +1376,7 @@ ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable)
(errmsg("could not send signal to process %d: %m", (errmsg("could not send signal to process %d: %m",
pid))); pid)));
} }
pfree(logbuf.data);
pfree(locktagbuf.data);
} }
else
LWLockRelease(ProcArrayLock);
/* prevent signal from being sent again more than once */ /* prevent signal from being sent again more than once */
allow_autovacuum_cancel = false; allow_autovacuum_cancel = false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment