Slow Postgres, part 4 of 8. The SQL and raw numbers for every test are on GitHub.

Plain query time on PostgreSQL 18.7, each index alone on the table. Black cells read every page of the table.
On PostgreSQL 17.7, the planner ignored an index on (status, created_at) for one day of orders and read all ten million rows, 393 ms. On 18.7 the same index answers it in 2.4 ms with B-tree skip scan. The other order got no rescue. Counting pending orders through (created_at, status) still reads the whole table on 18, 295 ms against 6.6 with the columns swapped.
One note before the numbers: the AlphaVPS link at the end is a referral link, so I may get a commission at no extra cost to you, and the netcup voucher I offer there comes from my own account.
The short version:
- Equality columns go first and the range column last, on 17 and 18 alike: for the same 4,031 failed orders of one week,
(status, created_at)read 19 index pages and(created_at, status)787. - Skip scan lets 18 use an index with no condition on its first column, searching it about once per value of that column: four statuses, seven searches, 2.4 ms.
- The planner counts on skipping only while the first column's estimated distinct values stay below the index's page count, 38,506 here. At 20,000 values it skipped, in 33 ms; at 50,000 it read the table.
- An
INlist on the first column still breaksORDER BY ... LIMIT: the newest 50 failed or pending orders took 357 ms on(status, created_at)on 18 (381 on 17) and 0.8 on(created_at, status). - Two single-column indexes handled every query, but on 18 the combined filter took 12 to 60 ms on them against 1.1 to 17 on the composite.
The lab: ten million orders, two versions
Everything ran on my M3 Max laptop with PostgreSQL 18.7 from Homebrew and, for the side-by-side runs, 17.7, an older 17 than the current 17.12. shared_buffers is 5 GB, so the table and every index stay in memory, and JIT and parallel workers are off. Nothing is read from disk, so 18's asynchronous I/O and its checksums-on default don't show up here.
The table holds one order every three seconds from January 1, 2026, inserted in time order, so the heap is sorted by created_at. The data ends on December 14, so the queries' last week and last day end on December 13. Status comes from id arithmetic: exactly 92.9% paid, 5% refunded, 2% failed and 0.1% pending, spread evenly through the year. Statistics are fresh; part 3 covers what happens when they aren't.
Four indexes: (status, created_at) and (created_at, status) at 305 MB each, (status) at 66 MB and (created_at) at 214 MB. To test one setup at a time, the script drops the other indexes inside a transaction, runs the query and rolls back. Do that only on a lab copy: DROP INDEX holds an ACCESS EXCLUSIVE lock until ROLLBACK, so every other query on orders waits, plain SELECTs included.
Each time is the median of 35 plain runs: 7 after two warm-ups in each of five rounds, alternating 18 and 17. The laptop was busy with other work, so timings are noisy. The plans and page counts were identical in every round and don't depend on the machine, so I lean on those.
How a two-column B-tree is sorted
An index on (status, created_at) is sorted by status, and by time only inside each status. An index on (created_at, status) is sorted by time, and status only breaks ties between equal timestamps, which never happen here. This is where "failed orders this week" starts reading in each:
(status, created_at) (created_at, status) failed 2026-12-06 23:58:57 2026-12-06 23:59:57 paid failed 2026-12-07 00:02:18 <- 2026-12-07 00:00:00 pending <- failed 2026-12-07 00:04:36 2026-12-07 00:00:03 paid failed 2026-12-07 00:06:54 2026-12-07 00:00:06 paid failed 2026-12-07 00:10:15 2026-12-07 00:00:09 paid failed 2026-12-07 00:12:33 2026-12-07 00:00:12 refunded On the left, the week's failed orders sit next to each other. On the right, the first failed one is entry 47.
The PostgreSQL 18 docs give the rule: equality conditions on the leading columns, plus an inequality on the first column without one, limit the part of the index that's scanned. Later columns are checked inside the index, which saves visits to the table. The 17 docs said they "do not reduce the portion of the index that has to be scanned"; 18 says "do not necessarily reduce", because of skip scan.
Equality first, range last
The dashboard query:
SELECT count(*), sum(amount_cents) FROM orders WHERE status = 'failed' AND created_at >= '2026-12-07' AND created_at < '2026-12-14'; With (status, created_at), Postgres goes straight to the first failed order of December 7 and reads forward to the end of the week, every entry a match. With (created_at, status), it reads all 201,600 orders of the week, checks each status and keeps one in fifty.
Both plans for count(*) alone, answered from the index without the table (18.7, costs and literals cut):
Index Only Scan using orders_status_created_idx on orders (actual time=0.024..0.191 rows=4031.00 loops=1) Index Cond: ((status = 'failed'::text) AND (created_at >= ...) AND (created_at < ...)) Index Searches: 1 Buffers: shared hit=19 Index Only Scan using orders_created_status_idx on orders (actual time=0.044..3.766 rows=4031.00 loops=1) Index Cond: ((created_at >= ...) AND (created_at < ...) AND (status = 'failed'::text)) Index Searches: 1 Buffers: shared hit=787 Both list status = 'failed' under Index Cond, and nothing there says the first plan used it to find where to start while the second checked it 201,600 times. Buffers does, the line part 2 reads in detail: 19 pages against 787. 17 reads the same pages but prints them only with BUFFERS; 18 prints them by default and adds Index Searches, the number of times the scan went down the tree from the root.

