Sub topic 1
Where the Data Lives: Heap Tuples and B-tree Entries
Prerequisites
Assume you know basic SQL, including SELECT, WHERE, CREATE TABLE, and CREATE INDEX. You should also know that a B-tree index keeps values in sorted order so PostgreSQL can find matching rows without scanning the whole table.
PostgreSQL stores table data and index data separately. The table itself is called the heap. A heap is a collection of fixed-size pages, usually 8 KiB each. Each page contains table rows, which PostgreSQL calls tuples.
A tuple contains the column values plus internal information. This information supports MVCC, PostgreSQL's method for letting transactions see different versions of rows safely. A tuple also has a physical location called a tuple identifier, or TID. A TID identifies a page and a slot on that page, such as (12, 5).
A B-tree index does not normally contain a complete copy of every table row. Its leaf entries contain indexed key values and a TID pointing to the corresponding heap tuple.
For example, create a table and an index:
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer_id integer NOT NULL,
total numeric(10, 2) NOT NULL,
status text NOT NULL
);
INSERT INTO orders (customer_id, total, status)
VALUES
(42, 125.00, 'paid'),
(17, 80.50, 'new'),
(42, 210.75, 'shipped');
CREATE INDEX orders_customer_idx ON orders (customer_id);The orders_customer_idx index stores the customer_id values and references to the matching heap tuples. It can quickly find the two entries for customer 42. PostgreSQL then follows their TIDs to the heap to retrieve total and status.
B-tree pages and heap pages
Both the heap and the index are divided into pages. PostgreSQL reads and writes pages as units, so a query's cost depends partly on how many pages it must access.
A B-tree has several kinds of pages:
- The root page is the starting point for a search.
- Internal pages direct the search toward the correct range of keys.
- Leaf pages contain the indexed keys and heap TIDs.
- Leaf pages are linked so PostgreSQL can scan a range of adjacent keys.
The heap has no requirement that rows appear in index order. Rows inserted at different times can occupy different pages. An index search therefore usually has two stages:
- Walk the B-tree to find matching leaf entries.
- Visit the heap pages identified by the leaf entries.
This distinction explains the difference between finding a row and retrieving a row. An index can identify matching TIDs efficiently, but the heap holds the remaining column values and transaction visibility information.
Updates make this relationship more complex. Under MVCC, PostgreSQL may create a new tuple version instead of overwriting the old version immediately. The index may need to point to the new version, while old versions remain until cleanup. As a result, an index lookup still requires visibility checks before PostgreSQL can return a tuple to the transaction.
These heap visits are central to the later distinction between an ordinary Index Scan and an Index Only Scan. An ordinary Index Scan uses the index to find rows and then reads the heap. An Index Only Scan can sometimes return the needed values from the index without reading the heap, but it still needs a way to confirm that the index entry is visible to the current transaction.
Choosing what an index stores
The indexed columns determine how PostgreSQL searches and orders entries. For orders_customer_idx, customer_id is the search key. Columns such as total and status are not present, so the index cannot supply their values by itself.
This leads to a design question: should an index contain extra payload columns for frequently used queries? PostgreSQL's INCLUDE clause provides that option. The next chapters explain how included columns support covering indexes, while the heap, MVCC, and visibility map still determine whether a heap visit can be skipped.
Exercise
For a query that filters on customerid and selects total, what does an index on customerid alone provide, and what additional work is usually required?
Show answer
It provides matching customer_id keys and TIDs. PostgreSQL usually follows those TIDs into the heap to retrieve total and check tuple visibility.