Sub topic 3
Inside a heap page: header, line pointers, free space, tuples
The previous chapter introduced the 8 KB page as the unit of storage and I/O. This chapter opens one page of an ordinary table, called a heap page, and shows how its bytes are arranged. It assumes you can run SQL in psql and know that a tuple is the stored form of a row. It also assumes you can use the pageinspect extension, which needs superuser rights or an explicit grant. Line pointers and ctid get a closer look in the next chapter. Here they appear only as part of the layout.
The four regions of a page
Every heap page has the same structure, whatever the table holds. From the first byte to the last, it has these regions:
- A fixed 24-byte page header.
- An array of line pointers, which grows toward the end of the page.
- Free space.
- Tuples, which are packed from the end of the page and grow toward the start.
The rest of this section describes the fifth item you may see in page_header output: the special space. Heap pages don't use it, so it is empty. Index pages use it for sibling links.
The two-ended design solves a problem. Tuples vary in length, and the page needs a stable way to find each one. Tuples fill from the back and pointers fill from the front, so the unused space stays in one contiguous block. The page is full when the two ends meet.
The page header
The header is a C struct called PageHeaderData. These are its fields:
| Field | Size | Meaning |
|---|---|---|
| `pd_lsn` | 8 bytes | WAL position of the last change to the page. It enforces write-ahead logging: the page cannot be flushed before WAL up to this point is flushed. |
| `pd_checksum` | 2 bytes | Page checksum, filled in only if data checksums are enabled. |
| `pd_flags` | 2 bytes | Hint bits such as "has free line pointers" and "all tuples visible to everyone". |
| `pd_lower` | 2 bytes | Offset of the end of the line pointer array, which is the start of free space. |
| `pd_upper` | 2 bytes | Offset of the start of the newest tuple, which is the end of free space. |
| `pd_special` | 2 bytes | Offset of the special space. |
| `pd_pagesize_version` | 2 bytes | Page size and layout version number. |
| `pd_prune_xid` | 4 bytes | Oldest transaction that left something prunable on the page, or 0. |
The two fields that matter most for this chapter are pd_lower and pd_upper. The free space on a page is pd_upper - pd_lower. Both are plain byte offsets from the start of the page, so a 2-byte field is enough for an 8192-byte page.
Line pointers
A line pointer (also called an item identifier) is a 4-byte entry that packs three values into 32 bits:
lp_off(15 bits): the byte offset of the tuple within the page.lp_flags(2 bits): the state of the entry. The values are unused (0), normal (1), redirect (2), and dead (3).lp_len(15 bits): the length of the tuple in bytes.
Line pointers are numbered from 1, in the order they were allocated. The number of a pointer never changes while the pointer is in use. This matters because indexes and ctid values refer to a tuple by page number and line pointer number, not by byte offset. The page can therefore move a tuple around inside itself, and only lp_off needs updating.
A line pointer slot is not removed when its tuple dies. Pruning and VACUUM mark it dead or unused, and a later insert can reuse an unused slot. The array shrinks only when unused slots at its tail are trimmed during vacuuming, which PostgreSQL 14 and later can do.
Tuples
Each tuple is stored as a header followed by the column data. The header is 23 bytes, and the data starts at the next multiple of 8, so a tuple with a short row still takes at least 24 bytes before any column data. The whole tuple is then padded so that the next one starts on an 8-byte boundary. The next chapters cover the header fields and the padding rules. For this chapter, the point is that every tuple occupies a multiple of 8 bytes. A page-level cost therefore applies to every row:
space per row = MAXALIGN(24 + column data) + 4 bytes for the line pointerThe upper limit on tuples per page is 291 for an 8 KB page. It follows from the minimum tuple size and the line pointer size, and the formula in the PostgreSQL source is (BLCKSZ - header) / (MAXALIGN(tuple header) + line pointer). Tables with only tiny rows hit this cap before they run out of bytes.
Watching the header move
The following session creates a small table and reads its header. Because the table is fresh, page 0 holds only these rows.
CREATE EXTENSION IF NOT EXISTS pageinspect;
CREATE TABLE page_demo (id int, note text);
INSERT INTO page_demo VALUES (1, 'one'), (2, 'two');
SELECT lower, upper, special, pagesize, upper - lower AS free_bytes
FROM page_header(get_raw_page('page_demo', 0));You should see lower = 32, which is the 24-byte header plus two 4-byte line pointers. You should also see special = 8192 and pagesize = 8192, because the heap uses no special space. The value of upper is 8192 minus the combined size of the two padded tuples.
Now add three more rows and read the header again:
INSERT INTO page_demo VALUES (3, 'three'), (4, 'four'), (5, 'five');
SELECT lower, upper, upper - lower AS free_bytes
FROM page_header(get_raw_page('page_demo', 0));Now lower is 44, which is 24 plus five line pointers. upper has moved down by the size of the three new tuples. The two pointers moved toward each other, and the free bytes shrank by the pointer cost plus the tuple cost for each row.
Counting how many rows fit
You can test the space formula directly. A table with one int column has a tuple of 24 header bytes plus 4 data bytes, padded to 32 bytes. Add the 4-byte line pointer and each row costs 36 bytes. The usable area is 8192 minus the 24-byte header, which is 8168 bytes, so a page should hold 226 rows (8168 / 36 = 226.9, rounded down).
CREATE TABLE one_int (a int);
INSERT INTO one_int SELECT g FROM generate_series(1, 1000) g;
SELECT (ctid::text::point)[0]::int AS block, count(*) AS rows
FROM one_int
GROUP BY block
ORDER BY block;Expect 226 rows in blocks 0, 1, and 2, and the remaining 322 rows in block 3. This assumes the default 8 KB block size, 8-byte maximum alignment, and the default fillfactor of 100 for tables. The cast from ctid to point is a common shortcut for pulling out the block number. It is a convenience for experiments, not something to put in application code.
Free space is contiguous only after cleanup
Free space between pd_lower and pd_upper is one block, but the page can also contain unusable gaps. When a tuple is deleted and its line pointer is later marked unused, the tuple's bytes sit in the middle of the tuple area as a hole. That hole is not counted in pd_upper - pd_lower. Page pruning compacts the tuples toward the end of the page. That joins the holes back into the central free space and raises the number of bytes an insert can use. A page can therefore hold a lot of reclaimable space while pd_upper - pd_lower looks small. For that reason page_header alone doesn't tell you how full a page really is. You need to look at the line pointers too.
Common mistakes
- Treating
lowerandupperas row counts. The number of line pointers is(lower - 24) / 4. It includes dead and unused slots, so it can exceed the number of live rows. - Expecting
specialto be 0. In a heap page it equals the page size, since there is no special space. B-tree pages show a smaller value. - Estimating row size from column types alone. The tuple header, padding, and line pointer add at least 28 bytes per row, which dominates for narrow tables.
- Forgetting that a row cannot span pages. A tuple must fit within one page. Larger values are compressed or moved out of line, as the TOAST chapters explain.
Exercise
A table has three int columns and default settings on an 8 KB page. How many rows fit on one page? Show the calculation.
Show answer
Each tuple needs 24 header bytes plus 12 data bytes, which is 36, padded up to 40 bytes. Adding the 4-byte line pointer gives 44 bytes per row. The usable area is 8192 - 24 = 8168 bytes. 8168 / 44 is about 185.6, so 185 rows fit.
Exercise
A page shows lower = 56 and upper = 7000. How many line pointers does the page have, and how many bytes of contiguous free space does it have?
Show answer
The line pointer count is (56 - 24) / 4 = 8. The free space is 7000 - 56 = 6944 bytes. The 8 pointers include any dead or unused slots, so the page may hold fewer than 8 live rows.