Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V9P9p-0008He-Gp for pgsql-performance@arkaria.postgresql.org; Wed, 14 Aug 2013 00:41:49 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1V9P9o-0007wz-Vb for pgsql-performance@arkaria.postgresql.org; Wed, 14 Aug 2013 00:41:49 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V9P9n-0007vL-CH for pgsql-performance@postgresql.org; Wed, 14 Aug 2013 00:41:47 +0000 Received: from outmail148108.authsmtp.net ([62.13.148.108]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V9P9k-0000Ml-5T for pgsql-performance@postgresql.org; Wed, 14 Aug 2013 00:41:46 +0000 Received: from mail-c226.authsmtp.com (mail-c226.authsmtp.com [62.13.128.226]) by punt5.authsmtp.com (8.14.2/8.14.2/Kp) with ESMTP id r7E0fhVx069523 for ; Wed, 14 Aug 2013 01:41:43 +0100 (BST) Received: from ayaki.localdomain (124-169-245-108.dyn.iinet.net.au [124.169.245.108]) (authenticated bits=0) by mail.authsmtp.com (8.14.2/8.14.2/) with ESMTP id r7E0fbEZ070828 (version=TLSv1/SSLv3 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO) for ; Wed, 14 Aug 2013 01:41:40 +0100 (BST) Message-ID: <520AD240.9060508@2ndquadrant.com> Date: Wed, 14 Aug 2013 08:41:36 +0800 From: Craig Ringer Organization: 2nd Quadrant User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130625 Thunderbird/17.0.7 MIME-Version: 1.0 To: "pgsql-performance@postgresql.org" Subject: Interesting case of IMMUTABLE significantly hurting performance X-Enigmail-Version: 1.5.2 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Server-Quench: 49298b95-047a-11e3-98a9-0025907ec6c5 X-AuthReport-Spam: If SPAM / abuse - report it at: http://www.authsmtp.com/abuse X-AuthRoute: OCdxZQATClZeVg0b BQteCiNZVAwpPBRK HVkIKg5MOFUSTAAU KktAIF1JNVQaTlxx QzlIHwMWBR4wBX0q KV0fbAFCEw8ZQAdq UUtZQlNYEARiBR1P BxwaTBh0fhtEfHtu ZEZqQXJeXkFldAg/ RTtUQz9zD2dnaGgd WA5bdAVSPgdDfBZN OFl6SXMJZjQaZ3s1 REpibzlgeG0HcnkE Gl4AJgwZHFAqGCIx XR0ZATQpEgUOTj4+ ZxsoYn85OXstG28W HBM6WF0eMhETAQRY A0VcSC9EPUUAQSVj FQ5AU0kEFTRbSio0 X-Authentic-SMTP: 61633235383639.1020:706 X-AuthFastPath: 0 (Was 255) X-AuthSMTP-Origin: 124.169.245.108/2525 X-AuthVirus-Status: No virus detected - but ensure you scan with your own anti-virus system. 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 Hi folks I've run into an interesting Stack Overflow post where the user shows that marking a particular function as IMMUTABLE significantly hurts the performance of a query. http://stackoverflow.com/q/18220761/398670 CREATE OR REPLACE FUNCTION to_datestamp_immutable(time_int double precision) RETURNS date AS $$ SELECT date_trunc('day', to_timestamp($1))::date; $$ LANGUAGE SQL IMMUTABLE; With IMMUTABLE: 33060.918 With STABLE: 6063.498 The plans are the same for both, though the cost estimate for the IMMUTABLE variant is (surprisingly) massively higher. The question contains detailed instructions to reproduce the issue, and I can confirm the same results on my machine. It looks like the difference is created by to_timestamp , in that if to_timestamp is replaced with interval maths the difference goes away. I'm very curious and am doing a quick profile now, but I wanted to raise this on the list for comment/opinions, since it's very counter-intuitive. IIRC docs don't suggest that IMMUTABLE can ever be more expensive. -- Craig Ringer http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, 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