Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 8F76E9FBC10 for ; Thu, 22 Mar 2007 11:42:26 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 07183-05 for ; Thu, 22 Mar 2007 11:42:20 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from vms042pub.verizon.net (vms042pub.verizon.net [206.46.252.42]) by postgresql.org (Postfix) with ESMTP id 76BDB9FBC15 for ; Thu, 22 Mar 2007 11:42:20 -0300 (ADT) Received: from osgiliath.mathom.us ([72.66.31.15]) by vms042.mailsrvcs.net (Sun Java System Messaging Server 6.2-6.01 (built Apr 3 2006)) with ESMTPA id <0JFB00ESI7HYIT49@vms042.mailsrvcs.net> for pgsql-performance@postgresql.org; Thu, 22 Mar 2007 09:41:59 -0500 (CDT) Received: from localhost (localhost [127.0.0.1]) by osgiliath.mathom.us (Postfix) with ESMTP id AEB08707E0 for ; Thu, 22 Mar 2007 10:41:52 -0400 (EDT) Received: from osgiliath.mathom.us ([127.0.0.1]) by localhost (osgiliath.home.mathom.us [127.0.0.1]) (amavisd-new, port 10024) with LMTP id ZATJsi-QHPrz for ; Thu, 22 Mar 2007 10:41:51 -0400 (EDT) Received: by osgiliath.mathom.us (Postfix, from userid 1000) id B83CB712BD; Thu, 22 Mar 2007 10:41:51 -0400 (EDT) Date: Thu, 22 Mar 2007 10:41:51 -0400 From: Michael Stone Subject: Re: Performance of count(*) In-reply-to: To: pgsql-performance@postgresql.org Mail-followup-to: pgsql-performance@postgresql.org Message-id: <20070322144149.GF11402@mathom.us> MIME-version: 1.0 Content-type: text/plain; charset=us-ascii; format=flowed Content-disposition: inline X-Virus-Scanned: Debian amavisd-new at mathom.us X-Pgp-Fingerprint: 53 FF 38 00 E7 DD 0A 9C 84 52 84 C5 EE DF 7C 88 References: User-Agent: Mutt/1.5.13 (2006-08-11) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200703/324 X-Sequence-Number: 23729 On Thu, Mar 22, 2007 at 09:39:18AM -0400, Merlin Moncure wrote: >You can get the approximate count by selecting reltuples from >pg_class. It is valid as of last analyze. Of course, that only works if you're not using any WHERE clause. Here's a (somewhat ugly) example of getting an approximate count based off the statistics info, which will work for more complicated queries: http://archives.postgresql.org/pgsql-sql/2005-08/msg00046.php The ugliness is that you have to provide the whole query as a parameter to the function, instead of using it as a drop-in replacement for count. I assume that the TODO item is to provide the latter, but for now this method can be useful for UI type stuff where you just want to know whether there's "a little" or "a lot". Mike Stone