skip to content

skip scan

postgres 18 can use an index on (a, b) for WHERE b = 5. how it hops, and when it can't.

address
everything.indexes.skip_scan
part
part 8, indexes, from the outside
chapter
chapter 4 of 12
reading time
7 min
version
pg 18+

before postgres 18, an index on (a, b) was no use for WHERE b = 5: the matches are scattered, one inside every value of a. postgres 18 adds skip scan: it treats the missing a as "every value of a", and goes down the tree once for each, reading only the bits where b = 5 can be. it pays off when the leading column has few distinct values, and the planner skips it when there are many.

the picture

back to the phone book, sorted by last name, then first name. you want every Ravi. reading the whole book works. but if the book only has four surnames (a very small town), you can open it at "Patel, Ravi", then at "Shah, Ravi", then at "Singh, Ravi", then at "Smith, Ravi". four lookups instead of the whole book.

that's skip scan pg 18+. here's the (a, b) index from multicolumn indexes again, six values of a. each marked entry is where a search down the tree landed.

WHERE
the same leaf level as one line of pages
1234567
SELECT a, b FROM ab WHERE b = 7
rows
6
leaf pages with a match
6
Index Searches
7
pages read (Buffers)
15
Index Only Scan · Index Cond: (b = 7)

b = 7: postgres 18 hops, one descent per value of a (a skip scan).

entries the query returnsstart of a new leaf pagewhere an index search landed
real output, postgres 18.6 (PGlite 0.5.12) · composite-grid
the same 240-entry index on (a, b). for b = 7 the scan goes down the tree for each value of a and reads one entry there: Index Searches: 7: six landings, plus one more search than there are values (the figure marks the six). a range on a hops only inside the range. a = 3 is the ordinary single search, for comparison.

the manual describes it as postgres generating an equality condition on its own: for the column with no condition, a "dynamic equality constraint" that matches every value stored in the index. so WHERE b = 7 runs like WHERE a = 1 AND b = 7, then a = 2 AND b = 7, and so on, where the next value of a is found by looking at the index, not by knowing it in advance.

few values hop, many don't

two tables with the same 30,000 rows of (a, b). in few, a has 4 distinct values. in many, it has 3,000. same query on both:

sqlempty db · session skip
CREATE TABLE few (a int NOT NULL, b int NOT NULL, pad text NOT NULL);
INSERT INTO few SELECT i % 4, i / 4, repeat('x', 40) FROM generate_series(0, 29999) AS i ORDER BY i;
CREATE INDEX few_a_b ON few (a, b);
CREATE TABLE many (a int NOT NULL, b int NOT NULL, pad text NOT NULL);
INSERT INTO many SELECT i % 3000, i / 3000, repeat('x', 40) FROM generate_series(0, 29999) AS i ORDER BY i;
CREATE INDEX many_a_b ON many (a, b);
VACUUM ANALYZE few, many;
 
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT * FROM few WHERE b = 5;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · INSERT 0 30000
#3 · CREATE INDEX
#4 · CREATE TABLE
#5 · INSERT 0 30000
#6 · CREATE INDEX
#7 · VACUUM
#8 · EXPLAIN
Bitmap Heap Scan on few (actual rows=4.00 loops=1) Recheck Cond: (b = 5) Heap Blocks: exact=1 Buffers: shared hit=8 read=6 -> Bitmap Index Scan on few_a_b (actual rows=4.00 loops=1) Index Cond: (b = 5) Index Searches: 5 Buffers: shared hit=7 read=6 Planning: Buffers: shared hit=12

look for Index Searches: {node(0)["Index Searches"]} on the Bitmap Index Scan: 4 values of a, 5 trips down the tree. the condition reads Index Cond: (b = 5), with no mention of a. the hopping is inside the index scan; EXPLAIN only shows it through that counter.

predict the output
the same query on many, where a has 3,000 values. what does postgres do?
sql
EXPLAIN SELECT * FROM many WHERE b = 5;
sqlempty db · session skip
continues session skip (1 earlier block run first)
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT * FROM many WHERE b = 5;
output, from postgres 18.6
EXPLAIN
Seq Scan on many (actual rows=3000.00 loops=1) Filter: (b = 5) Rows Removed by Filter: 27000 Buffers: shared hit=310 Planning: Buffers: shared hit=12

and if you take the seq scan away, the index scan postgres turned down doesn't hop at all. it reads the index from the first entry to the last (Index Searches: 1) and checks b in every entry:

table =
SELECT * FROM few WHERE b = 5
pages read by the whole query (Buffers: shared hit + read)
14 pages
11 pages
310 pages
115 pages

4 values of a: postgres hops. one search for each value of a, plus one.

real output, postgres 18.6 (PGlite 0.5.12) · skip-scan
the same b = 5, four ways. on few, the hops read 11 pages. on many, a full index scan reads 115 and the seq scan postgres actually picks reads all 310 pages of the table. the bars are whole-query buffer counts.

a real one

the usual way to meet skip scan isn't a table named few. it's an index someone built for one query, used by another. an index on orders (status, customer_id), made for "the paid orders of customer 42", and a query that only knows the customer:

show =
actual rowsbuffer hitsbuffer reads
real output, postgres 18.6 (PGlite 0.5.12) · skip-scan
an index on (status, customer_id), and WHERE customer_id = 42. status has 6 values, and the scan made 4 searches. the count isn't exactly one per value: the scan only goes back to the root when the next value it needs isn't on the leaf page it's already reading.

that's useful, and it's also a trap. the plan is fine today, with six statuses. it says nothing about what happens when someone adds a twentieth status, or when the leading column is a timestamp instead. if a query on customer_id alone is a hot path, give it its own index, or put customer_id first. skip scan is a safety net for the queries you didn't plan for, not a reason to stop thinking about column order.

when it can't

  • many distinct values in the skipped column. the planner estimates one search per value; past a point, the seq scan or a full index scan is cheaper. the manual says it's generally only chosen when most leaf pages can be skipped.
  • only b-trees. skip scan is part of the btree access method. GiST, GIN and BRIN multicolumn indexes work differently (index types).
  • a condition the index can't use at all. skip scan fills in a missing equality. it does nothing for lower(b) = 'x' on a plain index, or a type mismatch (why isn't my index used).
  • before postgres 18. the same query on 17 is a full index scan or a seq scan. if your production server is older than your laptop, check there.

keylines

  • postgres 18 can use an index on (a, b) for b alone: one search per value of a.
  • EXPLAIN ANALYZE shows it as Index Searches: N on an ordinary index scan.
  • few distinct leading values: it hops. many: seq scan.
  • it's a safety net. a hot query on b still deserves an index that starts with b.