From: Tom Lane <tgl@sss.pgh.pa.us>
To: Krystian Szladewski <krystian.szladewski@adspert.de>
Cc: pgsql-bugs@postgresql.org
Subject: Re: "could not open relation with OID XXX" when using recreated index in sql function
Date: Thu, 03 Aug 2017 11:41:51 -0400
Message-ID: <10210.1501774911@sss.pgh.pa.us> (raw)
In-Reply-To: <4FF21730-A69B-4582-8D02-F3BE795677F3@adspert.de>
References: <4FF21730-A69B-4582-8D02-F3BE795677F3@adspert.de>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
Krystian Szladewski <krystian.szladewski@adspert.de> 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, krystian.szladewski@adspert.de
Subject: Re: "could not open relation with OID XXX" when using recreated index in sql function
In-Reply-To: <10210.1501774911@sss.pgh.pa.us>
References: <4FF21730-A69B-4582-8D02-F3BE795677F3@adspert.de> <10210.1501774911@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox