Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZyiBU-0003jz-Id for pgsql-performance@arkaria.postgresql.org; Tue, 17 Nov 2015 15:28:40 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZyiBU-0001Yj-5B for pgsql-performance@arkaria.postgresql.org; Tue, 17 Nov 2015 15:28:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1Zyi9p-0006qB-9x for pgsql-performance@postgresql.org; Tue, 17 Nov 2015 15:26:57 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1Zyi9m-00020M-AR for pgsql-performance@postgresql.org; Tue, 17 Nov 2015 15:26:56 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id tAHFQbwh030719; Tue, 17 Nov 2015 10:26:37 -0500 From: Tom Lane To: David Rowley cc: Thomas Kellerer , postgres performance list Subject: Re: Why is now()::date so much faster than current_date In-reply-to: References: Comments: In-reply-to David Rowley message dated "Wed, 18 Nov 2015 00:03:57 +1300" Date: Tue, 17 Nov 2015 10:26:37 -0500 Message-ID: <30718.1447773997@sss.pgh.pa.us> X-Pg-Spam-Score: -2.5 (--) 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 David Rowley writes: > On 17 November 2015 at 21:49, Thomas Kellerer wrote: >> So I ran this on my Windows laptop with Postgres 9.4.5, 64bit and indeed >> now()::date is much faster than current_date: > You can see that the implementation of current_date requires using the > date_in() function as well as the date_out() function. date_in() parses the > 'now' string, then the resulting date is converted back into a date string > with date_out(). Using now()::date does not have to parse any date > strings, it just needs to call date_out() to give the final output. Actually, in the context of EXPLAIN ANALYZE, date_out() will never be invoked at all --- EXPLAIN just throws away the query output without bothering to transform it to text first. So what we're really comparing is timestamptz_date(now()) versus date_in('now'). The useful work ends up being exactly the same in either code path, but date_in has to expend additional cycles on parsing the string and recognizing that it means DTK_NOW. > The reason for this is likely best explained by the comment in gram.y: That bit of gram.y is just an old bad decision though, along with similar choices for some other SQL special functions. Quite aside from any efficiency issues, doing things this way makes it impossible to reverse-list a call of CURRENT_DATE as CURRENT_DATE, which we really ought to do if we pretend to be a SQL-compliant RDBMS. And it's just ugly at a code level too: the raw grammar is not the place to encode implementation decisions like these. I've had it on my to-do list for awhile to replace those things with some new expression node type, but haven't got round to it. regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance