skip to content

the system catalog

pg_class, pg_attribute, pg_type. the tables that describe your tables, and how names become numbers.

address
everything.execution.system_catalog
part
part 9, how a query runs
chapter
chapter 3 of 13
reading time
7 min

postgres describes your tables in tables. pg_class has a row for every table, index and sequence, pg_attribute a row for every column, pg_type a row for every type, pg_namespace a row for every schema. together they're the system catalog.

every object in them has a number, its OID. the analyzer turns the names in your query into those numbers, and from then on postgres only uses the numbers. 'customers'::regclass does that lookup for you.

the picture

you know how \d customers in psql knows every column, type and index of a table? it isn't magic, it's a query. psql asks the catalog, which is a set of ordinary tables you can SELECT from too.

think of a library. the books are your tables. the card catalogue at the door is a drawer of cards describing the books: one card per book (pg_class), one line on it per chapter (pg_attribute), a code for what kind of chapter it is (pg_type), which room the book is in (pg_namespace). and every card has a number on it, so the rest of the library can say "book 16385" instead of spelling out the title.

column =
pg_attributeone row per column
attrelidattnumattnameatttypid
163851id23
163852name25
163853email25
163854city25
163855country1042
163856created_at1184
pg_typeone row per type
oidtypnametyplen
23int44
25text-1
1042bpchar-1
1184timestamptz8
pg_classone row per table, index, sequence…
oidrelnamerelnamespacerelkind
16384customers_id_seq2200S
16385customers2200r
16394customers_pkey2200i
16396customers_email_key2200i
pg_namespaceone row per schema
oidnspname
11pg_catalog
2200public

customers.name is pg_attribute row (16385, 2). its attrelid 16385 is the pg_class row for customers, its atttypid 25 is the type text, and the table's relnamespace 2200 is the schema public.

real output, postgres 18.6 (PGlite 0.5.12) · the run blocks on this page
the catalog rows that describe shop's customers table. pick a column: its pg_attribute row lights up, and its numbers lead to the rest: attrelid to the table's pg_class row, atttypid to its type, and the table's relnamespace to its schema.

the four tables

the schemas first. public is where shop's tables live, and pg_catalog is where the catalog itself lives:

sqldb=shop
SELECT oid, nspname FROM pg_namespace WHERE nspname IN ('pg_catalog', 'public') ORDER BY oid;
output, from postgres 18.6
SELECT 2
oidoidnspnamename
11pg_catalog
2200public
(2 rows)

every relation, with its schema as a number:

sqldb=shop
SELECT oid, relname, relnamespace, relkind
FROM pg_class
WHERE relname LIKE 'customers%'
ORDER BY oid;
output, from postgres 18.6
SELECT 4
oidoidrelnamenamerelnamespaceoidrelkindchar
16384customers_id_seq2200S
16385customers2200r
16394customers_pkey2200i
16396customers_email_key2200i
(4 rows)

four relations for one CREATE TABLE: the table (relkind r), the sequence behind its identity column (S), and two indexes (i), one for the primary key and one for the UNIQUE email. pg_class is also where the planner finds relpages and reltuples (reading EXPLAIN priced a scan with them).

the columns, keyed by the table's oid and a column number:

sqldb=shop
SELECT attrelid, attnum, attname, atttypid
FROM pg_attribute
WHERE attrelid = 16385
ORDER BY attnum;
output, from postgres 18.6
SELECT 12
attrelidoidattnumint2attnamenameatttypidoid
16385-6tableoid26
16385-5cmax29
16385-4xmax28
16385-3cmin29
16385-2xmin28
16385-1ctid27
163851id23
163852name25
163853email25
163854city25
163855country1042
163856created_at1184
(12 rows)

six columns you declared, numbered 1 to 6, and six you didn't, with negative numbers: ctid, xmin, xmax, cmin, cmax and tableoid. those are the system columns every table has, the ones MVCC runs on and ctid points at. you can select them by name; SELECT * just leaves them out.

and the types, by oid:

sqldb=shop
SELECT oid, typname, typlen
FROM pg_type
WHERE oid IN (23, 25, 1042, 1184)
ORDER BY oid;
output, from postgres 18.6
SELECT 4
oidoidtypnamenametyplenint2
23int44
25text-1
1042bpchar-1
1184timestamptz8
(4 rows)

