What Is a B-tree Index? Using Equality, Range, and Ordering Efficiently
A B-tree index stores values in an ordered search structure so a database can find rows by equality or range conditions. It is PostgreSQL’s default index type and can also supply rows in an order that matches `ORDER BY`. An index is not automatically used by every query. Choose it for actual filtering and ordering patterns, then review the execution plan and the cost of maintaining it.
Which searches fit a B-tree?
A B-tree is a balanced tree that arranges comparable values in order. The practical idea is that the database can follow that order to narrow the candidate set instead of examining the whole table. PostgreSQL can create B-tree indexes for data types with a defined sort order. It considers them for <, <=, =, >=, and > comparisons. Conditions such as BETWEEN, IN, and IS NULL may also use one, depending on the query and data.
For example, consider finding one order by ID or selecting orders in a date range:
CREATE INDEX orders_order_id_idx ON orders (order_id);
CREATE INDEX orders_created_at_idx ON orders (created_at);
SELECT order_id, created_at
FROM orders
WHERE order_id = 9001;
SELECT order_id, created_at
FROM orders
WHERE created_at >= TIMESTAMP '2026-01-01 00:00:00'
AND created_at < TIMESTAMP '2026-02-01 00:00:00';The first query illustrates equality; the second, a range. The planner decides whether to use an index based on table size, value distribution, statistics, and estimated cost. If a condition matches most rows, reading the table sequentially may be cheaper than visiting many index entries and table pages. The SQL is illustrative and was not executed.
Can a B-tree help with sorting and LIMIT?
In PostgreSQL, B-tree can return entries in sorted order. If that order matches a query’s ORDER BY, the plan may avoid a separate sort. With ORDER BY and LIMIT, such as the 20 newest orders, a matching index may let the database read the first rows without processing the entire remainder. This is a possible plan, not a guarantee; the planner still compares estimated costs.
CREATE INDEX orders_customer_recent_idx
ON orders (customer_id, created_at DESC);
SELECT order_id, created_at, status
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC
LIMIT 20;This composite index targets a pattern that first filters by customer and then reads that customer’s orders by descending date. A multicolumn B-tree is especially effective when leading columns have equality conditions, followed by a range condition on the first column without equality. PostgreSQL 18 also has a skip-scan optimization that may help when a leading column is absent from the predicate and has few distinct values. Its use depends on data and planner estimates; it does not make column order irrelevant.
How should you order columns in a composite B-tree?
Write down the filters, sort order, and expected number of returned rows for representative queries. An index on (customer_id, created_at) may fit a query that finds a customer’s orders and sorts them by date. A query that filters only on created_at may not get the same benefit because the leading column is unconstrained. PostgreSQL may consider skip scan in some cases, but inspect the actual plan rather than treating it as a general replacement for matching the leading column.
Multicolumn indexes take storage and require maintenance on writes. In some workloads, separate single-column indexes or another combination may be better. Add only columns needed by important query patterns, then use EXPLAIN to inspect estimated rows and the selected plan. Estimated cost is not elapsed time, so use a safe, representative way to observe real workload behavior as well.
When should you consider another index type?
B-tree is broadly useful for ordinary comparisons, ranges, and ordering, but it does not fit every operation. PostgreSQL GIN can suit searches for a component in values such as arrays. GiST operator classes support other operations, including some spatial and nearest-neighbor searches. Hash indexes are limited to equality. Start from the operators and data type your query needs, then choose a supported index type. This article explains B-tree rather than comparing every type in depth.
What should you check before and after adding one?
Identify an important real query, including its predicates, ordering, and `LIMIT`.
Decide whether a single-column or composite key fits; for a composite index, check whether leading columns match the filters.
In PostgreSQL, inspect `EXPLAIN` for changes in sequential scans, index scans, and sort steps. Plan figures are estimates, not measurements.
Include write latency, storage, and operational effects of building an index on a large table. Check version-specific documentation for locking and concurrent-build conditions.
Review usage and representative query behavior over time, and consider removing an index that no longer provides value.
Sources and scope
[PostgreSQL 18: Index Types — B-tree](https://www.postgresql.org/docs/18/indexes-types.html)
[PostgreSQL 18: B-Tree Indexes and ordering semantics](https://www.postgresql.org/docs/18/btree.html)
[PostgreSQL 18: Indexes and ORDER BY](https://www.postgresql.org/docs/18/indexes-ordering.html)
[PostgreSQL 18: Multicolumn Indexes and skip scan](https://www.postgresql.org/docs/18/indexes-multicolumn.html)
PostgreSQL 18 documentation was checked on 2026-11-22. The default index type, supported operators, ordering behavior, and multicolumn guidance are PostgreSQL-specific; other systems may differ. The SQL was not executed and no performance measurements are claimed.