Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dwBoF-000626-Sq for pgsql-hackers@arkaria.postgresql.org; Sun, 24 Sep 2017 18:39:19 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dwBoE-00029M-Vb for pgsql-hackers@arkaria.postgresql.org; Sun, 24 Sep 2017 18:39:19 +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.84_2) (envelope-from ) id 1dwBoE-00029C-3x for pgsql-hackers@postgresql.org; Sun, 24 Sep 2017 18:39:18 +0000 Received: from mail-wr0-x230.google.com ([2a00:1450:400c:c0c::230]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1dwBo5-0005sU-Hw for pgsql-hackers@postgresql.org; Sun, 24 Sep 2017 18:39:17 +0000 Received: by mail-wr0-x230.google.com with SMTP id l22so4507067wrc.10 for ; Sun, 24 Sep 2017 11:39:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=l/ChAsxvGdDAUZh4q2CbumdOeTThR0aA9aBj+nS2TqM=; b=xGbWItD34WdTi7qsq2mBIMvYI6oBIoAeoxjXOKWM6yB50mH9T7rLFRCQKSmY6k/4fk ieBz7LlkOJfuuJazV5hqimuj+cywKN7kXXsh6FYF2lH7x3IPc64VWxS6BiyN/2wZ1a/U IxeX5bwiLK1GrO+SbZjQEPk7fuv8FBQIj1besE2lrrsJmWLUa8knErBzIeirOrRgUBbA KSAL1iT6jj/yqVBYJWgxr4qD9o6gvSrVPEY+JpKZxNY1HTa2D0L3YNgBJfRqbTE92TFN qTcGpFjuOY0CsbUrnUItZUy+qNUAPPBq2PtZA7xkj3JZejNW7e4GUDkScQGJdrPsNtXy 2qEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=l/ChAsxvGdDAUZh4q2CbumdOeTThR0aA9aBj+nS2TqM=; b=mjE0jjTdYgRd+tlEtZo94dP9yJUtwxPAG6nEKRja08fdsgfXCIdh6MIJ4k7X4jI54Y YTAgQErpsQf60RNFi4vigVS2UK3wbpmYMCtFZSA+OEy22H1sh5nwIwb6vEznNLXBH7Gh ISaTKxp3zK+A639jMEoEJqRa6ul9PoYYT5JE9tx8kgdFOAkGDoVP+2DJecLd8CY6VHNT lSofB9wQ5m2V9mJYngSda2BBqMj+t9CHBWQEuFNm0IEQX7HoyvNY95ERVjNtEhqkWISm IhAitqTJMDLvQqv0QhJ8CjRBHOTjN9e49bCNBCwbLRgNL/RrpQpFYRBl+sIuMVQ4+WvS y0uA== X-Gm-Message-State: AHPjjUhofEO4yyJeJ7SwIJXxvfFxIyje/VmeKXspZnzOK+dRllGFLYay OOhjImrltiipBXOXQ64jSQUqOa0Fro4byUtrism2rII4fnsqo+9oa/DG8tTgQsm4ue4z9xs30Q5 N+rakueksgbfEjNjr9jIfJ/kZXild9FZj+DuVveb3i+GOBB0PeLr3s9UEOWlIv+fsTFSziMviYo vm98btXEL5 X-Google-Smtp-Source: AOwi7QAiDHbEHQzqEXuv/K4GzXOay7NhP/YE5JKC25SUiy8erZxXRkW7bmPYw87MV1HtZNWZ9j6bEw== X-Received: by 10.223.187.74 with SMTP id x10mr3849841wrg.66.1506278347952; Sun, 24 Sep 2017 11:39:07 -0700 (PDT) Received: from [10.137.2.19] (ip-78-102-97-226.net.upcbroadband.cz. [78.102.97.226]) by smtp.gmail.com with ESMTPSA id i50sm6226585wrf.84.2017.09.24.11.39.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 24 Sep 2017 11:39:06 -0700 (PDT) Subject: Re: VACUUM and ANALYZE disagreeing on what reltuples means To: Haribabu Kommi Cc: Tom Lane , "pgsql-hackers@postgresql.org" References: <16db4468-edfa-830a-f921-39a50498e77e@2ndquadrant.com> <19990.1500936945@sss.pgh.pa.us> <3d250597-384e-c5f4-7533-b513e366888f@2ndquadrant.com> From: Tomas Vondra Message-ID: <94f58897-861b-accf-a9f1-af4be816c0d3@2ndquadrant.com> Date: Sun, 24 Sep 2017 20:39:03 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------EEE680DB6805783DFA634517" Content-Language: en-US List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org This is a multi-part message in MIME format. --------------EEE680DB6805783DFA634517 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit On 09/06/2017 09:45 AM, Haribabu Kommi wrote: > > > On Tue, Jul 25, 2017 at 9:33 PM, Tomas Vondra > > wrote: > > On 7/25/17 12:55 AM, Tom Lane wrote: > > Tomas Vondra > writes: > > It seems to me that VACUUM and ANALYZE somewhat disagree on what > exactly reltuples means. VACUUM seems to be thinking that > reltuples > = live + dead while ANALYZE apparently believes that reltuples = > live > > > The question is - which of the reltuples definitions is the > right > one? I've always assumed that "reltuples = live + dead" but > perhaps > not? > > > I think the planner basically assumes that reltuples is the live > tuple count, so maybe we'd better change VACUUM to get in step. > > > Attached is a patch that (I think) does just that. The disagreement > was caused by VACUUM treating recently dead tuples as live, while > ANALYZE treats both of those as dead. > > At first I was worried that this will negatively affect plans in the > long-running transaction, as it will get underestimates (due to > reltuples not including rows it can see). But that's a problem we > already have anyway, you just need to run ANALYZE in the other session. > > > Thanks for the patch. > From the mail, I understand that this patch tries to improve the > reltuples value update in the catalog table by the vacuum command > to consider the proper visible tuples similar like analyze command. > > -num_tuples); > +num_tuples - nkeep); > > With the above correction, there is a problem in reporting the number > of live tuples to the stats. > > postgres=# select reltuples, n_live_tup, n_dead_tup >               from pg_stat_user_tables join pg_class using (relname) >              where relname = 't'; >  reltuples | n_live_tup | n_dead_tup  > -----------+------------+------------ >     899818 |     799636 |     100182 > (1 row) > > > The live tuples data value is again decremented with dead tuples > value before sending them to stats in function lazy_vacuum_rel(), > > /* report results to the stats collector, too */ > new_live_tuples = new_rel_tuples - vacrelstats->new_dead_tuples; > > The fix needs a correction here also. Or change the correction in  > lazy_vacuum_rel() function itself before updating catalog table similar > like stats. > Ah, haven't noticed that for some reason - you're right, we estimate the reltuples based on (num_tuples - nkeep), so it doesn't make much sense to subtract nkeep again. Attached is v2 of the fix. I've removed the subtraction from lazy_vacuum_rel(), leaving just new_live_tuples = new_rel_tuples; and now it behaves as expected (no second subtraction). That means we can get rid of new_live_tuples altogether (and the protection against negative values), and use new_rel_tuples directly. Which pretty much means that in this case (pg_class.reltuples == pg_stat_user_tables.n_live_tup) but I guess that's fine, based on the initial discussion in this thread. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --------------EEE680DB6805783DFA634517 Content-Type: text/x-patch; name="vacuum-reltuples-fix-v2.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="vacuum-reltuples-fix-v2.patch" diff --git a/src/backend/commands/vacuumlazy.c b/src/backend/commands/vacuumlazy.c index 45b1859..1886f0d 100644 --- a/src/backend/commands/vacuumlazy.c +++ b/src/backend/commands/vacuumlazy.c @@ -198,7 +198,6 @@ lazy_vacuum_rel(Relation onerel, int options, VacuumParams *params, BlockNumber new_rel_pages; double new_rel_tuples; BlockNumber new_rel_allvisible; - double new_live_tuples; TransactionId new_frozen_xid; MultiXactId new_min_multi; @@ -335,13 +334,9 @@ lazy_vacuum_rel(Relation onerel, int options, VacuumParams *params, false); /* report results to the stats collector, too */ - new_live_tuples = new_rel_tuples - vacrelstats->new_dead_tuples; - if (new_live_tuples < 0) - new_live_tuples = 0; /* just in case */ - pgstat_report_vacuum(RelationGetRelid(onerel), onerel->rd_rel->relisshared, - new_live_tuples, + new_rel_tuples, vacrelstats->new_dead_tuples); pgstat_progress_end_command(); @@ -1267,7 +1262,7 @@ lazy_scan_heap(Relation onerel, int options, LVRelStats *vacrelstats, vacrelstats->new_rel_tuples = vac_estimate_reltuples(onerel, false, nblocks, vacrelstats->tupcount_pages, - num_tuples); + num_tuples - nkeep); /* * Release any remaining pin on visibility map page. --------------EEE680DB6805783DFA634517 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --------------EEE680DB6805783DFA634517--