Re: Why does pg_class.reltuples count only live tuples in indexes (after VACUUM runs)? - Mailing list pgsql-hackers

From Peter Geoghegan
Subject Re: Why does pg_class.reltuples count only live tuples in indexes (after VACUUM runs)?
Date
Msg-id CAH2-Wz=S2nLZakxOAhc=JuDrn3FkL1e3O1YH60sVejutJk=H3A@mail.gmail.com
Whole thread Raw
In response to Re: Why does pg_class.reltuples count only live tuples in indexes (after VACUUM runs)?  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: Why does pg_class.reltuples count only live tuples in indexes (after VACUUM runs)?  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
On Mon, Apr 18, 2022 at 12:15 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> > I don't see why it makes sense to treat indexes differently here. Why
> > allow the special case? Why include dead tuples like this?
>
> The index has presumably got entries corresponding to dead tuples,
> so that the number of entries it has ought to be more or less
> num_heap_tuples, not reltuples (with discrepancies for concurrent
> insertions of course).

I guess that pg_class.reltuples has to include some "recently dead"
tuples in the case of an index, just because of the impracticality of
accurately counting index tuples while knowing if they're dead or
alive. However, it would be practical to update pg_class.reltuples to
a value "IndexBulkDeleteResult.num_index_tuples -
recently_dead_tuples" in update_relstats_all_indexes to compensate.
Then everything is consistent.

> > We make a general assumption that pg_class.reltuples only includes
> > live tuples, which this code contravenes.
>
> Huh?  This is not pg_class.reltuples.  If an index AM wants that, it
> knows where to find it.

It's not, but it is how we calculate
IndexBulkDeleteResult.num_index_tuples, which is related. Granted,
that won't be used to update pg_class for the index in the case where
it's just an estimate anyway.

-- 
Peter Geoghegan



pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: Why does pg_class.reltuples count only live tuples in indexes (after VACUUM runs)?
Next
From: Andrew Dunstan
Date:
Subject: Re: Postgres perl module namespace