Sub topic 3
Inside a Heap Page: Header, Line Pointers, Free Space, and Tuples
PostgreSQL stores ordinary table rows in a heap. A heap page is a fixed-size block of table storage. This chapter assumes you know basic SQL, can use psql, and understand that a transaction groups database changes.
The 8 KB page
By default, PostgreSQL pages are 8 KB, or 8,192 bytes. The database reads and writes pages as units. A table larger than one page uses many pages, numbered from block 0.
A heap page has four important regions:
| Region | Purpose |
|---|---|
| Page header | Records page metadata and free-space boundaries |
| Line pointer array | Maps item numbers to tuple locations |
| Free space | Holds unused space between pointers and tuples |
| Tuples | Stores row versions near the end of the page |
The page header is at the beginning. The line pointer array grows forward from the header. Tuples are placed from the opposite end of the page toward the pointers. Free space remains between them.
The header contains values such as the page's log sequence number, the lower boundary of the line pointer array, and the upper boundary of free space. A special region at the end can hold access-method-specific data. Heap pages normally have little or no special space.
pd_lower marks where the line pointer array ends. pd_upper marks where tuple data begins. The bytes between these offsets are currently available for new pointers or tuple data.
Line pointers and tuple locations
A line pointer is a small entry that describes one tuple's location and length. It does not contain the row itself. It contains an offset into the page, a length, and a status flag.
The pair of a page number and a line pointer number identifies a tuple location. PostgreSQL exposes this address through the system column ctid. For example, (3,7) means block 3, item 7.
CREATE TABLE accounts (
id int PRIMARY KEY,
owner text,
balance numeric
);
INSERT INTO accounts VALUES
(1, 'alice', 100),
(2, 'bob', 200);
SHOW block_size;
SELECT ctid, id, owner, balance
FROM accounts;A ctid is a physical address, not a permanent row identifier. An update can create a new row version with a different ctid. Operations such as table rewrites can also change all physical addresses. Use a primary key to identify a logical row.
Line pointer numbers usually remain meaningful while a page is being maintained. If a tuple is removed, PostgreSQL can mark its line pointer unused or redirect it during page cleanup. Therefore, an item number is not always a live row.
Inspecting page boundaries
The pageinspect extension exposes page internals for diagnostic work. The following example reads block 0 of the table and shows its header boundaries:
CREATE EXTENSION IF NOT EXISTS pageinspect;
SELECT lower, upper, special, pagesize
FROM page_header(get_raw_page('accounts', 0));
SELECT lp, lp_off, lp_flags, lp_len
FROM heap_page_items(get_raw_page('accounts', 0));lower and upper correspond closely to the boundaries described above. pagesize is normally 8,192. Each row from heap_page_items describes one line-pointer slot. The lp_flags value indicates whether the slot is unused, normal, redirected, or otherwise marked.
You need sufficient privileges to create the extension and read the relation. Page 0 may not contain every row if the table spans multiple pages. Use the table's size and block count to decide which blocks to inspect.
A row's tuple includes more than its visible column values. The tuple has a header containing transaction metadata and flags, followed by null-bitmap and alignment information when needed, then the column data. The next chapter examines that tuple header in detail.
Exercise
In a heap page, what do the lower and upper boundaries describe, and why is free space located between them?
Show answer
lower marks the end of the page header and line-pointer array. upper marks where tuple data begins. Free space lies between them because line pointers grow upward from the start while tuples grow downward from the end.