X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id BFA49D1B4F6 for ; Mon, 6 Oct 2003 06:07:33 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 90800-06 for ; Mon, 6 Oct 2003 03:06:44 -0300 (ADT) Received: from smtp.pspl.co.in (unknown [202.54.11.65]) by svr1.postgresql.org (Postfix) with ESMTP id 9033ED1B4EB for ; Mon, 6 Oct 2003 03:06:41 -0300 (ADT) Received: (from root@localhost) by smtp.pspl.co.in (8.12.9/8.12.9) id h966AmTk013993 for ; Mon, 6 Oct 2003 11:40:49 +0530 Received: from persistent.co.in (daithan.intranet.pspl.co.in [192.168.7.161]) (authenticated bits=0) by persistent.co.in (8.12.9/8.12.9) with ESMTP id h966AlYM013922 for ; Mon, 6 Oct 2003 11:40:48 +0530 Message-ID: <3F81066C.90402@persistent.co.in> Date: Mon, 06 Oct 2003 11:36:36 +0530 From: Shridhar Daithankar Organization: Persistent Systems Pvt. Ltd. User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.5b) Gecko/20030917 Thunderbird/0.3a X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: count(*) slow on large tables References: <200310041819.h94IJkV07596@candle.pha.pa.us> In-Reply-To: <200310041819.h94IJkV07596@candle.pha.pa.us> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/167 X-Sequence-Number: 3915 Bruce Momjian wrote: > OK, I beefed up the TODO: > > * Use a fixed row count and a +/- count with MVCC visibility rules > to allow fast COUNT(*) queries with no WHERE clause(?) > > I can always give the details if someone asks. It doesn't seem complex > enough for a separate TODO.detail item. May I propose alternate approach for this optimisation? - Postgresql allows to maintain user defined variables in shared memory. - These variables obey transactions but do not get written to disk at all. - There should be a facility to detect whether such a variable is initialized or not. How it will help? This is in addition to trigger proposal that came up earlier. With triggers it's not possible to make values visible across backends unless trigger updates a table, which eventually leads to vacuum/dead tuples problem. 1. User creates a trigger to check updates/inserts for certain conditions. 2. It updates the count as and when required. 3. If the trigger detects the count is not initialized, it would issue the same query first time. There is no avoiding this issue. Besides providing facility of resident variables could be used imaginatively as well. Does this make sense? IMO this is more generalised approach over all. Just a thought. Shridhar