Sub topic 1
How a B-tree Index Is Searched
Prerequisites
This chapter assumes you know basic SQL, including SELECT, WHERE, ORDER BY, and CREATE TABLE. You should also know that an index is a separate data structure that helps PostgreSQL find table rows without scanning every row.
A B-tree is PostgreSQL's general-purpose index type. The name means balanced tree. The index keeps keys in sorted order and stores enough information to navigate from the top of the tree to the matching entries.
The search path
A B-tree has three useful levels:
- The root page is the starting point.
- Internal pages contain separator keys that guide the search.
- Leaf pages contain indexed keys and references to table rows.
The tree remains balanced as rows are inserted and deleted. Every search reaches a leaf after approximately the same number of levels. PostgreSQL can therefore search a large index by reading a small number of pages instead of checking every indexed value.
Suppose an index stores order IDs in this order:
| Index key | Table row reference |
|---|---|
| 101 | Row for order 101 |
| 102 | Row for order 102 |
| 103 | Row for order 103 |
To find key 102, PostgreSQL compares the search value with separator keys at the root, follows the appropriate branch, and then searches the leaf page. The leaf entry points to the table row, usually through a tuple identifier called a TID.
The tree narrows the search range at each level. This is why lookup time grows slowly even when the table contains many rows.
Which conditions can use a B-tree?
B-tree indexes work best with comparisons that have an ordered meaning:
- Equality:
column = value - Ranges:
column > value,column >= value,column < value, andcolumn <= value - Inclusive ranges:
column BETWEEN lower_value AND upper_value - Prefix searches such as
column LIKE 'ada%', depending on collation and operator-class details - Some ordering requirements, such as
ORDER BY column
For a range condition, PostgreSQL finds the first matching leaf entry and then reads nearby entries until the range ends. This is efficient because matching values are adjacent in sorted index order.
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer_id bigint NOT NULL,
status text NOT NULL,
total_cents integer NOT NULL
);
CREATE INDEX orders_total_idx ON orders (total_cents);
EXPLAIN
SELECT id, total_cents
FROM orders
WHERE total_cents >= 10000
AND total_cents < 50000;The plan may contain an Index Scan or Bitmap Index Scan. An index scan follows index entries and fetches table rows individually. A bitmap scan first collects matching row locations, then visits table pages in a more efficient order when many rows match.
A B-tree does not automatically make every condition fast. A condition that matches most of the table may cost less as a sequential scan. PostgreSQL's planner estimates both costs and chooses the cheaper plan.
Reading the basic plan
Look for these parts in EXPLAIN output:
| Plan detail | Meaning |
|---|---|
| `Index Scan` | The index locates entries and the table supplies row data |
| `Index Cond` | A condition used to navigate the index |
| `Filter` | A condition checked after a row is found |
| `Seq Scan` | PostgreSQL reads the table pages directly |
A condition shown as Index Cond is doing the main search work. A condition shown only as Filter did not narrow the index search.
Exercise
Exercise
What is the difference between an Index Cond and a Filter in an EXPLAIN plan?
Show answer
Index Cond narrows the search through the index. Filter is checked after PostgreSQL has found candidate rows.