skip to content

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
real output, postgres 18.6 (PGlite 0.5.12) · lab-files
every file of notes, as 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:

sqldb=lab
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;
output, from postgres 18.6
#1 · SELECT 2
oidoidrelnamenamerelfilenodeoidpathtext
16486notes16486base/5/16486
16493notes_pkey16493base/5/16493
(2 rows)
#2 · SELECT 3
oidoiddatnamename
1template1
4template0
5postgres
(3 rows)

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.

predict the output
you TRUNCATE notes. does it keep its file, 16486?
sql
SELECT relfilenode FROM pg_class WHERE relname = 'notes';  -- 16486
TRUNCATE notes;
SELECT relfilenode FROM pg_class WHERE relname = 'notes';  -- ?
sqldb=lab
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;
output, from postgres 18.6
#1 · SELECT 1
oidoidrelfilenodeoidpg_relation_filepathtext
1648616486base/5/16486
(1 row)
#2 · TRUNCATE TABLE
#3 · SELECT 1
oidoidrelfilenodeoidpg_relation_filepathtext
1648624576base/5/24576
(1 row)
#4 · SELECT 1
old_file_belongs_toregclass
null
(1 row)

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:

sqldb=lab
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;
output, from postgres 18.6
#1 · SELECT 2
filetextbytesint8
1648640960
16486_fsm24576
(2 rows)
#2 · VACUUM
#3 · SELECT 3
filetextbytesint8
1648640960
16486_fsm24576
16486_vm8192
(3 rows)
#4 · SELECT 1
mainint8fsmint8vmint8everythingint8
4096024576819298304
(1 row)

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 =

block 4 ÷ 131,072 blocks per segment = segment 0, so it's in the file 16486, 32,768 bytes in (4 × 8,192).

pick a block number. with 8 KB blocks and 1 GB segments, each file holds 131,072 blocks, so the block number divided by 131,072 says which file, and the remainder says where in it.

both sizes are compile-time settings, and these are the defaults:

sqldb=lab
SHOW block_size;
SHOW segment_size;
output, from postgres 18.6
#1 · SHOW
block_sizetext
8192
(1 row)
#2 · SHOW
segment_sizetext
1GB
(1 row)

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:

sqlempty db
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;
output, from a real postgres 18.6 server
#1 · CREATE TABLE
#2 · ALTER TABLE
#3 · INSERT 0 530000
#4 · SELECT 1
pagesint8
132500
(1 row)
#5 · SELECT 4
filetextbytesint8
<relfilenode>1073741824
<relfilenode>.111698176
<relfilenode>_fsm286720
<relfilenode>_init0
(4 rows)

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
the top of the data directory, as pg_ls_dir('.') lists it in the browser engine. a native server has the same entries and a few more.
sqldb=lab
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;
output, from postgres 18.6
SELECT 7
entrytext
PG_VERSION
base
global
pg_multixact
pg_tblspc
pg_wal
pg_xact
(7 rows)

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.