What is the use of postgres regclass datatype in user tables? When would one want to use them when regular data types are available? If any tables have these data types, since pg_upgrade would fail, what data types should those be converted to?
postgres=> select version();
version
----------------------------------------------------------------------------------------------------------
PostgreSQL 14.11 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 7.3.1 20180712 (Red Hat 7.3.1-12), 64-bit
(1 row)
dT pg_catalog.reg*
List of data types
Schema │ Name │ Description
════════════╪═══════════════╪══════════════════════════════════════
pg_catalog │ regclass │ registered class
pg_catalog │ regcollation │ registered collation
pg_catalog │ regconfig │ registered text search configuration
pg_catalog │ regdictionary │ registered text search dictionary
pg_catalog │ regnamespace │ registered namespace
pg_catalog │ regoper │ registered operator
pg_catalog │ regoperator │ registered operator (with args)
pg_catalog │ regproc │ registered procedure
pg_catalog │ regprocedure │ registered procedure (with args)
pg_catalog │ regrole │ registered role
pg_catalog │ regtype │ registered type
(11 rows)
postgres=> create table t1 (c1 int, c2 regclass);
CREATE TABLE
postgres=> d t1;
Table "public.t1"
Column | Type | Collation | Nullable | Default
--------+----------+-----------+----------+---------
c1 | integer | | |
c2 | regclass | | |
postgres=> insert into t1 values(1,1);
INSERT 0 1
postgres=> insert into t1 values(2,'2');
INSERT 0 1
postgres=> insert into t1 values(3,'3'::int);
INSERT 0 1
postgres=> insert into t1 values(4,'4'::text);
ERROR: relation "4" does not exist
postgres=> select * from t1;
c1 | c2
----+----
1 | 1
2 | 2
3 | 3
(3 rows)
What kind of data does these data types store – numbers or text? In above example am able to insert it as number as well as character ‘2’?
2
Answers
https://www.postgresql.org/docs/current/datatype-oid.html:
The
regclass
data type in PostgreSQL is a specialized type that provides a way to reference tables or other database objects by their names instead of by their internal object identifiers (OIDs).The
regclass
is an alias for the oid type, a unique identifier used internally by PostgreSQL for system objects like tables, indexes, and sequences.When you store a value in a
regclass
column, you can store either the symbolic name (like a table name) or the numeric OID of that object. PostgreSQL can automatically resolve the name to the corresponding OID when needed.Why Use regclass in User Tables?
Human-Readable References: Storing table references as regclass makes your database schema more readable and maintainable because it stores the table name instead of a raw numeric ID.
Automatic Resolution: When querying or working with system catalogs, regclass allows you to work with table names directly. PostgreSQL handles converting these names into OIDs behind the scenes, making operations more intuitive.
System Catalog Operations: If you’re writing custom functions or queries that interact with PostgreSQL’s system catalogs (e.g., querying pg_class), regclass simplifies these operations.