to_regclass()

A function translating a relation name to an OID

to_regclass() is a system function for translating a relation's name to its OID.

to_regclass() was added in PostgreSQL 9.4.

Usage

to_regclass ( text ) → regclass

to_regclass() is equivalent to using the regclass object identifier type cast, but returns NULL rather than raising an ERROR if no matching relation is found.

The current search path is considered when evaluating the provided relation name.

Change history

Examples

Basic usage example for to_regclass():

postgres=# SELECT to_regclass('foo'), to_regclass('foo')::oid;
 to_regclass | to_regclass 
-------------+-------------
 foo         |       16392
(1 row)

NULL is returned if the specified relation does not exist or is not found:

postgres=# SELECT to_regclass('bar') IS NULL;
 ?column? 
----------
 t
(1 row)

If the specified relation is not visible in the current search path, it must be schema-qualified:

postgres=# SET search_path TO '';
SET

postgres=# SELECT to_regclass('foo'), to_regclass('public.foo');
 to_regclass | to_regclass 
-------------+-------------
             | public.foo
(1 row)

Categories

System function

See also

regclass, to_regnamespace()