PostgreSQL Metadata Views, System Schemas & Catalogs
Table of Contents
1. Overview of PostgreSQL Schemas (User vs. System Schemas)
Every PostgreSQL database contains built-in schemas that store system metadata, internal data structures, and user objects. By default, \dn only displays user schemas. Adding the S flag or wildcard * reveals the hidden system schemas.
| Schema Name | Type | Purpose / Description |
|---|---|---|
public |
Default User Schema | Created by default in every database for general user objects (if no schema is specified). |
pg_catalog |
System Schema | The core engine dictionary containing system tables (pg_class, pg_database), built-in data types, functions, and operators. (Equivalent to Oracle’s SYS schema). |
information_schema |
System Schema | ANSI SQL-standard read-only views exposing metadata about tables, columns, privileges, and constraints across the current database. |
pg_toast |
System Schema | The Oversized-Attribute Storage Technique. Automatically stores large column values (like huge text/JSONB/bytea) out-of-line when rows exceed 2KB. |
pg_temp_N |
System Schema | Created dynamically per session when a user creates a TEMP TABLE. Automatically cleaned up on session disconnect. |
-- 1. Standard \dn shows only user schemas:
gebua=# \dn
List of schemas
Name | Owner
--------+-------------------
geb | gebadm
public | pg_database_owner
(2 rows)
-- 2. \dnS shows ONLY System Schemas:
gebua=# \dnS
List of schemas
Name | Owner
--------------------+-------------------
information_schema | postgres
pg_catalog | postgres
pg_toast | postgres
(3 rows)
-- 3. \dn+ * shows ALL schemas (User + System) with ACL privileges:
gebua=# \dn+ *
List of schemas
Name | Owner | Access privileges | Description
--------------------+-------------------+----------------------------------------+----------------------------------
geb | gebadm | gebadm=UC/gebadm +|
information_schema | postgres | postgres=UC/postgres +|
| | =U/postgres |
pg_catalog | postgres | postgres=UC/postgres +| system catalog schema
| | =U/postgres |
pg_toast | postgres | | reserved schema for TOAST tables
public | pg_database_owner | pg_database_owner=UC/pg_database_owner+| standard public schema
| | =U/pg_database_owner |
(5 rows)
2. pg_catalog vs. information_schema: Which One Should You Use?
β’
information_schema is built for portability. Its views follow the SQL standard across PostgreSQL, MySQL, and SQL Server, but it omits PostgreSQL-specific features (like OIDs, TOAST, Vacuum stats, and partitioning internals).β’
pg_catalog is the native source of truth. It is faster, exposes every PostgreSQL internal detail, and is automatically searched before anything in your search_path (so you never have to prefix pg_catalog.pg_database).3. The public Schema
Every new database starts with a public schema owned by pg_database_owner. When users run CREATE TABLE test(id int); without specifying a schema, PostgreSQL places it in public (assuming public is in their search_path).
CREATE + USAGE privileges on public by default. Starting in PostgreSQL 15, only USAGE is granted to PUBLIC, preventing non-owners from creating objects in the public schema unless explicitly granted.4. Exploring information_schema (ANSI Standard Views)
The information_schema contains views that filter metadata for the currently connected database based on what the current user has permission to see.
-- 1. List all tables in a specific schema using information_schema.tables
gebua=# SELECT table_schema, table_name, table_type
FROM information_schema.tables
WHERE table_schema = 'geb';
table_schema | table_name | table_type
--------------+------------+------------
geb | dept | BASE TABLE
geb | emp | BASE TABLE
geb | payroll | BASE TABLE
(3 rows)
-- 2. Inspect column data types and nullability using information_schema.columns
gebua=# SELECT column_name, data_type, is_nullable, column_default
FROM information_schema.columns
WHERE table_schema = 'geb' AND table_name = 'emp';
column_name | data_type | is_nullable | column_default
-------------+-------------------+-------------+--------------------------------------------
emp_id | integer | NO | nextval('geb.emp_emp_id_seq'::regclass)
emp_name | character varying | NO |
dept_id | integer | YES |
salary | numeric | YES |
(4 rows)
-- 3. Check table-level privileges granted in the schema
gebua=# SELECT grantor, grantee, table_name, privilege_type
FROM information_schema.role_table_grants
WHERE table_schema = 'geb' AND grantee IN ('raj', 'sugi', 'geb_ro');
grantor | grantee | table_name | privilege_type
---------+---------+------------+----------------
gebadm | geb_ro | dept | SELECT
gebadm | sugi | dept | SELECT
gebadm | geb_ro | emp | SELECT
gebadm | raj | emp | SELECT
gebadm | geb_ro | payroll | SELECT
(5 rows)
5. Core pg_catalog Tables (The Engine Room)
In pg_catalog, base catalog tables store raw metadata using Object Identifiers (oid). The most important catalog tables for DBAs are:
pg_database: Stores all databases in the cluster, their owners, encodings, collations, and connection limits.pg_namespace: Stores all schemas in the current database.pg_class: The master object catalog (similar to Oracle’sDBA_OBJECTS). Stores tables, indexes, sequences, views, materialized views, and TOAST tables.pg_attribute: Stores every column of every table inpg_class.
-- 1. Inspect databases via pg_database (Cluster-wide catalog)
gebua=# SELECT oid, datname, pg_get_userbyid(datdba) AS owner,
pg_encoding_to_char(encoding) AS encoding, datconnlimit
FROM pg_database;
oid | datname | owner | encoding | datconnlimit
-------+-----------+----------+----------+--------------
5 | postgres | postgres | UTF8 | -1
41538 | gebua | postgres | UTF8 | -1
1 | template1 | postgres | UTF8 | -1
4 | template0 | postgres | UTF8 | -1
(4 rows)
-- 2. Inspect schemas via pg_namespace
gebua=# SELECT oid, nspname AS schema_name, pg_get_userbyid(nspowner) AS owner
FROM pg_namespace;
oid | schema_name | owner
-------+--------------------+-------------------
99 | pg_toast | postgres
11 | pg_catalog | postgres
2200 | public | pg_database_owner
13410 | information_schema | postgres
41539 | geb | gebadm
(5 rows)
-- 3. Inspect objects in 'geb' schema via pg_class
gebua=# SELECT c.oid, c.relname AS object_name, c.relkind,
pg_size_pretty(pg_total_relation_size(c.oid)) AS total_size
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = 'geb';
oid | object_name | relkind | total_size
-------+------------------------+---------+------------
41540 | dept_dept_id_seq | S | 8192 bytes
41541 | dept | r | 24 kB
41545 | dept_pkey | i | 16 kB
41547 | emp_emp_id_seq | S | 8192 bytes
41548 | emp | r | 16 kB
41552 | emp_pkey | i | 16 kB
(6 rows)
pg_class.relkind Legend (Object Types):
| Code | Object Type | Code | Object Type |
|---|---|---|---|
| r | Ordinary Table | v | View |
| i | Index | m | Materialized View |
| S | Sequence | t | TOAST Table |
| p | Partitioned Table | f | Foreign Table |
6. User-Friendly pg_catalog Views (pg_tables, pg_views, pg_indexes)
Because joining pg_class and pg_namespace every time is tedious, PostgreSQL provides pre-joined human-readable views inside pg_catalog:
-- 1. Query pg_tables
gebua=# SELECT schemaname, tablename, tableowner, tablespace, hasindexes
FROM pg_tables
WHERE schemaname = 'geb';
schemaname | tablename | tableowner | tablespace | hasindexes
------------+-----------+------------+------------+------------
geb | dept | gebadm | | t
geb | emp | gebadm | | t
geb | payroll | gebadm | | t
(3 rows)
-- 2. Query pg_indexes (includes full index creation DDL!)
gebua=# SELECT schemaname, tablename, indexname, indexdef
FROM pg_indexes
WHERE schemaname = 'geb';
schemaname | tablename | indexname | indexdef
------------+-----------+--------------+----------------------------------------------------------------------
geb | dept | dept_pkey | CREATE UNIQUE INDEX dept_pkey ON geb.dept USING btree (dept_id)
geb | emp | emp_pkey | CREATE UNIQUE INDEX emp_pkey ON geb.emp USING btree (emp_id)
geb | payroll | payroll_pkey | CREATE UNIQUE INDEX payroll_pkey ON geb.payroll USING btree (payroll_id)
(3 rows)
-- 3. Query pg_sequences
gebua=# SELECT schemaname, sequencename, sequenceowner, last_value
FROM pg_sequences
WHERE schemaname = 'geb';
schemaname | sequencename | sequenceowner | last_value
------------+------------------------+---------------+------------
geb | dept_dept_id_seq | gebadm | 3
geb | emp_emp_id_seq | gebadm | 3
geb | payroll_payroll_id_seq | gebadm | 1
(3 rows)
7. Understanding pg_toast (Oversized Data Storage)
PostgreSQL uses a fixed page size (default 8 KB) and does not allow a single row to span across multiple pages. When a table has variable-length columns (VARCHAR, TEXT, JSONB, BYTEA), PostgreSQL automatically creates a companion TOAST table in the pg_toast schema to compress and break large values into chunks.
-- How to find the hidden pg_toast table associated with your user table:
gebua=# SELECT c.relname AS table_name,
c.oid AS table_oid,
t.relname AS toast_table_name,
t.oid AS toast_oid
FROM pg_class c
LEFT JOIN pg_class t ON c.reltoastrelid = t.oid
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = 'geb' AND c.relkind = 'r';
table_name | table_oid | toast_table_name | toast_oid
------------+-----------+------------------+-----------
dept | 41541 | |
emp | 41548 | |
payroll | 41558 | pg_toast_41558 | 41562
(3 rows)
-- Notice the toast table name is always 'pg_toast_' + the parent table's OID!
gebua=# \d+ pg_toast.pg_toast_41558
TOAST table "pg_toast.pg_toast_41558"
Column | Type | Storage
------------+---------+---------
chunk_id | oid | plain
chunk_seq | integer | plain
chunk_data | bytea | plain
Owning table: "geb.payroll"
Indexes:
"pg_toast_41558_index" PRIMARY KEY, btree (chunk_id, chunk_seq)
8. Temporary Schemas (pg_temp & pg_toast_temp)
When a session creates a temporary table, PostgreSQL assigns that session a dedicated temporary schema named pg_temp_N (and a matching pg_toast_temp_N). Other sessions cannot see your temp tables without qualifying the schema.
gebua=# CREATE TEMP TABLE session_scratchpad (id int, notes text);
CREATE TABLE
gebua=# SELECT n.nspname AS schema_name, c.relname AS table_name
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname = 'session_scratchpad';
schema_name | table_name
-------------+--------------------
pg_temp_4 | session_scratchpad
(1 row)
9. Dynamic Performance & Statistics Views (pg_stat_*)
PostgreSQL tracks live runtime activity and object access statistics in real-time views prefixed with pg_stat_ (similar to Oracle’s V$ dynamic performance views):
-- 1. Active sessions & running queries (pg_stat_activity)
gebua=# SELECT pid, usename, datname, client_addr, state, query
FROM pg_stat_activity
WHERE datname = 'gebua';
pid | usename | datname | client_addr | state | query
-------+----------+---------+-------------+--------+----------------------------------------------------------
88412 | postgres | gebua | | active | SELECT pid, usename, datname, client_addr, state, query...
(1 row)
-- 2. Table scan, row count, and VACUUM/ANALYZE health (pg_stat_user_tables)
gebua=# SELECT schemaname, relname, seq_scan, idx_scan, n_live_tup, n_dead_tup, last_autovacuum
FROM pg_stat_user_tables
WHERE schemaname = 'geb';
schemaname | relname | seq_scan | idx_scan | n_live_tup | n_dead_tup | last_autovacuum
------------+---------+----------+----------+------------+------------+-----------------
geb | dept | 4 | 3 | 3 | 0 |
geb | emp | 8 | 0 | 3 | 0 |
geb | payroll | 3 | 0 | 1 | 0 |
(3 rows)
-- 3. Database-level commits, rollbacks, cache hits, and temp files (pg_stat_database)
gebua=# SELECT datname, numbackends, xact_commit, xact_rollback, blks_read, blks_hit
FROM pg_stat_database
WHERE datname = 'gebua';
datname | numbackends | xact_commit | xact_rollback | blks_read | blks_hit
---------+-------------+-------------+---------------+-----------+----------
gebua | 1 | 412 | 14 | 285 | 14820
(1 row)
10. DBA Trick: Reveal Hidden Metadata Queries in psql
Ever wonder which catalog tables psql queries when you type \dt, \du, or \dn+? Enable ECHO_HIDDEN inside psql (or start psql with psql -E) to print the exact internal SQL query before its output!
gebua=# \set ECHO_HIDDEN on
gebua=# \dn
********* QUERY **********
SELECT n.nspname AS "Name",
pg_catalog.pg_get_userbyid(n.nspowner) AS "Owner"
FROM pg_catalog.pg_namespace n
WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'
ORDER BY 1;
**************************
List of schemas
Name | Owner
--------+-------------------
geb | gebadm
public | pg_database_owner
(2 rows)
-- Turn it back off when done:
gebua=# \set ECHO_HIDDEN off
11. Oracle vs. PostgreSQL Data Dictionary Cheat Sheet
| Metadata Purpose | Oracle View / Table | PostgreSQL View / Catalog |
|---|---|---|
| Database Information | V$DATABASE |
pg_database |
| Instance Parameters | V$PARAMETER / V$SPPARAMETER |
pg_settings / pg_file_settings |
| All Database Objects | DBA_OBJECTS |
pg_class |
| Tables | DBA_TABLES / ALL_TABLES |
pg_tables / information_schema.tables |
| Table Columns | DBA_TAB_COLUMNS |
information_schema.columns / pg_attribute |
| Indexes | DBA_INDEXES |
pg_indexes / pg_index |
| Views | DBA_VIEWS |
pg_views / information_schema.views |
| Users & Roles | DBA_USERS / DBA_ROLES |
pg_user / pg_roles / pg_authid |
| Role Memberships | DBA_ROLE_PRIVS |
pg_auth_members |
| Table Grants / Privileges | DBA_TAB_PRIVS |
information_schema.role_table_grants |
| Tablespaces | DBA_TABLESPACES |
pg_tablespace |
| Active Sessions | V$SESSION / V$PROCESS |
pg_stat_activity |
| Locks | V$LOCK / DBA_BLOCKERS |
pg_locks / pg_blocking_pids() |