Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YjwW8-0005Vs-BY for pgsql-performance@arkaria.postgresql.org; Sun, 19 Apr 2015 21:12:40 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YjwW7-0002Vb-Fi for pgsql-performance@arkaria.postgresql.org; Sun, 19 Apr 2015 21:12:39 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YjwW6-0002VQ-7b for pgsql-performance@postgresql.org; Sun, 19 Apr 2015 21:12:38 +0000 Received: from mail-wi0-f169.google.com ([209.85.212.169]) by makus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YjwW2-00077j-MJ for pgsql-performance@postgresql.org; Sun, 19 Apr 2015 21:12:36 +0000 Received: by wizk4 with SMTP id k4so77236576wiz.1 for ; Sun, 19 Apr 2015 14:12:31 -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 :cc:subject:references:in-reply-to:content-type :content-transfer-encoding; bh=nc9mCt+RuoWjSevgH036yi9ARE7HxeUUFs/2ASku67Q=; b=V9PpTxv07O6+jJu2+Hn8O2RffTo1Acq+cQ4Y9f3rndDO1usWqIjFEZ+94M0GAKD6nT KPikhZw+uEa77o0vKGaQiLX2bj1taoRzA9nb7tH74tIIx0zkUe6jXyx7+uMAetTnu4Il D2ZhPDkScD4vkjt6KVXscMz3Gd0dkz6pS8kKETkIwn+uGKBuo17rfwDRL/+MuJiZl4Ys ng/D6OP7lg5sVt2GLaNSJSQ2Dt8ZSJu16UQ6sMw6mXnuPNNJelcDi4b5DvunNRkZKJnQ nzm9WVTpaX+qfCHzWLE/W7YRwlB7CrfU8nSrUV1HmuTCcwcdNHReRsc+R4qEbMqu/1KH U/FQ== X-Gm-Message-State: ALoCoQl8w0bQIrCD5adY/pBJKyQXI5pEqEBcsuP0wsniXYEy7UMrGqeFCesRHzQATYj3fpCGghoUCaEChxvCY+P4NQGfXy/FJ+T9W0FjHqRR0ZvAMIAAH8w9XjXobKi9dtt+J68z/c386Ne48sNuWwsp+DLADJ7ZJbCTWxtR2Hh3XAe1Y9j0/m7yvNpzBKREMg12gKRLtQ7e X-Received: by 10.180.90.82 with SMTP id bu18mr19553860wib.88.1429477951822; Sun, 19 Apr 2015 14:12:31 -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 wo10sm24459934wjb.35.2015.04.19.14.12.30 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 19 Apr 2015 14:12:31 -0700 (PDT) Message-ID: <55341A3C.1080101@2ndquadrant.com> Date: Sun, 19 Apr 2015 23:12:28 +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: Jon Dufresne CC: pgsql-performance@postgresql.org Subject: Re: extract(year from date) doesn't use index but maybe could? References: <5533E8F4.2050400@2ndquadrant.com> 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 22:10, Jon Dufresne wrote: > On Sun, Apr 19, 2015 at 10:42 AM, Tomas Vondra > wrote: > >> Or you might try creating an expression index ... >> >> CREATE INDEX date_year_idx ON dates((extract(year from d))); >> > > Certainly, but won't this add additional overhead in the form of two > indexes; one for the column and one for the expression? It will, but it probably will be more efficient than poorly performing queries. Another option is to use the first type of queries with explicit date ranges, thus making it possible to use a single index. > > My point is, why force the user to take these extra steps or add > overhead when the the two queries (or two indexes) are functionally > equivalent. Shouldn't this is an optimization handled by the > database so the user doesn't need to hand optimize these differences? Theoretically yes. But currently the "extract" function call is pretty much a black box for the planner, just like any other function - it has no idea what happens inside, what fields are extracted and so on. It certainly is unable to infer the date range as you propose. It's possible that in the future someone will implement an optimization like this, but I'm not aware of anyone working on that and I wouldn't hold my breath. Until then you either have to create an expression index, or use queries with explicit date ranges (without "extract" calls). 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