Skip to content
Back to Blog
Search
PostgreSQL / Indexing• 25 Sept 2026• 5 min read

Five of my old index notes were wrong on PostgreSQL 18

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:

Momentcorrelationrowspagestime
before CLUSTER0.0021,0281,0030.47 ms
right after CLUSTER (took 2.5 s)1.0001,028170.09 ms
after +10% inserts, 10% updates0.6731,1352830.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:

Indexindex pagestotal pagesPG 18PG 17
(created_at, shop_id)6321,4601.59 ms1.85 ms
(shop_id, created_at)68340.22 ms0.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:

Setupplanpagestime
PG 17, (shop_id, created_at)Parallel Seq Scan22,70436.0 ms
PG 17, index forcedwhole index read12,46022.7 ms
PG 18, (shop_id, created_at)Bitmap Index Scan, 202 searches5,4471.92 ms
PG 18, plain index on (created_at)Bitmap Index Scan4,8121.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:

Setupplanpagestime
PG 18, (customer_id, created_at)Parallel Seq Scan22,70444.8 ms
PG 18, same index forcedwhole index read, 1 search12,46025.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 / indexplanpagestime
email = '...' / plainIndex Scan40.01 ms
LIKE 'abc%' / plainParallel Seq Scan7,35319.1 ms
LIKE 'abc%' / text_pattern_opsIndex Scan2350.07 ms
LIKE 'abc%' / COLLATE "C"Index Scan2350.08 ms
LIKE '%9f3a1%' / text_pattern_opsParallel Seq Scan7,35328.3 ms
LIKE '%9f3a1%' / pg_trgm GINBitmap Index Scan190.11 ms
LIKE 'abc%' / pg_trgm GINBitmap Index Scan2600.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:

Queryplanpagestime
WHERE is_mobile (50%)Seq Scan, index ignored22,70473.8 ms
WHERE is_refunded (1%), index disabledSeq Scan22,70439.6 ms
WHERE is_refunded (1%)Index Only Scan211.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. CLUSTER sorts 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 Cond shows 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 LIKE needs text_pattern_ops or a "C" collation. Fragments need pg_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.

PostgreSQL / IndexingRSSBack to Blog