Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dwCk7-000192-FD for pgsql-hackers@arkaria.postgresql.org; Sun, 24 Sep 2017 19:39:07 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dwCk5-0001GK-S7 for pgsql-hackers@arkaria.postgresql.org; Sun, 24 Sep 2017 19:39:05 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dwCiW-0006xO-Lv for pgsql-hackers@postgresql.org; Sun, 24 Sep 2017 19:37:28 +0000 Received: from mail-wm0-x229.google.com ([2a00:1450:400c:c09::229]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1dwCiS-0002K9-Ty for pgsql-hackers@postgresql.org; Sun, 24 Sep 2017 19:37:26 +0000 Received: by mail-wm0-x229.google.com with SMTP id b195so14278679wmb.5 for ; Sun, 24 Sep 2017 12:37:24 -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:content-transfer-encoding; bh=0URwzu8WrEJ7JaoRUGWJR2jTnaA0fk5AZ6BqdprxKwU=; b=sDIRkGoK37PieFLDRMJ8zaeBXvSn4mNA9JEFIj8GwwTleh59JU+JTdi1UwZ1mDYoI+ +uZmyn1tIjlpC9hrduTT+kinX+d2jzythXImOqUTAai5kikJu5dJseN5Jtw/7rzslLK1 OFZiY1b2YA/t8yofkWYEZXPkwuikcFVieVXZo0T+46mQHrhvK/8CEuS2445/siou4f2w iAatb/5NB6t8NXrSLKOG3pxqPp8Ekq3AMvIhWjMkRaOo6zV8JlwP3cqXQ1VQ4ftWa3/w mLaHsoybgt75QvdrLhyoPgIziaX1VgghTOC3Q8q58a5dngErMkgK8sNd9sz31xF1RCu0 uGzQ== 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 :content-transfer-encoding; bh=0URwzu8WrEJ7JaoRUGWJR2jTnaA0fk5AZ6BqdprxKwU=; b=lB2pToAB3eG+N0FilTP0DqeHJfv0wZcTehW2euwiUOq08bR7bUemEcWWFo+xKlq3hf 3lhJHjezcKO/newQcSxFPur/qWFDsgLda2ckFDukfVX+xLWKS1dZKSAYIoDkgaHlhnF5 aQuFIZzK5nbY2mu0x+g2qXFZIurSnGUFfno3CHkF9uPWXwQ5chLoNn8nwtUl8+OGEyaE odJn4wtM9Hno6NX/ypKOXX8X79zcdWkA1DY+9+7JfW6co6uaLbeUV4vl5zTobhGWaYWJ IjcBCo2GSXjL+/9g4MP4E8Aq8kNfjkP7fwTNMHieyGreMHexbVk1KtnttVLdCxgRYvVX 6Nwg== X-Gm-Message-State: AHPjjUi2FuqDDpGCHxL9j6P/UfOVa0gMZ46gaX+nk3Qb9zKF6XGipMqP 8t/P0rR9luDqHHBR4LMpHVFmyZYjAuqHdve8HiWyb7D/wGbxvHRS3Kjj6YCtnz4cEj/U21UD9sY VRf4JwxYsujZMqg6Hzr9WqoJHhxIjntoNYrTsX9feiHKYlUDWVqCNymQxQzrbK3OjFmG5DxdfKx Sj8C7yOBem X-Google-Smtp-Source: AOwi7QAkZAQylXuXF0USJWNusWePru9NeoC870GXHxh95BPtuQPPWCLmfi9vdYlDuHdHsow5FLFBXg== X-Received: by 10.28.29.129 with SMTP id d123mr8551258wmd.130.1506281842020; Sun, 24 Sep 2017 12:37:22 -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 o59sm9647479wrc.45.2017.09.24.12.37.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 24 Sep 2017 12:37:20 -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: Date: Sun, 24 Sep 2017 21:37:17 +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: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit 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 Hi, Apologies, I forgot to respond to the second part of your message. On 09/06/2017 09:45 AM, Haribabu Kommi wrote: > > While testing this patch, I found another problem that is not related to > this patch. When the vacuum command is executed mutiple times on > a table with no dead rows, the number of reltuples value is slowly > reducing. > > 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  > -----------+------------+------------ >     899674 |     899674 |          0 > (1 row) > > postgres=# vacuum t; > VACUUM > 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  > -----------+------------+------------ >     899622 |     899622 |          0 > (1 row) > > postgres=# vacuum t; > VACUUM > 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  > -----------+------------+------------ >     899570 |     899570 |          0 > (1 row) > > > In lazy_scan_heap() function, we force to scan the last page of the > relation to avoid the access exclusive lock in lazy_truncate_heap if > there are tuples in the last page. Because of this reason, the > scanned_pages value will never be 0, so the vac_estimate_reltuples > function will estimate the tuples based on the number of tuples from > the last page of the relation. This estimation is leading to reduce > the number of retuples. > Hmmm, that's annoying. Perhaps if we should not update the values in this case, then? I mean, if we only scan the last page, how reliable the derived values are? For the record - AFAICS this issue is unrelated to do with the patch (i.e. it's not introduced by it). > I am thinking whether this problem really happen in real world > scenarios to produce a fix? > Not sure. As vacuum run decrements the query only a little bit, so you'd have to run the vacuum many times to be actually bitten by it. For people relying on autovacuum that won't happen, as it only runs on tables with certain number of dead tuples. So you'd have to be running VACUUM in a loop or something (but not VACUUM ANALYZE, because that works fine) from a script, or something like that. That being said, fixing a bug is always a good thing I guess. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers