4

I have a function that checks whether a table exists on PostgreSQL or not, using the following code:

CREATE OR REPLACE FUNCTION public.sp_table_exists(p_in_table_name character varying)
  RETURNS boolean AS
$$
    DECLARE QUERY_COUNT INTEGER DEFAULT 1;
    QUERY_STRING VARCHAR(300);
    BEGIN
        QUERY_STRING := CONCAT('SELECT RELNAME FROM PG_CLASS WHERE RELNAME = ''',p_in_table_name,'''');
        EXECUTE QUERY_STRING;
        GET DIAGNOSTICS QUERY_COUNT = ROW_COUNT;
        IF QUERY_COUNT > 0  THEN
            RETURN TRUE;
        ELSE
            RETURN FALSE;
        END IF;     
    END;
$$ LANGUAGE plpgsql;

I'm trying to use the output of the above function to assign to a boolean value, but PostgreSQL doesn't allow me to do so.

DECLARE DEBUG_ENABLED boolean DEFAULT FALSE;
DEBUG_ENABLED := PERFORM sp_table_exists('temp_table');

OR

DEBUG_ENABLED := SELECT * FROM sp_table_exists('temp_table');

Can you please help me resolve this?

2 Answers 2

2

Perform, to the best of my understanding, lets you execute a function without returning any value(s). As such, it makes sense that this would return nothing.

As far as assigning it to your variable, I think it's easier than you imagined:

DEBUG_ENABLED := sp_table_exists('temp_table');

select ... into is generally used when you have a field or value from a query that you want in a variable (which isn't your situation):

select count (*) > 0
into DEBUG_ENABLED
from information_schema.tables
where table_name = 'temp_table'
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming pl/pgsql, this should work:

SELECT sp_table_exists('temp_table')
  INTO DEBUG_ENABLED

Looks like you're being bugged by something outside Postgres. See this query.

The CREATE FUNCTION / SELECT INTO works fine at my end.

testdb=# CREATE OR REPLACE FUNCTION public.sp_table_exists(p_in_table_name character varying)
testdb-#   RETURNS boolean AS
testdb-# $$
testdb$# DECLARE QUERY_COUNT INTEGER DEFAULT 1;
testdb$# QUERY_STRING VARCHAR(300);
testdb$#     BEGIN
testdb$#         QUERY_STRING := CONCAT('SELECT RELNAME FROM PG_CLASS WHERE RELNAME = ''',p_in_table_name,'''');
testdb$#         EXECUTE QUERY_STRING;
testdb$#         GET DIAGNOSTICS QUERY_COUNT = ROW_COUNT;
testdb$#         IF QUERY_COUNT > 0  THEN
testdb$#             RETURN TRUE;
testdb$#         ELSE
testdb$#             RETURN FALSE;
testdb$#         END IF;
testdb$#     END;
testdb$# $$ LANGUAGE plpgsql;
CREATE FUNCTION
testdb=# SELECT sp_table_exists('temp_table')
testdb-#   INTO DEBUG_ENABLED;
SELECT 1
testdb=#

5 Comments

I'm using pl/pgsql, your suggestion still gives me the same error as earlier. "Invalid type name" :(
BTW... just checked. The CREATE FUNCTION and SELECT INTO works perfectly on my local postgres installation.
This is frustrating, I've tried all the combinations from the above and also your suggestion - still the same error!
Just to cross-check. You are running this directly on psql prompt right (if so, could you tell the postgres version)? If not, could you elaborate how are you running this? Is it via an external script (python / ruby etc.)?
I'm running this on pgAdmin - running this on two PostgreSQL versions (9.4.5 & 9.5)

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.