Index pages come from the index-only `count()`, heap pages from the full query. PostgreSQL 18.7; 17.7 reads the same pages.*
The full query also reads amount_cents from 1,493 heap pages in both orders, so the gap shrinks to 1.1 ms against 4.5 on 18 (1.1 against 5.7 on 17), then grows with the window: 5.4 against 19 ms for 30 days and 17 against 55 for 90, on 18.
The common advice is to put the most selective column first. Here that's created_at, unique on every row, and it's the wrong first column: both filters keep 2% of the rows. What decides is how each column is filtered. An equality keeps the matching entries together, and a range spreads them over everything between its two ends.
The query an index can't start
The same rule works against each order. Count pending orders with only (created_at, status): the condition is on the second column, so there's nowhere to start, and Postgres reads the table, 295 ms on 18 and 396 on 17, against 6.6 and 6.5 on (status, created_at). Both versions read the same 74,075 pages. Why 18 needed a quarter less time for them I can't explain; on the one-day filter below, its Seq Scans were about as fast as 17's.
Skip scan can't help, since created_at has ten million distinct values. Forced onto the index with enable_seqscan = off, both versions read the whole index plus the 10,000 matching rows, 48,866 pages, in 169 and 170 ms. 18 shows Index Searches: 1.
Ask (status, created_at) for one day of orders on 17 and the problem flips: a Seq Scan of 393 ms against 2.4 on (created_at, status). Forced onto the index, 17 read 39,535 pages in 156 ms, but the planner prices index pages as random reads, the arithmetic from part 1, so it costed that walk at 314,033 against 224,225 for the Seq Scan. On 18 this query needs neither.
PostgreSQL 18: skip scan
The release notes say skip scan lets a multicolumn B-tree be used "when there are no restrictions on the first or early indexed columns". It searches once per value of the first column, as if the query said status = 'failed', then status = 'paid', and so on. On the one-day query (costs and casts cut):
Index Scan using orders_status_created_idx on orders (actual time=0.019..1.880 rows=28800.00 loops=1) Index Cond: ((created_at >= '2026-12-13 00:00:00+00') AND (created_at < '2026-12-14 00:00:00+00')) Index Searches: 7 Buffers: shared hit=808 Seven searches for four statuses: the EXPLAIN docs warn that the count often won't match the query one to one. 808 pages with the table included, where (created_at, status) reads 329 and the Seq Scan 74,075. 2.4 ms, close to the 2.0 of (created_at, status).
To find where it stops paying off, I built (x, created_at) indexes with 100 to 200,000 distinct values of x (customer_id % 100 and so on, then customer_id):

