X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 80A199F9C18 for ; Tue, 16 May 2006 00:15:25 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 27353-06 for ; Tue, 16 May 2006 00:15:16 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from mail2.sea5.speakeasy.net (mail2.sea5.speakeasy.net [69.17.117.4]) by postgresql.org (Postfix) with ESMTP id 72BD29FA1B0 for ; Tue, 16 May 2006 00:15:14 -0300 (ADT) Received: (qmail 13272 invoked from network); 16 May 2006 03:15:13 -0000 Received: from dsl093-038-250.pdx1.dsl.speakeasy.net (HELO [192.168.1.20]) (davidw@[66.93.38.250]) (envelope-sender ) by mail2.sea5.speakeasy.net (qmail-ldap-1.03) with RC4-SHA encrypted SMTP for ; 16 May 2006 03:15:13 -0000 Mime-Version: 1.0 (Apple Message framework v750) Content-Transfer-Encoding: 7bit Message-Id: <2D4C0FA5-DB75-45EA-91D2-7B4F6DD55083@kineticode.com> Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed To: pgsql-performance From: David Wheeler Subject: IMMUTABLE? Date: Mon, 15 May 2006 20:15:11 -0700 X-Mailer: Apple Mail (2.750) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/336 X-Sequence-Number: 19123 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