Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YkxJA-0005Ir-1Y for pgsql-performance@arkaria.postgresql.org; Wed, 22 Apr 2015 16:15:28 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YkxJ9-0003ML-D0 for pgsql-performance@arkaria.postgresql.org; Wed, 22 Apr 2015 16:15:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YkxJ8-0003MB-Hr for pgsql-performance@postgresql.org; Wed, 22 Apr 2015 16:15:26 +0000 Received: from mail-bn1on0647.outbound.protection.outlook.com ([2a01:111:f400:fc10::647] helo=na01-bn1-obe.outbound.protection.outlook.com) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YkxJ2-00068m-If for pgsql-performance@postgresql.org; Wed, 22 Apr 2015 16:15:25 +0000 Received: from CY1PR11MB0683.namprd11.prod.outlook.com (25.163.236.21) by CY1PR11MB0779.namprd11.prod.outlook.com (25.163.237.149) with Microsoft SMTP Server (TLS) id 15.1.136.25; Wed, 22 Apr 2015 16:15:14 +0000 Authentication-Results: postgresql.org; dkim=none (message not signed) header.d=none; Received: from decina.attlocal.net (2602:306:ce8b:3460:655b:5f4c:5733:7d45) by CY1PR11MB0683.namprd11.prod.outlook.com (25.163.236.21) with Microsoft SMTP Server (TLS) id 15.1.136.25; Wed, 22 Apr 2015 16:15:12 +0000 Message-ID: <5537C910.1030507@BlueTreble.com> Date: Wed, 22 Apr 2015 11:15:12 -0500 From: Jim Nasby User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: Tom Lane , Tomas Vondra CC: Jon Dufresne , Subject: Re: extract(year from date) doesn't use index but maybe could? References: <5533E8F4.2050400@2ndquadrant.com> <55341A3C.1080101@2ndquadrant.com> <6287.1429479191@sss.pgh.pa.us> In-Reply-To: <6287.1429479191@sss.pgh.pa.us> Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [2602:306:ce8b:3460:655b:5f4c:5733:7d45] X-ClientProxiedBy: BLUPR11CA0009.namprd11.prod.outlook.com (10.141.240.19) To CY1PR11MB0683.namprd11.prod.outlook.com (25.163.236.21) X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:; SRVR:CY1PR11MB0683; UriScan:; BCL:0; PCL:0; RULEID:; SRVR:CY1PR11MB0779; X-Forefront-Antispam-Report: BMV:1; SFV:NSPM; SFS:(10009020)(6009001)(377424004)(24454002)(377454003)(479174004)(80316001)(19580395003)(2950100001)(83506001)(65816999)(54356999)(46102003)(92566002)(122386002)(40100003)(4001350100001)(50986999)(53416004)(87266999)(42186005)(76176999)(5001770100001)(93886004)(65956001)(47776003)(77156002)(62966003)(15975445007)(23746002)(33656002)(86362001)(36756003)(50466002)(3826002)(85282002)(336755003)(18886065003); DIR:OUT; SFP:1101; SCL:1; SRVR:CY1PR11MB0683; H:decina.attlocal.net; FPR:; SPF:None; MLV:sfv; LANG:en; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(5002010)(5005006); SRVR:CY1PR11MB0683; BCL:0; PCL:0; RULEID:; SRVR:CY1PR11MB0683; X-Forefront-PRVS: 0554B1F54F X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Apr 2015 16:15:12.4386 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY1PR11MB0683 X-OriginatorOrg: bluetreble.com X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On 4/19/15 4:33 PM, Tom Lane wrote: > We might still be willing to do it if there were a sufficiently wide range > of examples that could be handled by the same extra machinery, but this > doesn't look too promising from that angle: AFAICS only the "year" case > could yield a useful index restriction. "date_trunc() op val" is where we'd want to do this. Midnight, month and quarter boundaries are the cases I've commonly seen. The first is especially bad, because people got in the habit of doing timestamptz::date = '2015-1-1' and then got very irritated when that cast became volatile. Perhaps this could be better handled by having a way to translate date/time specifications into a range? So something like "date_trunc('...', timestamptz) = val" would become "date_trunk('...', timestamptz) <@ [val, val+)". <,<=,>= and > wouldn't even need to be a range. To cover the broadest base, we'd want to recognize that extract(year) could transform to date_trunk('year',...), and timestamp/timestamptz::date transforms to date_trunc('day',...). I think these transforms would be lossless, so they could always be made, though we'd not want to do the transformation if there was already an index on something like date_trunc(...). I don't readily see any other data types where this would be useful, but this is so common with timestamps that I think it's worth handling just that case. -- Jim Nasby, Data Architect, Blue Treble Consulting Data in Trouble? Get it in Treble! http://BlueTreble.com -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance