Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id 916142E0038 for ; Fri, 6 Jun 2008 10:21:19 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 02979-08 for ; Fri, 6 Jun 2008 10:21:11 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mailserver.ttmail.com (unknown [88.255.235.9]) by developer.postgresql.org (Postfix) with ESMTP id 6B3662E002D for ; Fri, 6 Jun 2008 10:21:15 -0300 (ADT) Received: from STT1EXF01.TTHMC.LOCAL ([192.168.19.136]) by mailserver.ttmail.com with Microsoft SMTPSVC(6.0.3790.1830); Fri, 6 Jun 2008 16:21:02 +0300 Received: from alamut.mobiliz.com.tr ([213.139.194.86]) by STT1EXF01.TTHMC.LOCAL with Microsoft SMTPSVC(6.0.3790.1830); Fri, 6 Jun 2008 16:21:02 +0300 Received: from (unknown [213.139.194.86]) by AVGW-TTMAIL-5.ttmail.com with smtp id 6d3e_67898290_33cb_11dd_a918_0011433771ac; Fri, 06 Jun 2008 16:21:01 +0300 From: Volkan YAZICI To: pgsql-performance@postgresql.org Subject: Optimizing AGE() Date: Fri, 06 Jun 2008 16:20:59 +0300 Message-ID: <87abhyaedg.fsf@alamut.mobiliz.com.tr> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-NAI-Spam-Report: 1 Rules triggered * -1.2 -- BAYES_01 -- Bayesian spam probability is 1 to 10% X-OriginalArrivalTime: 06 Jun 2008 13:21:02.0561 (UTC) FILETIME=[2AD64D10:01C8C7D8] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200806/38 X-Sequence-Number: 30388 Hi, Am I wrong or AGE() always gets directed to a sequential scan? # BEGIN; ] SET enable_seqscan TO off; ] EXPLAIN ANALYZE ] SELECT count(1) ] FROM incomingmessageslog ] WHERE AGE(time) < '1 year'; ] ROLLBACK; BEGIN SET QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------- Aggregate (cost=100000528.33..100000528.34 rows=1 width=0) (actual time=13.789..13.790 rows=1 loops=1) -> Seq Scan on incomingmessageslog (cost=100000000.00..100000520.00 rows=3333 width=0) (actual time=13.783..13.783 rows=0 loops=1) Filter: (age((('now'::text)::date)::timestamp without time zone, "time") < '1 year'::interval) Total runtime: 13.852 ms (4 rows) ROLLBACK As far as I know, AGE() can take advantage of a very simple equation for constant comparisons: = AGE(field) < constant_criteria = AGE(field, constant_ts) < constant_criteria = AGE(field) < constant_criteria + constant_ts = AGE(field) < CONSTANT_CRITERIA How much does such a hack into optimizer cost? I don't know about its implications but I'll really appreciate such a functionality. At the moment, I'm trying replace every AGE() usage in my code and it really feels a PITA. Regards.