pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Wheeler <david@kineticode.com>
To: pgsql-performance <pgsql-performance@postgresql.org>
Subject: IMMUTABLE?
Date: Mon, 15 May 2006 20:15:11 -0700
Message-ID: <2D4C0FA5-DB75-45EA-91D2-7B4F6DD55083@kineticode.com> (raw)

Performance Folks,

I just had an article[1] published in which I demonstrated recursive  
PL/pgSQL functions with this function:

CREATE OR REPLACE FUNCTION fib (
     fib_for int
) RETURNS integer AS $$
BEGIN
     IF fib_for < 2 THEN
         RETURN fib_for;
     END IF;
     RETURN fib(fib_for - 2) + fib(fib_for - 1);
END;
$$ LANGUAGE plpgsql;

Naturally, it's slow:

try=# \timing
try=# select fib(28);
   fib
--------
317811
(1 row)

Time: 10642.803 ms

Now, I mistakenly said in my article that PostgreSQL doesn't have  
native memoization, and so demonstrated how to use a table for  
caching to speed up the function. It's pretty fast:

try=# select fib_cached(28);
fib_cached
------------
      317811
(1 row)

Time: 193.316 ms

But over the weekend, I was looking at the Pg docs and saw IMMUTABLE,  
and said, "Oh yeah!". So I recreated the function with IMMUTABLE. But  
the performance was not much better:

try=# select fib(28);
   fib
--------
317811
(1 row)

Time: 8505.668 ms
try=# select fib_cached(28);
fib_cached
------------
      317811
(1 row)

So, what gives? Am I missing something, or not understanding how  
IMMUTABLE works?

Many TIA,

David

1. http://www.onlamp.com/pub/a/onlamp/2006/05/11/postgresql-plpgsql.html



view thread (11+ messages)  latest in thread

Message-ID: <2D4C0FA5-DB75-45EA-91D2-7B4F6DD55083@kineticode.com>
Permalink:  ../2D4C0FA5-DB75-45EA-91D2-7B4F6DD55083@kineticode.com/
Also on:    postgresql.org/message-id/2D4C0FA5-DB75-45EA-91D2-7B4F6DD55083@kineticode.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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-performance@postgresql.org
  Cc: david@kineticode.com
  Subject: Re: IMMUTABLE?
  In-Reply-To: <2D4C0FA5-DB75-45EA-91D2-7B4F6DD55083@kineticode.com>
  References: <2D4C0FA5-DB75-45EA-91D2-7B4F6DD55083@kineticode.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