Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id B385A1337B50 for ; Wed, 13 Oct 2010 04:40:10 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 73659-06 for ; Wed, 13 Oct 2010 07:40:04 +0000 (UTC) X-Greylist: delayed 00:09:07.546093 by SQLgrey-1.7.6 Received: from lerner.co.il (unknown [69.55.232.87]) by mail.postgresql.org (Postfix) with ESMTP id 5A8EF1337B4B for ; Wed, 13 Oct 2010 04:40:03 -0300 (ADT) Received: by lerner.co.il (Postfix, from userid 1003) id 49E785D6D08; Wed, 13 Oct 2010 09:30:53 +0200 (IST) Received: from squirrelmail.lerner.co.il (localhost [127.0.0.1]) by lerner.co.il (Postfix) with ESMTP id EE8615D6C86; Wed, 13 Oct 2010 09:30:45 +0200 (IST) Received: from 109.186.100.66 (SquirrelMail authenticated user reuven) by squirrelmail.lerner.co.il with HTTP; Wed, 13 Oct 2010 09:30:45 +0200 Message-ID: <5549b04ab43625e39f74a859a2801ec3.squirrel@squirrelmail.lerner.co.il> Date: Wed, 13 Oct 2010 09:30:45 +0200 Subject: SQL functions vs. PL/PgSQL functions From: "Reuven M. Lerner" To: pgsql-performance@postgresql.org User-Agent: SquirrelMail/1.4.19 MIME-Version: 1.0 Content-Type: text/plain;charset=UTF-8 Content-Transfer-Encoding: 8bit X-Priority: 3 (Normal) Importance: Normal X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.593 tagged_above=-10 required=5 tests=BAYES_50=0.8, RDNS_NONE=0.793 X-Spam-Level: * X-Archive-Number: 201010/195 X-Sequence-Number: 40614 Hi, everyone. I'm working with a client to try to optimize their use of PostgreSQL. They're running 8.3 on a Windows platform, packaged as part of a physical product that is delivered to customers. We're planning to upgrade to 9.0 at some point in the coming months, but this question is relevant for 8.3 (and perhaps beyond). All of the database-related logic for this application is in server-side functions, written in PL/PgSQL. That is, the application never issues a SELECT or INSERT; rather, it invokes a function with parameters, and the function handles the query. It's not unusual for a function to invoke one or more other PL/PgSQL functions as part of its execution. Since many of these PL/PgSQL functions are just acting as wrappers around queries, I thought that it would be a cheap speedup for us to change some of them to SQL functions, rather than PL/PgSQL. After all, PL/PgSQL is (I thought) interpreted, whereas SQL functions can be inlined and handled directly by the optimizer and such. We made the change to one or two functions, and were rather surprised to see the performance drop by quite a bit. My question is whether this is somehow to be expected. Under what conditions will SQL functions be slower than PL/PgSQL functions? Is there a heuristic that I can/should use to know this in advance? Does it matter if the SELECT being executed operates against a table, or a PL/PgSQL function? Thanks in advance for any insights everyone can offer. Reuven -- Reuven M. Lerner -- Web development, consulting, and training Mobile: +972-54-496-8405 * US phone: 847-230-9795 Skype/AIM: reuvenlerner