2

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

3
  • 4
    You should to use plpgsql language. When I see your example, please, start with read documentation postgresql.org/docs/current/static/plpgsql.html Commented Aug 24, 2018 at 13:40
  • 4
    Plus you can't say it returns void but then make it return everything from your table. Commented Aug 24, 2018 at 13:46
  • I think your comments are slightly off topic here... I was making a point with my example regarding the issue I had with a nonexisting table - there's no point in this function anyway...:) Commented Aug 29, 2018 at 13:50

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, really helpfull!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.