Discussion:
check if type is valid pg type
(too old to reply)
Dominik Moritz
2012-10-01 22:47:05 UTC
Permalink
Hi all,

I need to check, whether a type is a valid postgres type or not. My first take was to check in the pg_type table. However, pg_type does not list types such as integer (but it does list int4) or float (but it does indeed list float8). What is the best way to check whether a type is valid or not?

Thanks,
Dominik
Josh Kupershmidt
2012-10-10 21:20:26 UTC
Permalink
Post by Dominik Moritz
I need to check, whether a type is a valid postgres type or not. My first take was to check in the pg_type table. However, pg_type does not list types such as integer (but it does list int4) or float (but it does indeed list float8). What is the best way to check whether a type is valid or not?
You could cast the type name to regtype, e.g.

SELECT 'integer'::regtype, 'float'::regtype;

Josh
--
Sent via pgsql-novice mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice
Dominik Moritz
2012-10-12 13:45:21 UTC
Permalink
Hi Josh,

That is exactly what I needed. Thank you very much. However, is there a way to do this without getting an exception if the type does not exist?

Cheers,
Dominik
Post by Josh Kupershmidt
Post by Dominik Moritz
I need to check, whether a type is a valid postgres type or not. My first take was to check in the pg_type table. However, pg_type does not list types such as integer (but it does list int4) or float (but it does indeed list float8). What is the best way to check whether a type is valid or not?
You could cast the type name to regtype, e.g.
SELECT 'integer'::regtype, 'float'::regtype;
Josh
Josh Kupershmidt
2012-10-12 22:12:38 UTC
Permalink
Post by Dominik Moritz
That is exactly what I needed. Thank you very much. However, is there a way to do this without getting an exception if the type does not exist?
You could wrap the lookup in a little PL/pgSQL function, something like this:

CREATE OR REPLACE FUNCTION is_valid_type(v_type text)
RETURNS boolean
AS $$
BEGIN
PERFORM v_type::regtype;
RETURN true;
EXCEPTION WHEN undefined_object THEN
RETURN false;
END;
$$ LANGUAGE plpgsql stable;

Josh
--
Sent via pgsql-novice mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice
Loading...