Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g9o8a-0002tY-Nw for pgsql-hackers@arkaria.postgresql.org; Tue, 09 Oct 2018 09:17:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g9o8Y-0007mL-QW for pgsql-hackers@arkaria.postgresql.org; Tue, 09 Oct 2018 09:17:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g9o8Y-0007mD-JB for pgsql-hackers@lists.postgresql.org; Tue, 09 Oct 2018 09:17:06 +0000 Received: from tama500.ecl.ntt.co.jp ([129.60.39.148]) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g9o8U-00021C-CB for pgsql-hackers@lists.postgresql.org; Tue, 09 Oct 2018 09:17:05 +0000 Received: from vc2.ecl.ntt.co.jp (vc2.ecl.ntt.co.jp [129.60.86.154]) by tama500.ecl.ntt.co.jp (8.13.8/8.13.8) with ESMTP id w999GmO9025434; Tue, 9 Oct 2018 18:16:48 +0900 Received: from vc2.ecl.ntt.co.jp (localhost [127.0.0.1]) by vc2.ecl.ntt.co.jp (Postfix) with ESMTP id E732F638853; Tue, 9 Oct 2018 18:16:47 +0900 (JST) Received: from jcms-pop21.ecl.ntt.co.jp (jcms-pop21.ecl.ntt.co.jp [129.60.87.134]) by vc2.ecl.ntt.co.jp (Postfix) with ESMTP id C6EB8638832; Tue, 9 Oct 2018 18:16:47 +0900 (JST) Received: from localhost (unknown [129.60.241.55]) by jcms-pop21.ecl.ntt.co.jp (Postfix) with ESMTPSA id BDBC1400161; Tue, 9 Oct 2018 18:16:47 +0900 (JST) Date: Tue, 09 Oct 2018 18:15:36 +0900 (Tokyo Standard Time) Message-Id: <20181009.181536.142257785.horiguchi.kyotaro@lab.ntt.co.jp> Subject: Re: pgsql: Improve autovacuum logging for aggressive and anti-wraparound ru From: Kyotaro HORIGUCHI In-Reply-To: <20181005063504.GB14664@paquier.xyz> References: <20181005031603.GF1629@paquier.xyz> <20181005063504.GB14664@paquier.xyz> User-Agent: Mew version 6.5 on Emacs 22.2 / Mule 5.0 =?iso-2022-jp?B?KBskQjpnGyhCKQ==?= / Meadow-3.01-dev (TSUBO-SUMIRE) Mime-Version: 1.0 Content-Type: Multipart/Mixed; boundary="--Next_Part(Tue_Oct_09_18_15_36_2018_869)--" Content-Transfer-Encoding: 7bit X-CC-Mail-RelayStamp: 1 To: michael@paquier.xyz Cc: sawada.mshk@gmail.com, tgl@sss.pgh.pa.us, alvherre@2ndquadrant.com, sk@zsrv.org, nasbyj@amazon.com, andres@anarazel.de, robertmhaas@gmail.com, pgsql-hackers@lists.postgresql.org X-TM-AS-MML: disable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ----Next_Part(Tue_Oct_09_18_15_36_2018_869)-- Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit At Fri, 5 Oct 2018 15:35:04 +0900, Michael Paquier wrote in <20181005063504.GB14664@paquier.xyz> > On Fri, Oct 05, 2018 at 12:16:03PM +0900, Michael Paquier wrote: > > So, I have come back to this stuff, and finished with the attached > > instead, so as the assertion is in a single place. I find that > > clearer. The comments have also been improved. Thoughts? > > And so... I have been looking at committing this thing, and while > testing in-depth I have been able to trigger a case where an autovacuum > has been able to be not aggressive but anti-wraparound, which is exactly > what should not be possible, no? I have simply created an instance with > autovacuum_freeze_max_age = 200000, then ran pgbench with > autovacuum_freeze_table_age=200000 set for each table, and also ran > installcheck-world in parallel. This has been able to trigger the > assertion pretty quickly. I investigated it and in short, it can happen. It is a kind of race consdition between two autovacuum processes. do_autovacuum() looks into pg_class (using a snapshot) and vacuum_set_xid_limits() looks into relcache. If concurrent vacuum happens and one has finished the relation, another gets relcache invalidation and relfrozenxid is updated. If this happens between do_autovacuum() and vacuum_set_xid_limits(), the latter sees newer relfrozenxid than the former. The problem happens when it moves by more than 5% of autovacuum_freeze_max_age. If lazy_vacuum_rel() sees the situation, the relation is already aggressively vacuumed by a cocurrent worker. We can just ingore the state safely but also we know that the vacuum is useless. 1. Just allow the case there (and add comment). Causes redundant anti-wraparound vacuum. 2. Skip the relation by the condition. I think that we can safely skip the relation in the case. (attached) 3. Ensure that do_autovacuum always sees the same relfrozenxid with vacuum_set_xid_limits(). 4. Prevent concurrent acuuming of the same relation rigorously, somehow. Thoughts? regards. -- Kyotaro Horiguchi NTT Open Source Software Center ----Next_Part(Tue_Oct_09_18_15_36_2018_869)-- Content-Type: Text/X-Patch; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="skip_vacuum_after_concurrently_processed.patch" diff --git a/src/backend/commands/vacuumlazy.c b/src/backend/commands/vacuumlazy.c index 8996d366e9..436d573454 100644 --- a/src/backend/commands/vacuumlazy.c +++ b/src/backend/commands/vacuumlazy.c @@ -249,6 +249,21 @@ lazy_vacuum_rel(Relation onerel, int options, VacuumParams *params, if (options & VACOPT_DISABLE_PAGE_SKIPPING) aggressive = true; + /* + * When is_wraparound, relfrozenxid is old enough and aggressive must be + * true here. However, if another concurrent vacuum process has processed + * this relation, relfrozenxid in relcache can be moved forward enough so + * that aggressive is calculated as false here. This relation no longer + * needs to be vacuumed. Bail out. + */ + if (params->is_wraparound && !aggressive) + { + elog(DEBUG1, "relation %d has been vacuumd ocncurrently, skip", + onerel->rd_id); + return; + } + + vacrelstats = (LVRelStats *) palloc0(sizeof(LVRelStats)); vacrelstats->old_rel_pages = onerel->rd_rel->relpages; ----Next_Part(Tue_Oct_09_18_15_36_2018_869)----