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