Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khIUt-0001dL-0L for pgsql-hackers@arkaria.postgresql.org; Mon, 23 Nov 2020 20:31:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1khIUr-00078e-U3 for pgsql-hackers@arkaria.postgresql.org; Mon, 23 Nov 2020 20:31:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khIUr-00078S-2n for pgsql-hackers@lists.postgresql.org; Mon, 23 Nov 2020 20:31:37 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khIUj-0002k7-JG for pgsql-hackers@lists.postgresql.org; Mon, 23 Nov 2020 20:31:36 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id 8149F5C0067; Mon, 23 Nov 2020 15:31:27 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Mon, 23 Nov 2020 15:31:27 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:subject:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=yC7DX0IkVcFoBDxpG Pdk62fbK0Eg7wSsuVv7D8nRV0E=; b=RHDhFpvryn/UYZGtg6I+Ty+8vF0aB2bsV F7+XQXkLLzKVCQmq4kxKn4Xr0mOtAME9yQhfw3BXAswZpepvlmBQtxGQBBq2TnGC 8k0RTY1YWZ29M6etjLJBkOubKajLg5C602C+QzyqbBctmfgKy94Cj0HkgFt4+Aur O1d5b0TQ1v1LiZ6tK5OhA5jW2o8mHrJ82QdNbCF/j9M+nrg2DiEzzilS4ekMv0vl faxG2F8t3ge6+NQo52QQlP2MfjC1bZ3F9oFqqvrmdlenHsSlTKm4T1YDMhKUsw3t DJryc+/beOAKvJThI/cYNU4ZDaB48xqqZEEaubx+FgekU1wsEriww== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudegjedgtdegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvffukfggtggujggfsehmtderredtredvnecuhfhrohhmpeetlhhvrghr ohcujfgvrhhrvghrrgcuoegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhgqe enucggtffrrghtthgvrhhnpeeigefhleeuudehgedtkeejheeuffdvudejgfetudeikeej hfdufeegfeeivdejteenucfkphepudeltddrleehrddukedrjeelnecuvehluhhsthgvrh fuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprghlvhhhvghrrhgvsegrlhhv hhdrnhhoqdhiphdrohhrgh X-ME-Proxy: Received: from perhan.alvh.no-ip.org (unknown [190.95.18.79]) by mail.messagingengine.com (Postfix) with ESMTPA id BD60F3064AA6; Mon, 23 Nov 2020 15:31:26 -0500 (EST) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 1B22F2A1006; Mon, 23 Nov 2020 17:31:24 -0300 (-03) Date: Mon, 23 Nov 2020 17:31:24 -0300 From: Alvaro Herrera To: Michael Paquier Cc: Andres Freund , Tom Lane , Dmitry Dolgov <9erthalion6@gmail.com>, James Coleman , Pg Hackers Subject: Re: "as quickly as possible" (was: remove spurious CREATE INDEX CONCURRENTLY wait) Message-ID: <20201123203124.GA8215@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="qMm9M+Fa2AknHoGS" Content-Disposition: inline In-Reply-To: <20201119033918.GF26172@paquier.xyz> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --qMm9M+Fa2AknHoGS Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On 2020-Nov-19, Michael Paquier wrote: > On Thu, Nov 19, 2020 at 12:13:44PM +0900, Michael Paquier wrote: > > That still looks useful for debugging, so DEBUG1 sounds fine to me. > > By the way, it strikes me that you could just do nothing as long as > (log_min_messages > DEBUG1), so you could encapsulate most of the > logic that plays with the lock tag using that. Good idea, done. I also noticed that if we're going to accept a race (which BTW already exists) we may as well simplify the code about it. I think the attached is the final form of this. --qMm9M+Fa2AknHoGS Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-Don-t-hold-ProcArrayLock-longer-than-needed-in-rare-.patch" From cab95d8813cd79adb7b2a1b5501714c2dd87bec2 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Tue, 17 Nov 2020 21:09:22 -0300 Subject: [PATCH] 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.pgsql Reviewed-by: Michael Paquier --- src/backend/storage/lmgr/proc.c | 63 ++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c index d1738c65f5..dbfdd7b3c7 100644 --- a/src/backend/storage/lmgr/proc.c +++ b/src/backend/storage/lmgr/proc.c @@ -1311,40 +1311,58 @@ ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable) { PGPROC *autovac = GetBlockingAutoVacuumPgproc(); uint8 statusFlags; + uint8 lockmethod_copy; + LOCKTAG locktag_copy; - LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE); + /* + * 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_SHARED); + 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 * wraparound. */ - statusFlags = ProcGlobal->statusFlags[autovac->pgxactoff]; if ((statusFlags & PROC_IS_AUTOVACUUM) && !(statusFlags & PROC_VACUUM_FOR_WRAPAROUND)) { int pid = autovac->pid; - StringInfoData locktagbuf; - StringInfoData logbuf; /* errdetail for server log */ - initStringInfo(&locktagbuf); - initStringInfo(&logbuf); - DescribeLockTag(&locktagbuf, &lock->tag); - appendStringInfo(&logbuf, - _("Process %d waits for %s on %s."), - MyProcPid, - GetLockmodeName(lock->tag.locktag_lockmethodid, - lockmode), - locktagbuf.data); + /* report the case, if configured to do so */ + if (log_min_messages <= DEBUG1 || client_min_messages <= DEBUG1) + { + StringInfoData locktagbuf; + StringInfoData logbuf; /* errdetail for server log */ - /* release lock as quickly as possible */ - LWLockRelease(ProcArrayLock); + initStringInfo(&locktagbuf); + initStringInfo(&logbuf); + DescribeLockTag(&locktagbuf, &locktag_copy); + appendStringInfo(&logbuf, + _("Process %d waits for %s on %s."), + MyProcPid, + GetLockmodeName(lockmethod_copy, lockmode), + locktagbuf.data); + + ereport(DEBUG1, + (errmsg("sending cancel to blocking autovacuum PID %d", + pid), + errdetail_log("%s", logbuf.data))); + + pfree(logbuf.data); + pfree(locktagbuf.data); + } /* send the autovacuum worker Back to Old Kent Road */ - ereport(DEBUG1, - (errmsg("sending cancel to blocking autovacuum PID %d", - pid), - errdetail_log("%s", logbuf.data))); - if (kill(pid, SIGINT) < 0) { /* @@ -1362,12 +1380,7 @@ ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable) (errmsg("could not send signal to process %d: %m", pid))); } - - pfree(logbuf.data); - pfree(locktagbuf.data); } - else - LWLockRelease(ProcArrayLock); /* prevent signal from being sent again more than once */ allow_autovacuum_cancel = false; -- 2.20.1 --qMm9M+Fa2AknHoGS--