Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id 116442E00C6 for ; Sun, 8 Jun 2008 03:12:46 -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 47744-01 for ; Sun, 8 Jun 2008 03:12:29 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from wa-out-1112.google.com (wa-out-1112.google.com [209.85.146.182]) by developer.postgresql.org (Postfix) with ESMTP id A98542E00EB for ; Sun, 8 Jun 2008 03:09:20 -0300 (ADT) Received: by wa-out-1112.google.com with SMTP id n4so1313319wag.5 for ; Sat, 07 Jun 2008 23:09:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:cc:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=ya2895vedYRyBkTVpk4HvmJZl6IAGXZgWXRHpPinOl8=; b=aaQ1Dqp/BHwTedDEAGWicyaQgQUT7HKK6CRP3xeKnm3x8m8Z3kOU1fPW5dRdj2zr5z H5VX4zCzc6ZIZdSlFVwFBr3TyagqybsI2QLPITp8G8nuq/QDz7pMPHSGFXlqPU6EpFH7 E6vAGs24UYSvZzITb9TiLPvAPMmnjv3F0AUG4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=fNBj+BOXVZ/WEDYBWCNE5ojKWIVpu67CGNMpLMrsd1cR2eOLhBJSEDP3gN0pgYxwms AA09nUU28lfHuTB1l0FofTcHhUfFz1ElS58n7OtPJVNGZkn9m0dvAW7CXjP5KvnxVwc4 bYYevKPJ86r8wwC2p7vo99DoGIr3udlCXS0og= Received: by 10.114.73.1 with SMTP id v1mr2020248waa.16.1212905359258; Sat, 07 Jun 2008 23:09:19 -0700 (PDT) Received: by 10.114.130.17 with HTTP; Sat, 7 Jun 2008 23:09:19 -0700 (PDT) Message-ID: Date: Sun, 8 Jun 2008 00:09:19 -0600 From: "Scott Marlowe" To: "Volkan YAZICI" Subject: Re: Optimizing AGE() Cc: pgsql-performance@postgresql.org In-Reply-To: <87abhyaedg.fsf@alamut.mobiliz.com.tr> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <87abhyaedg.fsf@alamut.mobiliz.com.tr> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200806/41 X-Sequence-Number: 30391 On Fri, Jun 6, 2008 at 7:20 AM, Volkan YAZICI wrote: > 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. Yeah, age() isn't real performent in such situations. I generally stick to simpler date math like: where timestampvalue < now() - interval '2 year' which can use an index on timestampvalue The problem with age is it's always compared to now, so there's always gonna be some math.