X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id E479FD905E for ; Tue, 18 Oct 2005 11:50:00 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 76280-01 for ; Tue, 18 Oct 2005 14:49:55 +0000 (GMT) Received: from pjetlic.mobart.hr (pjetlic.mobart.hr [80.80.51.10]) by svr1.postgresql.org (Postfix) with ESMTP id A63BED8601 for ; Tue, 18 Oct 2005 11:49:57 -0300 (ADT) Received: from localhost (localhost.localdomain [127.0.0.1]) by pjetlic.mobart.hr (Postfix) with ESMTP id 7AED017A0D; Tue, 18 Oct 2005 16:49:59 +0200 (CEST) Received: from pjetlic.mobart.hr ([127.0.0.1]) by localhost (pjetlic [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 04571-04; Tue, 18 Oct 2005 16:49:59 +0200 (CEST) Received: from mike.mobart.hr (mike.mobart.hr [192.168.10.94]) (using SSLv3 with cipher RC4-MD5 (128/128 bits)) (No client certificate requested) by pjetlic.mobart.hr (Postfix) with ESMTP id 23BF417A05; Tue, 18 Oct 2005 16:49:59 +0200 (CEST) Subject: Re: Consulta! From: Mario Splivalo Reply-To: mario.splivalo@mobart.hr To: Adriana Marcela Aguirre Cc: pgsql-sql@postgresql.org In-Reply-To: <20051018142700.79582.qmail@web36706.mail.mud.yahoo.com> References: <20051018142700.79582.qmail@web36706.mail.mud.yahoo.com> Content-Type: text/plain Organization: Mob-Art Date: Tue, 18 Oct 2005 16:49:59 +0200 Message-Id: <1129646999.7511.5.camel@ekim> Mime-Version: 1.0 X-Mailer: Evolution 2.2.1.1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by OpenSource AV+AS Mail Server X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.103 required=5 tests=[AWL=-0.103, UPPERCASE_25_50=0.207] X-Spam-Level: X-Archive-Number: 200510/186 X-Sequence-Number: 23039 On Tue, 2005-10-18 at 14:26 +0000, Adriana Marcela Aguirre wrote: > Hola a todos!!... Hi! :) That's a known postges 'problem', one which, I guess, every pg-newbie encounters, especialy if he/she came from other RDBMSes. (I personaly came from MSSQL). > CREATE OR REPLACE FUNCTION "public"."f_test" () RETURNS SETOF > "pg_catalog"."record" AS' > BEGIN > CREATE TEMPORARY TABLE test( x integer ) ON COMMIT DROP; > > INSERT INTO test values (1); > > --RETORNA LOS RESULTADOS > FOR res IN SELECT x FROM test LOOP > RETURN NEXT res; > END LOOP; > RETURN; > END; > 'LANGUAGE 'plpgsql' IMMUTABLE CALLED ON NULL INPUT SECURITY DEFINER; > Postgres parsed your function, and compilled it, when first run. When doing so it refferenced your 'test' temp table via the oid. You do drop the table at the end of the proc, but prepared query plan for that function still references the OID that temp table had when it did exists. So, you need to stop postgres to 'prepare' query plan for that table. You do so by EXECUTEing the desired query, in your case: FOR res IN EXECUTE SELECT x FROM test LOOP ... It is more convinient to add the SQL query to a variable, and then exec that variable, like so: _SQL := 'SELECT a, b FROM tmpTable WHERE colText=' || quote_literal(someParametar); FOR res IN EXECUTE _SQL LOOP ... Mike -- Mario Splivalo Mob-Art mario.splivalo@mobart.hr "I can do it quick, I can do it cheap, I can do it well. Pick any two."