pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Krystian Szladewski <krystian.szladewski@adspert.de>
To: pgsql-bugs@postgresql.org
Subject: "could not open relation with OID XXX" when using recreated index in sql function
Date: Thu, 3 Aug 2017 14:35:57 +0200
Message-ID: <4FF21730-A69B-4582-8D02-F3BE795677F3@adspert.de> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

Hi guys,

I think I found a bug in sql (not plpgsql) functions. 
This is the error I’m getting:

ERROR:  XX000: could not open relation with OID 13053550
CONTEXT:  SQL function "fail" statement 3
LOCATION:  relation_open, heapam.c:1130

It happens when I try to recreate an index and use it afterwards (both within the function).

My postgres version:
PostgreSQL 9.6.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.2 20140120 (Red Hat 4.8.2-16), 64-bit

Here is some sql to reproduce it:

BEGIN;

CREATE TABLE test_1 (
    test_id     BIGINT PRIMARY KEY,
    other_id    BIGINT NOT NULL
);
CREATE INDEX test_1_other_idx ON test_1(other_id);

— Make sure query planner uses the index scan
SET enable_seqscan=off;

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;
$$;

SELECT fail();

ROLLBACK;


If you modify this function to use plpgsql, it won’t fail anymore:

CREATE OR REPLACE FUNCTION ok() RETURNS bigint
LANGUAGE plpgsql AS
$$
    BEGIN

    -- Re-create the index
    DROP INDEX IF EXISTS test_1_other_idx;
    CREATE INDEX test_1_other_idx ON test_1(other_id);

    RETURN (SELECT test_id FROM test_1 WHERE other_id = 1);

    END
$$;

SELECT ok();


It also works fine when you paste contents of fail() function into psql session.


Best Regards,
Krystian Szladewski

view thread (2+ messages)  latest in thread

Message-ID: <4FF21730-A69B-4582-8D02-F3BE795677F3@adspert.de>
Permalink:  ../4FF21730-A69B-4582-8D02-F3BE795677F3@adspert.de/
Also on:    postgresql.org/message-id/4FF21730-A69B-4582-8D02-F3BE795677F3@adspert.de

 · 

reply

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: krystian.szladewski@adspert.de
  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>

* 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