Pavel Durov
Pavel Durov

Reputation: 1297

PostgreSQL create function that references a non existing table

I'm trying to define a function that references a temp table that will be defined in the future, but I'm getting the following error on creation:

DROP FUNCTION IF EXISTS test_function();
CREATE FUNCTION test_function() 
RETURNS void AS $$
    SELECT * FROM temp_table
$$ LANGUAGE SQL;

ERROR: relation "_temp_table" does not exist LINE 7: FROM temp_table

Is there a way to define a Postgresql function that referenes a table that doesn't exist?

I can create an empty temp table as part of the function creation - but it's a bit ugly...

Any help will be appreciated...

Thanks

Upvotes: 2

Views: 751

Answers (1)

JGH
JGH

Reputation: 17836

You can use check_function_bodies to turn off the references while loading the functions, as it is done by pg_dump.

set check_function_bodies = off;

DROP FUNCTION IF EXISTS test_function();
CREATE FUNCTION test_function() 
RETURNS void AS $$
    //your code actually doing something with tables/functions that don't exist yet
$$ LANGUAGE SQL;

set check_function_bodies = on;

Upvotes: 2

Related Questions