From stonec.register@sympatico.ca Fri Jan 27 00:09:31 2012 Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id A85981F07123 for ; Thu, 26 Jan 2012 20:09:31 -0400 (AST) Received: from toroondcbmts08-srv.bellnexxia.net ([207.236.237.42]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RqZNg-0004Qi-4W for pgsql-performance@postgresql.org; Fri, 27 Jan 2012 00:09:30 +0000 Received: from toip59-bus.srvr.bell.ca ([67.69.240.186]) by toroondcbmts08-srv.bellnexxia.net (InterMail vM.8.00.01.00 201-2244-105-20090324) with ESMTP id <20120127000914.DTTA10851.toroondcbmts08-srv.bellnexxia.net@toip59-bus.srvr.bell.ca> for ; Thu, 26 Jan 2012 19:09:14 -0500 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Av4EAELjIU9GMVPj/2dsb2JhbABCgk2sA4EFgXkIfgUGEBQ+GiUBBBsDoAKfaok+MhECDoJnSQNRBWGCOWMEiD+FBwGNUYx2 Received: from unknown (HELO CAPRICA) ([70.49.83.227]) by toip59-bus.srvr.bell.ca with ESMTP; 26 Jan 2012 19:09:14 -0500 From: "Carlo Stonebanks" To: Subject: pl/pgsql functions outperforming sql ones? Date: Thu, 26 Jan 2012 19:09:13 -0500 Message-ID: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0AEB_01CCDC5D.FE101D80" X-Mailer: Microsoft Office Outlook 11 Thread-Index: Aczch7aKwtMGJ1uqQTmh/tUUxVbzuw== X-MimeOLE: Produced By Microsoft MimeOLE V6.1.7601.17609 X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201201/78 X-Sequence-Number: 46090 This is a multi-part message in MIME format. ------=_NextPart_000_0AEB_01CCDC5D.FE101D80 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Assuming there was some sort of cost to pl/pgsql, I rewrote a bunch of stored functions s in straight SQL. Each stored proc was calling the next, so to get the full effect I had to track down all the pl/pgsql stored functions and convert them to sql. However, I was surprised to find after all of the rewrites, the LANGUAGE sql procs caused the queries to run slower than the LANGUAGE plpgsql. None of the stored functions selected from tables, the operated on and returned scalar values - it was all assign variables, if/then/else - not even any looping. For those who need the dirty details, here they are. If you happen to think this behavior is expected, I needn't bore you - just let me know! Thanks, Carlo This was all triggered during the optimization of a query like this: SELECT myVar FROM myTable WHERE myFunc(myVar); Looking at EXPLAIN ANALYSE I saw something like this: Filter: myFunc(myVar) I rewrote the body of myFunc(myVar) something like this: SELECT CASE WHEN myVar IS NULL THEN false ELSE myOtherFunc(myVar) END When I reran EXPLAIN ANALYZE I got this: Filter: SELECT CASE WHEN myVar IS NULL THEN false ELSE myOtherFunc(myVar) END Nice. So, I did the same treatment to myOtherFunc() (converted to straight sql) but the EXPLAIN ANALYZE didn't change (reasonable, I guess - how deep would I expect it to go?) All of the procs were IMMUTABLE. I was very surprised to find that the query now ran much slower by a factor of 4. ------=_NextPart_000_0AEB_01CCDC5D.FE101D80 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

Assuming there was some sort of cost to = pl/pgsql, I rewrote a bunch of stored functions s in straight SQL. Each stored proc = was calling the next, so to get the full effect I had to track down all the pl/pgsql stored functions and convert them to sql. However, I was = surprised to find after all of the rewrites, the LANGUAGE sql procs caused the = queries to run slower than the LANGUAGE plpgsql.

 

None of the stored functions selected from = tables, the operated on and returned scalar values - it was all assign variables, = if/then/else - not even any looping.

 

For those who need the dirty details, here = they are. If you happen to think this behavior is expected, I needn’t bore = you – just let me know!

 

Thanks,

 

Carlo

 

This was all triggered during the = optimization of a query like this:

 

SELECT myVar

FROM myTable

WHERE = myFunc(myVar);

 

Looking at EXPLAIN ANALYSE I saw something = like this:

 

Filter: = myFunc(myVar)

 

I rewrote the body of myFunc(myVar) something = like this:

 

SELECT CASE WHEN myVar IS NULL THEN false = ELSE myOtherFunc(myVar) END

 

When I reran EXPLAIN ANALYZE I got = this:

 

Filter: SELECT CASE WHEN myVar IS NULL THEN = false ELSE myOtherFunc(myVar) END

 

Nice. So, I did the same treatment to = myOtherFunc() (converted to straight sql) but the EXPLAIN ANALYZE didn’t change = (reasonable, I guess – how deep would I expect it to = go?)

 

All of the procs were = IMMUTABLE.

 

I was very surprised to find that the query = now ran much slower by a factor of 4.

 

 

------=_NextPart_000_0AEB_01CCDC5D.FE101D80-- From mmoncure@gmail.com Fri Jan 27 15:47:24 2012 Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id DDEB81259185 for ; Fri, 27 Jan 2012 11:47:24 -0400 (AST) Received: from mail-gy0-f174.google.com ([209.85.160.174]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Rqo1K-0004sA-Iz for pgsql-performance@postgresql.org; Fri, 27 Jan 2012 15:47:24 +0000 Received: by ghrr11 with SMTP id r11so838963ghr.19 for ; Fri, 27 Jan 2012 07:47:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=LXhq1JpBbWfnVKEgLCrhyL1/Y5F8ySrIgtFFqwEHYxw=; b=xkorisBUUF4xXsWscNOhoSNK/nP/h8IdgRP9a364zgkqZYd/t60X5qOIWPp0OkRNIk voXU4wp9f1ocpN5akHqvXi2GroqZLNccM0YARQjKNWbK3fkCeIrQudpNkjKYe4m3aaS+ l9d5o64bGU6RUke/fEf10RJM7Efra2vofZ74s= MIME-Version: 1.0 Received: by 10.101.11.28 with SMTP id o28mr3437188ani.70.1327679229645; Fri, 27 Jan 2012 07:47:09 -0800 (PST) Received: by 10.236.156.161 with HTTP; Fri, 27 Jan 2012 07:47:09 -0800 (PST) In-Reply-To: References: Date: Fri, 27 Jan 2012 09:47:09 -0600 Message-ID: Subject: Re: pl/pgsql functions outperforming sql ones? From: Merlin Moncure To: Carlo Stonebanks Cc: pgsql-performance@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 X-Pg-Spam-Score: -2.7 (--) X-Archive-Number: 201201/88 X-Sequence-Number: 46100 On Thu, Jan 26, 2012 at 6:09 PM, Carlo Stonebanks wrote: > Assuming there was some sort of cost to pl/pgsql, I rewrote a bunch of > stored functions s in straight SQL. Each stored proc was calling the next, > so to get the full effect I had to track down all the pl/pgsql stored > functions and convert them to sql. However, I was surprised to find after > all of the rewrites, the LANGUAGE sql procs caused the queries to run slower > than the LANGUAGE plpgsql. One reason that plpgsql can outperform sql functions is that plpgsql caches plans. That said, I don't think that's what's happening here. Did you confirm the performance difference outside of EXPLAIN ANALYZE? In particular cases EXPLAIN ANALYZE can skew times, either by injecting time calls or in how it discards results. merlin From stonec.register@sympatico.ca Fri Jan 27 18:37:12 2012 Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 01F1F1259185 for ; Fri, 27 Jan 2012 14:37:12 -0400 (AST) Received: from toroondcbmts07.bellnexxia.net ([207.236.237.41] helo=toroondcbmts07-srv.bellnexxia.net) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Rqqfe-0000Nw-GG for pgsql-performance@postgresql.org; Fri, 27 Jan 2012 18:37:12 +0000 Received: from toip40-bus.srvr.bell.ca ([67.69.240.41]) by toroondcbmts07-srv.bellnexxia.net (InterMail vM.8.00.01.00 201-2244-105-20090324) with ESMTP id <20120127183656.SDTN21786.toroondcbmts07-srv.bellnexxia.net@toip40-bus.srvr.bell.ca>; Fri, 27 Jan 2012 13:36:56 -0500 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgoFAILhIk9GMVPj/2dsb2JhbABDoiOMLoEFgXIBAQUIMj8MAQMCBgMNAQMEAQEBCR4HGQgSEwkIAQEEEwgDwCWJEAEkMhECCwOCZ0kDgSWCS2MEiD+FBAGNT4UJh2k Received: from unknown (HELO CAPRICA) ([70.49.83.227]) by toip40-bus.srvr.bell.ca with ESMTP; 27 Jan 2012 13:36:49 -0500 From: "Carlo Stonebanks" To: "'Merlin Moncure'" Cc: References: In-Reply-To: Subject: Re: pl/pgsql functions outperforming sql ones? Date: Fri, 27 Jan 2012 13:36:49 -0500 Message-ID: <90C49E334DFE48C582A3214BA56E1DD4@CAPRICA> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 11 Thread-Index: AczdCvSZ546mRk+1TyuPzcMZSjMcCAAFlZxQ X-MimeOLE: Produced By Microsoft MimeOLE V6.1.7601.17609 X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201201/94 X-Sequence-Number: 46106 Yes, I did test it - i.e. I ran the functions on their own as I had always noticed a minor difference between EXPLAIN ANALYZE results and direct query calls. Interesting, so sql functions DON'T cache plans? Will plan-caching be of any benefit to SQL that makes no reference to any tables? The SQL is emulating the straight non-set-oriented procedural logic of the original plpgsql. -----Original Message----- From: Merlin Moncure [mailto:mmoncure@gmail.com] Sent: January 27, 2012 10:47 AM To: Carlo Stonebanks Cc: pgsql-performance@postgresql.org Subject: Re: [PERFORM] pl/pgsql functions outperforming sql ones? On Thu, Jan 26, 2012 at 6:09 PM, Carlo Stonebanks wrote: > Assuming there was some sort of cost to pl/pgsql, I rewrote a bunch of > stored functions s in straight SQL. Each stored proc was calling the next, > so to get the full effect I had to track down all the pl/pgsql stored > functions and convert them to sql. However, I was surprised to find after > all of the rewrites, the LANGUAGE sql procs caused the queries to run slower > than the LANGUAGE plpgsql. One reason that plpgsql can outperform sql functions is that plpgsql caches plans. That said, I don't think that's what's happening here. Did you confirm the performance difference outside of EXPLAIN ANALYZE? In particular cases EXPLAIN ANALYZE can skew times, either by injecting time calls or in how it discards results. merlin From fecastle@gmail.com Fri Jan 27 19:29:22 2012 Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id EE8461259185 for ; Fri, 27 Jan 2012 15:29:22 -0400 (AST) Received: from mail-lpp01m010-f46.google.com ([209.85.215.46]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RqrU7-0006KG-71 for pgsql-performance@postgresql.org; Fri, 27 Jan 2012 19:29:23 +0000 Received: by lagu2 with SMTP id u2so1156940lag.19 for ; Fri, 27 Jan 2012 11:29:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=3o7lTSXWhQHP7jHaHxIXtnH/ma1l9DRRTU8APzuLIH0=; b=tp3IvF9w0tIC7VjskfOghycVH2lWANMqNVJ8O96ls6OyPCoKe1BCUH/8P6ePyP7kiQ GxDAxPsK2kkwvN4PXnFz2rR3zX5PFrbck+ipvFFgzv2POM9Xu/LLWh75z2mh9GkaQjc1 KN88eD3ZJkMMYdSn1id/AvNAuulSMen7iLpqA= MIME-Version: 1.0 Received: by 10.112.85.199 with SMTP id j7mr2153939lbz.26.1327692545977; Fri, 27 Jan 2012 11:29:05 -0800 (PST) Received: by 10.112.44.167 with HTTP; Fri, 27 Jan 2012 11:29:05 -0800 (PST) In-Reply-To: <90C49E334DFE48C582A3214BA56E1DD4@CAPRICA> References: <90C49E334DFE48C582A3214BA56E1DD4@CAPRICA> Date: Fri, 27 Jan 2012 12:29:05 -0700 Message-ID: Subject: Re: pl/pgsql functions outperforming sql ones? From: Deron To: Carlo Stonebanks Cc: pgsql-performance@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201201/95 X-Sequence-Number: 46107 You can use PREPARE... EXECUTE to "cache" the plan (as well as parsing). However, I find it unlikely this will would explain the loss in performance you experienced. Deron On Fri, Jan 27, 2012 at 11:36 AM, Carlo Stonebanks wrote: > Yes, I did test it =A0- i.e. I ran the functions on their own as I had al= ways > noticed a minor difference between EXPLAIN ANALYZE results and direct que= ry > calls. > > Interesting, so sql functions DON'T cache plans? Will plan-caching be of = any > benefit to SQL that makes no reference to any tables? The SQL is emulatin= g > the straight non-set-oriented procedural logic of the original plpgsql. > > -----Original Message----- > From: Merlin Moncure [mailto:mmoncure@gmail.com] > Sent: January 27, 2012 10:47 AM > To: Carlo Stonebanks > Cc: pgsql-performance@postgresql.org > Subject: Re: [PERFORM] pl/pgsql functions outperforming sql ones? > > On Thu, Jan 26, 2012 at 6:09 PM, Carlo Stonebanks > wrote: >> Assuming there was some sort of cost to pl/pgsql, I rewrote a bunch of >> stored functions s in straight SQL. Each stored proc was calling the nex= t, >> so to get the full effect I had to track down all the pl/pgsql stored >> functions and convert them to sql. However, I was surprised to find afte= r >> all of the rewrites, the LANGUAGE sql procs caused the queries to run > slower >> than the LANGUAGE plpgsql. > > One reason that plpgsql can outperform sql functions is that plpgsql > caches plans. =A0That said, I don't think that's what's happening here. > Did you confirm the performance difference outside of EXPLAIN ANALYZE? > =A0In particular cases EXPLAIN ANALYZE can skew times, either by > injecting time calls or in how it discards results. > > merlin > > > -- > Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org= ) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-performance From stonec.register@sympatico.ca Fri Jan 27 19:59:29 2012 Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 20FD91259185 for ; Fri, 27 Jan 2012 15:59:29 -0400 (AST) Received: from toroondcbmts05.bellnexxia.net ([207.236.237.39] helo=toroondcbmts05-srv.bellnexxia.net) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RqrxG-0001xk-8y for pgsql-performance@postgresql.org; Fri, 27 Jan 2012 19:59:28 +0000 Received: from toip40-bus.srvr.bell.ca ([67.69.240.41]) by toroondcbmts05-srv.bellnexxia.net (InterMail vM.8.00.01.00 201-2244-105-20090324) with ESMTP id <20120127195912.TONM9955.toroondcbmts05-srv.bellnexxia.net@toip40-bus.srvr.bell.ca>; Fri, 27 Jan 2012 14:59:12 -0500 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgsFAK/9Ik9GMVPj/2dsb2JhbABEoiOML4EFgXIBAQUIZAIIAwwBAwIGAw0EBAEBAQkEGgcZCBITCQgBAQQTCAECh3m4EokPJjIRAg6CZ0kDgSQBgktjBIgMM4UEAY1PhQmHaQ Received: from unknown (HELO CAPRICA) ([70.49.83.227]) by toip40-bus.srvr.bell.ca with ESMTP; 27 Jan 2012 14:59:08 -0500 From: "Carlo Stonebanks" To: "'Deron'" Cc: References: <90C49E334DFE48C582A3214BA56E1DD4@CAPRICA> In-Reply-To: Subject: Re: pl/pgsql functions outperforming sql ones? Date: Fri, 27 Jan 2012 14:59:08 -0500 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Office Outlook 11 Thread-Index: AczdKfMrffLFPMKcR/m7MD0XtM7aBgAA+mKw X-MimeOLE: Produced By Microsoft MimeOLE V6.1.7601.17609 X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201201/97 X-Sequence-Number: 46109 Was I even right in thinking I would gain any performance by converting = to SQL? -----Original Message----- From: Deron [mailto:fecastle@gmail.com]=20 Sent: January 27, 2012 2:29 PM To: Carlo Stonebanks Cc: pgsql-performance@postgresql.org Subject: Re: [PERFORM] pl/pgsql functions outperforming sql ones? You can use PREPARE... EXECUTE to "cache" the plan (as well as parsing). However, I find it unlikely this will would explain the loss in performance you experienced. Deron On Fri, Jan 27, 2012 at 11:36 AM, Carlo Stonebanks wrote: > Yes, I did test it =A0- i.e. I ran the functions on their own as I had always > noticed a minor difference between EXPLAIN ANALYZE results and direct query > calls. > > Interesting, so sql functions DON'T cache plans? Will plan-caching be = of any > benefit to SQL that makes no reference to any tables? The SQL is = emulating > the straight non-set-oriented procedural logic of the original = plpgsql. > > -----Original Message----- > From: Merlin Moncure [mailto:mmoncure@gmail.com] > Sent: January 27, 2012 10:47 AM > To: Carlo Stonebanks > Cc: pgsql-performance@postgresql.org > Subject: Re: [PERFORM] pl/pgsql functions outperforming sql ones? > > On Thu, Jan 26, 2012 at 6:09 PM, Carlo Stonebanks > wrote: >> Assuming there was some sort of cost to pl/pgsql, I rewrote a bunch = of >> stored functions s in straight SQL. Each stored proc was calling the next, >> so to get the full effect I had to track down all the pl/pgsql stored >> functions and convert them to sql. However, I was surprised to find = after >> all of the rewrites, the LANGUAGE sql procs caused the queries to run > slower >> than the LANGUAGE plpgsql. > > One reason that plpgsql can outperform sql functions is that plpgsql > caches plans. =A0That said, I don't think that's what's happening = here. > Did you confirm the performance difference outside of EXPLAIN ANALYZE? > =A0In particular cases EXPLAIN ANALYZE can skew times, either by > injecting time calls or in how it discards results. > > merlin > > > -- > Sent via pgsql-performance mailing list = (pgsql-performance@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-performance From agentm@themactionfaction.com Fri Jan 27 20:09:42 2012 Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id BED7B1259187 for ; Fri, 27 Jan 2012 16:09:42 -0400 (AST) Received: from inconvenient.huggl.es ([78.46.85.58]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Rqs7B-0006xT-Kc for pgsql-performance@postgresql.org; Fri, 27 Jan 2012 20:09:42 +0000 Received: from [10.0.1.41] (rrcs-184-74-226-63.nyc.biz.rr.com [184.74.226.63]) by inconvenient.huggl.es (Postfix) with ESMTPSA id 8769130841A for ; Fri, 27 Jan 2012 20:09:27 +0000 (GMT) Content-Type: text/plain; charset=iso-8859-1 Mime-Version: 1.0 (Apple Message framework v1251.1) Subject: Re: pl/pgsql functions outperforming sql ones? From: "A.M." In-Reply-To: Date: Fri, 27 Jan 2012 15:09:26 -0500 Content-Transfer-Encoding: quoted-printable Message-Id: <5A6D37A1-9FD9-4011-B61E-FA383CA43B7E@themactionfaction.com> References: <90C49E334DFE48C582A3214BA56E1DD4@CAPRICA> To: pgsql-performance@postgresql.org X-Mailer: Apple Mail (2.1251.1) X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201201/98 X-Sequence-Number: 46110 On Jan 27, 2012, at 2:59 PM, Carlo Stonebanks wrote: > Was I even right in thinking I would gain any performance by = converting to > SQL? As always, it depends. I converted an immutable pl/pgsql function to an = SQL function and the body of the function barely changed. However, I = experienced an order-of-magnitude speed-up because the SQL function = could be folded into the plan (like a view) while a pl/pgsql function = will never be folded (and the planner punts and assumes the function = will return 100 rows for set-returning functions). However, not all SQL = functions can be folded into the plan. On the other hand, a pl/pgsql function can make use of memoization for = number-crunching routines and make business-logical short-circuiting = decisions. Cheers, M= From pavel.stehule@gmail.com Sat Jan 28 06:38:28 2012 Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id AF43A53811F for ; Sat, 28 Jan 2012 02:38:28 -0400 (AST) Received: from mail-qw0-f53.google.com ([209.85.216.53]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Rr1vg-00084u-HG for pgsql-performance@postgresql.org; Sat, 28 Jan 2012 06:38:28 +0000 Received: by qafk1 with SMTP id k1so756693qaf.19 for ; Fri, 27 Jan 2012 22:38:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=WZE1/iTvBj8EZyDaIIRwlL94KxX2Bi+WXkC8HK9QdR0=; b=Tu12ZU2Vkw4jBY6KSCwr5+FayxvFFlkKz10H6o2opbIOumiy45opitTOSIoLSlDICb 91fNRo+msBt2uq6ADxzvsxFstx4jNXvcfCbEChSSPtLUEK+trDNqYem6gpMn6e1mFv0x 3I9BaAHlJhVGaUetOUheEscMVsZJp9flR+1wQ= Received: by 10.224.111.147 with SMTP id s19mr12326343qap.45.1327732696134; Fri, 27 Jan 2012 22:38:16 -0800 (PST) MIME-Version: 1.0 Received: by 10.229.211.145 with HTTP; Fri, 27 Jan 2012 22:37:36 -0800 (PST) In-Reply-To: <90C49E334DFE48C582A3214BA56E1DD4@CAPRICA> References: <90C49E334DFE48C582A3214BA56E1DD4@CAPRICA> From: Pavel Stehule Date: Sat, 28 Jan 2012 07:37:36 +0100 Message-ID: Subject: Re: pl/pgsql functions outperforming sql ones? To: Carlo Stonebanks Cc: Merlin Moncure , pgsql-performance@postgresql.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201201/103 X-Sequence-Number: 46115 2012/1/27 Carlo Stonebanks : > Yes, I did test it =C2=A0- i.e. I ran the functions on their own as I had= always > noticed a minor difference between EXPLAIN ANALYZE results and direct que= ry > calls. > > Interesting, so sql functions DON'T cache plans? Will plan-caching be of = any > benefit to SQL that makes no reference to any tables? The SQL is emulatin= g > the straight non-set-oriented procedural logic of the original plpgsql. > It is not necessary usually - simple SQL functions are merged to outer query - there are e few cases where this optimization cannot be processed and then there are performance lost. For example this optimization is not possible (sometimes) when some parameter is volatile Regards Pavel Stehule From stonec.register@sympatico.ca Sun Jan 29 04:20:57 2012 Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id A74FBCEC21F for ; Sun, 29 Jan 2012 00:20:57 -0400 (AST) Received: from toroondcbmts06.bellnexxia.net ([207.236.237.40] helo=toroondcbmts06-srv.bellnexxia.net) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RrMG6-0002O0-IL for pgsql-performance@postgresql.org; Sun, 29 Jan 2012 04:20:56 +0000 Received: from toip53-bus.srvr.bell.ca ([67.69.240.54]) by toroondcbmts06-srv.bellnexxia.net (InterMail vM.8.00.01.00 201-2244-105-20090324) with ESMTP id <20120129042040.MOOM8650.toroondcbmts06-srv.bellnexxia.net@toip53-bus.srvr.bell.ca>; Sat, 28 Jan 2012 23:20:40 -0500 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Av8EAGq7JE9GMVPj/2dsb2JhbABCrliBBYFyAQEFCGYLDAEDAgYDDQQEAQEKBBoHGRoTCQgBAQQTCAECh3q4EIgqESYyEQIOgmdJA4EkAYJLYwSIDDOFBAGNTox3 Received: from unknown (HELO CAPRICA) ([70.49.83.227]) by toip53-bus.srvr.bell.ca with ESMTP; 28 Jan 2012 23:20:40 -0500 From: "Carlo Stonebanks" To: "'Pavel Stehule'" Cc: "'Merlin Moncure'" , References: <90C49E334DFE48C582A3214BA56E1DD4@CAPRICA> In-Reply-To: Subject: Re: pl/pgsql functions outperforming sql ones? Date: Sat, 28 Jan 2012 23:20:40 -0500 Message-ID: <0238E40E527049828C48675488422F6D@CAPRICA> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Office Outlook 11 Thread-Index: Aczdh483IUOBsrbCSdGdWu6mwhjAKQAs7d3Q X-MimeOLE: Produced By Microsoft MimeOLE V6.1.7601.17609 X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201201/120 X-Sequence-Number: 46132 Update: The main stored function in question and all of its sub sub-functions were recoded to new pure sql functions.=20 I then stub tested the sub functions sql vs. plpgsql. Here were the results for new sql vs old plpgsql: Individual sub functions tested 20-30% faster But the main function calling new sql sub functions ran 100% slower So I tried this: I modified the old plpgsql function to call the new sql sub functions. THAT ran 20-30% faster then the unmodified version. That modified function is listed below. All the functions ending in 2 = are the new SQL versions. Any thoughts or insight would be much appreciated. Carlo CREATE OR REPLACE FUNCTION mdx_lib.lex_compare_candidate3(character = varying, character varying) RETURNS numeric AS $BODY$ /* Rate two strings candidacy for lex_compare. param 1: first string to compare param 2: 2nd string to compare returns: numeric result like mdx_lib.lex_distance 0 is a failure, 1 a perfect match */ declare str1 varchar =3D $1; str2 varchar =3D $2; acro1 varchar; acro2 varchar; str_dist numeric; acro_dist numeric; result numeric; begin if str1 =3D str2 then result =3D 0; else str1 =3D lower(regexp_replace(str1, '[^[:alnum:]]', '', 'g')); str2 =3D lower(regexp_replace(str2, '[^[:alnum:]]', '', 'g')); if str1 =3D str2 then result =3D 0.1; else str_dist =3D mdx_lib.lex_distance2(str1, str2); acro1 =3D mdx_lib.lex_acronym2(str1); acro2 =3D mdx_lib.lex_acronym2(str2); acro_dist =3D mdx_lib.lex_distance2(acro1, acro2); result =3D (acro_dist + (str_dist * 2)) / 2; end if; end if; result =3D 1 - result; if result < 0 then result =3D 0; end if; return result; end; $BODY$ LANGUAGE plpgsql IMMUTABLE COST 100; -----Original Message----- From: pgsql-performance-owner@postgresql.org [mailto:pgsql-performance-owner@postgresql.org] On Behalf Of Pavel = Stehule Sent: January 28, 2012 1:38 AM To: Carlo Stonebanks Cc: Merlin Moncure; pgsql-performance@postgresql.org Subject: Re: [PERFORM] pl/pgsql functions outperforming sql ones? 2012/1/27 Carlo Stonebanks : > Yes, I did test it =A0- i.e. I ran the functions on their own as I had always > noticed a minor difference between EXPLAIN ANALYZE results and direct query > calls. > > Interesting, so sql functions DON'T cache plans? Will plan-caching be = of any > benefit to SQL that makes no reference to any tables? The SQL is = emulating > the straight non-set-oriented procedural logic of the original = plpgsql. > It is not necessary usually - simple SQL functions are merged to outer query - there are e few cases where this optimization cannot be processed and then there are performance lost. For example this optimization is not possible (sometimes) when some parameter is volatile Regards Pavel Stehule --=20 Sent via pgsql-performance mailing list = (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance From stonec.register@sympatico.ca Sun Jan 29 23:05:10 2012 Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 6DDB3E60239 for ; Sun, 29 Jan 2012 19:05:10 -0400 (AST) Received: from toroondcbmts08.bellnexxia.net ([207.236.237.42] helo=toroondcbmts08-srv.bellnexxia.net) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Rrdo4-0007oz-Du for pgsql-performance@postgresql.org; Sun, 29 Jan 2012 23:05:10 +0000 Received: from toip41-bus.srvr.bell.ca ([67.69.240.42]) by toroondcbmts08-srv.bellnexxia.net (InterMail vM.8.00.01.00 201-2244-105-20090324) with ESMTP id <20120129230453.YGZT10851.toroondcbmts08-srv.bellnexxia.net@toip41-bus.srvr.bell.ca>; Sun, 29 Jan 2012 18:04:53 -0500 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Av8EAAnMJU9GMVPj/2dsb2JhbABDrlqBBYFyAQEFCGYLDAEDAgYDDQQEAQEKBBoHGRoTCQgBAQQTCAECh3q3X4gtDykEKRgHBoJ1P4NhYwSIDDOFBAGNTox3 Received: from unknown (HELO CAPRICA) ([70.49.83.227]) by toip41-bus.srvr.bell.ca with ESMTP; 29 Jan 2012 18:04:53 -0500 From: "Carlo Stonebanks" To: "'Pavel Stehule'" Cc: "'Merlin Moncure'" , References: <90C49E334DFE48C582A3214BA56E1DD4@CAPRICA> In-Reply-To: Subject: Re: pl/pgsql functions outperforming sql ones? Date: Sun, 29 Jan 2012 18:04:53 -0500 Message-ID: <5EE15F7F99964B33A11D6029F39A4FE8@CAPRICA> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Office Outlook 11 Thread-Index: Aczdh483IUOBsrbCSdGdWu6mwhjAKQBULP+A X-MimeOLE: Produced By Microsoft MimeOLE V6.1.7601.17609 X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201201/113 X-Sequence-Number: 46125 Pavel, are you saying that the code of the stored function is actually = being added to the SQL query, instead of a call to it? For example, I have = seen this: SELECT myVar FROM myTable WHERE myVar > 0 AND myFunc(myVar) And seen the SQL body of myVar appended to the outer query: ... Filter: SELECT CASE WHERE myVar < 10 THEN true ELSE false END Is this what we are talking about? Two questions: 1) Is this also done when the function is called as a SELECT column;=20 e.g. would: SELECT myFunc(myVar) AS result=20 - become: SELECT ( SELECT CASE WHERE myVar < 10 THEN true ELSE false END ) AS result? 2) Does that not bypass the benefits of IMMUTABLE? -----Original Message----- From: pgsql-performance-owner@postgresql.org [mailto:pgsql-performance-owner@postgresql.org] On Behalf Of Pavel = Stehule Sent: January 28, 2012 1:38 AM To: Carlo Stonebanks Cc: Merlin Moncure; pgsql-performance@postgresql.org Subject: Re: [PERFORM] pl/pgsql functions outperforming sql ones? 2012/1/27 Carlo Stonebanks : > Yes, I did test it =A0- i.e. I ran the functions on their own as I had always > noticed a minor difference between EXPLAIN ANALYZE results and direct query > calls. > > Interesting, so sql functions DON'T cache plans? Will plan-caching be = of any > benefit to SQL that makes no reference to any tables? The SQL is = emulating > the straight non-set-oriented procedural logic of the original = plpgsql. > It is not necessary usually - simple SQL functions are merged to outer query - there are e few cases where this optimization cannot be processed and then there are performance lost. For example this optimization is not possible (sometimes) when some parameter is volatile Regards Pavel Stehule --=20 Sent via pgsql-performance mailing list = (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance From pavel.stehule@gmail.com Mon Jan 30 07:57:52 2012 Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id BC98FBED7F2 for ; Mon, 30 Jan 2012 03:57:52 -0400 (AST) Received: from mail-qy0-f174.google.com ([209.85.216.174]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Rrm7c-0008FT-A5 for pgsql-performance@postgresql.org; Mon, 30 Jan 2012 07:57:53 +0000 Received: by qcsg15 with SMTP id g15so2060974qcs.19 for ; Sun, 29 Jan 2012 23:57:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=w29YH73BSD9v/if4P/eNiTvTg4AWKIrN96dRtm8QYBc=; b=ND5082IJH1rbJrBYUyoPt9isLxE360O5qw4lXqVV1dyCLSyi5B085tjMgKyeRnS0sy OoSlDIqEPV85BbF1seym1Xgu79s7ZG3jdviH4wm1uyI7n6Gck5Yk1eb1Ih1QQq5JQ4re Xjx5Rs1FwPk/4CPFE951a4TogogkfyIFasTSQ= Received: by 10.224.116.144 with SMTP id m16mr19686000qaq.19.1327910258153; Sun, 29 Jan 2012 23:57:38 -0800 (PST) MIME-Version: 1.0 Received: by 10.229.211.145 with HTTP; Sun, 29 Jan 2012 23:56:58 -0800 (PST) In-Reply-To: <5EE15F7F99964B33A11D6029F39A4FE8@CAPRICA> References: <90C49E334DFE48C582A3214BA56E1DD4@CAPRICA> <5EE15F7F99964B33A11D6029F39A4FE8@CAPRICA> From: Pavel Stehule Date: Mon, 30 Jan 2012 08:56:58 +0100 Message-ID: Subject: Re: pl/pgsql functions outperforming sql ones? To: Carlo Stonebanks Cc: Merlin Moncure , pgsql-performance@postgresql.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201201/116 X-Sequence-Number: 46128 Hello 2012/1/30 Carlo Stonebanks : > Pavel, are you saying that the code of the stored function is actually be= ing > added to the SQL query, instead of a call to it? For example, I have seen > this: > > SELECT myVar > FROM myTable > WHERE myVar > 0 AND myFunc(myVar) > > And seen the SQL body of myVar appended to the outer query: > > ... Filter: SELECT CASE WHERE myVar < 10 THEN true ELSE false END > > Is this what we are talking about? Two questions: yes - it is SQL function "inlining" > > 1) Is this also done when the function is called as a SELECT column; > =C2=A0 e.g. would: > =C2=A0 =C2=A0 =C2=A0SELECT myFunc(myVar) AS result > =C2=A0 - become: > =C2=A0 =C2=A0 =C2=A0SELECT ( > =C2=A0 =C2=A0 =C2=A0 =C2=A0 SELECT CASE WHERE myVar < 10 THEN true ELSE f= alse END > =C2=A0 =C2=A0 =C2=A0) AS result? > yes CREATE OR REPLACE FUNCTION public.fx(integer, integer) RETURNS integer LANGUAGE sql AS $function$ select coalesce($1, $2) $function$ postgres=3D# explain verbose select fx(random()::int, random()::int); QUERY PLAN -------------------------------------------------------------- Result (cost=3D0.00..0.02 rows=3D1 width=3D0) Output: COALESCE((random())::integer, (random())::integer) (2 rows) > 2) Does that not bypass the benefits of IMMUTABLE? > no - optimizator works with expanded query - usually is preferred style a writing SQL functions without flags, because optimizer can work with definition of SQL function and can set well flags. SQL function is not black box for optimizer like plpgsql does. And SQL optimizer chooses a inlining or some other optimizations. Sometimes explicit flags are necessary, but usually not for scalar SQL functions. postgres=3D# create or replace function public.fxs(int) postgres-# returns setof int as $$ postgres$# select * from generate_series(1,$1) postgres$# $$ language sql; CREATE FUNCTION postgres=3D# explain verbose select * from fxs(10); QUERY PLAN ------------------------------------------------------------------- Function Scan on public.fxs (cost=3D0.25..10.25 rows=3D1000 width=3D4) Output: fxs Function Call: fxs(10) (3 rows) postgres=3D# create or replace function public.fxs(int) returns setof int as $$ select * from generate_series(1,$1) $$ language sql IMMUTABLE; CREATE FUNCTION postgres=3D# explain verbose select * from fxs(10); QUERY PLAN ---------------------------------------------------------------------------= -------- Function Scan on pg_catalog.generate_series (cost=3D0.00..10.00 rows=3D1000 width=3D4) Output: generate_series.generate_series Function Call: generate_series(1, 10) --<<<< inlined query (3 rows) Regards Pavel Stehule > > > -----Original Message----- > From: pgsql-performance-owner@postgresql.org > [mailto:pgsql-performance-owner@postgresql.org] On Behalf Of Pavel Stehul= e > Sent: January 28, 2012 1:38 AM > To: Carlo Stonebanks > Cc: Merlin Moncure; pgsql-performance@postgresql.org > Subject: Re: [PERFORM] pl/pgsql functions outperforming sql ones? > > 2012/1/27 Carlo Stonebanks : >> Yes, I did test it =C2=A0- i.e. I ran the functions on their own as I ha= d > always >> noticed a minor difference between EXPLAIN ANALYZE results and direct > query >> calls. >> >> Interesting, so sql functions DON'T cache plans? Will plan-caching be of > any >> benefit to SQL that makes no reference to any tables? The SQL is emulati= ng >> the straight non-set-oriented procedural logic of the original plpgsql. >> > > It is not necessary usually - simple SQL functions are merged to outer > query - there are e few cases where this optimization cannot be > processed and then there are performance lost. > > For example this optimization is not possible (sometimes) when some > parameter is volatile > > Regards > > Pavel Stehule > > -- > Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org= ) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-performance > From stonec.register@sympatico.ca Mon Jan 30 23:15:35 2012 Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 9ACDF86E04B for ; Mon, 30 Jan 2012 19:15:35 -0400 (AST) Received: from toroondcbmts06-srv.bellnexxia.net ([207.236.237.40]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Rs0Rg-0002aM-ET for pgsql-performance@postgresql.org; Mon, 30 Jan 2012 23:15:35 +0000 Received: from toip38-bus.srvr.bell.ca ([67.69.240.39]) by toroondcbmts06-srv.bellnexxia.net (InterMail vM.8.00.01.00 201-2244-105-20090324) with ESMTP id <20120130231517.OVHA8650.toroondcbmts06-srv.bellnexxia.net@toip38-bus.srvr.bell.ca>; Mon, 30 Jan 2012 18:15:17 -0500 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgkFALoeJ09GMVPj/2dsb2JhbABDoi6MKYEFgXIBAQQBCGYLBQcBAwIGAw0EBAEBCgQaBxkIEhMJCAEBBBMIAQKHcQm4YYp+KykEDQ+CeoEyKwOCR2MEiAwzhQQBjU6FDYdq Received: from unknown (HELO CAPRICA) ([70.49.83.227]) by toip38-bus.srvr.bell.ca with ESMTP; 30 Jan 2012 18:15:17 -0500 From: "Carlo Stonebanks" To: "'Pavel Stehule'" Cc: "'Merlin Moncure'" , References: <90C49E334DFE48C582A3214BA56E1DD4@CAPRICA> <5EE15F7F99964B33A11D6029F39A4FE8@CAPRICA> In-Reply-To: Subject: Re: pl/pgsql functions outperforming sql ones? Date: Mon, 30 Jan 2012 18:15:17 -0500 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Office Outlook 11 Thread-Index: AczfJNc/R67PLRWhRtWCSVlGAu7KFwAf3FIA X-MimeOLE: Produced By Microsoft MimeOLE V6.1.7601.17609 X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201201/141 X-Sequence-Number: 46153 Pavel, thank you very much for your explanation. Is it possible to define under what conditions that sql procs will outperform plpgsql ones, and vice-versa? -----Original Message----- From: Pavel Stehule [mailto:pavel.stehule@gmail.com]=20 Sent: January 30, 2012 2:57 AM To: Carlo Stonebanks Cc: Merlin Moncure; pgsql-performance@postgresql.org Subject: Re: [PERFORM] pl/pgsql functions outperforming sql ones? Hello 2012/1/30 Carlo Stonebanks : > Pavel, are you saying that the code of the stored function is actually being > added to the SQL query, instead of a call to it? For example, I have = seen > this: > > SELECT myVar > FROM myTable > WHERE myVar > 0 AND myFunc(myVar) > > And seen the SQL body of myVar appended to the outer query: > > ... Filter: SELECT CASE WHERE myVar < 10 THEN true ELSE false END > > Is this what we are talking about? Two questions: yes - it is SQL function "inlining" > > 1) Is this also done when the function is called as a SELECT column; > =A0 e.g. would: > =A0 =A0 =A0SELECT myFunc(myVar) AS result > =A0 - become: > =A0 =A0 =A0SELECT ( > =A0 =A0 =A0 =A0 SELECT CASE WHERE myVar < 10 THEN true ELSE false END > =A0 =A0 =A0) AS result? > yes CREATE OR REPLACE FUNCTION public.fx(integer, integer) RETURNS integer LANGUAGE sql AS $function$ select coalesce($1, $2) $function$ postgres=3D# explain verbose select fx(random()::int, random()::int); QUERY PLAN -------------------------------------------------------------- Result (cost=3D0.00..0.02 rows=3D1 width=3D0) Output: COALESCE((random())::integer, (random())::integer) (2 rows) > 2) Does that not bypass the benefits of IMMUTABLE? > no - optimizator works with expanded query - usually is preferred style a writing SQL functions without flags, because optimizer can work with definition of SQL function and can set well flags. SQL function is not black box for optimizer like plpgsql does. And SQL optimizer chooses a inlining or some other optimizations. Sometimes explicit flags are necessary, but usually not for scalar SQL functions. postgres=3D# create or replace function public.fxs(int) postgres-# returns setof int as $$ postgres$# select * from generate_series(1,$1) postgres$# $$ language sql; CREATE FUNCTION postgres=3D# explain verbose select * from fxs(10); QUERY PLAN ------------------------------------------------------------------- Function Scan on public.fxs (cost=3D0.25..10.25 rows=3D1000 width=3D4) Output: fxs Function Call: fxs(10) (3 rows) postgres=3D# create or replace function public.fxs(int) returns setof int as $$ select * from generate_series(1,$1) $$ language sql IMMUTABLE; CREATE FUNCTION postgres=3D# explain verbose select * from fxs(10); QUERY PLAN -------------------------------------------------------------------------= --- ------- Function Scan on pg_catalog.generate_series (cost=3D0.00..10.00 rows=3D1000 width=3D4) Output: generate_series.generate_series Function Call: generate_series(1, 10) --<<<< inlined query (3 rows) Regards Pavel Stehule > > > -----Original Message----- > From: pgsql-performance-owner@postgresql.org > [mailto:pgsql-performance-owner@postgresql.org] On Behalf Of Pavel = Stehule > Sent: January 28, 2012 1:38 AM > To: Carlo Stonebanks > Cc: Merlin Moncure; pgsql-performance@postgresql.org > Subject: Re: [PERFORM] pl/pgsql functions outperforming sql ones? > > 2012/1/27 Carlo Stonebanks : >> Yes, I did test it =A0- i.e. I ran the functions on their own as I = had > always >> noticed a minor difference between EXPLAIN ANALYZE results and direct > query >> calls. >> >> Interesting, so sql functions DON'T cache plans? Will plan-caching be = of > any >> benefit to SQL that makes no reference to any tables? The SQL is emulating >> the straight non-set-oriented procedural logic of the original = plpgsql. >> > > It is not necessary usually - simple SQL functions are merged to outer > query - there are e few cases where this optimization cannot be > processed and then there are performance lost. > > For example this optimization is not possible (sometimes) when some > parameter is volatile > > Regards > > Pavel Stehule > > -- > Sent via pgsql-performance mailing list = (pgsql-performance@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-performance > From pavel.stehule@gmail.com Tue Jan 31 04:57:21 2012 Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id EDB313ED9DB for ; Tue, 31 Jan 2012 00:57:21 -0400 (AST) Received: from mail-qy0-f174.google.com ([209.85.216.174]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Rs5mT-0006y5-3a for pgsql-performance@postgresql.org; Tue, 31 Jan 2012 04:57:22 +0000 Received: by qcqw6 with SMTP id w6so100913qcq.19 for ; Mon, 30 Jan 2012 20:57:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=alwrMA9fiOmnjkAMqHpzuZsmBzh00wAy7iQLQ7ChnEg=; b=O0/E712Jlo01RuDFRKa6w8AaFNcsuaOjAUcLxE/AEn7Vw7gHBRWTJwAebrsB1S2QTC aNjTMg6IN4bONrbVUBJwAUgaMfHiQa1TSw80B5wHa5UsmvUFg5lgNlQOG8M8J/BGxO22 BRV7hAoPp9OmItaBD8q8dxap6JV+ZoBPpvHhY= Received: by 10.229.69.21 with SMTP id x21mr3564209qci.112.1327985827637; Mon, 30 Jan 2012 20:57:07 -0800 (PST) MIME-Version: 1.0 Received: by 10.229.211.145 with HTTP; Mon, 30 Jan 2012 20:56:27 -0800 (PST) In-Reply-To: References: <90C49E334DFE48C582A3214BA56E1DD4@CAPRICA> <5EE15F7F99964B33A11D6029F39A4FE8@CAPRICA> From: Pavel Stehule Date: Tue, 31 Jan 2012 05:56:27 +0100 Message-ID: Subject: Re: pl/pgsql functions outperforming sql ones? To: Carlo Stonebanks Cc: Merlin Moncure , pgsql-performance@postgresql.org Content-Type: text/plain; charset=UTF-8 X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201201/143 X-Sequence-Number: 46155 2012/1/31 Carlo Stonebanks : > Pavel, thank you very much for your explanation. > > Is it possible to define under what conditions that sql procs will > outperform plpgsql ones, and vice-versa? yes, little bit :) when inlining is possible, then SQL function will be faster - typical use case is simple scalar functions (with nonvolatile real parameters). Regards Pavel > > -----Original Message----- From robertmhaas@gmail.com Wed Feb 8 20:33:22 2012 Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id E86FA862DB7 for ; Wed, 8 Feb 2012 16:33:22 -0400 (AST) Received: from mail-iy0-f174.google.com ([209.85.210.174]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RvECg-00041k-1O for pgsql-performance@postgresql.org; Wed, 08 Feb 2012 20:33:22 +0000 Received: by iacb35 with SMTP id b35so1378229iac.19 for ; Wed, 08 Feb 2012 12:33:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=kzQuihWCsnoe04xUO5TtRwI4K7N4p456bjB4FfKDug4=; b=WbxYMKH0j76jIudfRh3ri1fSyUqWUyGcx0pTmwbUJrIRSRsWbcpHGj8+RcregLH+n/ kGXRguLJQGp9/drKiPgUkroH23CfGN0uzBMg2k+1nan0q3CRVxCKSXjMq1XzeL8Yjs5S /Mr36wNV2jHQNFp9xb51syopcAO6aqn+wO4Xw= MIME-Version: 1.0 Received: by 10.50.169.9 with SMTP id aa9mr26096515igc.23.1328733188955; Wed, 08 Feb 2012 12:33:08 -0800 (PST) Received: by 10.50.208.104 with HTTP; Wed, 8 Feb 2012 12:33:08 -0800 (PST) In-Reply-To: <0238E40E527049828C48675488422F6D@CAPRICA> References: <90C49E334DFE48C582A3214BA56E1DD4@CAPRICA> <0238E40E527049828C48675488422F6D@CAPRICA> Date: Wed, 8 Feb 2012 15:33:08 -0500 Message-ID: Subject: Re: pl/pgsql functions outperforming sql ones? From: Robert Haas To: Carlo Stonebanks Cc: Pavel Stehule , Merlin Moncure , pgsql-performance@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 X-Pg-Spam-Score: -1.4 (-) X-Archive-Number: 201202/59 X-Sequence-Number: 46231 On Sat, Jan 28, 2012 at 11:20 PM, Carlo Stonebanks wrote: > Update: The main stored function in question and all of its sub > sub-functions were recoded to new pure sql functions. > > I then stub tested the sub functions sql vs. plpgsql. > > Here were the results for new sql vs old plpgsql: > > Individual sub functions tested 20-30% faster > > But the main function calling new sql sub functions ran 100% slower > > So I tried this: > > I modified the old plpgsql function to call the new sql sub functions. > > THAT ran 20-30% faster then the unmodified version. > > That modified function is listed below. All the functions ending in 2 are > the new SQL versions. One advantage of PL/pgsql for code like this is that you can compute values once and save them in variables. SQL doesn't have variables, so you can end up repeating the same SQL in multiple places (causing multiple evaluation), or even if you manage to avoid that, the system can inline things in multiple places and produce the same effect. -- Robert Haas EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company