Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id ECB47B5DC0A for ; Wed, 25 May 2011 19:26:33 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 41991-03 for ; Wed, 25 May 2011 22:26:26 +0000 (UTC) X-Greylist: delayed 03:40:24.920387 by SQLgrey-1.7.6 Received: from lerner.co.il (server.lerner.co.il [69.55.231.200]) by mail.postgresql.org (Postfix) with ESMTP id 54D0CB5DC0E for ; Wed, 25 May 2011 19:26:26 -0300 (ADT) Received: by lerner.co.il (Postfix, from userid 1003) id BC6255D62F1; Thu, 26 May 2011 01:26:24 +0300 (IDT) X-DKIM: Sendmail DKIM Filter v2.8.3 lerner.co.il BC6255D62F1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=lerner.co.il; s=MAILOUT; t=1306362384; bh=+d5KfXQmo7RzJvYpZAJ3qN7vV3wJ6+3+wURQgCaflAk=; h=Message-ID:Date:From:MIME-Version:To:CC:Subject:References: In-Reply-To:Content-Type:Content-Transfer-Encoding; b=LyB2QX15udrvBCzR/nGXa7pp7Ba5aENkj5I/pwHGXxKGKLboDNDO7xyVoTqHr+F4Z gd5F2O4xRbfLEcj8hR8aeb3zzlsYRL6dYJs491kp47Fu3p8r/6sQSL3ZHHe+APokKi aFn6Alv4OQMzD30svTlOpfKsN+sAlWdXna6o0+vE= Received: from Reuvens-MacBook-Pro.local (bzq-84-108-249-150.cablep.bezeqint.net [84.108.249.150]) by lerner.co.il (Postfix) with ESMTPS id AE99A5D62F1; Thu, 26 May 2011 01:26:19 +0300 (IDT) X-DKIM: Sendmail DKIM Filter v2.8.3 lerner.co.il AE99A5D62F1 Message-ID: <4DDD8209.4020208@lerner.co.il> Date: Thu, 26 May 2011 01:26:17 +0300 From: "Reuven M. Lerner" User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 MIME-Version: 1.0 To: Merlin Moncure CC: pgsql-performance@postgresql.org Subject: Re: Speeding up loops in pl/pgsql function References: <4DDD358E.20307@lerner.co.il> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed 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/351 X-Sequence-Number: 43860 Hi, everyone. Merlin wrote: > let's see the source. I bet we can get this figured out. 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. Thanks for any suggestions you can offer. CREATE OR REPLACE FUNCTION translate_octals_into_decimals(bytea_string BYTEA) RETURNS BYTEA AS $$ DECLARE bytea_string_length INTEGER := length(bytea_string); current_substring TEXT := ''; translated_string_array BYTEA[]; output_number INTEGER := 0; output_number_text TEXT := ''; current_digit TEXT := ''; BEGIN RAISE NOTICE '[translate_octals_into_decimals] start at %, string of length %', clock_timestamp(), pg_size_pretty(length(bytea_string)); FOR i IN 1..length(bytea_string) BY 3 LOOP current_substring := substring(bytea_string from i for 3); output_number := 0; FOR j IN 0..(length(current_substring) - 1) LOOP current_digit := substring(current_substring from (length(current_substring) - j) for 1); output_number := output_number + current_digit::integer * (8 ^ j); END LOOP; output_number_text = lpad(output_number::text, 3, '0'); IF output_number_text::int = 92 THEN translated_string_array := array_append(translated_string_array, E'\\\\'::bytea); ELSIF output_number_text::int = 0 THEN translated_string_array := array_append(translated_string_array, E'\\000'::bytea); ELSE translated_string_array := array_append( translated_string_array, chr(output_number_text::integer)::bytea ); END IF; END LOOP; RETURN array_to_string(translated_string_array, ''); END; $$ LANGUAGE 'plpgsql'; Reuven -- Reuven M. Lerner -- Web development, consulting, and training Mobile: +972-54-496-8405 * US phone: 847-230-9795 Skype/AIM: reuvenlerner