1.Hash Indexes
A Hash index stores a hash code of the indexed value and supports ONLY equality (=) comparisons — no ranges, no ORDER BY, no BETWEEN. Because a B-tree already handles equality just as well while also supporting range queries and sorting, Hash indexes are rarely the right choice in practice; B-tree is preferred even for pure-equality workloads unless a specific, measured reason says otherwise.
Key features
- Hash indexes support only the = operator — no <, <=, >=, >, no BETWEEN, no ORDER BY support
- B-tree already supports equality efficiently, in addition to range/sort — Hash offers no capability B-tree lacks, only a narrower one
- Hash indexes cannot be declared UNIQUE — only B-tree indexes can
- Since PostgreSQL 10, Hash indexes are WAL-logged (crash-safe) and usable in replication, which was not always true historically — worth knowing if reasoning from older material
- The practical guidance is straightforward: default to B-tree even for pure-equality workloads; reach for Hash only with a specific, measured reason (rare in practice)
Hash vs B-tree, for an equality-only workload
Command
CREATE INDEX idx_hash_status ON orders USING hash (status);
SELECT * FROM orders WHERE status = 'pending'; -- usable
SELECT * FROM orders WHERE status > 'pending'; -- NOT usable -- Hash has no range supportA Hash index supports only equality (=) — no ranges, no ORDER BY, and it cannot be UNIQUE. B-tree already handles equality just as well while also supporting everything Hash cannot, which is why B-tree remains the default even for equality-only workloads — reach for Hash only with a specific, measured reason.