Skip to content

Chapter 3 of 7

Specialized Access Methods

Hash, GIN, GiST, SP-GiST, and BRIN each address workloads that B-tree handles poorly. A Hash index supports only equality (=) comparisons using a hash of the key. Since PostgreSQL 10, Hash indexes are WAL-logged and therefore crash-safe, and they are sometimes chosen over B-trees for very large tables where equality lookups occur on wide keys (long strings or UUIDs) because they can be slightly smaller. They remain useless for range queries or ordering.

GIN stands for Generalized Inverted Index and is designed for values that contain many sub-elements: arrays, JSONB documents, full-text tsvectors, and trigrams. The operator class dictates what is indexed; for JSONB, jsonb_ops indexes every key and value and supports the existence operators (?, ?|, ?&) as well as containment (@>), while jsonb_path_ops indexes only the paths to values, supports only @>, and is smaller and faster for containment-only queries. GIN maintains a pending list of recent updates for speed; VACUUM flushes this list, and the fastupdate option (on by default) controls the trade-off.

GiST, the Generalized Search Tree, is a balanced structure that hosts many search strategies including range overlap (&&), nearest-neighbor searches (<->), geometry and full-text types, and exclusion constraints. SP-GiST, the Space-Partitioned variant, handles non-balanced data such as quadtrees, kd-trees, and radix trees, useful for phone numbers, IP addresses via the inet type, and geometric point data. BRIN, Block Range INdex, takes a fundamentally different approach: it stores a summary (typically min/max, optionally other aggregates) for each range of heap pages, making it tiny in size. BRIN shines on very large append-only tables where data is naturally correlated with physical order, such as monotonically increasing timestamps in time-series logs, but performs poorly on tables with random inserts and updates because the correlations it relies on break down. The pages_per_range parameter tunes the granularity: smaller values produce more accurate and larger indexes, larger values produce more compact but coarser ones.

All chapters
  1. 1Index Fundamentals
  2. 2The B-tree Family
  3. 3Specialized Access Methods
  4. 4Extensions for Special Workloads
  5. 5Index Maintenance and Lifecycle
  6. 6The Planner, Statistics, and EXPLAIN
  7. 7Indexes Beyond SELECT Queries

Drill it

Reading is not remembering. These come from the Postgresql Indexing Strategies deck:

Q

What is an index in PostgreSQL?

A secondary data structure that lets the planner find rows matching a predicate without scanning the whole table, at the cost of extra storage and write overhea...

Q

What is a B-tree index in PostgreSQL?

The default index type, a balanced tree that keeps keys sorted and supports equality and range predicates, plus IS NULL, IS NOT NULL, and ORDER BY traversal in...

Q

Which index access method is the default in PostgreSQL?

btree.

Q

What index types does PostgreSQL ship with for general use?

B-tree, Hash, GIN, GiST, SP-GiST, BRIN, and the extension-provided Bloom (and others like zombodb, rum, pg_trgm).