Heap Pages and TOAST

Topics Covered

Files, Forks and Segments

The data directory

Relfilenode versus object identifier

One gigabyte segments

Forks

Tablespaces

Measuring relation size

Catalogs are tables too

Unlogged and temporary tables

What the WAL directory holds

Never edit the files directly

Before looking inside a page, it helps to know where the pages live. Every table, index, sequence and materialized view in PostgreSQL is a relation, and every relation is stored as one or more ordinary files in the data directory. There is no single large database file and no custom file system. This simplicity is deliberate: Postgres leaves caching, read ahead and block allocation to the operating system and keeps its own storage layer small. It also means that a great deal can be learned about a database by looking at the files directly, and that some operational facts, such as why a table rewrite needs as much free disk as the table itself, follow directly from the layout.

The data directory

The data directory, often called PGDATA, holds everything a cluster needs: configuration files, the WAL in the pg_wal directory, transaction status in pg_xact, the control file in global, and the databases themselves under base. Each database has a subdirectory named after its object identifier, and each relation in it has files named after its relfilenode, a number recorded in the pg_class catalog. The function pg_relation_filepath returns the path of a relation, which is handy when checking disk usage or correlating file system activity with tables. Shared catalogs that belong to the whole cluster, such as the list of databases and roles, live in the global directory instead.

Relfilenode versus object identifier

A relation has a stable object identifier that never changes, but its relfilenode can change. Commands that rewrite a table completely, such as TRUNCATE, VACUUM FULL, CLUSTER and many forms of ALTER TABLE, build a brand new file with a new relfilenode, switch the catalog to point at it when the transaction commits and delete the old file afterwards. This is how those commands achieve atomicity without logging every row: either the new file becomes the table at commit, or the transaction aborts and the new file is discarded. It is also why they need free disk space equal to the size of the new table while they run, and why they take strong locks that block other access until they finish.

Commands that rewrite a table copy the live rows into a new file. At commit the catalog points at the new file and the old one is removed.

One gigabyte segments

A relation file is split into segments of at most 1 gigabyte by default. The first segment has the plain relfilenode name, and later ones add a suffix, so a 3.5 gigabyte table appears as four files named 16384, 16384.1, 16384.2 and 16384.3. The limit dates from file systems that could not handle large files, and is kept because it makes some operations easier, for example truncating the end of a table during vacuum by removing whole files. Each segment is a sequence of 8 kilobyte blocks, so a block number together with the segment size identifies the exact file and offset of any page.

Forks

Each relation can have several forks, stored as separate files with a suffix on the relfilenode. The main fork holds the data itself. The free space map fork, with suffix fsm, records approximately how much free space each page has, so inserts can find room quickly. The visibility map fork, with suffix vm, records for each page whether all its tuples are visible to every transaction and whether they are all frozen. Unlogged tables also have an init fork, an empty copy that replaces the main fork after a crash, since unlogged data is not protected by WAL. The last section of this lesson returns to the free space map and the visibility map, which are small but have large effects on performance.

Tablespaces

A tablespace is a directory outside the data directory where Postgres can place relations, created with CREATE TABLESPACE and referenced through a symbolic link in the pg_tblspc directory. Tablespaces were once the main way to spread data over several disks or to put hot indexes on faster storage. On modern systems with large volumes, they are used less often, and they complicate backups and replicas, which need the same paths. One use remains common: setting temp_tablespaces to point temporary files at a fast local disk, so that large sorts and hashes spill to cheap scratch storage rather than to the main volume.

Measuring relation size

Several functions report sizes. pg_relation_size reports one fork of one relation, pg_table_size includes the free space map, visibility map and TOAST table, pg_indexes_size sums the indexes, and pg_total_relation_size adds everything together. Comparing these with the number of live rows in pg_stat_user_tables is a quick way to estimate bloat: a table with few rows but many gigabytes on disk is mostly dead space or free space. Because sizes come straight from the file system, they are exact, while the row counts are estimates updated by ANALYZE and vacuum, so the comparison is approximate but useful.

Catalogs are tables too

The system catalogs, such as pg_class for relations, pg_attribute for columns and pg_index for indexes, are stored as ordinary heap tables with their own files, pages and tuples. They follow the same MVCC rules as user tables, which is why DDL is transactional in Postgres: creating a table inside a transaction inserts catalog rows that no one else can see until the transaction commits, and rolling back makes them invisible again. It also means catalogs can bloat. Workloads that create and drop many temporary tables per second generate dead rows in pg_class and pg_attribute at a high rate, and a bloated catalog slows down every query planning step that reads it. Monitoring catalog sizes is a small but real part of operating such workloads.

Unlogged and temporary tables

Two kinds of tables skip the WAL. Unlogged tables write no WAL for their data changes, which makes writes much faster, at the price that after a crash their contents are replaced by the empty init fork and that they are not replicated to standbys. They suit caches and staging data that can be rebuilt. Temporary tables are private to one session, stored in per session files, also skip WAL, and are dropped at the end of the session or transaction. Temporary tables use local buffers in backend private memory, sized by temp_buffers, rather than shared buffers, so large temporary tables can be slow if that setting is left at its small default.

What the WAL directory holds

Next to the base directory, the pg_wal directory holds the write ahead log as a series of 16 megabyte segment files by default, named with long hexadecimal strings that encode the timeline and position. These files are the most critical part of the data directory: losing them after a crash means losing committed transactions. The directory grows when checkpoints are infrequent, when archiving fails or when a replication slot holds WAL for a disconnected replica, and a full disk under pg_wal stops the server from accepting writes. A later lesson covers WAL in detail. For now, the key operational point is that its size is driven by settings and by consumers, not by the size of the data.

Never edit the files directly

Knowing the file layout is useful for reading disk usage and debugging, but the files must never be copied, moved or deleted by hand while the server runs. A file copied mid write is inconsistent, a file deleted outside Postgres leaves the catalog pointing at nothing, and restoring an old copy of one relation file mixes pages from different points in time with a WAL that describes neither. Every supported way of copying data, from pg_basebackup to file system snapshots taken correctly, works at the level of the whole cluster and relies on WAL to make the copy consistent. The later lesson on backups explains why.

A relation is a set of files

Every table and index is a set of files named by relfilenode in the data directory, split into 1 gigabyte segments of 8 kilobyte pages, with separate forks for the free space map and the visibility map. Rewriting commands build a new file and switch to it at commit.