One day of orders from an index on (x, created_at), the plan the planner chose. PostgreSQL 18.7, with 17.7 for comparison.
Every value costs one more search from the root down to a leaf. At 10,000 values that's 10,001 searches and 30,261 index pages, 19 ms. At 20,000 it's 19,892 searches and 60,079 index pages, more than the 38,506 the whole index has, because each search rereads the root and an inner page. Still 33 ms.
The cutoff comes from the cost model. In 18's btcostestimate (selfuncs.c), a skipped column costs one search per distinct value plus one, with the count taken from n_distinct in pg_stats. When that passes the index's page count, the planner drops the skipping from its estimate and prices a scan of the whole index, which loses to the Seq Scan here. ANALYZE estimated 20,115 values for customer_id % 20000 and 48,944 for customer_id % 50000, against 38,506 pages. With no statistics on the column, the planner assumes no skipping at all.
Forced onto the index at 50,000 values, 18 showed 59 index searches and read the whole index, 38,433 pages in 118 ms, against 428 for the planner's Seq Scan. I don't know why it's 59 and not 50,001. 17, forced, read these indexes whole too, in about the same time. Past the cutoff, the planner gives up an index read over three times faster in memory, for the page-pricing reason above.
What 18 changes is whether you need a second index. With up to 1,000 values in the first column, a query that skips it took 4.3 ms at most, reading ten times the pages of a matching index (3,438 against 329), and may not need an index of its own.
ORDER BY and the order the index returns
Sorting adds a third kind of column. For the newest 50 failed orders, both indexes return rows already in order with no Sort node, 0.2 ms and 0.3. Add one status, WHERE status IN ('failed', 'pending') ORDER BY created_at DESC LIMIT 50, and that changes.
Now (status, created_at) holds two groups, each sorted on its own, and Postgres doesn't merge them. It collected all 210,000 failed and pending orders with a bitmap scan over every page of the table, then sorted them to keep 50: 357 ms on 18, 381 on 17. (created_at, status) walked time backwards past about 2,400 other orders, 0.8 ms on both.
So the sort column goes right after the equality columns, and an IN list on the leading column breaks the order the way a range does. Part 5 covers ORDER BY and LIMIT.
Two single-column indexes and BitmapAnd
The other common setup is one index per column. Postgres can turn each index scan into a bitmap of row locations, AND the two, and visit only the rows in both. The pair takes 280 MB against 305 for one composite.
Neither version chose a BitmapAnd for the combined query. Postgres walked one index and filtered on the other column: created_at for 7 and 30 days (Rows Removed by Filter: 197569 for the week), status for 90, which visited every page of the table. Forced with enable_indexscan = off, the BitmapAnd was costed at 18,227 for the week against 8,107 for the walk on 18, which I put down to the correlation of 1.0 between created_at and the table order.

Plain query time on PostgreSQL 18.7; 17.7 picked the same plans. Grey: the plan Postgres chose with two single-column indexes.
On 18 the forced BitmapAnd beat the planner's choice at 30 days, 26 ms against 54. At 90 days the bitmap outgrew the default 4 MB of work_mem and went partly lossy, 92 ms; part 7 covers lossy bitmaps. The composite stayed ahead at every window.
On 18 every other query ran fine on the two single indexes, all under 7 ms. For an internal dashboard polled a few times a minute, 12 to 60 ms is fine. For a filter on every API request, build the composite with the equality column first. On 17, keep a created_at index next to it for time-only queries; on 18, first check whether skip scan covers them. Each index slows every insert, which part 8 measures.
How to check this on your own database
- For each query on the table, mark how it uses every column: equality, range or sort.
- Put the equality columns first, then the one range or sort column. An
INlist counts as a range if you need the rows sorted. - Compare
Buffers, notIndex Cond, which looks the same for both column orders. On 17, ask forBUFFERS. - On 18, before adding an index for queries that leave out the first column, compare that column's
n_distinctinpg_stats(negative means a fraction of the rows) with the index'srelpagesinpg_class. Far below means skip scan can cover them, andIndex Searchesabove 1 shows it skipped.
Run it on your own server
All the numbers come from my M3 Max laptop (14 cores, 36 GB of RAM) with Homebrew's PostgreSQL 18.7 and 17.7. Your milliseconds will differ; the plans and page counts should match. The SQL, a script that runs the whole lab on either version, every plan and the chart code are on GitHub. Run it on your own server, and if you get a different picture or think I got something wrong, open an issue or a PR.
My own projects run on netcup (VPS from €2.21 a month) and AlphaVPS (VPS from €2.54 a month). For netcup, message me on LinkedIn and I'll send you a €5 voucher for new customers.
Good luck with your plans. Part 5 is next: ORDER BY and LIMIT, keyset pagination and what OFFSET costs.
Finish
I hope you understood the instructions correctly. If you have any edits to this article, I will be waiting for your comments. If I really helped you, I kindly ask you to subscribe to my LinkedIn and provide feedback on this article.
Link to follow me:
All my articles and links: fomo.moe

