Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 7ED176326E5 for ; Thu, 12 Aug 2010 09:23:06 -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 05724-03 for ; Thu, 12 Aug 2010 12:22:59 +0000 (UTC) Received: from news.hub.org (news.hub.org [200.46.204.72]) by mail.postgresql.org (Postfix) with ESMTP id 6B2DA6324DF for ; Thu, 12 Aug 2010 09:22:59 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.14.4/8.14.4) with ESMTP id o7CCMofG093881 for ; Thu, 12 Aug 2010 09:22:50 -0300 (ADT) (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.14.4/8.14.4/Submit) id o7CC1YW0028962 for pgsql-performance@postgresql.org; Thu, 12 Aug 2010 09:01:34 -0300 (ADT) (envelope-from news) From: "Davor J." X-Newsgroups: pgsql.performance Subject: Re: Slow function in queries SELECT clause. Date: Thu, 12 Aug 2010 14:00:36 +0200 Organization: Hub.Org Networking Services Lines: 35 Message-ID: References: <25116.1277047267@sss.pgh.pa.us> X-Complaints-To: usenet@news.hub.org X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 X-RFC2646: Format=Flowed; Response X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5931 To: pgsql-performance@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.79 tagged_above=-10 required=5 tests=BAYES_50=0.8, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201008/146 X-Sequence-Number: 39989 > "Tom Lane" wrote in message > news:25116.1277047267@sss.pgh.pa.us... >> "Davor J." writes: >>> Suppose 2 functions: factor(int,int) and offset(int, int). >>> Suppose a third function: convert(float,int,int) which simply returns >>> $1*factor($2,$3)+offset($2,$3) >>> All three functions are IMMUTABLE. >> >> You should write the third function as a SQL function, which'd allow it >> to be inlined. >> >>> VERY FAST (half a second): >>> ---------------- >>> SELECT data*factor(1,2)+offset(1,2) FROM tbl_data; >> >> In this case both factor() calls are folded to constants, hence executed >> only once. >> >>> VERY SLOW (a minute): >>> ---------------- >>> SELECT convert(data, 1, 2) FROM tbl_data; >> >> Without inlining, there's no hope of any constant-folding here. >> The optimizer just sees the plpgsql function as a black box and >> can't do anything with it. >> > Your concepts of "inlining" and "black box" really cleared things up for > me. With fnc_unit_convert() written in SQL and declared as STABLE I indeed > have fast performance now. A note on performance here: If I declare the fast SQL function fnc_unit_convert() as STRICT or as SECURITY DEFINER, then I suddenly get slow performance again (i.e. no apparent inlining).