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.

Kindindex
Example LanguageSQL

📝 Syntax

CREATE [UNIQUE] INDEX <index_name> ON <table_name> USING btree ( <column> [ASC | DESC], ... )

⚙️ Parameters

index_nameName of the new index; it must be unique within the table. If omitted, PostgreSQL derives one from the table and column names.
table_nameThe table to index.
column_nameThe 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