Sub topic 7
Querying `information_schema` and `pg_catalog`
This chapter assumes you know basic SELECT, WHERE, JOIN, and ORDER BY statements. You should also understand tables, columns, schemas, primary keys, and foreign keys. The examples use the current database connection, so replace public and orders with names from your database.
PostgreSQL stores information about database objects in system catalogs. Two useful ways to inspect that information are:
information_schema, a set of standard SQL views designed to work across database systems.pg_catalog, PostgreSQL's own collection of catalog tables and functions.
information_schema is usually the best starting point for portable metadata queries. Use pg_catalog when you need PostgreSQL-specific details, such as relation sizes, object types, or internal identifiers.
| Source | Best for | Portability |
|---|---|---|
| `information_schema` | Columns, tables, views, constraints, and privileges | High |
| `pg_catalog` | PostgreSQL-specific object details and statistics | PostgreSQL only |
Inspecting columns with `information_schema`
The information_schema.columns view lists columns visible to your current role. This query shows the column name, data type, nullability, and position for public.orders:
SELECT column_name,
data_type,
is_nullable,
ordinal_position
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'orders'
ORDER BY ordinal_position;The is_nullable value is text, usually YES or NO. A NO value means the column has a NOT NULL constraint or an equivalent restriction.
You can also list tables and views:
SELECT table_schema, table_name, table_type
FROM information_schema.tables
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
ORDER BY table_schema, table_name;The catalog views apply visibility rules. A table or column that your role cannot access might not appear, even if it exists.
Inspecting PostgreSQL objects with `pg_catalog`
PostgreSQL represents tables, indexes, views, sequences, and other relations in pg_class. The relkind column identifies the object type. Join it to pg_namespace to turn an internal namespace identifier into a schema name.
SELECT n.nspname AS schema_name,
c.relname AS object_name,
c.relkind
FROM pg_catalog.pg_class AS c
JOIN pg_catalog.pg_namespace AS n
ON n.oid = c.relnamespace
WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY n.nspname, c.relname;Common relkind values include r for ordinary tables, v for views, m for materialized views, i for indexes, and S for sequences. PostgreSQL versions can add or interpret codes differently, so use the current PostgreSQL documentation when you need every possible value.
A catalog query can also calculate storage usage:
SELECT n.nspname AS schema_name,
c.relname AS table_name,
pg_size_pretty(pg_total_relation_size(c.oid)) AS total_size
FROM pg_catalog.pg_class AS c
JOIN pg_catalog.pg_namespace AS n
ON n.oid = c.relnamespace
WHERE c.relkind = 'r'
AND n.nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY pg_total_relation_size(c.oid) DESC
LIMIT 10;pg_total_relation_size includes the table, indexes, and associated TOAST storage. The oid value is PostgreSQL's internal object identifier. You normally pass it to catalog functions rather than displaying it to users.
Choosing the catalog
Use information_schema when portability and readable column names matter. Use pg_catalog when you need PostgreSQL-specific information or functions. In production scripts, qualify catalog names with pg_catalog. and filter out system schemas so internal objects do not obscure your results.
Exercise
Which source should you use for a portable query that lists the columns of a table, and which view should you query?
Show answer
Use informationschema and query informationschema.columns.