I used these PostgreSQL index notes to prepare for interviews. When I ran them against a database, five rules needed rewriting.
I used PostgreSQL 18.6 and 17.10 in Docker with en_US.utf8, 2 million orders and 1 million customer emails. Each query ran warm five times. The tables report median time and 8 kB pages touched from EXPLAIN (ANALYZE, BUFFERS).
0. The plan names came from Oracle
My notes described plans as INDEX RANGE SCAN and TABLE ACCESS FULL. Those are Oracle’s operations. PostgreSQL prints these:
Seq Scan read the whole table, page by page Index Scan walk the index, fetch each matching row from the table Index Only Scan answer from the index alone (visibility map permitting) Bitmap Index Scan collect matching row locations from the index first, Bitmap Heap Scan then visit the table pages in physical order
If a set of notes uses another database’s vocabulary, some of its conclusions probably come from that database too, and several of mine did.
1. “A table can have one clustered index”: PostgreSQL has none
My notes borrowed SQL Server’s clustered-index syntax. PostgreSQL rejects it:
CREATE CLUSTERED INDEX x ON orders_c (customer_id); -- ERROR: syntax error at or near "CLUSTERED" CREATE NONCLUSTERED INDEX x ON orders_c (customer_id); -- ERROR: syntax error at or near "NONCLUSTERED"
PostgreSQL’s CLUSTER table USING index rewrites the heap once. I measured a customer-range query before clustering, right after and after more writes:
| Moment | correlation | rows | pages | time |
|---|---|---|---|---|
| before CLUSTER | 0.002 | 1,028 | 1,003 | 0.47 ms |
| right after CLUSTER (took 2.5 s) | 1.000 | 1,028 | 17 | 0.09 ms |
| after +10% inserts, 10% updates | 0.673 | 1,135 | 283 | 0.13 ms |
Right after CLUSTER the query touched 17 pages instead of 1,003. One round of inserts and updates brought it back to 283. indisclustered still said true: it only records which index to use the next time you run CLUSTER. The CLUSTER docs say the same: clustering is a one-time operation.
The rewrite holds ACCESS EXCLUSIVE. I confirmed that even a plain read could time out:
SET lock_timeout = '2s'; SELECT count(*) FROM orders_c WHERE customer_id = 4242; -- ERROR: canceling statement due to lock timeout (after 2.07 s)
I would run this as maintenance, in a window where blocking reads is acceptable.
2. “Put the most selective column first”: put the equality column first
My notes said both “most selective first” and “equality first.” I built both indexes for one shop’s orders in one month:
SELECT * FROM orders WHERE shop_id = 42 AND created_at >= '2025-06-01' AND created_at < '2025-07-01'; -- 842 rows; the month alone holds 164,243 orders
created_at is nearly unique; shop_id has 200 values. Here is what the two index orders read:
| Index | index pages | total pages | PG 18 | PG 17 |
|---|---|---|---|---|
| (created_at, shop_id) | 632 | 1,460 | 1.59 ms | 1.85 ms |
| (shop_id, created_at) | 6 | 834 | 0.22 ms | 0.29 ms |
With the date first, the scan walks the month across all shops. With the shop first, it jumps to one contiguous range.
Both plans list the same conditions:
-- (created_at, shop_id) -- (shop_id, created_at)
Bitmap Index Scan pages=632 Bitmap Index Scan pages=6
Index Cond: created_at >= ... AND Index Cond: shop_id = 42 AND
created_at < ... AND created_at >= ... AND
shop_id = 42 created_at < ...Index Cond shows what the index checked. BUFFERS shows the difference in pages read: 632 versus 6.
3. “An index is useless without its first column”: true until PostgreSQL 18
I queried one day of orders using (shop_id, created_at) without a shop filter. PostgreSQL 17 scanned the table in this test; 18 used the index:
| Setup | plan | pages | time |
|---|---|---|---|
| PG 17, (shop_id, created_at) | Parallel Seq Scan | 22,704 | 36.0 ms |
| PG 17, index forced | whole index read | 12,460 | 22.7 ms |
| PG 18, (shop_id, created_at) | Bitmap Index Scan, 202 searches | 5,447 | 1.92 ms |
| PG 18, plain index on (created_at) | Bitmap Index Scan | 4,812 | 1.70 ms |
PostgreSQL 18’s B-tree skip scan can search again for each distinct leading key. Look at Index Searches:
Bitmap Index Scan on ix_skip (actual rows=5396) Index Cond: (created_at >= '2025-06-01' AND created_at < '2025-06-02') Index Searches: 202 Buffers: shared hit=652
With 200 shops it worked well. I tried the same query with customer_id first and about 200,000 distinct values:
| Setup | plan | pages | time |
|---|---|---|---|
| PG 18, (customer_id, created_at) | Parallel Seq Scan | 22,704 | 44.8 ms |
| PG 18, same index forced | whole index read, 1 search | 12,460 | 25.1 ms |
The planner did not use skip scan, because 200,000 searches would cost more than reading everything. Skip scan helps when the leading column has few distinct values; otherwise you still need an index that starts with the column you filter. On 17 and earlier the old rule still holds in full.
4. “LIKE can use an index if % is not first”: not your default index
With en_US.utf8 and a plain B-tree on email, equality used the index; LIKE 'abc%' did not:
| Query / index | plan | pages | time |
|---|---|---|---|
| email = '...' / plain | Index Scan | 4 | 0.01 ms |
| LIKE 'abc%' / plain | Parallel Seq Scan | 7,353 | 19.1 ms |
| LIKE 'abc%' / text_pattern_ops | Index Scan | 235 | 0.07 ms |
| LIKE 'abc%' / COLLATE "C" | Index Scan | 235 | 0.08 ms |
| LIKE '%9f3a1%' / text_pattern_ops | Parallel Seq Scan | 7,353 | 28.3 ms |
| LIKE '%9f3a1%' / pg_trgm GIN | Bitmap Index Scan | 19 | 0.11 ms |
| LIKE 'abc%' / pg_trgm GIN | Bitmap Index Scan | 260 | 0.34 ms |
A linguistic collation does not guarantee that all strings with the same prefix sit together. text_pattern_ops or a suitable COLLATE "C" index gives the planner a prefix range. See the operator class docs.
In this test the pattern index could not serve the normal ORDER BY email. For leading-wildcard fragment search I used pg_trgm; that GIN index also served the prefix query.
5. “Don't index a boolean”: unless one value is rare, and then do it partially
I indexed two booleans: one true for half the orders and one true for about 1%. The same index type gave different plans:
| Query | plan | pages | time |
|---|---|---|---|
| WHERE is_mobile (50%) | Seq Scan, index ignored | 22,704 | 73.8 ms |
| WHERE is_refunded (1%), index disabled | Seq Scan | 22,704 | 39.6 ms |
| WHERE is_refunded (1%) | Index Only Scan | 21 | 1.07 ms |
The planner ignored the balanced index and used the one for the rare value.
For refunded orders in a date range, I compared a full composite index with a partial one:
CREATE INDEX ON orders (is_refunded, created_at); -- 60 MB, 0.32 ms CREATE INDEX ON orders (created_at) WHERE is_refunded; -- 456 kB, 0.44 ms
The partial index covered only the refunded orders and gave similar query time. The query predicate has to imply the index predicate.
What the notes say now
- PostgreSQL has no clustered index.
CLUSTERsorts the table once, blocks reads while it runs, and decays with the next writes. - Put equality columns first and the range column last. Read plans with
BUFFERS:Index Condshows what was checked, not what was skipped. - On PostgreSQL 18, a composite index can serve its second column when the first has few distinct values. Look for
Index Searches. - Prefix
LIKEneedstext_pattern_opsor a"C"collation. Fragments needpg_trgm. - Index a rare value, usually with a partial index, instead of the whole column.
Checking all five took two Docker containers and a few dozen EXPLAIN (ANALYZE, BUFFERS) runs. The notes had been wrong for years. I now measure a claim before I write it down, rather than after I have repeated it in an interview.