cascade_deferrable_bug.sql

text/plain

Filename: cascade_deferrable_bug.sql
Type: text/plain
Part: 0
Message: Re: DROP SCHEMA xxx CASCADE: ERROR: could not open relation with OID yyy
-- {
CREATE OR REPLACE FUNCTION makeTopo()
RETURNS void AS $$
BEGIN

    CREATE SCHEMA topo;
    CREATE TABLE topo.edge (
        id int4 PRIMARY KEY,
        ref int4,
        FOREIGN KEY(ref) REFERENCES topo.edge(id)
            DEFERRABLE INITIALLY DEFERRED);

    -- NOTE: returning here "fixes" the bug
    --RETURN;

    INSERT INTO topo.edge VALUES (1,1);

END
$$ language 'plpgsql' VOLATILE STRICT;
-- }


DROP SCHEMA IF EXISTS topo CASCADE;

BEGIN;

 SELECT makeTopo();

 SELECT 'topo.edge'::regclass::oid;

 -- NOTE: this fixes it!
 -- SET CONSTRAINTS ALL IMMEDIATE;

 -- ERROR:  could not open relation with OID XXXXXXX
 -- NOTE: XXXXX is 'topo.edge'::regclass::oid
 DROP TABLE topo.edge CASCADE;

COMMIT;

-- NOTE: doing it here is fine
--DROP TABLE topo.edge CASCADE;