Sub topic 5
Looking at Real Pages with `pageinspect`
This chapter assumes basic SQL, familiarity with psql, and a general idea of transactions, files, and bytes. You will use the pageinspect extension to inspect PostgreSQL heap pages. The extension exposes internal storage details for learning and diagnosis. It is not a replacement for normal SQL queries.
Prepare a Small Table
Create a table with a few rows:
CREATE TABLE accounts (
id integer PRIMARY KEY,
owner text,
balance numeric
);
INSERT INTO accounts VALUES
(1, 'alice', 100),
(2, 'bob', 200);
SELECT ctid, xmin, xmax, *
FROM accounts;The ctid column is a physical address for the current row version. PostgreSQL writes it as (block, offset). The block is the page number inside the table's main data fork. The offset identifies a line pointer on that page.
The address can change after an update, table rewrite, or other storage operation. Treat ctid as an inspection value, not a permanent identifier. The primary key remains the logical identifier.
Install and Read Page Metadata
Install the extension if your database role has sufficient privileges:
CREATE EXTENSION IF NOT EXISTS pageinspect;A table page is normally 8,192 bytes. You can confirm the configured page size and find the table's relation file:
SHOW block_size;
SELECT pg_relation_filepath('accounts');
SELECT pg_relation_size('accounts') AS bytes,
pg_relation_size('accounts')
/ current_setting('block_size')::integer AS pages;pg_relation_filepath returns a path relative to PostgreSQL's data directory. A large relation is split into segment files. PostgreSQL also maintains related forks, such as the free space map and visibility map. pageinspect usually examines the main heap fork unless you specify another fork.
The page header describes the boundaries of the page:
SELECT lower, upper, special, pagesize
FROM page_header(get_raw_page('accounts', 0));lower marks the end of the header and line-pointer area. upper marks the beginning of tuple data growing backward from the end of the page. The space between them is available for new line pointers and tuples. special reserves space for access-method-specific data. Heap pages normally have no special data, so this value is usually the page size.
Inspect Line Pointers and Tuples
Use heap_page_items to inspect the page's line pointers and tuple headers:
SELECT lp, lp_off, lp_flags, lp_len,
t_xmin, t_xmax, t_ctid
FROM heap_page_items(get_raw_page('accounts', 0));| Field | Meaning |
|---|---|
| `lp` | Line-pointer number, used in the second part of a `ctid` |
| `lp_off` | Byte offset where the tuple begins |
| `lp_flags` | Whether the line pointer is normal, unused, redirected, or dead |
| `lp_len` | Tuple length in bytes |
| `t_xmin` | Transaction that inserted this row version |
| `t_xmax` | Transaction that deleted or replaced this row version |
| `t_ctid` | Link to the row version's current physical location |
A line pointer is a small directory entry at the front of the page. It lets PostgreSQL find a tuple even though tuple data is stored elsewhere on the same page. When a row moves, PostgreSQL can update the pointer or leave a forwarding relationship rather than changing every reference immediately.
>callout note > get_raw_page accepts a zero-based block number. Page 0 is the first heap page, but a table with more data can have many additional pages. Do not assume that a row's logical order matches its physical order.
Observe a New Row Version
An update usually creates a new row version instead of overwriting the old bytes in place:
UPDATE accounts
SET balance = balance + 50
WHERE id = 1;
SELECT lp, lp_flags, t_xmin, t_xmax, t_ctid
FROM heap_page_items(get_raw_page('accounts', 0));You may see both the old and new versions on the page. The old version has a nonzero t_xmax after the updating transaction commits. The new version has a different transaction identity and often points to itself through t_ctid. Whether both versions appear on page 0 depends on available space and the update plan.
pageinspect shows physical facts, not a cleaned-up relational view. Transaction visibility rules still decide which version a normal SELECT can see.
Exercise
In ctid = (3, 7), what do 3 and 7 identify?
Show answer
3 identifies heap block 3, and 7 identifies line pointer 7 on that block.