typlen -1 means variable length (text and char(n) both store a length header); 4 and 8 are fixed sizes in bytes, which is what tuple layout aligns.

the catalog describes itself, too. pg_class has a row for pg_class:

sqldb=shop
SELECT oid, relname, relkind
FROM pg_class
WHERE relname IN ('pg_class', 'pg_attribute', 'pg_type', 'pg_namespace')
ORDER BY oid;
output, from postgres 18.6
SELECT 4
oidoidrelnamenamerelkindchar
1247pg_typer
1249pg_attributer
1259pg_classr
2615pg_namespacer
(4 rows)

oids under 10,000 are fixed when postgres is built, so pg_class is 1259 on every postgres there is. the oids of your objects are handed out as they're created. and most of what pg_class describes isn't yours:

sqldb=shop
SELECT relnamespace::regnamespace AS schema, count(*) AS relations
FROM pg_class
GROUP BY relnamespace
ORDER BY relations DESC, schema;
output, from postgres 18.6
SELECT 4
schemaregnamespacerelationsint8
pg_catalog268
pg_toast84
information_schema69
public13
(4 rows)
pg_catalog268 relations
pg_toast84 relations
information_schema69 relations
public13 relations
rows in pg_class on the shop database, by schema. shop's four tables, their indexes and sequences are the short bar; the rest is postgres describing itself, plus the toast tables that hold long values.

names become numbers

when you write SELECT name FROM customers, the parser only sees words. the analyzer is the step that looks them up (the life of a query):

hover a stage.

how customers.name becomes numbers, with this page's real values. hover a stage.

regclass is that first lookup as a cast. text to regclass resolves a name through the search_path; an oid cast to regclass prints the name:

sqldb=shop
SELECT 'customers'::regclass::oid AS oid, 16385::regclass AS name;
output, from postgres 18.6
SELECT 1
oidoidnameregclass
16385customers
(1 row)

and a name that doesn't resolve fails right there, which is exactly the error the analyzer gives you for a typo in FROM:

sqldb=shop
SELECT 'custmers'::regclass;
output, from postgres 18.6
ERROR 42P01: relation "custmers" does not exist
1 │ SELECT 'custmers'::regclass;
at line 1, column 8

regtype, regproc, regnamespace and friends do the same for other catalogs. attrelid = 'customers'::regclass is the idiom you'll see everywhere instead of a raw oid.

a table you create gets the next free oid. in this page's engine the first one is 24576, because the database you're running on was restored from a snapshot:

sqldb=shop
CREATE TABLE notes (body text);
SELECT 'notes'::regclass::oid AS oid;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · SELECT 1
oidoid
24576
(1 row)
predict the output
a view uses customers. you rename the table to clients. what happens to the view?
sql
CREATE VIEW paris AS SELECT name FROM customers WHERE city = 'Paris';
ALTER TABLE customers RENAME TO clients;
SELECT count(*) FROM paris;
sqldb=shop
CREATE VIEW paris AS SELECT name FROM customers WHERE city = 'Paris';
ALTER TABLE customers RENAME TO clients;
 
SELECT count(*) FROM paris;
SELECT pg_get_viewdef('paris');
output, from postgres 18.6
#1 · CREATE VIEW
#2 · ALTER TABLE
#3 · SELECT 1
countint8
66
(1 row)
#4 · SELECT 1
pg_get_viewdeftext
(1 row)

the standard's version

pg_catalog is postgres's own, and its layout changes between versions. the SQL standard defines information_schema, a set of views with the same information in a portable shape, which postgres builds on top of the catalog:

sqldb=shop
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'customers'
ORDER BY ordinal_position;
output, from postgres 18.6
SELECT 6
column_namenamedata_typevarcharis_nullablevarchar
idintegerNO
nametextNO
emailtextYES
citytextYES
countrycharacterNO
created_attimestamp with time zoneNO
(6 rows)

the standard's names are longer and its types are spelled out (timestamp with time zone, not timestamptz). tools that talk to many databases use information_schema; anything postgres-specific (relpages, oids, system columns) is only in pg_catalog.

keylines

  • the catalog is ordinary tables: pg_class, pg_attribute, pg_type, pg_namespace and many more.
  • every object has an oid; columns are (table oid, attnum).
  • the analyzer turns names into oids; after that, postgres only uses numbers.
  • regclass casts a name to an oid through the search_path, and back.
  • information_schema is the standard's portable view of the same facts.