Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 211D9B5DBF8 for ; Wed, 25 May 2011 14:05: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 03011-07 for ; Wed, 25 May 2011 17:05:25 +0000 (UTC) X-Greylist: delayed 00:05:10.123238 by SQLgrey-1.7.6 Received: from lerner.co.il (unknown [69.55.231.201]) by mail.postgresql.org (Postfix) with ESMTP id 84876B5DBE5 for ; Wed, 25 May 2011 14:05:25 -0300 (ADT) Received: by lerner.co.il (Postfix, from userid 1003) id 3D7205D7C38; Wed, 25 May 2011 20:00:13 +0300 (IDT) X-DKIM: Sendmail DKIM Filter v2.8.3 lerner.co.il 3D7205D7C38 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=lerner.co.il; s=MAILOUT; t=1306342813; bh=xMNSGMPmQvIr2vf0rhXOGbdN/gRZFyV2LGtKJ7owlEY=; h=Message-ID:Date:From:MIME-Version:To:Subject:Content-Type: Content-Transfer-Encoding; b=Ujt/EF8DDEx1hSyqR5NCP+MCtC+E0FsEGDaxhOtVRNZNVf0uzW9qzuiITpjFv/27J JQPMw/lWZX/OKKIAbHAhwXo0Xp6JDPmvUg33eavVHVcpTWEyUH+9ApW+Tlnk8ssM1w PYpHVVqZtY8V4t2zgLT0rbqlUPvTdyYcFhqNgol8= Received: from 2-54-162-198.orange.net.il (unknown [2.54.162.198]) by lerner.co.il (Postfix) with ESMTPS id 365695D6D67; Wed, 25 May 2011 20:00:03 +0300 (IDT) X-DKIM: Sendmail DKIM Filter v2.8.3 lerner.co.il 365695D6D67 Message-ID: <4DDD358E.20307@lerner.co.il> Date: Wed, 25 May 2011 19:59:58 +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: pgsql-performance@postgresql.org Subject: Speeding up loops in pl/pgsql function 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.107 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RDNS_NONE=0.793 X-Spam-Level: X-Archive-Number: 201105/337 X-Sequence-Number: 43846 Hi, everyone. I'm working on a project that's using PostgreSQL 8.3, that requires me to translate strings of octal digits into strings of characters -- so '141142143' should become 'abc', although the database column containing this data (both before and after) is a bytea. While the function I've written is accurate, it turns out that it's also ridiculously slow. I've managed to speed it up a fair amount, to twice what it was previously doing, by folding a helper function into a main one, and appending to an array (which I then join into a string at the end of the function) instead of concatenating a string onto itself time after time. I realize that pl/pgsql is not a good choice for doing this sort of task, and that another language -- say, one with direct support for octal digits, or with built-in, speedy array functions such as pop() and push() -- would be a better choice. But that's not an option at this point. I should also note that I'm not manipulating a huge amount of data here. We're talking about 300 or so rows, each of which contains about 250 KB of data. (Hmm, could the problem be that I'm constantly forcing the system to compress and uncompress the data in TOAST? I hadn't thought of that until just now...) I thus have two basic questions: (1) Are there any good guidelines for what operations in pl/pgsql are optimized for which data structures? For example, it turns out that a great deal of time is being spent in the substring() function, which surprised me. I thought that by switching to an array, it might be faster, but that wasn't the case, at least in my tests. Having a sense of what I should and shouldn't be trying, and which built-in functions are particularly fast or slow, would be useful to know. (2) Is there any configuration setting that would (perhaps) speed things up a bit? I thought that maybe work_mem would help, but the documentation didn't indicate this at all, and sure enough, nothing really changed when I increased it. Of course, any suggestions for how to deal with octal digits in PostgreSQL 8.3, such as an octal equivalent to the x'ff' syntax, would be more than welcome. Thanks in advance, Reuven -- Reuven M. Lerner -- Web development, consulting, and training Mobile: +972-54-496-8405 * US phone: 847-230-9795 Skype/AIM: reuvenlerner