pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Craig Ringer <craig@2ndquadrant.com>
To: pgsql-performance@postgresql.org <pgsql-performance@postgresql.org>
Subject: Interesting case of IMMUTABLE significantly hurting performance
Date: Wed, 14 Aug 2013 08:41:36 +0800
Message-ID: <520AD240.9060508@2ndquadrant.com> (raw)
List-Unsubscribe:  <mailto:majordomo@postgresql.org?body=unsub%20pgsql-performance>

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



view thread (7+ messages)  latest in thread

Message-ID: <520AD240.9060508@2ndquadrant.com>
Permalink:  ../520AD240.9060508@2ndquadrant.com/
Also on:    postgresql.org/message-id/520AD240.9060508@2ndquadrant.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: craig@2ndquadrant.com
  Subject: Re: Interesting case of IMMUTABLE significantly hurting performance
  In-Reply-To: <520AD240.9060508@2ndquadrant.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox