Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YjtEf-00053F-5J for pgsql-performance@arkaria.postgresql.org; Sun, 19 Apr 2015 17:42:25 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YjtEd-0005yi-Gd for pgsql-performance@arkaria.postgresql.org; Sun, 19 Apr 2015 17:42:23 +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 1YjtEc-0005yP-BU for pgsql-performance@postgresql.org; Sun, 19 Apr 2015 17:42:22 +0000 Received: from mail-wi0-f177.google.com ([209.85.212.177]) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YjtEX-0002Ua-Se for pgsql-performance@postgresql.org; Sun, 19 Apr 2015 17:42:21 +0000 Received: by wiun10 with SMTP id n10so68170060wiu.1 for ; Sun, 19 Apr 2015 10:42:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :subject:references:in-reply-to:content-type :content-transfer-encoding; bh=K5R2qmlJ334vOXK48elafTL1omS4U1OTdR68RompM2k=; b=QDXfqHIX1cvo5PTSS2vM1dkN+Ht0+6XUYu1pcTzITTpcs2m5jn/T13oijng9M5xcSL n5hQxWYtlyfkuGTazLs60LM+z8apbddHboboyht0bayXlYxJqQRbptU1YWie7f4FQFT5 A35/J6Dmeh7JG9Ef7Xra+yE75lRnvjlDNoSxb0OJ/mxzijxcf/+vCFoBMH0l3meyff8A S7fPrX9av+pXg2CqLl5gLqf4cfnYFFGAQsLE+XK5+PbocjetQDbNC1JluPemtJQ64Flz ROY+V8GYVDGzS0ntc8joUBjio2AyCrXalyo9auKxtYbTigp5+c/7UThTRcCnijGYdzsm rcFg== X-Gm-Message-State: ALoCoQlLl97qUvOw1ZNjaWYIRfgGrdId/I7U6W7yYabmjnePP7XUuBlpS4CRhww94G2YDhAhqgmNo0ItVL0sY6TG67ARj0nNKjflXHqAR7IZZc8mtrSQ36EfRrBNA9ce8lNMSvOkStPDMHNeoifZyU91s7Mx064gcl2y9hik3ZiSFvZKI7zjuXDECugvQ4XZ2Ujn6l4jr04d X-Received: by 10.180.99.39 with SMTP id en7mr18623105wib.31.1429465335916; Sun, 19 Apr 2015 10:42:15 -0700 (PDT) Received: from [10.137.2.12] (ip-78-45-139-138.net.upcbroadband.cz. [78.45.139.138]) by mx.google.com with ESMTPSA id n3sm12146895wix.1.2015.04.19.10.42.14 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 19 Apr 2015 10:42:15 -0700 (PDT) Message-ID: <5533E8F4.2050400@2ndquadrant.com> Date: Sun, 19 Apr 2015 19:42:12 +0200 From: Tomas Vondra User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: extract(year from date) doesn't use index but maybe could? References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) 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 04/19/15 19:16, Jon Dufresne wrote: > Given the table: > > CREATE TABLE dates (id SERIAL, d DATE NOT NULL, t TEXT NOT NULL) > > With an *index* on field d. The following two queries are functionally > equivalent: > > 1. SELECT * FROM dates WHERE d >= '1900-01-01' > 2. SELECT * FROM dates WHERE EXTRACT(year from d) >= 1900' > > By functionally equivalent, they will return the same result set. > > Query 2 does not use the index, adding a performance cost. It seems > there is an opportunity for optimization to handle these two queries > equivalently to take advantage of the index. Or you might try creating an expression index ... CREATE INDEX date_year_idx ON dates((extract(year from d))); regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance