pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tomas Vondra <tomas.vondra@2ndquadrant.com>
To: Ranjith Ramachandra <ranjith@mammoth.io>
To: pgsql-general@lists.postgresql.org
Subject: Re: Seems like there is an issue with reltuples showing twice the number of rows
Date: Tue, 3 Apr 2018 13:02:44 +0200
Message-ID: <7d986cf7-157e-b8b3-0b01-27e96797af71@2ndquadrant.com> (raw)
In-Reply-To: <CAFWmNu8SfSgBWcMCaWJfDLbcFUN3riC9jDuzOd08QsJgAqv+4A@mail.gmail.com>
References: <CAFWmNu8SfSgBWcMCaWJfDLbcFUN3riC9jDuzOd08QsJgAqv+4A@mail.gmail.com>

On 04/03/2018 11:14 AM, Ranjith Ramachandra wrote:
> I am relying on reltuples on my web app to get fast row counts.
> 
> This was recommended by this article to get fast approx row
> counts: https://wiki.postgresql.org/wiki/Count_estimate
> 
> 
> However for some table I am getting twice as many values when I try to
> do this. I did some more research and came up with this query.
> 
> select reltuples, n_live_tup, n_dead_tup
>               from pg_stat_user_tables join pg_class using (relname)
>              where relname =
> 'main_csv_0f247511f5c247c8813ba3cec90c6ae1_yf34fbb38d';
> 
> 
> 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. This
> is causing me a lot of problems since this inaccuracy does not make any
> sense to me.
> 

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.

Hard to say if this is a case of that, but judging by the number of dead
tuples chances are it is.

It's probably better to use n_live_tup instead, though. I'd say that's
closer to the "live tuples" definition.

regards

-- 
Tomas Vondra                  http://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services




view thread (4+ messages)  latest in thread

Message-ID: <7d986cf7-157e-b8b3-0b01-27e96797af71@2ndquadrant.com>
Permalink:  ../7d986cf7-157e-b8b3-0b01-27e96797af71@2ndquadrant.com/
Also on:    postgresql.org/message-id/7d986cf7-157e-b8b3-0b01-27e96797af71@2ndquadrant.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tomas.vondra@2ndquadrant.com, ranjith@mammoth.io
  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: <CAFWmNu8SfSgBWcMCaWJfDLbcFUN3riC9jDuzOd08QsJgAqv+4A@mail.gmail.com> <7d986cf7-157e-b8b3-0b01-27e96797af71@2ndquadrant.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox