the files behind a table
relfilenodes, the main, fsm and vm forks, 1 GB segments, and pg_relation_filepath.
- address
- everything.storage.files_and_forks
- part
- part 12, pages and the buffer pool
- chapter
- chapter 2 of 12
- reading time
- 8 min
a table is a handful of ordinary files in the data directory, named not by the table's name but by a number, its relfilenode. the rows are in the main fork. next to it sit the free space map (_fsm) and the visibility map (_vm), and every file is cut into 1 GB segments: 16486, 16486.1, 16486.2, and so on.
pg_relation_filepath() tells you which file is which, and the browser engine lets you list them.
the picture
you know how CREATE TABLE gives you a table, and somewhere on the server's disk a file appears? it's not called notes. it's called something like 16486. (the file is the easy part. what a database actually is is about everything the server adds on top of it.)
think of an archive where every drawer is labelled with a number, never a name, and a ledger at the front says which number belongs to which name (the catalog). in each drawer there's the main folder with the documents (the main fork), a sticky note saying roughly how much room is left in each section (the free space map), and a checklist of sections already inspected (the visibility map). when a folder gets too thick, it's split: volume 1, volume 2, volume 3 (segments).
here are the lab's notes table and everything that belongs to it, listed from the data directory:
- the database's directory
- ├── 16486 40,960 Bnotes, the table: 5 pages
- ├── 16486_fsm 24,576 Bnotes's free space map: 3 pages
- ├── 16491 0 Bpg_toast_16486, its toast table: empty
- ├── 16492 8,192 Bpg_toast_16486_index, an index: 1 page
- └── 16493 16,384 Bnotes_pkey, an index: 2 pages
pg_ls_dir() and pg_stat_file() saw them. the names are numbers; the notes say what each one is.five files for one small table. the heap itself (5 pages), its free space map, an index for the primary key, and a toast table with its own index, empty because no note is big enough to need it (TOAST is what that's for).
the name is a number
the catalog maps names to files. pg_class has two numbers for every table: its oid, its identity forever, and its relfilenode, the name of its file right now:
SELECT oid, relname, relfilenode, pg_relation_filepath(oid) AS path
FROM pg_class WHERE relname IN ('notes', 'notes_pkey') ORDER BY relname;
SELECT oid, datname FROM pg_database ORDER BY oid;| oidoid | relnamename | relfilenodeoid | pathtext |
|---|---|---|---|
| 16486 | notes | 16486 | base/5/16486 |
| 16493 | notes_pkey | 16493 | base/5/16493 |
| oidoid | datnamename |
|---|---|
| 1 | template1 |
| 4 | template0 |
| 5 | postgres |
base/5/16486: the base directory holds one directory per database, named by the database's oid (5 is postgres, the database the browser runs in), and inside it one file per relation, named by its relfilenode. for notes they're the same number. they don't have to be.
SELECT relfilenode FROM pg_class WHERE relname = 'notes'; -- 16486
TRUNCATE notes;
SELECT relfilenode FROM pg_class WHERE relname = 'notes'; -- ?SELECT oid, relfilenode, pg_relation_filepath('notes') FROM pg_class WHERE relname = 'notes';
TRUNCATE notes;
SELECT oid, relfilenode, pg_relation_filepath('notes') FROM pg_class WHERE relname = 'notes';
SELECT pg_filenode_relation(0, 16486) AS old_file_belongs_to;| oidoid | relfilenodeoid | pg_relation_filepathtext |
|---|---|---|
| 16486 | 16486 | base/5/16486 |
| oidoid | relfilenodeoid | pg_relation_filepathtext |
|---|---|---|
| 16486 | 24576 | base/5/24576 |
| old_file_belongs_toregclass |
|---|
| null |
same oid, new relfilenode, and the old number now belongs to nobody. (anything that has to survive a rewrite, like a foreign key or a grant, refers to the oid.)
forks
a relation's files share the relfilenode and differ by suffix. each one is a fork:
- main, no suffix: the table's pages, or the index's.
- fsm,
_fsm: the free space map, roughly how much room each page has. - vm,
_vm: the visibility map, two bits per page. - init,
_init: only for unlogged tables and their indexes. an empty copy that replaces the main fork after a crash.
the free space and visibility maps are created when they're first needed. the lab's notes were never vacuumed, so it has no visibility map yet. VACUUM makes one:
SELECT f AS file, (pg_stat_file('base/5/' || f)).size AS bytes
FROM pg_ls_dir('base/5') AS f WHERE f LIKE '16486%' ORDER BY f;
VACUUM notes;
SELECT f AS file, (pg_stat_file('base/5/' || f)).size AS bytes
FROM pg_ls_dir('base/5') AS f WHERE f LIKE '16486%' ORDER BY f;
SELECT pg_relation_size('notes', 'main') AS main, pg_relation_size('notes', 'fsm') AS fsm,
pg_relation_size('notes', 'vm') AS vm, pg_total_relation_size('notes') AS everything;| filetext | bytesint8 |
|---|---|
| 16486 | 40960 |
| 16486_fsm | 24576 |
| filetext | bytesint8 |
|---|---|
| 16486 | 40960 |
| 16486_fsm | 24576 |
| 16486_vm | 8192 |
| mainint8 | fsmint8 | vmint8 | everythingint8 |
|---|---|---|---|
| 40960 | 24576 | 8192 | 98304 |
16486_vm appears, one 8 KB page. pg_relation_size() reads one fork, and pg_total_relation_size() adds up every fork of the table, its indexes and its toast table: 98,304 bytes for 40,960 bytes of rows.
1 GB segments
no file grows past 1 GB. when a fork fills its first gigabyte, the next block goes into a new file with .1 on the end, then .2, and so on. so finding block b is plain arithmetic:
block 4 ÷ 131,072 blocks per segment = segment 0, so it's in the file 16486, 32,768 bytes in (4 × 8,192).
both sizes are compile-time settings, and these are the defaults:
SHOW block_size;
SHOW segment_size;| block_sizetext |
|---|
| 8192 |
| segment_sizetext |
|---|
| 1GB |
the browser can't hold a gigabyte, so here's one on a native postgres 18.6: an unlogged table, filled with rows of 2,000 characters until it's just over 1 GB:
CREATE UNLOGGED TABLE big (pad text);
ALTER TABLE big ALTER COLUMN pad SET STORAGE PLAIN;
INSERT INTO big SELECT repeat('x', 2000) FROM generate_series(1, 530000);
SELECT pg_relation_size('big') / 8192 AS pages;
SELECT replace(f, pg_relation_filenode('big')::text, '<relfilenode>') AS file,
(pg_stat_file(regexp_replace(pg_relation_filepath('big'), '[0-9]+$', '') || f)).size AS bytes
FROM pg_ls_dir(regexp_replace(pg_relation_filepath('big'), '/[0-9]+$', '')) AS f
WHERE f LIKE pg_relation_filenode('big') || '%'
ORDER BY f;| pagesint8 |
|---|
| 132500 |
| filetext | bytesint8 |
|---|---|
| <relfilenode> | 1073741824 |
| <relfilenode>.1 | 11698176 |
| <relfilenode>_fsm | 286720 |
| <relfilenode>_init | 0 |
checked on a real postgres 18.6 server. the browser engine can't run this one.
the first segment is exactly 1,073,741,824 bytes, 131,072 pages, and the rest spill into .1. the free space map got big enough to notice, and there's the _init fork, empty: an unlogged table's main fork is replaced by that empty copy after a crash, because without WAL there's nothing to rebuild it from. (SET STORAGE PLAIN keeps the 2,000-character values from being compressed, so the rows stay big.)
the rest of the data directory
base/ is one part of the data directory. the browser engine's has the same layout as any postgres server:
- ├── PG_VERSIONthe major version, as text
- ├── base/one directory per database, named by its oid
- ├── global/catalogs shared by every database
- ├── pg_multixact/multixact state, for rows locked by several transactions
- ├── pg_tblspc/links to tablespaces
- ├── pg_wal/the write-ahead log
- └── pg_xact/the commit log: two bits per transaction
pg_ls_dir('.') lists it in the browser engine. a native server has the same entries and a few more.SELECT d AS entry FROM pg_ls_dir('.') AS d
WHERE d IN ('base', 'global', 'pg_wal', 'pg_xact', 'pg_multixact', 'pg_tblspc', 'PG_VERSION')
ORDER BY d;| entrytext |
|---|
| PG_VERSION |
| base |
| global |
| pg_multixact |
| pg_tblspc |
| pg_wal |
| pg_xact |
global/ holds the catalogs shared by all databases (pg_database itself lives there). pg_wal/ is the write-ahead log. pg_xact/ is the commit log, two bits per transaction. pg_tblspc/ has links to tablespaces, other directories you can put tables in.
keylines
- a table is files named by its relfilenode, in base/<database oid>/.
- forks: main (the pages), _fsm, _vm, and _init for unlogged tables.
- each fork is cut into 1 GB segments: 131,072 pages per file.
- TRUNCATE and VACUUM FULL write a new file: same oid, new relfilenode.
- the files are only consistent with the WAL and the commit log. never back up by copying them.