X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 7293252A17 for ; Fri, 8 Jul 2005 11:34:46 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 34125-10 for ; Fri, 8 Jul 2005 14:34:44 +0000 (GMT) Received: from wolff.to (wolff.to [66.93.197.194]) by svr1.postgresql.org (Postfix) with SMTP id C2C9252A0E for ; Fri, 8 Jul 2005 11:34:42 -0300 (ADT) Received: (qmail 24459 invoked by uid 500); 8 Jul 2005 14:35:08 -0000 Date: Fri, 8 Jul 2005 09:35:08 -0500 From: Bruno Wolff III To: Steve Wampler Cc: Chris Browne , pgsql-sql@postgresql.org Subject: Re: Make COUNT(*) Faster? Message-ID: <20050708143508.GD22725@wolff.to> Mail-Followup-To: Bruno Wolff III , Steve Wampler , Chris Browne , pgsql-sql@postgresql.org References: <9EF2931D-1097-470D-8ECB-59D00166C96F@apple.com> <60vf3mklue.fsf@dba2.int.libertyrms.com> <42CE89CA.2070607@noao.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <42CE89CA.2070607@noao.edu> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.007 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200507/70 X-Sequence-Number: 22048 On Fri, Jul 08, 2005 at 07:12:26 -0700, Steve Wampler 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.