pg_class.reltuples VS pg_stat_all_tables.n_live_tup for estimation of table

Started by William Dunnalmost 11 years ago3 messagesgeneral
Jump to latest
#1William Dunn
dunnwjr@gmail.com

Hello,

Does anyone which is a more accurate estimate of a table's live
rows: pg_class.reltuples (
http://www.postgresql.org/docs/current/static/catalog-pg-class.html)
OR pg_stat_all_tables.n_live_tup (
http://www.postgresql.org/docs/current/static/monitoring-stats.html#PG-STAT-ALL-TABLES-VIEW)?
In my test database their values are not the same.

   - Is pg_class.reltuples an estimation of live tuples only, or is it of
   all tuples (both live and dead)? I would guess it's live only but that is a
   guess
   - In the database I am testing:
   - pg_class.reltuples <> pg_stat_all_tables.n_live_tup
      - pg_class.reltuples <> (pg_stat_all_tables.n_live_tup
      + pg_stat_all_tables.n_dead_tup)

I understand that pg_stat_all_tables is probably the more expensive view to
query but I am already querying both so I only care which is more accurate.

Thanks!
Will

*Will J. Dunn*
*willjdunn.com <http://willjdunn.com&gt;*

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: William Dunn (#1)
Re: pg_class.reltuples VS pg_stat_all_tables.n_live_tup for estimation of table

William Dunn <dunnwjr@gmail.com> writes:

Does anyone which is a more accurate estimate of a table's live
rows: pg_class.reltuples (
http://www.postgresql.org/docs/current/static/catalog-pg-class.html)
OR pg_stat_all_tables.n_live_tup (
http://www.postgresql.org/docs/current/static/monitoring-stats.html#PG-STAT-ALL-TABLES-VIEW)?

They're both inaccurate with different sources of inaccuracy. I dunno
that you should assume that one is necessarily better than the other.

- Is pg_class.reltuples an estimation of live tuples only, or is it of
all tuples (both live and dead)? I would guess it's live only but that is a
guess

Hm. I'm pretty sure the planner takes it as counting live tuples only,
but it looks like VACUUM thinks it includes recently-dead-but-not-yet-
removable tuples. We might need to do some adjustment there.

regards, tom lane

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#3William Dunn
dunnwjr@gmail.com
In reply to: Tom Lane (#2)
Re: pg_class.reltuples VS pg_stat_all_tables.n_live_tup for estimation of table

Thanks so much Tom!

*Will J. Dunn*
*willjdunn.com <http://willjdunn.com&gt;*

On Wed, Jun 17, 2015 at 3:48 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Show quoted text

William Dunn <dunnwjr@gmail.com> writes:

Does anyone which is a more accurate estimate of a table's live
rows: pg_class.reltuples (
http://www.postgresql.org/docs/current/static/catalog-pg-class.html)
OR pg_stat_all_tables.n_live_tup (

http://www.postgresql.org/docs/current/static/monitoring-stats.html#PG-STAT-ALL-TABLES-VIEW
)?

They're both inaccurate with different sources of inaccuracy. I dunno
that you should assume that one is necessarily better than the other.

- Is pg_class.reltuples an estimation of live tuples only, or is it of
all tuples (both live and dead)? I would guess it's live only but

that is a

guess

Hm. I'm pretty sure the planner takes it as counting live tuples only,
but it looks like VACUUM thinks it includes recently-dead-but-not-yet-
removable tuples. We might need to do some adjustment there.

regards, tom lane