Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 7E1CA633E43 for ; Thu, 20 May 2010 18:08:27 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 22090-08 for ; Thu, 20 May 2010 21:08:15 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-ew0-f224.google.com (mail-ew0-f224.google.com [209.85.219.224]) by mail.postgresql.org (Postfix) with ESMTP id 1ABC1634128 for ; Thu, 20 May 2010 18:08:15 -0300 (ADT) Received: by ewy24 with SMTP id 24so68342ewy.34 for ; Thu, 20 May 2010 14:08:14 -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 :user-agent:mime-version:to:cc:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=LX3EygOaGFbqgvPBwlXR3LLRBEDKbVz6BuvQPELEtyM=; b=ErJl/ytnr2B60H2mqrGuBW+z3D+DfE04wgTccqdOSP02UFoU86VgXvm4rl5baxwDbj bdc7nySTJr5EbAto8J/RQY0NXcWKaYj+Ns8PrOeYXvfHli0XCq/0L+zeta+FQdnf2c5w TbFoMn+d5dKyqhyM4DR8Cgav4UP/1nYdkPOcc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; b=HPD30fsaGgjG9GILDhhBh3LLBZe3txN7SrdN1LWrGEMEQe7IVLhwJNTXt2HsgRUQVy vn5P0PLGXK3VAyB/nfvFlGWetVQUsd3HPA14jPetFDCnPm+9E9P+Nf+8X2gmZNJe//Ui AOTOYJSKHUkNf3NmboIB90AJpDidUdG9VIzsY= Received: by 10.213.43.66 with SMTP id v2mr209158ebe.97.1274389694066; Thu, 20 May 2010 14:08:14 -0700 (PDT) Received: from [0.0.0.0] (5357DA75.cable.casema.nl [83.87.218.117]) by mx.google.com with ESMTPS id 16sm157334ewy.15.2010.05.20.14.08.12 (version=TLSv1/SSLv3 cipher=RC4-MD5); Thu, 20 May 2010 14:08:13 -0700 (PDT) Message-ID: <4BF5A4C5.5090505@gmail.com> Date: Thu, 20 May 2010 23:08:21 +0200 From: Yeb Havinga User-Agent: Thunderbird 2.0.0.24 (Windows/20100228) MIME-Version: 1.0 To: Tom Lane CC: David Jarvis , Thom Brown , pgsql-performance@postgresql.org Subject: Re: Optimize date query for large child tables: GiST or GIN? References: <7143.1274382139@sss.pgh.pa.us> <7767.1274385170@sss.pgh.pa.us> <7917.1274385834@sss.pgh.pa.us> <8132.1274386738@sss.pgh.pa.us> In-Reply-To: <8132.1274386738@sss.pgh.pa.us> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201005/129 X-Sequence-Number: 38713 Tom Lane wrote: > David Jarvis writes: > >> I was hoping to eliminate this part of the query: >> (cast(extract( YEAR FROM m.taken ) + greatest( -1 * >> sign( >> (extract( YEAR FROM m.taken )||'-12-31')::date - >> (extract( YEAR FROM m.taken )||'-01-01')::date ), 0 >> ) AS text)||'-12-31')::date >> >> That uses functions to create the dates, which is definitely the problem. >> > > Well, it's not the functions per se that's the problem, it's the lack of > a useful index on the expression. But as somebody remarked upthread, > that expression doesn't look correct at all. Doesn't the whole > greatest() subexpression reduce to a constant? > That somebody was probably me. I still think the whole BETWEEN expression is a tautology. A small test did not provide a counterexample. In the select below everything but the select was copy/pasted. create table m (taken timestamptz); insert into m values (now()); insert into m values ('1900-12-31'); insert into m values ('2000-04-06'); select m.taken BETWEEN /* Start date. */ (extract( YEAR FROM m.taken )||'-01-01')::date AND /* End date. Calculated by checking to see if the end date wraps into the next year. If it does, then add 1 to the current year. */ (cast(extract( YEAR FROM m.taken ) + greatest( -1 * sign( (extract( YEAR FROM m.taken )||'-12-31')::date - (extract( YEAR FROM m.taken )||'-01-01')::date ), 0 ) AS text)||'-12-31')::date from m; ?column? ---------- t t t (3 rows) Another thing is that IF the climate measurements is partitioned on time (e.g each year?), then a function based index on the year part of m.taken is useless, pardon my french. I'm not sure if it is partitioned that way but it is an interesting thing to inspect, and perhaps rewrite the query to use constraint exclusion. regards, Yeb Havinga