Re: Make COUNT(*) Faster?

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Steve Wampler <swampler(at)noao(dot)edu>
Cc: Chris Browne <cbbrowne(at)acm(dot)org>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Make COUNT(*) Faster?
Date: 2005-07-08 14:35:08
Message-ID: 20050708143508.GD22725@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Fri, Jul 08, 2005 at 07:12:26 -0700,
Steve Wampler <swampler(at)noao(dot)edu> wrote:
>
> Hmmm, I understand this and don't doubt it, but out of curiousity, how
> does the current SELECT COUNT(*) handle this? It doesn't lock the entire

It only counts tuples visible to the current transaction.

> table while counting (I assume) so the current implementation is really
> just an approximate count in the above scenario anyway. Or even when

No, it is an exact count.

> not, since the true 'count' is likely to have changed by the time the

There is no single true count. There is a separate true count for each
transaction.

> user does anything with the result of SELECT COUNT(*) on any active table
> (and on an inactive table, pg_class.reltuples is nearly as good as
> SELECT COUNT(*) and far faster to get to.)
>
> I assume this has been beaten well past death, but I don't see why it
> wouldn't be possible to keep pg_class.reltuples a bit more up-to-date
> instead of updating it only on vacuums.

Because it costs resources to keep track of that and people don't usually need
exact tuple counts for whole tables. Those that do and are willing to pay the
price can use triggers to maintain a count in a separate table.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message PFC 2005-07-08 14:49:44 Re: two sums in one query
Previous Message Steve Wampler 2005-07-08 14:12:26 Re: Make COUNT(*) Faster?