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.
| attrelid | attnum | attname | atttypid |
|---|---|---|---|
| 16385 | 1 | id | 23 |
| 16385 | 2 | name | 25 |
| 16385 | 3 | 25 | |
| 16385 | 4 | city | 25 |
| 16385 | 5 | country | 1042 |
| 16385 | 6 | created_at | 1184 |
| oid | typname | typlen |
|---|---|---|
| 23 | int4 | 4 |
| 25 | text | -1 |
| 1042 | bpchar | -1 |
| 1184 | timestamptz | 8 |
| oid | relname | relnamespace | relkind |
|---|---|---|---|
| 16384 | customers_id_seq | 2200 | S |
| 16385 | customers | 2200 | r |
| 16394 | customers_pkey | 2200 | i |
| 16396 | customers_email_key | 2200 | i |
| oid | nspname |
|---|---|
| 11 | pg_catalog |
| 2200 | public |
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.
the four tables
the schemas first. public is where shop's tables live, and pg_catalog is where the catalog itself lives:
SELECT oid, nspname FROM pg_namespace WHERE nspname IN ('pg_catalog', 'public') ORDER BY oid;| oidoid | nspnamename |
|---|---|
| 11 | pg_catalog |
| 2200 | public |
every relation, with its schema as a number:
SELECT oid, relname, relnamespace, relkind
FROM pg_class
WHERE relname LIKE 'customers%'
ORDER BY oid;| oidoid | relnamename | relnamespaceoid | relkindchar |
|---|---|---|---|
| 16384 | customers_id_seq | 2200 | S |
| 16385 | customers | 2200 | r |
| 16394 | customers_pkey | 2200 | i |
| 16396 | customers_email_key | 2200 | i |
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:
SELECT attrelid, attnum, attname, atttypid
FROM pg_attribute
WHERE attrelid = 16385
ORDER BY attnum;| attrelidoid | attnumint2 | attnamename | atttypidoid |
|---|---|---|---|
| 16385 | -6 | tableoid | 26 |
| 16385 | -5 | cmax | 29 |
| 16385 | -4 | xmax | 28 |
| 16385 | -3 | cmin | 29 |
| 16385 | -2 | xmin | 28 |
| 16385 | -1 | ctid | 27 |
| 16385 | 1 | id | 23 |
| 16385 | 2 | name | 25 |
| 16385 | 3 | 25 | |
| 16385 | 4 | city | 25 |
| 16385 | 5 | country | 1042 |
| 16385 | 6 | created_at | 1184 |
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:
SELECT oid, typname, typlen
FROM pg_type
WHERE oid IN (23, 25, 1042, 1184)
ORDER BY oid;| oidoid | typnamename | typlenint2 |
|---|---|---|
| 23 | int4 | 4 |
| 25 | text | -1 |
| 1042 | bpchar | -1 |
| 1184 | timestamptz | 8 |
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:
SELECT oid, relname, relkind
FROM pg_class
WHERE relname IN ('pg_class', 'pg_attribute', 'pg_type', 'pg_namespace')
ORDER BY oid;| oidoid | relnamename | relkindchar |
|---|---|---|
| 1247 | pg_type | r |
| 1249 | pg_attribute | r |
| 1259 | pg_class | r |
| 2615 | pg_namespace | r |
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:
SELECT relnamespace::regnamespace AS schema, count(*) AS relations
FROM pg_class
GROUP BY relnamespace
ORDER BY relations DESC, schema;| schemaregnamespace | relationsint8 |
|---|---|
| pg_catalog | 268 |
| pg_toast | 84 |
| information_schema | 69 |
| public | 13 |
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.
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:
SELECT 'customers'::regclass::oid AS oid, 16385::regclass AS name;| oidoid | nameregclass |
|---|---|
| 16385 | customers |
and a name that doesn't resolve fails right there, which is exactly the error the analyzer gives you for a typo in FROM:
SELECT 'custmers'::regclass;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:
CREATE TABLE notes (body text);
SELECT 'notes'::regclass::oid AS oid;| oidoid |
|---|
| 24576 |
CREATE VIEW paris AS SELECT name FROM customers WHERE city = 'Paris';
ALTER TABLE customers RENAME TO clients;
SELECT count(*) FROM paris;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');| countint8 |
|---|
| 66 |
| pg_get_viewdeftext |
|---|
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:
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'customers'
ORDER BY ordinal_position;| column_namename | data_typevarchar | is_nullablevarchar |
|---|---|---|
| id | integer | NO |
| name | text | NO |
| text | YES | |
| city | text | YES |
| country | character | NO |
| created_at | timestamp with time zone | NO |
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.