Hands-on Lab·PostgreSQL
PostgreSQL Index Types and Index Selection
A partial index is a fifth the size and answers its query in 0.285 ms. A GIN index on the same table produces no improvement at all - 15.9 ms against 16.5 ms - because the predicate matches a third of the rows.
Performance Guide 34 of 47 Advanced
- OSUbuntu 26.04 LTS
- PostgreSQL18.6-0ubuntu0.26.04.1
- Test data400,000 rows, 2 core / 4 GB host
- TimeAbout 17 min
GIN is the index type for containment queries on jsonb and arrays. It cannot help extraction predicates like payload->>'tier' = 'gold', which need a btree on the expression instead.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| db-a01 | 192.168.0.81 | Ubuntu 26.04 LTS | Primary / Source / Replica Set Member 1 | 2 Core | 4 GB | 50 GB |
This guide includes
Use this when choosing an index type rather than reaching for a btree by reflex. This matters because a partial index can be a fifth the size and still answer its query faster - and a GIN index can turn out not to help at all.
- indexing only the rows you actually query
- measuring what the partial index does, including the buffers it touches
- adding a GIN index, and finding that it does not help
Before you start
- The
eventstable with its jsonb payload column.
-
Index only the rows you query
-
Measure what the partial index does
-
Add a GIN index, and find it does not help