Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f3MmZ-00066e-77 for pgsql-general@arkaria.postgresql.org; Tue, 03 Apr 2018 14:19:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f3MmY-0007cc-7P for pgsql-general@arkaria.postgresql.org; Tue, 03 Apr 2018 14:19:30 +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 1f3MmX-0007cT-Vx for pgsql-general@lists.postgresql.org; Tue, 03 Apr 2018 14:19:30 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f3MmT-0000IY-RR for pgsql-general@lists.postgresql.org; Tue, 03 Apr 2018 14:19:29 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w33EJN0a003273; Tue, 3 Apr 2018 10:19:23 -0400 From: Tom Lane To: Tomas Vondra cc: Ranjith Ramachandra , pgsql-general@lists.postgresql.org Subject: Re: Seems like there is an issue with reltuples showing twice the number of rows In-reply-to: <7d986cf7-157e-b8b3-0b01-27e96797af71@2ndquadrant.com> References: <7d986cf7-157e-b8b3-0b01-27e96797af71@2ndquadrant.com> Comments: In-reply-to Tomas Vondra message dated "Tue, 03 Apr 2018 13:02:44 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <3271.1522765163.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Tue, 03 Apr 2018 10:19:23 -0400 Message-ID: <3272.1522765163@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Tomas Vondra writes: > On 04/03/2018 11:14 AM, Ranjith Ramachandra wrote: >> it returns >>  reltuples  | n_live_tup | n_dead_tup >> -------------+------------+------------ >> 2.7209e+06 |    1360448 |    1360448 >> >> If I run analyze main_csv_0f247511f5c247c8813ba3cec90c6ae1_yf34fbb38d >> and I run the same query again, >>   reltuples  | n_live_tup | n_dead_tup >> -------------+------------+------------ >>  1.36045e+06 |    1360448 |    1360448 >> >> But after some time the value goes back to being double the value. > There was a difference between VACUUM and ANALYZE in handling recently > dead rows (essentially deleted rows that can't be removed yet), causing > similar changes to reltuples. Essentially if you do VACUUM and ANALYZE, > it may set reltuples to rather different estimates. That is fixed now > and should be in the next minor release. No, I think this is the *other* thing we fixed recently: VACUUM thinks it should set reltuples to total tuples (live + dead) whereas ANALYZE counts only live tuples. We did not risk back-patching that. The question I'd ask about this case is why is there persistently 100% bloat? Those dead tuples should've gotten reclaimed by autovacuum. Perhaps an open prepared transaction, or some such? > It's probably better to use n_live_tup instead, though. I'd say that's > closer to the "live tuples" definition. Yeah, you might be better off looking at that, particularly since it updates on-the-fly not just after a vacuum or analyze. regards, tom lane