Overview

The pg_class stores information about tables and indexes in the database. It contains exactly one row per table (or index) created in the database. It mimics the PostgreSQL system catalog pg_class.

Columns

This table is designed for compatibility with tools that require PostgreSQL system tables, so it mostly has dummy data. Please note that not all columns in pg_class are applicable to every type of relation.

The following columns are available for querying in pg_class:

ColumnTypeDescription
oidintThis column represents the table/index object ID (OID) generated by oxla
relnametextThis column represents the table/index name as specified by the user during creation
relnamespaceintThis column represents the OID of the namespace the relation resides in
relhasindexboolReturns true if the table has any indexes
relkindtextThis column represents the type of relation: r for tables and i for indexes
reltypeintUnused
reloftypeintUnused
relownerintUnused
relamintUnused
relfilenodeintUnused
reltablespaceintUnused
relpagesintUnused
reltuplesfloatUnused
relallvisibleintUnused
reltoastrelidintUnused
relissharedboolUnused
relpersistencetextUnused
relnattsintUnused
relchecksintUnused
relhasrulesboolUnused
relhastriggersboolUnused
relhassubclassboolUnused
relrowsecurityboolUnused
relforcerowsecurityboolUnused
relispopulatedboolUnused
relreplidenttextUnused
relispartitionboolUnused
relrewriteintUnused
relfrozenxidintUnused
relacltextUnused
reloptionstextUnused
relminmxidtextUnused
relpartboundtextUnused

Example

  1. Create a table and define its schema.
CREATE TABLE customer_orders (
    order_id INT,
    customer_id INT,
    order_date DATE,
    total_amount INT
);
  1. Create an index on the customer_orders table for the customer_id column.
CREATE INDEX idx_customer_id ON customer_orders (customer_id);
  1. Query the pg_class catalog to retrieve information about the customer_orders table and the index you’ve just created.
SELECT oid, relname, relkind, relhasindex, relnamespace 
FROM pg_class 
WHERE relname IN ('customer_orders', 'idx_customer_id');
  1. The query will return information about the customer_orders table and the index.
 oid  |     relname     | relkind | relhasindex | relnamespace 
------+-----------------+---------+-------------+--------------
 1013 | idx_customer_id | i       | f           |            0
 1012 | customer_orders | r       | t           |            0
(2 rows)