X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 1D808D1B503 for ; Sun, 28 Sep 2003 16:25:11 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 80900-03 for ; Sun, 28 Sep 2003 13:24:39 -0300 (ADT) Received: from joeconway.com (66-146-172-86.skyriver.net [66.146.172.86]) by svr1.postgresql.org (Postfix) with ESMTP id 92AFCD1B53A for ; Sun, 28 Sep 2003 13:24:38 -0300 (ADT) Received: from [192.168.5.3] (account jconway HELO joeconway.com) by joeconway.com (CommuniGate Pro SMTP 4.1.3) with ESMTP-TLS id 390684; Sun, 28 Sep 2003 09:24:09 -0700 Message-ID: <3F770A8D.9030400@joeconway.com> Date: Sun, 28 Sep 2003 09:21:33 -0700 From: Joe Conway User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3) Gecko/20030312 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Tom Lane Cc: Josh Berkus , Richard Sydney-Smith , pgsql-sql@postgresql.org Subject: Re: Mystery function error References: <000c01c38572$73c03c50$251a32d2@athlon2000> <200309272229.50601.josh@agliodbs.com> <9398.1064765182@sss.pgh.pa.us> In-Reply-To: <9398.1064765182@sss.pgh.pa.us> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/360 X-Sequence-Number: 15338 Tom Lane wrote: > Josh Berkus writes: >>Also, this will run faster if you do it as a SQL function: > >>CREATE FUNCTION locate ( text, text ) RETURNS INT AS ' >>SELECT POSITION($2, $1); >>' LANGUAGE SQL IMMUTABLE STRICT; > > This is definitely the solution I'd recommend for 7.4 (because 7.4 would > inline the SQL function definition, resulting in zero runtime overhead). > In 7.3 I suspect the plpgsql version might be a tad faster, or anyway > comparable. Has anyone done any head-to-head performance comparisons > on such simple functions? > I did a quick check last night on 7.3.4 and found that plpgsql was faster: CREATE OR REPLACE FUNCTION public.locate(bpchar, bpchar) RETURNS int4 AS ' -- search for the position of $2 in $1 declare srcstr alias for $1; searchstr alias for $2; begin return position(searchstr in srcstr); end; ' LANGUAGE 'plpgsql' IMMUTABLE; regression=# explain analyze select locate('abc','b'); QUERY PLAN ---------------------------------------------------------------------------------- Result (cost=0.00..0.01 rows=1 width=0) (actual time=0.01..0.01 rows=1 loops=1) Total runtime: 0.03 msec (2 rows) DROP FUNCTION public.locate(bpchar, bpchar); CREATE OR REPLACE FUNCTION public.locate(bpchar, bpchar) RETURNS int4 AS ' select position($2 in $1) ' LANGUAGE 'sql'; regression=# explain analyze select locate('abc','b'); QUERY PLAN ---------------------------------------------------------------------------------- Result (cost=0.00..0.01 rows=1 width=0) (actual time=0.24..0.24 rows=1 loops=1) Total runtime: 0.26 msec (2 rows) On 7.4 (different hardware), I get this: plpgsql -------- regression=# explain analyze select locate('abc','b'); QUERY PLAN ---------------------------------------------------------------------------------- Result (cost=0.00..0.01 rows=1 width=0) (actual time=0.00..0.00 rows=1 loops=1) Total runtime: 0.05 msec (2 rows) sql -------- regression=# explain analyze select locate('abc','b'); QUERY PLAN ---------------------------------------------------------------------------------- Result (cost=0.00..0.01 rows=1 width=0) (actual time=0.01..0.01 rows=1 loops=1) Total runtime: 0.03 msec (2 rows) Joe