Sub topic 2
Pages: the 8 KB Unit of Storage and I/O
This chapter assumes you know basic SQL, can use psql, understand files and bytes, and have a general idea of database transactions. You will use PostgreSQL size functions to connect a table's logical rows with its physical pages.
A table is stored as pages
PostgreSQL stores a table in a relation file. The file is divided into fixed-size blocks, also called pages. In a standard PostgreSQL build, each page is 8,192 bytes, or 8 KB.
A page is PostgreSQL's main unit for reading and writing table data. The buffer manager loads pages into shared memory, and queries inspect rows within those pages. The operating system and storage device may use different physical I/O sizes, but PostgreSQL addresses table data in these logical 8 KB blocks.
The page size is fixed when PostgreSQL is compiled. You can check the setting in your server:
SHOW block_size;A table's main data fork is numbered from block 0. Its relation file can grow into multiple segment files, commonly limited to 1 GB per segment. Segmentation does not change the page numbering. PostgreSQL continues counting blocks across the segments.
A relation can also have auxiliary forks. The free space map records pages with reusable space, and the visibility map records pages whose tuples are known to be visible to all transactions. These forks support maintenance and query planning; they are separate from the main heap data fork.
Measuring pages with SQL
Create a small table and inspect its relation path and size:
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') AS relation_path;
SELECT
pg_relation_size('accounts') AS bytes,
pg_relation_size('accounts')
/ current_setting('block_size')::int AS pages;pg_relation_filepath returns a path relative to PostgreSQL's data directory. The path identifies the relation's main data file. pg_relation_size reports the size of that fork in bytes. Dividing by block_size gives the number of allocated pages.
The result is usually larger than the number of rows suggests. Each page contains metadata, line pointers, unused space, and tuple data. A table with only two short rows still needs at least one complete allocated page.
What an 8 KB page contains
A heap page has four important regions:
- A page header at the beginning. It stores page-level metadata such as boundaries and pruning information.
- Line pointers, also called item identifiers. Each pointer describes where one tuple is located on the page.
- Free space between the pointers and tuples.
- Tuple data at the end of the page. Tuples are placed from the end toward the beginning.
As rows are inserted, line pointers grow upward and tuple data grows downward. The two regions meet when the page has no space for another tuple and its pointer.
This design lets PostgreSQL move a tuple's bytes while keeping an indirection entry for the tuple's location. The line pointer also supports the ctid system column, which identifies a tuple by page number and line-pointer position.
Practical limits
A single ordinary heap tuple must fit within a page after accounting for page metadata and a line pointer. Very large column values therefore use TOAST, which compresses them or stores them in a separate relation. TOAST lets the main tuple keep a reference to the large value instead of requiring the complete value to fit in the heap page.
Page size also affects how many rows fit in a table. Smaller rows pack more densely, while tuple headers, alignment padding, NULL bitmaps, and line pointers consume space even when column values are short.
Exercise
If pg_relation_size('accounts') returns 16384 and block_size is 8192, how many pages does the table occupy?
Show answer
It occupies 2 pages, because 16384 / 8192 = 2.