B-tree Index (the Default Index Type)
Last updated: 2026-11-04
B-tree Index (the Default Index Type)
B-tree is PostgreSQL's default index type. It stores entries in a balanced tree, keeping keys in sorted order, so it serves equality (=), range comparisons (>, <, >=, <=, BETWEEN) and ORDER BY output efficiently. It is the right choice for most workloads. When the planner picks it up, the plan shows Index Scan or Index Only Scan; a Seq Scan means the optimizer estimated a full table scan to be cheaper.
| Kind | index |
|---|---|
| Example Language | SQL |
📝 Syntax
CREATE [UNIQUE] INDEX <index_name> ON <table_name> USING btree ( <column> [ASC | DESC], ... )
⚙️ Parameters
| index_name | Name of the new index; it must be unique within the table. If omitted, PostgreSQL derives one from the table and column names. |
|---|---|
| table_name | The table to index. |
| column_name | The indexed column(s); several columns form a composite index. ASC (default) and DESC affect whether ORDER BY can reuse the ordering stored in the index. |
Returns:No return value. On success the command tag is CREATE INDEX.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
CREATE TABLE
INSERT 0 20000
CREATE INDEX
ANALYZE
-- First EXPLAIN: the equality predicate uses the index
Index Scan using idx_btree_email on idx_demo_btree (cost=0.29..8.30 rows=1 width=11)
Index Cond: (email = 'user00042@example.com'::text)
Index Searches: 1
Buffers: shared hit=1 read=2
Planning Time: 0.137 ms
Execution Time: 0.127 ms
-- Second EXPLAIN: the range predicate uses the index as well
Index Scan using idx_btree_email on idx_demo_btree (cost=0.29..8.31 rows=1 width=11)
Index Cond: ((email >= 'user19990@example.com'::text) AND (email < 'user19995@example.com'::text))
Index Searches: 1
Buffers: shared hit=3
Planning Time: 0.129 ms
Execution Time: 0.017 ms
(The EXPLAIN plans above come from a real PostgreSQL 18.6 run; actual time / Planning Time / Execution Time are indicative values and vary with machine load.)
📌 Key Points
- USING btree may be omitted since CREATE INDEX defaults to B-tree; writing it explicitly is clearer.
- The planner needs accurate row estimates, so run ANALYZE before EXPLAIN — as every example here does.
- Indexes are not free: each one slows down INSERT / UPDATE / DELETE and consumes extra disk space.
- After indexing a column, the WHERE expression must match the indexed expression (e.g. both lower(name)), otherwise the index is not used.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QWhy does EXPLAIN still show a Seq Scan after I create an index?
AUsually one of three things: the table is too small for an index scan to pay off; you never ran ANALYZE so the planner lacks statistics; or the WHERE expression does not match the indexed expression. Tighten the predicate and run ANALYZE — an Index Scan normally appears.
QWhat is the difference between Index Scan and Index Only Scan?
AAn Index Only Scan means every column the query needs is stored in the index, so the heap never has to be visited. It relies on the visibility map marking pages as all-visible, which normally requires VACUUM (or autovacuum) to have run.