Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 94482B5DBE9 for ; Wed, 29 Jun 2011 13:03:32 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 74161-06 for ; Wed, 29 Jun 2011 16:03:26 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from spinlock.commandprompt.com (host-215.commandprompt.net [207.173.203.215]) by mail.postgresql.org (Postfix) with ESMTP id 70F89B5DBDF for ; Wed, 29 Jun 2011 13:03:26 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by spinlock.commandprompt.com (Postfix) with ESMTP id 8D27A348046; Wed, 29 Jun 2011 09:03:25 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at commandprompt.com Received: from spinlock.commandprompt.com ([127.0.0.1]) by localhost (spinlock.commandprompt.com.commandprompt.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id rjZmlX-qpNTa; Wed, 29 Jun 2011 09:03:23 -0700 (PDT) Received: from perhan.alvh.no-ip.org (unknown [190.95.18.231]) by spinlock.commandprompt.com (Postfix) with ESMTPSA id 760AE348044; Wed, 29 Jun 2011 09:03:23 -0700 (PDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id A502F6E38F; Wed, 29 Jun 2011 12:03:20 -0400 (CLT) Content-Type: text/plain; charset=UTF-8 Cc: pgsql-bugs Subject: Re: Bug with plpgsql, temp tables and TOAST? From: Alvaro Herrera To: Matthijs Bomhoff In-reply-to: <0C41674C-FA02-4768-9E1B-548E568875BD@quarantainenet.nl> References: <0C41674C-FA02-4768-9E1B-548E568875BD@quarantainenet.nl> Date: Wed, 29 Jun 2011 12:03:20 -0400 Message-Id: <1309363131-sup-6268@alvh.no-ip.org> User-Agent: Sup/git Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201106/216 X-Sequence-Number: 30595 Excerpts from Matthijs Bomhoff's message of mié jun 29 07:40:07 -0400 2011: > CREATE OR REPLACE FUNCTION foo(size_ INTEGER) RETURNS TEXT AS $EOF$ > DECLARE > acc_ TEXT := ''; > cur_rec_ RECORD; > BEGIN > EXECUTE 'CREATE TEMPORARY TABLE foo_tab(blob TEXT NOT NULL)'; > > -- Construct a string with random characters to prevent compression (with high probability) > LOOP > EXIT WHEN length(acc_) >= size_; > acc_ := acc_ || chr(ceil(random()*64)::integer + 32); > END LOOP; > > EXECUTE 'INSERT INTO foo_tab(blob) values (' || quote_literal(acc_) || ')'; > EXECUTE 'SELECT * FROM foo_tab LIMIT 1' INTO cur_rec_; > EXECUTE 'DROP TABLE foo_tab'; > RETURN cur_rec_.blob; > END > $EOF$ LANGUAGE plpgsql; Hmm, so what's happening here, I think, is that the value is getting assigned to the record variable without detoasting. I guess we should detoast the value prior to assigning it, but it seems to me that that would have a large performance penalty for other cases in which the toast table is not dropped; in fact, you can even imagine some cases in which the toasted value is not even accessed, so getting to the point of detoasting it would be a severe penalization. -- Álvaro Herrera The PostgreSQL Company - Command Prompt, Inc. PostgreSQL Replication, Consulting, Custom Development, 24x7 support