Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ddIGB-0007WI-46 for pgsql-bugs@arkaria.postgresql.org; Thu, 03 Aug 2017 15:42:03 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1ddIGA-0003PC-KC for pgsql-bugs@arkaria.postgresql.org; Thu, 03 Aug 2017 15:42:02 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ddIGA-0003P2-2Y for pgsql-bugs@postgresql.org; Thu, 03 Aug 2017 15:42:02 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ddIG1-0004Gs-Tx for pgsql-bugs@postgresql.org; Thu, 03 Aug 2017 15:41:59 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v73FfpaC010211; Thu, 3 Aug 2017 11:41:52 -0400 From: Tom Lane To: Krystian Szladewski cc: pgsql-bugs@postgresql.org Subject: Re: "could not open relation with OID XXX" when using recreated index in sql function In-reply-to: <4FF21730-A69B-4582-8D02-F3BE795677F3@adspert.de> References: <4FF21730-A69B-4582-8D02-F3BE795677F3@adspert.de> Comments: In-reply-to Krystian Szladewski message dated "Thu, 03 Aug 2017 14:35:57 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <10209.1501774911.1@sss.pgh.pa.us> Date: Thu, 03 Aug 2017 11:41:51 -0400 Message-ID: <10210.1501774911@sss.pgh.pa.us> List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org Krystian Szladewski writes: > CREATE OR REPLACE FUNCTION fail() RETURNS bigint > LANGUAGE sql AS > $$ > -- Re-create the index > DROP INDEX IF EXISTS test_1_other_idx; > CREATE INDEX test_1_other_idx ON test_1(other_id); > -- Fail! > SELECT test_id FROM test_1 WHERE other_id = 1000; > $$; Yeah, this is unsurprising per the NOTE here: https://www.postgresql.org/docs/current/static/xfunc-sql.html Note: The entire body of a SQL function is parsed before any of it is executed. While a SQL function can contain commands that alter the system catalogs (e.g., CREATE TABLE), the effects of such commands will not be visible during parse analysis of later commands in the function. Thus, for example, CREATE TABLE foo (...); INSERT INTO foo VALUES(...); will not work as desired if packaged up into a single SQL function, since foo won't exist yet when the INSERT command is parsed. It's recommended to use PL/PgSQL instead of a SQL function in this type of situation. (I see that this NOTE only talks about parse analysis, but actually the planning is done in one fell swoop as well, which is what leads to your problem.) At some point somebody will probably rewrite the SQL function executor to fix this, and get rid of some of its other unpleasant properties like not using the plancache; but it hasn't been high priority. regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs