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 53EA352995 for ; Fri, 8 Jul 2005 12:30:43 -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 04840-05 for ; Fri, 8 Jul 2005 15:30:39 +0000 (GMT) Received: from zproxy.gmail.com (zproxy.gmail.com [64.233.162.207]) by svr1.postgresql.org (Postfix) with ESMTP id 8B05F52820 for ; Fri, 8 Jul 2005 12:30:36 -0300 (ADT) Received: by zproxy.gmail.com with SMTP id n29so205990nzf for ; Fri, 08 Jul 2005 08:30:36 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=oTd66lxaMVbU5QHOKuDx1kWha8ATWWzPFnx61tq9N2M2L45Os9Lpb5jcNHb1+b6554sijw2VeiQM5C1Rlcs2XaQuV9V/4XMHuHd8HosDJCKbblWyllus825B3Ecipu7iFZ0CdSxbsnaUL5f7Y0IgTi6zMsWyHs7+AvFz16s4F00= Received: by 10.36.222.42 with SMTP id u42mr701558nzg; Fri, 08 Jul 2005 08:30:35 -0700 (PDT) Received: by 10.36.22.20 with HTTP; Fri, 8 Jul 2005 08:30:35 -0700 (PDT) Message-ID: <758d5e7f05070808305c049aae@mail.gmail.com> Date: Fri, 8 Jul 2005 17:30:35 +0200 From: Dawid Kuroczko Reply-To: Dawid Kuroczko To: Steve Wampler Subject: Re: Make COUNT(*) Faster? Cc: Chris Browne , pgsql-sql@postgresql.org In-Reply-To: <42CE89CA.2070607@noao.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: <9EF2931D-1097-470D-8ECB-59D00166C96F@apple.com> <60vf3mklue.fsf@dba2.int.libertyrms.com> <42CE89CA.2070607@noao.edu> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.61 tagged_above=0 required=5 tests=AWL, FROM_ENDS_IN_NUMS, RCVD_BY_IP X-Spam-Level: X-Archive-Number: 200507/74 X-Sequence-Number: 22052 On 7/8/05, Steve Wampler wrote: > > None of those transactions have COMMITted, so there are some 78 tuples > > "in limbo" spread across 16 transactions. > > > > If there were some "single secret place" with a count, how would you > > suggest it address those 78 tuples and 16 transactions that aren't yet > > (and maybe never will be) part of the count? >=20 > 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 > table while counting (I assume) so the current implementation is really > just an approximate count in the above scenario anyway. Or even when > not, since the true 'count' is likely to have changed by the time the > 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.) >=20 > 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. Use EXPLAIN SELECT * FROM yourcountedtable; Planner seems to track estimated statistics on-the-fly. :) You can even wrap EXPLAIN SELECT in a pgsql function if you need it. Regards, Dawid PS: And be aware that these are 'statistics'. And the statement that there are lies, big lies and statistics is sometimes true even for PostgreSQL. ;-= )