X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 911A15E46C3 for ; Mon, 30 Aug 2004 17:33:11 -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 33179-06 for ; Mon, 30 Aug 2004 20:33:07 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 700C45E46F2 for ; Mon, 30 Aug 2004 17:33:01 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i7UKWwA4011134; Mon, 30 Aug 2004 16:32:58 -0400 (EDT) To: Rosser Schwarz Cc: pgsql-sql@postgresql.org Subject: Re: function fails where individual statements succeed In-reply-to: <37d451f7040830101763682930@mail.gmail.com> References: <37d451f7040830101763682930@mail.gmail.com> Comments: In-reply-to Rosser Schwarz message dated "Mon, 30 Aug 2004 12:17:29 -0500" Date: Mon, 30 Aug 2004 16:32:57 -0400 Message-ID: <11133.1093897977@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200408/326 X-Sequence-Number: 18823 Rosser Schwarz writes: > When we group the statements together and wrap them in > a CREATE FUNCTION, running the function fails with: > ERROR: index expressions and predicates may refer only to the table > being indexed I believe what is happening is that the entire SQL function is parsed, analyzed, and planned before we start to execute it. By the time control arrives at the CREATE INDEX command, the table named certegy.masterpcb is not the same one that that name referred to when the function's statements were parsed. I haven't bothered to work out exactly how that leads to this particular error message rather than some other failure, but I can say that this isn't gonna work out well in general. If you want to encapsulate this set of operations in a function, I'd suggest using plpgsql and being careful to EXECUTE each query rather than letting plpgsql try to cache a plan for it. regards, tom lane