Sub topic 4
Line Pointers and `ctid`: How a Row Is Addressed
This chapter assumes basic SQL, familiarity with psql, and a general idea of transactions. You should also know that PostgreSQL stores table data in fixed-size pages, usually 8 KB each.
A table row has a logical identity, such as an id column. PostgreSQL also gives each stored row version a physical address called ctid. The ctid system column is a pair:
(block number, line pointer number)For example, (0, 3) means “use line pointer 3 in page 0.” The line pointer then identifies the tuple, which is PostgreSQL's internal name for a stored row version.
flowchart TD
A["ctid: (block, offset)"] --> B["Heap page 0"]
B --> C["Line pointer 3"]
C --> D["Tuple header and column data"]caption: How a ctid reaches a tuple alt: A ctid identifies a heap page and a line pointer, which leads to a tuple containing its header and column data.
A page uses line pointers as an index
A heap page contains a page header, an array of line pointers, free space, and tuples. Line pointers grow from the beginning of the page. Tuples are stored from the other end and grow toward the line pointers.
A line pointer is small metadata that records where a tuple begins, how long it is, and what state it has. It prevents other structures from needing to store a tuple's exact byte position. If PostgreSQL moves a tuple within the page, it can update the line pointer while preserving the line pointer number.
| Part of a `ctid` | Meaning | Example |
|---|---|---|
| Block number | Heap page number within the relation | `0` |
| Offset number | Line pointer number on that page | `3` |
| Complete address | Page and line pointer together | `(0, 3)` |
The offset number is not a byte offset. It is an ordinal number in the page's line-pointer array.
Reading `ctid`
You can select ctid like an ordinary column:
DROP TABLE IF EXISTS accounts;
CREATE TABLE accounts (
id integer PRIMARY KEY,
owner text,
balance numeric
);
INSERT INTO accounts VALUES
(1, 'alice', 100),
(2, 'bob', 200);
SELECT ctid, id, owner, balance
FROM accounts
ORDER BY id;The exact ctid values depend on the table's physical history. A newly created table might show values such as (0,1) and (0,2). The first number is the page, and the second number identifies a line pointer on that page.
You can use a known ctid to find a row:
SELECT ctid, id, owner, balance
FROM accounts
WHERE ctid = '(0,1)';This is useful for storage inspection and tightly controlled diagnostic work. It is not a replacement for a primary key. A ctid can change whenever PostgreSQL creates a new row version.
Why `ctid` changes after an update
PostgreSQL uses multiversion concurrency control, or MVCC. Instead of overwriting a row immediately, an UPDATE normally creates a new tuple version and marks the old version as no longer current.
UPDATE accounts
SET balance = balance + 50
WHERE id = 1
RETURNING ctid, id, balance;The returned ctid can differ from the value selected before the update. The old tuple may remain on the page until it is no longer needed by any transaction. Its tuple header records transaction information, and its t_ctid field can point to the newer version.
A DELETE also leaves a version behind temporarily. VACUUM later removes or makes dead line pointers reusable. Therefore, a physical address is temporary storage information, not durable row identity.
When `ctid` is useful
ctid helps when you need to:
- inspect how rows are distributed across pages;
- connect SQL-visible rows to
pageinspectoutput; - diagnose duplicate or unexpected physical versions;
- perform a carefully scoped one-time operation on a known tuple.
Indexes also use a tuple identifier internally. It contains the same kind of physical page-and-line-pointer address. PostgreSQL can follow that address to reach the heap tuple, then check whether the tuple is visible to the current transaction.
Exercise
In ctid = (4, 7), what does the 7 identify, and why should this value not be used as a permanent row ID?
Show answer
The 7 identifies line pointer 7 on heap page 4. The address can change when PostgreSQL creates a new row version or physically rewrites the table.