Sub topic 1
Where a Table Lives on Disk: Files, Segments, and Forks
PostgreSQL stores a table as a relation. A relation is a database object with one or more files on disk. This chapter assumes basic SQL, familiarity with psql, a general idea of transactions, and basic knowledge of files and bytes.
The table name in SQL is a logical name. PostgreSQL maps that name to an internal relation identifier and then to a path inside a database directory. You normally should not edit these files directly. PostgreSQL manages their names, contents, and lifecycle.
Prerequisites
You should know how to run CREATE TABLE, INSERT, and SELECT statements. The examples also use PostgreSQL-specific functions and settings. Run them in a test database, because the later chapters will inspect physical storage in more detail.
From a table name to a file
Every database has a data directory. Inside it, each database has a directory identified by an internal database object identifier. A table's main data file is stored there, unless the table belongs to a user-defined tablespace.
PostgreSQL gives the relation a file-node number, often called a relfilenode. This number is not always the same as the table's object identifier. Operations such as TRUNCATE, some forms of REINDEX, and table rewrites can create a new physical file while preserving the logical table name.
Use pg_relation_filepath to find the path relative to PostgreSQL's data directory:
CREATE TABLE accounts (
id int PRIMARY KEY,
owner text,
balance numeric
);
INSERT INTO accounts
VALUES (1, 'alice', 100), (2, 'bob', 200);
SELECT pg_relation_filepath('accounts');A result might look like base/16384/24576. The first number identifies the database directory. The final number identifies the relation's main file. The exact values differ between installations.
The result is relative to the data directory, not necessarily a path that your operating-system user can read. A managed PostgreSQL service may not expose the data directory at all.
Segments: splitting a large relation
A relation's main file is divided into segments. PostgreSQL normally creates a new segment when the current segment reaches about 1 GB. The first segment has the base relfilenode as its filename. Later segments add suffixes such as .1, .2, and .3.
For example:
| Logical relation | Possible files |
|---|---|
| `accounts` | `24576`, `24576.1`, `24576.2` |
Segments do not change the table's logical identity. PostgreSQL treats them as one relation and calculates a block number across the complete relation. A table can therefore have block numbers that refer to pages in later segment files.
The segment limit keeps individual files manageable and supports filesystems with size limits. It does not mean that every table has multiple files. Small tables usually have only the first segment.
Forks: different physical views of one relation
A relation can have several forks. Each fork stores a different kind of physical information.
| Fork | Typical suffix | Purpose |
|---|---|---|
| Main | none | Table rows or index entries |
| Free space map | `_fsm` | Approximate free space available on pages |
| Visibility map | `_vm` | Whether pages contain tuples visible to all transactions |
| Init | `_init` | Initialization data for unlogged relations |
The main fork is the one that holds ordinary table data. Indexes also have a main fork, but their pages contain index structures rather than heap tuples.
The free space map helps PostgreSQL find pages with room for new or updated tuples. The visibility map records page-level visibility information used by operations such as index-only scans and efficient vacuuming. An unlogged table can have an initialization fork. PostgreSQL uses it to restore the table to an empty state after a crash.
This relationship can be summarized as follows:
Inspecting size without opening files
SQL size functions report the storage used by a relation:
SHOW block_size;
SELECT pg_relation_filepath('accounts');
SELECT pg_relation_size('accounts') AS bytes,
pg_relation_size('accounts')
/ current_setting('block_size')::int AS pages;pg_relation_size reports the size of one fork, which defaults to the main fork. Its result includes the relation's physical pages, including unused space inside the relation.
pg_table_size includes the table's main fork and its auxiliary storage, such as the free space map, visibility map, and TOAST table. pg_total_relation_size also includes indexes. These functions measure allocated relation storage, not just the logical length of values returned by a query.
Exercise
A table has a main file named 50000 and also files named 50000.1 and 50000_fsm. What do these files represent?
Show answer
The files named 50000 and 50000.1 are segments of the main fork. The file named 50000_fsm belongs to the free space map fork.