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 1jJzPZ-0000zn-VZ for pgsql-hackers@arkaria.postgresql.org; Thu, 02 Apr 2020 12:57:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jJzPY-00070N-AF for pgsql-hackers@arkaria.postgresql.org; Thu, 02 Apr 2020 12:57:32 +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 1jJzPY-00070G-1W for pgsql-hackers@lists.postgresql.org; Thu, 02 Apr 2020 12:57:32 +0000 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jJzPQ-0002sp-Im for pgsql-hackers@lists.postgresql.org; Thu, 02 Apr 2020 12:57:31 +0000 Received: by mail-wr1-x442.google.com with SMTP id t7so4031710wrw.12 for ; Thu, 02 Apr 2020 05:57:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=4JGjxVlhh+Ybs6H0gz6WzJYSa5RgL2pBPIP6G1X/vu8=; b=k/Fri4ttOVVwcPAlXQdzGdbOSzp8RaQWHzhtAA21rtVZMD8XzDffavypzkgKTUerYs 35Hagb8IINCC6HNHX2a7sK8C6Sq9DI/ASWKPNoOVbvenhsQPlp7VZYGaT30B9k9oahA5 +TuGPLfLtjTE3IQhti3WCJlv7DCJ1Ui5AP5OQ3/T6Ap2CdVVtkqPg5pFeF0S0QlPLrFH uFx4cCXhvOsmaKJnR9oijsZtQS69g1psNIHpZt0g1yVF3d1RwPCXa4rGp7Pf09FwdXvB fuCjtTVTlNJ0kJFDI5wPTUO4SeSUgPEZSAC2f9aBttdn8OXyzG6pr0JhTDheZ7jbnsPt Lssw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=4JGjxVlhh+Ybs6H0gz6WzJYSa5RgL2pBPIP6G1X/vu8=; b=V3/4WtxvMhiUeckA+QqVgv82KppOw45Bzjew2wzeUcP+lVKaWE8+Y8TpqGlSAv3AmY 5yfEkiXMM/L5axVt5bgeA3A12/MaxbxjdiPU2C1HbDTXMJKunjZe+FaNRoVS7phcMJYs O6TVCV7QFJFjEiUeBKJcMIIQktykDkoFNkPyeMM6SGc7Osgnhe09eUKqSZI7QJZZSBKi BG5EDNmlvOloPIeZ6Wv8PFfIcPoLQwgxOx1Im69EnVhrH4YzkYZEKwdBdHFW/tk7YCws Py/irXd+ft9s8e/IGAyzPLKHzreVtZi8KyCjyyD7ZaevtsB1cRgan9dJeFcRGULFXMZ3 taPg== X-Gm-Message-State: AGi0PuaJWwLVXEpLBtn5Z8y9ZKxwN80CI8LWole11rMdxLBj7coH4eGw jF1VDUMQM1RX3rGnT6s0LFSKEg== X-Google-Smtp-Source: APiQypKC5Yr/9KPp3q4EL5sXL6TXBdcjzQLO7w15Z8P6IcNvbJIMkkJn3mZKrKxwPMEs4KjUWJMz+w== X-Received: by 2002:a5d:4305:: with SMTP id h5mr3292758wrq.69.1585832243596; Thu, 02 Apr 2020 05:57:23 -0700 (PDT) Received: from localhost (ip-86-49-253-141.net.upcbroadband.cz. [86.49.253.141]) by smtp.gmail.com with ESMTPSA id u22sm7180950wmu.43.2020.04.02.05.57.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Apr 2020 05:57:22 -0700 (PDT) Date: Thu, 2 Apr 2020 14:57:19 +0200 From: Tomas Vondra To: Tom Lane Cc: Dean Rasheed , David Rowley , Laurenz Albe , Andres Freund , Alexander Korotkov , Justin Pryzby , Alvaro Herrera , Masahiko Sawada , Amit Kapila , Masahiko Sawada , Darafei =?utf-8?Q?Kom=D1=8Fpa?= Praliaskouski , PostgreSQL Hackers , Michael Banck Subject: Re: Berserk Autovacuum (let's save next Mandrill) Message-ID: <20200402125719.h2js6xlofbx6ovpd@development> References: <61d1ab62a22ee7976b8d099dda8c8089fae74a9f.camel@cybertec.at> <490.1585687710@sss.pgh.pa.us> <30041.1585689399@sss.pgh.pa.us> <22297.1585797192@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <22297.1585797192@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, Apr 01, 2020 at 11:13:12PM -0400, Tom Lane wrote: >Dean Rasheed writes: >> Yeah, that makes sense. I still can't see what might be causing those >> failures. The tests that were doing an ALTER COLUMN and then expecting >> to see the results of a non-analysed table ought to be fixed by >> 0936d1b6f, but that doesn't match the buildfarm failures. Possibly >> 0936d1b6f will help with those anyway, but if so, it'll be annoying >> not understanding why. > >Quite :-(. While it's too early to declare victory, we've seen no >more failures of this ilk since 0936d1b6f, so it's sure looking like >autovacuum did have something to do with it. > >Just to save people repeating the search I did, these are the buildfarm >failures of interest so far: > >https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=lousyjack&dt=2020-03-28%2006%3A33%3A02 >https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=petalura&dt=2020-03-28%2009%3A20%3A05 >https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=petalura&dt=2020-03-28%2013%3A20%3A05 >https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=lousyjack&dt=2020-03-28%2020%3A03%3A03 >https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=grison&dt=2020-03-28%2022%3A00%3A19 >https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=hoverfly&dt=2020-03-29%2006%3A45%3A02 >https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=petalura&dt=2020-03-30%2002%3A20%3A03 >https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=xenodermus&dt=2020-03-30%2006%3A00%3A06 >https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=pogona&dt=2020-03-30%2006%3A10%3A05 >https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=pogona&dt=2020-03-30%2023%3A10%3A03 >https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=conchuela&dt=2020-03-31%2005%3A00%3A35 > >The first of those is unlike the rest, and I'm not trying to account for >it here. In the rest, what we see is that sometimes the estimates are off >by a little bit from what's expected, up or down just a percent or two. >And those deltas kick at inconsistent spots partway through a series of >similar tests, so it's hard to deny that *something* asynchronous to the >test script is causing it. > >After contemplating the failures for awhile, I have a theory that >at least partially matches the data. What I think is happening is >that autovacuum (NOT auto-analyze) launches on the table, and since >it is running concurrently with the foreground test script, it fails >to immediately acquire buffer lock on one or more of the table pages. >Since this isn't an aggressive vacuum scan, it just politely backs >off and doesn't scan those pages. And that translates to not getting >a perfectly accurate reltuples estimate at the end of the vacuum. >On my x86_64 machine, which matches the buildfarm critters having >trouble, the actual contents of both of the troublesome tables will >be 5000 tuples in 31 pages --- which comes out to be 30 pages with >162 tuples each and then 140 tuples in the last page. Working through >the math in vac_estimate_reltuples (and assuming that the "old" values >were accurate numbers from the test script's own ANALYZE), what I find >is that autovacuum will conclude there are 4999 tuples if it misses >scanning one of the first 30 pages, or 5021 tuples if it misses scanning >the last page, because its interpolation from the old tuple density >figure will underestimate or overestimate the number of missed tuples >accordingly. Once that slightly-off number gets pushed into pg_class, >we start to get slightly-off rowcount estimates in the test cases. > >So what I'm hypothesizing is that the pg_statistic data is perfectly >fine but pg_class.reltuples goes off a little bit after autovacuum. >The percentage changes in reltuples that I predict this way don't >quite square with the percentage changes we see in the overall >rowcount estimates, which is a problem for this theory. But the test >cases are exercising some fairly complex estimation logic, and it >wouldn't surprise me much if the estimates aren't linearly affected by >reltuples. (Tomas, do you want to comment further on that point?) > I think this theory makes perfect sense. I think it's much less likely to see the last page skipped, so we're likely to end up with reltuples lower than 5000 (as opposed to seeing the 5021). That kinda matches the reports, where we generally see estimates reduced by 1 or 2. The -1 change could be explained by rounding errors, I guess - with 5000 we might have produced 139.51, rounded up to 140, a slight drop may get us 139. Not sure about the -2 changes, but I suppose it's possible we might actually skip multiple pages, reducing the reltuples estimate even more? regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services