Number of tuples in pg_class - bug or misunderstanding?

Started by Martin Neumannalmost 26 years ago2 messagesbugs
Jump to latest

I'm just digging a little bit in PostgreSQL internals and I found the
following thing. Shouldn't the reltuples field in pg_class contain the
real number of tuples? It is always a little bit to low ...

irc=# SELECT reltuples FROM pg_class WHERE relname = 'events';
reltuples
-----------
188
(1 row)

irc=# SELECT COUNT(*) FROM events;
count
-------
195
(1 row)

This annoys me a little bit as I can do
SELECT reltuples FROM pg_class WHERE relname = $1;
from plpgsql, but not
SELECT COUNT(*) FROM $1;
--
Martin Neumann
Appartement 201, Emil-Figge-Str. 9, 44227 Dortmund, Germany
mne@mne.de - http://www.mne.de/ - PGP key @ http://www.mne.de/mne/pgp.txt

#2Bruce Momjian
bruce@momjian.us
In reply to: Martin Neumann (#1)
Re: Number of tuples in pg_class - bug or misunderstanding?

I'm just digging a little bit in PostgreSQL internals and I found the
following thing. Shouldn't the reltuples field in pg_class contain the
real number of tuples? It is always a little bit to low ...

irc=# SELECT reltuples FROM pg_class WHERE relname = 'events';
reltuples
-----------
188
(1 row)

irc=# SELECT COUNT(*) FROM events;
count
-------
195
(1 row)

This annoys me a little bit as I can do
SELECT reltuples FROM pg_class WHERE relname = $1;
from plpgsql, but not
SELECT COUNT(*) FROM $1;

reltuples is updated only by VACUUM. The performance hit is too great
to keep it up-to-date, we think, though if it was accurate, we could
optimize COUNT(*) with no WHERE.

-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026