Subject: Approximate count(*)

From: David Fetter <[email-masked]>
Date: Thu, 24 Mar 2005 09:01:37 -0800

Folks,

Please find enclosed a patch that shows how to get a quick
approximation of count(*) on a table.

Cheers,
D
-- 
David Fetter [email-masked] http://fetter.org/
phone: +1 510 893 6100   mobile: +1 415 235 3778

Remember to vote!
Index: doc/src/sgml/func.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql/doc/src/sgml/func.sgml,v
retrieving revision 1.241
diff -c -r1.241 func.sgml
*** doc/src/sgml/func.sgml	14 Mar 2005 18:31:19 -0000	1.241
--- doc/src/sgml/func.sgml	24 Mar 2005 16:25:55 -0000
***************
*** 7330,7339 ****
     </para>
  
     <para>
!     Unfortunately, there is no similarly trivial query that can be
!     used to improve the performance of <function>count()</function>
!     when applied to the entire table.
     </para>
    </note>
  
   </sect1>
--- 7330,7348 ----
     </para>
  
     <para>
!     When the table has been <command>VACUUM</command>ed recently, but
!     only then, a good approximation of count(*) for an entire table
!     can be obtained as follows:
! <programlisting>
! SELECT reltuples FROM pg_class WHERE relname = 'sometable';
! </programlisting>
!    </para>
! 
!    <para>
!     Unfortunately, there is not yet a general trivial query that can
!     be used to improve the performance of <function>count()</function>.
     </para>
+ 
    </note>
  
   </sect1>

---

From: Tom Lane <[email-masked]>
Date: Thu, 24 Mar 2005 12:34:51 -0500

> approximation of count(*) on a table.

I'm not sure we should be encouraging people to look at reltuples...
for one thing, it's deliberately a moving average under 8.0.

			regards, tom lane

---

From: David Fetter <[email-masked]>
Date: Thu, 24 Mar 2005 09:58:27 -0800

> for one thing, it's deliberately a moving average under 8.0.

Should there be more caveats?

Cheers,
D

Remember to vote!

---

From: "Greg Sabino Mullane" <[email-masked]>
Date: Fri, 25 Mar 2005 02:34:14 -0000

> approximation of count(*) on a table.
  
You should mention that ANALYZE will also populate reltuples.
  
To be real anal, you should say pg_catalog.pg_class too. :)

> Should there be more caveats?
  
Well, it already says "approximate", the value-laden word is "good".
Perhaps if that went away...

- --
Greg Sabino Mullane [email-masked]
PGP Key: 0x14964AC8 200503242126
http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8

---

From: Josh Berkus <[email-masked]>
Date: Thu, 24 Mar 2005 19:19:00 -0800

David,

If Jim and I finish our work for 8.1, then you'll be able to do:

SELECT approx_records FROM pg_sysviews.pg_tables WHERE schema_name = 'schema' 
AND table_name = 'table'

But the same caveats will apply.

-- 
Josh Berkus
Aglio Database Solutions
San Francisco

---

From: "Jim C. Nasby" <[email-masked]>
Date: Sun, 27 Mar 2005 15:24:15 -0600

Josh, David-

Do you think it's worth adding a function that would do the select for
you? IE: tuplecount('schema', 'tablename')?

On Thu, Mar 24, 2005 at 07:19:00PM -0800, Josh Berkus wrote:
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
> 

-- 
Jim C. Nasby, Database Consultant               [email-masked] 
Give your computer some brain candy! www.distributed.net Team #1828

Windows: "Where do you want to go today?"
Linux: "Where do you want to go tomorrow?"
FreeBSD: "Are you guys coming, or what?"