Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 1828BB5DBF7 for ; Thu, 26 May 2011 06:07:20 -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 43540-01 for ; Thu, 26 May 2011 09:07:11 +0000 (UTC) X-Greylist: delayed 00:20:06.435441 by SQLgrey-1.7.6 Received: from mail.erwied.eu (mail.erwied.eu [85.214.80.157]) by mail.postgresql.org (Postfix) with ESMTP id 4E2D9B5DBE6 for ; Thu, 26 May 2011 06:07:10 -0300 (ADT) Received: from dslb-094-220-111-120.pools.arcor-ip.net ([94.220.111.120] helo=erwied.dns2go.com ident=mail) by mail.erwied.eu with esmtpsa (Exim 4.76#1) id 1QPWDb-0001P2-JS ; Thu, 26 May 2011 10:46:59 +0200 Received: from port-87-234-32-189.static.qsc.de ([87.234.32.189] helo=localhost) by erwied.dns2go.com with esmtpsa (Exim 4.76#1) id 1QPWDZ-00048h-Aw ; Thu, 26 May 2011 10:46:57 +0200 Date: Thu, 26 May 2011 10:46:55 +0200 From: Jochen Erwied Organization: Creative Chaos Computing X-Priority: 3 (Normal) Message-ID: <589023449.20110526104655@erwied.eu> To: "Reuven M. Lerner" CC: pgsql-performance@postgresql.org Subject: Re: Speeding up loops in pl/pgsql function In-Reply-To: <4DDD8209.4020208@lerner.co.il> References: <4DDD358E.20307@lerner.co.il> <4DDD8209.4020208@lerner.co.il> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201105/363 X-Sequence-Number: 43872 Thursday, May 26, 2011, 12:26:17 AM you wrote: > Here you go... it looked nicer before I started to make optimizations; > I've gotten it to run about 2x as fast as the previous version, but now > I'm sorta stuck, looking for further optimizations, including possible > use of builtin functions. I've got only a 9.0.4 to play with, and bytea's are passed as an hexadecimal string, so I resorted to writing the function with TEXT as parameters, but maybe the following helps a bit, avoiding a few IMHO useless string/int-operations: CREATE OR REPLACE FUNCTION translate_octals_into_decimals(bytea_string text) RETURNS text AS $$ DECLARE bytea_string_length INTEGER := length(bytea_string); translated_string_array BYTEA[]; output_number INTEGER := 0; num1 INTEGER; num2 INTEGER; num3 INTEGER; npos INTEGER; nlen INTEGER; BEGIN RAISE NOTICE '[translate_octals_into_decimals] start at %, string of length %', clock_timestamp(), pg_size_pretty(length(bytea_string)); npos := 1; FOR i IN 1..bytea_string_length BY 3 LOOP num1 := substring(bytea_string from i for 1); num2 := substring(bytea_string from i+1 for 1); num3 := substring(bytea_string from i+2 for 1); output_number := 64*num1 + 8*num2 + num3; IF output_number = 0 THEN translated_string_array[npos] := E'\\000'::bytea; ELSIF output_number = 92 THEN translated_string_array[npos] := E'\\\\'::bytea; ELSE translated_string_array[npos] := chr(output_number)::bytea; END IF; npos := npos+1; END LOOP; RETURN array_to_string(translated_string_array, ''); END; $$ LANGUAGE 'plpgsql'; -- Jochen Erwied | home: jochen@erwied.eu +49-208-38800-18, FAX: -19 Sauerbruchstr. 17 | work: joe@mbs-software.de +49-2151-7294-24, FAX: -50 D-45470 Muelheim | mobile: jochen.erwied@vodafone.de +49-173-5404164