What Is a Hash Index? Benefits and Limits of Equality-Only Search
A hash index computes a hash value from a column value and uses it to find a bucket of candidates for an `=` lookup. PostgreSQL’s hash index stores only a 32-bit hash code, so it can be smaller than a B-tree for long values, but that does not guarantee better speed or suitability. It cannot serve range searches, ordering, or uniqueness checks, and bucket overflow and growth behavior also matter.
How does a hash index find a value?
A hash function maps an input value to a fixed-size code, and the index uses that code to locate a bucket page. A lookup checks candidates with the same hash code. Different original values can produce the same code, so PostgreSQL hash-index scans are lossy: even after the index narrows the candidates, the database rechecks the actual table value to determine whether it matches.
Hash is not PostgreSQL’s default index type. You must request it with USING HASH.
CREATE INDEX sessions_token_hash_idx
ON sessions USING HASH (token);
SELECT session_id, expires_at
FROM sessions
WHERE token = 'example-token';This example illustrates an equality lookup. The SQL was not run, and its efficiency was not checked against a real schema, data type, or workload. Based on statistics and cost estimates, the planner may choose a B-tree or sequential scan instead.
Which conditions does it support—and which does it not?
PostgreSQL hash indexes support only the = operator. They can be candidates for exact lookups such as WHERE token = ..., but not for ranges such as >, <, or BETWEEN, or for ordered output requested with ORDER BY. PostgreSQL hash indexes are also limited to one column and cannot enforce UNIQUE. Use a separate unique constraint or an index type that supports uniqueness when the data must be unique.
B-tree also supports equality, while additionally serving range searches and ordered retrieval. So “hash is always better for equality” is not a sound rule. Consider value length, selectivity, table size, memory and storage conditions, write rate, and the PostgreSQL version in use. PostgreSQL’s documentation describes hash indexes as potentially useful for equality-heavy SELECT and UPDATE workloads on larger tables; it does not guarantee a speedup or present that as universal advice.
Why do bucket overflow and growth matter?
If many rows map to one bucket or its page fills, PostgreSQL links overflow pages to that bucket. Searches may then need to inspect those pages too, increasing block accesses depending on value distribution and insert patterns. The official documentation warns that an unbalanced hash index can require more block accesses than a B-tree for some data. Do not assume that a hash index is always faster.
As rows are added, the index can grow its number of bucket pages. Adding a bucket splits an existing bucket and moves entries. This expansion happens in the foreground and can increase insert time, so hash indexes may not suit rapidly growing tables. Freed overflow pages can be reused, but PostgreSQL does not shrink the index or return those pages to the operating system; rebuilding with REINDEX may be needed to reduce its footprint. Check documentation for the PostgreSQL version you operate.
A practical review process
Confirm that the actual bottleneck is an exact equality comparison. Rule out hash if the query needs ranges, sorting, or uniqueness.
Check whether a B-tree already fits the same data type and operator. If other search patterns matter too, the narrower purpose of a hash index may not justify another index.
Inspect the plan for index use and estimated rows. Do not mistake estimates for elapsed time; investigate statistics, selectivity, and cost when the index is not chosen.
In a safe, representative environment, assess read behavior alongside write time, bucket overflow, storage, and recovery or rebuild effort. No query or benchmark was run for this article.
Before production use, review version-specific PostgreSQL documentation and deployment/recovery procedures; later, use real usage statistics to decide whether keeping the index is worthwhile.
Sources and scope
[PostgreSQL 18: Index Types — Hash](https://www.postgresql.org/docs/18/indexes-types.html)
[PostgreSQL 18: Hash Indexes — overview and implementation](https://www.postgresql.org/docs/18/hash-index.html)
[PostgreSQL 18: Introduction to Indexes — planner choice and maintenance cost](https://www.postgresql.org/docs/18/indexes-intro.html)
The PostgreSQL 18 documentation was checked on 2026-11-22. The 32-bit hash code, single-column and equality-only limits, overflow behavior, and bucket growth describe PostgreSQL’s implementation. Other databases may behave differently. The SQL was not executed and no performance figures are claimed.