PostgreSQL Index Types: B-Tree, GIN, GiST, BRIN

Which PostgreSQL index fits which query: B-tree, GIN for JSONB and arrays, GiST, BRIN, partial and INCLUDE, plus index-only scans.

In this chapter

PostgreSQL index types: B-tree, GIN, GiST, BRIN, partial and INCLUDE

More on PostgreSQL

Keep learning

Overview

= < <= > >= BETWEEN IN, IS NULL, LIKE 'abc%' (C collation) Default. Scalars, ranges, ORDER BY, UNIQUE, primary keys. Equality only. Rarely better than a B-tree. Arrays, jsonb, full-text tsvector, trigrams (pg_trgm). Ranges, geometry (PostGIS), nearest neighbour, exclusion constraints. Non-overlapping partitions: points, text prefixes, inet. = < > (per block range) Huge append-only tables whose column follows insert order. B-tree on col. B-tree (a, b): equality columns first, then the range or sort column. Small hot subset (pending, unread, not deleted) Partial index WHERE on that subset. Index-only scan that needs one more column B-tree (key) INCLUDE (extra). Keep the table vacuumed. Array, jsonb or text search Overlapping ranges, nearest neighbour, no-overlap rule GiST (plus btree_gist for scalar columns in an EXCLUDE). Billions of time-ordered rows BRIN on the time column, after checking pg_stats.correlation. Adding an index to a live table A B-tree index on id holds sorted keys, each with a ctid pointing to a page and slot in the heap. Heap pages store rows in insert order, so neighbouring keys point to different pages. B-tree on id (sorted) Heap (insert order, not sorted) Keys 3, 7, 8 are neighbours in the index but live on pages 1, 0 and 2. A range read jumps around the heap. Every PostgreSQL index, the primary key included, stores a ctid (page, slot) and points into the heap. 1. index entries (all columns needed are here) 2. visibility map bit per heap page 1 skip 0 check 3. heap pages actually read Heap Fetches: 2 VACUUM sets the all-visible bits. A table updated faster than it is vacuumed turns index-only scans back into index scans. chapters · postgresql PostgreSQL index types: B-tree, GIN, GiST, BRIN, partial and INCLUDE PostgreSQL ships six index types and three ways to shape them. Picking one is a question of which operator the query uses.

1. The heap is not sorted, and every index points into it

PostgreSQL stores rows in a heap: insert order, then wherever free space appears. The primary key is an ordinary unique B-tree beside it. Every index entry carries a ctid, the (page, slot) address of a row version. CLUSTER orders USING orders_created_idx rewrites the heap in index order once, under an exclusive lock. New rows are not kept in order afterwards. How an index lookup proceeds in general is in index structure and index lookup.

2. Pick the index type from the operator

CREATE INDEX without USING builds a B-tree. Since PostgreSQL 13 B-tree leaves store a repeated key once with a list of ctids (deduplication), so low-cardinality columns cost far less than they used to. Six access methods Operators it answers Typical use

5. Index-only scans, column order and building without downtime

In a multicolumn B-tree, equality columns go first and the range or sort column last, exactly as in range scans. PostgreSQL can also skip-scan a leading column in version 18, not in 16 or 17. A plain CREATE INDEX blocks writes for the whole build. CONCURRENTLY scans the table twice and waits for old transactions, but inserts and updates keep running. It cannot run inside a transaction block.

Recap

BRIN is thousands of times smaller than a B-tree, and useless when the column does not follow physical order. Index-only scans depend on the visibility map, which VACUUM maintains. Build on live tables with CREATE INDEX CONCURRENTLY and check for INVALID leftovers.

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.