Sub topic 4
Line pointers and ctid: how a row is addressed
This chapter assumes you have read the earlier chapters on 8 KB pages and the heap page layout. It also assumes you can run SQL in psql and have the pageinspect extension available. You already know that a page holds a header, an array of line pointers, free space, and tuples. Here you will see how that array lets PostgreSQL address a row, and what the ctid column reports.
What a line pointer is
A line pointer (the source code calls it an ItemId) is a 4-byte entry in the array right after the page header. Each entry describes one tuple slot on the page. It packs three fields into 32 bits:
| Field | Bits | Meaning |
|---|---|---|
| `lp_off` | 15 | Byte offset from the start of the page to the tuple. For a redirect, it holds the target line pointer number instead. |
| `lp_flags` | 2 | State of the slot: unused, normal, redirect, or dead. |
| `lp_len` | 15 | Length of the tuple in bytes. |
The array grows forward from the header, and tuples are stored from the end of the page backward. The space between pd_lower (end of the array) and pd_upper (start of the newest tuple) is free. Because the array adds 4 bytes per slot, pd_lower equals 24 (the header size) plus 4 times the number of line pointers.
The 15-bit fields are enough because a page is only 8192 bytes. Offsets never exceed 8191 for the default block size.
The four states of a line pointer
The lp_flags value tells you what the slot currently means.
| `lp_flags` | Name | Meaning |
|---|---|---|
| 0 | `LP_UNUSED` | The slot is free. A new tuple can reuse it. |
| 1 | `LP_NORMAL` | The slot points to a tuple that has storage (`lp_off` and `lp_len` are valid). |
| 2 | `LP_REDIRECT` | The slot has no tuple. `lp_off` holds the number of another line pointer on the same page. |
| 3 | `LP_DEAD` | The tuple is dead. The slot may still have storage, or may have been emptied while the slot is kept for index entries. |
Redirects exist only for HOT chains, which a later chapter covers in full. Dead slots matter because indexes may still refer to them. Until VACUUM has removed the index entries, the line pointer must stay in place.
What ctid is
Every row version has a physical address called a tuple identifier, or TID. The ctid system column shows it as a pair (block, offset):
blockis the block number (page number) within the table's main fork, starting at 0.offsetis the line pointer number within that page, starting at 1.
So (0,3) means "the third line pointer on page 0". It does not mean "the tuple at byte offset 3". The address points at the slot, and the slot points at the bytes.
CREATE TABLE accounts (id int PRIMARY KEY, owner text, balance numeric);
INSERT INTO accounts VALUES (1, 'alice', 100), (2, 'bob', 200);
SELECT ctid, id, owner FROM accounts;On a fresh table the result is (0,1) for alice and (0,2) for bob. Both rows are on page 0, in line pointers 1 and 2.
The tid type is a built-in data type, so you can compare and filter on it. A query that names a TID exactly is answered with a TID Scan, which reads one page and goes straight to one slot:
SELECT * FROM accounts WHERE ctid = '(0,2)';
EXPLAIN SELECT * FROM accounts WHERE ctid = '(0,2)';The plan shows Tid Scan on accounts. From PostgreSQL 14, range conditions such as ctid >= '(0,1)' AND ctid < '(10,0)' use a TID Range Scan.
To split a ctid into numbers, you can cast through text and the point type. This works because the text form (0,2) is also valid point syntax:
SELECT ctid,
(ctid::text::point)[0]::int AS block,
(ctid::text::point)[1]::int AS line_pointer
FROM accounts;Why there is a layer of indirection
An index entry stores the TID of a heap tuple. It points at a line pointer, never at a byte offset. This indirection solves a real problem.
Inside a page, tuples sometimes need to move. When pruning removes dead tuples, PostgreSQL compacts the survivors toward the end of the page to merge the holes into one contiguous free area. If an index entry held a byte offset, every compaction would require updating every index. Instead, compaction rewrites only lp_off inside the page. The line pointer number stays the same, so index entries remain valid.
The cost is that line pointers are sticky. A slot cannot simply be deleted from the middle of the array, because that would renumber the slots after it and invalidate their TIDs. A freed slot is marked unused and reused later, but the array itself never shrinks while the page lives.
Watching it with pageinspect
Create the extension if you have not already, then list the line pointers of page 0:
CREATE EXTENSION IF NOT EXISTS pageinspect;
SELECT lower, upper FROM page_header(get_raw_page('accounts', 0));
SELECT lp, lp_off, lp_flags, lp_len, t_xmin, t_xmax, t_ctid
FROM heap_page_items(get_raw_page('accounts', 0));With two rows, lower is 32, which is 24 plus 2 slots of 4 bytes. Each row appears as one line with lp_flags = 1. The lp_off values decrease from one row to the next, because each new tuple is placed below the previous one. The t_ctid column of each tuple equals its own address, such as (0,1). That is how a tuple says "I am the newest version of this row".
What UPDATE does to addresses
An UPDATE writes a new tuple and leaves the old one in place. The new tuple gets a new TID, so the row's ctid changes:
UPDATE accounts SET balance = balance + 50 WHERE id = 1;
SELECT ctid, id, balance FROM accounts;
SELECT lp, lp_off, lp_flags, lp_len, t_xmin, t_xmax, t_ctid
FROM heap_page_items(get_raw_page('accounts', 0));You should see alice at (0,3) and bob still at (0,2). In the page dump there are now three line pointers, and lower has grown to 36. Line pointer 1 still has lp_flags = 1 and a t_xmax set to the updating transaction. Its t_ctid is now (0,3), a forward link to the newer version. Line pointer 3 holds the new version, and its t_ctid points to itself.
Following t_ctid from an old version gives you the update chain. Concurrent transactions rely on it. Under READ COMMITTED, a transaction that waited on a row lock follows the chain to find the latest version before it re-checks its WHERE clause.
Because balance is not indexed, this update is a HOT update, and the new tuple is on the same page. The primary key index still contains one entry for id = 1, pointing at (0,1). A later index lookup lands on line pointer 1, sees the old version is not the newest, and follows the chain to (0,3). If the update had changed an indexed column or the page had no room, the new tuple would need its own index entries.
What VACUUM does to line pointers
Run VACUUM and look at the page again:
VACUUM accounts;
SELECT lp, lp_off, lp_flags, lp_len, t_xmin, t_xmax, t_ctid
FROM heap_page_items(get_raw_page('accounts', 0));When no other transaction can still see the old version, its storage is reclaimed. Because the old tuple was the head of a HOT chain, its line pointer cannot simply be freed, since the index entry still points at slot 1. PostgreSQL turns it into a redirect: lp_flags = 2, lp_len = 0, and lp_off = 3, which is the line pointer number of the live version. Lookups through the index arrive at slot 1, are forwarded to slot 3, and find the tuple.
If a dead tuple is not part of a HOT chain, VACUUM first removes its index entries. Only then can it mark the slot LP_UNUSED so that a later INSERT reuses it. The order matters. If a slot were reused while an index still held its TID, the index would point to an unrelated row.
The exact output depends on timing and on any open transactions. A long-running transaction can keep old versions alive, and then VACUUM changes nothing for them.
Limits and consequences
- Maximum slots per page. Even if tuples were tiny, an 8 KB page holds at most 291 heap tuples (
MaxHeapTuplesPerPage). The limit comes from the 24-byte header plus the minimum tuple size. A table of very narrow rows can still use more space per row than the data alone suggests. - Per-row overhead. Each tuple costs its header plus 4 bytes for its line pointer. Both count toward the page.
- Bloated arrays. A page that once held many short-lived tuples keeps a long line pointer array. Unused slots are reused, but the array occupies space until the page is rewritten.
- Block numbers are 32 bits. Together with 8 KB pages, this caps a single table at 32 TB with the default block size.
Using ctid safely
ctid is a useful debugging and maintenance tool, but it is not an identifier you can store.
- It changes when the row is updated.
- It changes when
VACUUM FULL,CLUSTER, or a table rewrite fromALTER TABLEmoves rows. - Different rows can have the same
ctidover time, because freed slots are reused. - It gives no useful ordering. Sorting by
ctidapproximates the physical order, but that is not a guarantee.
A legitimate use is deleting exact duplicates in a table with no key. Within one statement the ctid values are stable, so you can keep one physical copy:
DELETE FROM accounts a
USING accounts b
WHERE a.id = b.id
AND a.ctid > b.ctid;Run this in a single statement, or inside a transaction that holds a lock which prevents concurrent updates. Otherwise a concurrent UPDATE may move a row between your read and your write.
Common mistakes
- Reading the offset as bytes. The second number in a TID is a line pointer number, counted from 1. Byte positions are in
lp_off. - Expecting
ctidto stay the same after an UPDATE. It changes every time a new version is written. - Assuming a dead row's slot is free immediately. Until VACUUM, the slot is still referenced, and its storage may still be needed for visibility checks.
- Counting
lpvalues as row counts.heap_page_itemslists every slot, including redirects, dead, and unused ones. It is not a count of visible rows.
Exercise
After running UPDATE on a single row of a table with a primary key, SELECT ctid FROM t WHERE id = 1 changes from (0,1) to (0,4). Explain what happened to line pointers 1 through 4 on page 0, and why the primary key index does not need to change if the update was HOT. What will line pointer 1 look like after a VACUUM that finds no old transactions?
Show answer
The table already had line pointers 1 to 3 (for example three rows), and the UPDATE wrote the new version of row 1 into a new slot, line pointer 4. Line pointer 1 still holds the old version, now with a t_xmax set and a t_ctid of (0,4). In a HOT update the indexed columns did not change, so the new version stays on the same page and the existing index entry for id = 1 still points to (0,1). A lookup goes to slot 1 and follows the chain to slot 4. After VACUUM, the old tuple's storage is reclaimed. Line pointer 1 becomes a redirect, with lp_flags = 2, lp_len = 0, and lp_off = 4, so the index entry stays valid.
Exercise
A page's page_header shows lower = 56. How many line pointers does the page have, and what is the smallest and largest lp value you would see in heap_page_items?
Show answer
lower equals 24 plus 4 times the slot count, so (56 - 24) / 4 = 8 line pointers. heap_page_items shows lp values 1 through 8, whatever their flags. Some of them may be unused, dead, or redirects, and the page may hold fewer than 8 live tuples.