HASH Index (Equality Lookups Only)

Last updated: 2026-11-04

HASH Index (Equality Lookups Only)

A HASH index hashes keys into fixed-length buckets. It supports equality lookups (=, IN) only — no range scans, no ordering, and no unique constraints. Because entries are not sorted within a bucket, it cannot provide ordered output and cannot speed up ORDER BY. The PostgreSQL documentation explicitly recommends B-tree over HASH for virtually all workloads.

Kindindex
Example LanguageSQL

📝 Syntax

CREATE INDEX <index_name> ON <table_name> USING hash ( <column> )

⚙️ Parameters

index_nameName of the new index.
table_nameThe table to index.
column_nameThe indexed column; it must have a hash operator class available (typically text or integer types).

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 HASH index
Index Scan using idx_hash_token on idx_demo_hash  (cost=0.00..8.02 rows=1 width=4)
  Index Cond: (token = 'a1d0c6e83f027327d8461063f4ac58a6'::text)
  Index Searches: 1
  Buffers: shared hit=3
Planning Time: 0.150 ms
Execution Time: 0.176 ms

-- Second EXPLAIN: hits has no index, so the range predicate triggers a seq scan
Aggregate  (cost=487.55..487.56 rows=1 width=8)
  ->  Seq Scan on idx_demo_hash  (cost=0.00..487.00 rows=220 width=0)
        Filter: ((hits >= 10) AND (hits <= 20))
        Rows Removed by Filter: 19780
  Buffers: shared hit=187
Planning Time: 0.068 ms
Execution Time: 1.075 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

QIf B-tree handles everything, why keep HASH indexes at all?
AMostly historical, plus a niche where a hash table's memory footprint and constant-time lookup can pay off under very large volumes of equality lookups. For almost every project, use B-tree.
QDoes a HASH index slow down writes?
AYes. Every index must be maintained on INSERT / UPDATE / DELETE; a HASH index computes the hash and locates a bucket on each write. The more indexes a table has, the slower writes get.
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%

🙏 帮我们做得更好

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

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