How to Read a Postgres Query Plan: Estimates, Pages and Skipped Indexes

When a query is slow, Postgres will tell you exactly what it did, if you ask with EXPLAIN. The output looks like noise the first few times: a tree of nodes, two sets of numbers on every line, words...

Status, November 2026: every plan in this article is real output from PostgreSQL 18.6, the version the Make search fast lab runs. Your numbers will differ a little; the shapes will not.

When a query is slow, Postgres will tell you exactly what it did, if you ask with EXPLAIN. The output looks like noise the first few times: a tree of nodes, two sets of numbers on every line, words like "Bitmap Heap Scan" and "Rows Removed by Filter". Once you know where to look, it reads like a short story about your data: what Postgres expected to find, what it actually found, and how many pages it touched to find it.

This article is the reading before the lab. It covers what you need to follow the plans there: the difference between EXPLAIN and EXPLAIN (ANALYZE, BUFFERS), how to read the tree, estimates against actual rows, pages, the four ways Postgres reads a table, statistics, and the three reasons the planner walks past an index you thought it would use. How Search Works on Tirraflow explains the full-text search this site runs on; this one is about reading what Postgres does with any query, search included.


The Table

Every example runs against a table shaped like the lab's: 30,000 articles, each with a title, a body of 40 to 69 words, one of six topics, one of 200 authors, a publish date spread over about three years, and a view count where most articles have few views and a handful have many.

CREATE TABLE articles (
  id           integer PRIMARY KEY,
  title        text NOT NULL,
  body         text NOT NULL,
  topic        text NOT NULL,
  author_id    integer NOT NULL,
  published_at timestamptz NOT NULL,
  views        integer NOT NULL
);

The table takes 1,799 pages on disk. A page is Postgres's unit of storage and of I/O: 8 kB, holding about 17 of these rows. Keep that number in mind; it comes back in almost every plan.

To catch the moment between loading data and Postgres learning about it, I turned autovacuum off for these runs. You should not do that on a real server. I also added indexes one at a time, as each section needs them, so every plan shows what the planner could choose from at that point.


EXPLAIN Guesses, EXPLAIN ANALYZE Measures

A question the author page of a blog asks all the time: the ten newest articles by one author.

SELECT id, title
FROM articles
WHERE author_id = 17
ORDER BY published_at DESC
LIMIT 10;

Plain EXPLAIN plans the query and stops. Nothing runs, so it is safe on any query and instant on any table:

Limit  (cost=3069.19..3069.22 rows=10 width=44)
  ->  Sort  (cost=3069.19..3070.45 rows=504 width=44)
        Sort Key: published_at DESC
        ->  Seq Scan on articles  (cost=0.00..3058.30 rows=504 width=44)
              Filter: (author_id = 17)

Each line is a node, one step of the plan. In the parentheses:

All three are estimates. To see what really happens, run the query with EXPLAIN (ANALYZE, BUFFERS):

Limit  (cost=3069.19..3069.22 rows=10 width=44) (actual time=13.110..13.112 rows=10.00 loops=1)
  Buffers: shared hit=1802
  ->  Sort  (cost=3069.19..3070.45 rows=504 width=44) (actual time=13.107..13.109 rows=10.00 loops=1)
        Sort Key: published_at DESC
        Sort Method: top-N heapsort  Memory: 26kB
        Buffers: shared hit=1802
        ->  Seq Scan on articles  (cost=0.00..3058.30 rows=504 width=44) (actual time=0.042..12.993 rows=151.00 loops=1)
              Filter: (author_id = 17)
              Rows Removed by Filter: 29849
              Buffers: shared hit=1799
Planning Time: 0.069 ms
Execution Time: 13.149 ms

Each node now has a second set of parentheses with the measurements: actual time (milliseconds to the first row and to the last), rows it really returned, and loops. The Buffers lines count pages, which the section after next is about. The node-specific lines matter too: Rows Removed by Filter: 29849 says the scan read 30,000 rows to keep 151.

Two things to know before you type it anywhere:

Look at the Seq Scan line again: the planner expected 504 rows and got 151. We'll come back to why.


Read the Tree From the Inside Out

Postgres prints the plan root first: the node that hands rows to you is at the top, and each -> introduces a child, indented under its parent. Data flows the other way. The most indented nodes read the tables, and each parent consumes what its children produce.

A query plan read inside out: the Seq Scan reads all 1,799 pages and passes 151 rows to the Sort, which keeps the newest 10 for the Limit, which returns them to the client. EXPLAIN prints the same tree the other way up, Limit first.

The plan above, read inside out. EXPLAIN prints the same three nodes upside down, with the Limit first.

So read from the innermost node outward:

  1. Seq Scan on articles: read every row of the table, keep those with author_id = 17. 151 rows.

  2. Sort: order them by published_at DESC. top-N heapsort means it knew a Limit was waiting and only ever kept the best 10 in 26 kB of memory.

  3. Limit: hand back 10 rows and stop.

A parent's numbers include its children's. The Limit's actual time=13.110..13.112 and its Buffers: shared hit=1802 contain everything under it, so the root tells you the total and the children tell you where it went. Here nearly all of it went to the Seq Scan: 12.99 ms and 1,799 of the 1,802 pages.

When a plan has two children under one node, as joins do, the first is the outer side and the second the inner. That is where loops starts to matter.


Estimated Rows, Actual Rows, and Loops

Every planning decision starts from row estimates. Whether to use an index, which join method to use, which table to read first: all of it is chosen by comparing costs, and every cost is built on how many rows the planner expects at each node. A plan is only as good as its estimates.

So the most useful habit when reading a plan is to compare rows in the first parentheses with rows in the second, node by node, starting at the bottom. A node off by 2× is normal. A node off by 100× is where the trouble usually starts, and everything above it was planned on a wrong number.

There is one trap. For a node that runs more than once, the actual numbers are per loop. Here I join a small authors table (200 rows) to the articles of three authors:

SELECT au.name, a.title
FROM authors au
JOIN articles a ON a.author_id = au.id
WHERE au.id IN (17, 42, 99);
Nested Loop  (cost=5.45..1310.08 rows=450 width=41) (actual time=0.033..1.052 rows=460.00 loops=1)
  Buffers: shared hit=450 read=2
  ->  Seq Scan on authors au  (cost=0.00..4.75 rows=3 width=14) (actual time=0.004..0.011 rows=3.00 loops=1)
        Filter: (id = ANY ('{17,42,99}'::integer[]))
        Rows Removed by Filter: 197
        Buffers: shared hit=2
  ->  Bitmap Heap Scan on articles a  (cost=5.45..433.61 rows=150 width=35) (actual time=0.026..0.336 rows=153.33 loops=3)
        Recheck Cond: (au.id = author_id)
        Heap Blocks: exact=444
        Buffers: shared hit=448 read=2
        ->  Bitmap Index Scan on articles_author_id  (cost=0.00..5.41 rows=150 width=0) (actual time=0.014..0.015 rows=153.33 loops=3)
              Index Cond: (author_id = au.id)
              Index Searches: 3
              Buffers: shared hit=4 read=2
...
Planning Time: 0.471 ms
Execution Time: 1.089 ms

A Nested Loop reads its outer child once (three authors) and runs its inner child once for each outer row. The inner Bitmap Heap Scan says rows=153.33 loops=3: on average 153.33 rows each time, three times, so 460 rows in total, which is what the Nested Loop returned. The estimate on that node, rows=150, is per loop as well. Always multiply rows and time by loops before deciding a node is cheap.

Buffers and Heap Blocks are the exception: they are totals over all loops. 444 heap pages is for the three authors together.

Nested loops are where a bad estimate hurts most. The planner chooses one when it expects the outer side to return a handful of rows. If it returns thousands instead, the inner side runs thousands of times. When you find that, don't look at the loop first: look for the lowest node whose estimate parted ways with reality, and ask why.


Pages: Shared Hit and Read

Every Buffers line counts 8 kB pages, split by where Postgres found them:

You may also see dirtied and written (pages this query changed or had to write out), and temp read/temp written when a sort or hash spills to disk.

Here is the same query twice, right after a restart, so the cache starts empty. I'll explain the Bitmap nodes in the next section; for now, look at the Buffers lines.

Bitmap Heap Scan on articles  (cost=5.46..463.85 rows=151 width=35) (actual time=0.159..0.710 rows=151.00 loops=1)
  Recheck Cond: (author_id = 17)
  Heap Blocks: exact=146
  Buffers: shared read=148
  ->  Bitmap Index Scan on articles_author_id  (cost=0.00..5.42 rows=151 width=0) (actual time=0.038..0.039 rows=151.00 loops=1)
        Index Cond: (author_id = 17)
        Index Searches: 1
        Buffers: shared read=2
...
Planning Time: 1.262 ms
Execution Time: 0.781 ms

And again:

Bitmap Heap Scan on articles  (cost=5.46..463.85 rows=151 width=35) (actual time=0.034..0.082 rows=151.00 loops=1)
  Recheck Cond: (author_id = 17)
  Heap Blocks: exact=146
  Buffers: shared hit=148
  ->  Bitmap Index Scan on articles_author_id  (cost=0.00..5.42 rows=151 width=0) (actual time=0.016..0.016 rows=151.00 loops=1)
        Index Cond: (author_id = 17)
        Index Searches: 1
        Buffers: shared hit=2
Planning Time: 0.033 ms
Execution Time: 0.113 ms

The same 148 pages both times: read the first time, hit the second. The time went from 0.78 ms to 0.11 ms. A Seq Scan for one topic just after does the same thing at a larger scale: 146 pages hit (the ones the query above had just loaded) and 1,653 read, 4.2 ms. Run it again and all 1,799 are hits, in 2.0 ms.

Seq Scan on articles  (cost=0.00..2174.00 rows=4982 width=35) (actual time=0.036..4.064 rows=4982.00 loops=1)
  Filter: (topic = 'security'::text)
  Rows Removed by Filter: 25018
  Buffers: shared hit=146 read=1653
...
Planning Time: 0.083 ms
Execution Time: 4.183 ms

This is why I budget queries in pages, not milliseconds. Time depends on what else the server is doing, what is in the cache, and the machine. It changed by 7× above for identical work, and the very first Seq Scan in this article took 13 ms where the same scan took 2.6 ms a minute later. Pages count the work itself: run the same query on the same data with the same plan and you get the same total of hit plus read, on a laptop or on a loaded production server. A page count that grows with the table is a plan that will get slower as the table grows, even if today it is fast because everything is cached.

The lab checks your work the same way: it reads shared hit + read at the root of your plan, and one of its tasks gives you a budget in pages.

Pages have one blind spot: CPU. A query that reads few pages but does expensive work on every row (parsing text, say) is slow in a way no Buffers line shows. Watch both: pages for how the work scales, time for what each row costs.


Four Ways to Read a Table

A table in Postgres is a heap: pages of rows in no particular order. An index is a separate structure, usually a B-tree, whose entries point at rows in the heap by their address, a page number and a slot in that page. Every way of reading a table is a different way of combining the two.

How Postgres finds author 17's rows: two index pages, the root and a leaf, give 151 row addresses; a bitmap sorts them into 146 heap page numbers, and each of those pages is read once. A Seq Scan reads all 1,799 heap pages instead.

Index pages and heap pages for author_id = 17: the index finds the rows, the heap holds them.

Seq Scan

Read every page of the heap, in order, and test each row. Its cost depends on the size of the table, not on the size of the answer. The plan for one author, once Postgres has statistics but no index on author_id:

Seq Scan on articles  (cost=0.00..2174.00 rows=151 width=35) (actual time=0.015..2.764 rows=151.00 loops=1)
  Filter: (author_id = 17)
  Rows Removed by Filter: 29849
  Buffers: shared hit=1799
...
Planning Time: 0.107 ms
Execution Time: 2.773 ms

That cost is simple arithmetic: 1,799 pages × 1.0, plus 30,000 rows × 0.0125 for reading and testing each row, is 2,174. A Seq Scan is the right plan for a small table, or when the query wants a large share of the rows. It's the wrong plan when you want 151 rows out of 30,000.

Index Scan

Walk the B-tree to the first matching entry, then for each entry fetch its row from the heap. Fetching one article by primary key:

Index Scan using articles_pkey on articles  (cost=0.29..8.30 rows=1 width=35) (actual time=0.008..0.008 rows=1.00 loops=1)
  Index Cond: (id = 4242)
  Index Searches: 1
  Buffers: shared hit=3
Planning Time: 0.015 ms
Execution Time: 0.013 ms

Three pages: the index's root, one leaf, and the heap page with the row. An Index Scan returns rows in index order, which, as we'll see, can save a Sort. Its weakness is that consecutive entries may point anywhere in the heap, so it can visit the same heap page many times and jump around between them.

Bitmap Index Scan + Bitmap Heap Scan

The middle road, and the node pair you'll see most when a query matches more than a few rows. After CREATE INDEX articles_author_id ON articles (author_id):

Bitmap Heap Scan on articles  (cost=5.46..463.85 rows=151 width=35) (actual time=0.033..0.096 rows=151.00 loops=1)
  Recheck Cond: (author_id = 17)
  Heap Blocks: exact=146
  Buffers: shared hit=146 read=2
  ->  Bitmap Index Scan on articles_author_id  (cost=0.00..5.42 rows=151 width=0) (actual time=0.019..0.019 rows=151.00 loops=1)
        Index Cond: (author_id = 17)
        Index Searches: 1
        Buffers: shared read=2
...
Planning Time: 0.288 ms
Execution Time: 0.126 ms

Two steps. The Bitmap Index Scan reads the index (2 pages) and builds a bitmap of the heap pages that hold a match. The Bitmap Heap Scan then reads those pages in physical order, each one once. Heap Blocks: exact=146 says 151 rows lived on 146 different pages. Recheck Cond is there because a bitmap that grows too big for work_mem drops to one bit per page, and then each row on the page has to be tested again. "exact" means that didn't happen. The rows come out in heap order, not index order, so a Bitmap Heap Scan never saves a Sort. A GIN index, the kind full-text search uses, is always read this way.

Index Only Scan

If every column the query needs is in the index, Postgres can skip the heap. Counting one author's articles needs only author_id. But on my freshly loaded table, the planner did not choose an Index Only Scan:

Aggregate  (cost=464.23..464.24 rows=1 width=8) (actual time=0.056..0.056 rows=1.00 loops=1)
  Buffers: shared hit=148
  ->  Bitmap Heap Scan on articles  (cost=5.46..463.85 rows=151 width=0) (actual time=0.020..0.050 rows=151.00 loops=1)
        Recheck Cond: (author_id = 17)
        Heap Blocks: exact=146
        Buffers: shared hit=148
        ->  Bitmap Index Scan on articles_author_id  (cost=0.00..5.42 rows=151 width=0) (actual time=0.008..0.008 rows=151.00 loops=1)
              Index Cond: (author_id = 17)
              Index Searches: 1
              Buffers: shared hit=2
...
Planning Time: 0.039 ms
Execution Time: 0.076 ms

The catch is visibility. An index entry doesn't say whether its row is visible to your transaction; only the heap knows. Postgres can skip the heap only for pages its visibility map marks as all-visible, and VACUUM sets those marks. With autovacuum off, nothing was marked, so an index-only scan would have gone to the heap for every row anyway. After VACUUM articles:

Aggregate  (cost=7.31..7.32 rows=1 width=8) (actual time=0.020..0.020 rows=1.00 loops=1)
  Buffers: shared hit=3
  ->  Index Only Scan using articles_author_id on articles  (cost=0.29..6.93 rows=151 width=0) (actual time=0.010..0.015 rows=151.00 loops=1)
        Index Cond: (author_id = 17)
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=3
...
Planning Time: 0.038 ms
Execution Time: 0.030 ms

3 pages instead of 148. Heap Fetches: 0 is the line to check: it counts rows for which Postgres had to visit the heap after all. On a table that changes a lot between vacuums, that number climbs and the Index Only Scan quietly turns back into an Index Scan.


Statistics: Where the Estimates Come From

Back to the first plan's mystery: 504 rows expected, 151 returned. That plan ran before Postgres had ever analyzed the table. Here are two different queries from that moment, one author and one topic:

SELECT id, title FROM articles WHERE author_id = 17;       -- 151 rows
SELECT id, title FROM articles WHERE topic = 'security';  -- 4,982 rows

The topic query returned 4,982 rows, and its plan expected the same 504 the author query did:

Seq Scan on articles  (cost=0.00..3058.30 rows=504 width=36) (actual time=0.004..1.630 rows=4982.00 loops=1)
  Filter: (topic = 'security'::text)
  Rows Removed by Filter: 25018
  Buffers: shared hit=1799
...
Planning Time: 0.031 ms
Execution Time: 1.727 ms

Same estimate for two very different questions. With no statistics, Postgres knows neither how many rows the table has nor how its values are spread, so it estimates the row count from the table's size on disk and applies a fixed share for an equality test, whatever the value. An estimate that doesn't change when the value changes is the sign that statistics are missing.

ANALYZE fixes it. It reads a sample of the table (300 × default_statistics_target rows, so 30,000 by default) and stores, per column, the number of distinct values, the most common values with their frequencies, a histogram of the rest, and more. You can read them in pg_stats:

SELECT attname, n_distinct, most_common_vals
FROM pg_stats
WHERE tablename = 'articles' AND attname IN ('topic', 'author_id');
  attname  | n_distinct |                    most_common_vals
-----------+------------+----------------------------------------------------------
 topic     |          6 | {ai-agents,devops,security,databases,frontend,streaming}
 author_id |        200 | {158,171,186,169,107,19,109,148,123,132,42,64,126,16,...}

After ANALYZE articles, the author query expects 151 rows (it is the Seq Scan plan in the previous section), and the topic query:

Seq Scan on articles  (cost=0.00..2174.00 rows=4982 width=35) (actual time=0.004..2.405 rows=4982.00 loops=1)
  Filter: (topic = 'security'::text)
  Rows Removed by Filter: 25018
  Buffers: shared hit=1799
...
Planning Time: 0.035 ms
Execution Time: 2.498 ms

151 and 4,982, exactly. On a 30,000-row table the sample is the whole table, so the estimates are exact; on a big table they are close rather than exact. The cost dropped too, from 3,058 to 2,174, because the planner now knows how many rows there are.

Autovacuum runs ANALYZE for you once enough of a table has changed. Two things it doesn't notice:

In both cases, run ANALYZE yourself. It is cheap and takes a lock that doesn't block reads or writes.


Why the Planner Skips Your Index

There is an index, the query looks like it should use it, and the plan says Seq Scan. In my experience it is one of three reasons.

1. The query doesn't contain the indexed expression

An index on an expression is matched by the expression, written the same way, not by what it means. To find titles case-insensitively, I index lower(title):

CREATE INDEX articles_title_lower ON articles (lower(title));
ANALYZE articles;

SELECT id, title FROM articles
WHERE lower(title) = 'system data cluster release schema';
Index Scan using articles_title_lower on articles  (cost=0.41..8.43 rows=1 width=35) (actual time=0.024..0.025 rows=1.00 loops=1)
  Index Cond: (lower(title) = 'system data cluster release schema'::text)
  Index Searches: 1
  Buffers: shared hit=1 read=3
...
Planning Time: 0.173 ms
Execution Time: 0.039 ms

Four pages. Now the same lookup written two other ways, both returning the same row:

Seq Scan on articles  (cost=0.00..2174.00 rows=1 width=35) (actual time=0.280..1.939 rows=1.00 loops=1)
  Filter: (title = 'System Data Cluster Release Schema'::text)
  Rows Removed by Filter: 29999
  Buffers: shared hit=1799
Planning Time: 0.027 ms
Execution Time: 1.946 ms
Seq Scan on articles  (cost=0.00..2174.00 rows=3 width=35) (actual time=1.946..13.845 rows=1.00 loops=1)
  Filter: (title ~~* 'system data cluster release schema'::text)
  Rows Removed by Filter: 29999
  Buffers: shared hit=1799
Planning Time: 0.140 ms
Execution Time: 13.850 ms

Both read all 1,799 pages. title = '…' compares the column, not lower(title). ILIKE means the same thing to you, but it is a different operator on a different expression, and the planner does not reason about meaning; it looks for the indexed expression in the WHERE clause. The ILIKE version also took 13.8 ms against 1.9 ms for the plain comparison on the same pages: case-insensitive matching costs CPU on every row, the blind spot from the pages section.

The same rule holds for every expression index, including full-text ones, where two calls of to_tsvector that look interchangeable may not be. When a query has to use an expression index, write the expression in one place (a view, a function, a generated column) so nobody types it slightly differently.

2. The estimate is wrong

If the planner thinks a value matches a third of the table, it will read the whole table, and it is right to, given what it believes. This is the statistics section again: compare estimated and actual rows on the scan, and if the estimate doesn't move when you change the value, run ANALYZE.

3. The value is common, and the planner is right

The third reason isn't a bug. With an index on views, ask for the rarely viewed and the much viewed articles:

CREATE INDEX articles_views ON articles (views);

SELECT id, title FROM articles WHERE views > 45000;  -- much viewed
SELECT id, title FROM articles WHERE views < 20000;  -- rarely viewed

The much-viewed ones, 773 rows (2.6% of the table), use the index:

Bitmap Heap Scan on articles  (cost=14.36..1444.23 rows=784 width=35) (actual time=0.130..0.468 rows=773.00 loops=1)
  Recheck Cond: (views > 45000)
  Heap Blocks: exact=639
  Buffers: shared hit=639 read=4
  ->  Bitmap Index Scan on articles_views  (cost=0.00..14.17 rows=784 width=0) (actual time=0.079..0.079 rows=773.00 loops=1)
        Index Cond: (views > 45000)
        Index Searches: 1
        Buffers: shared read=4
...
Planning Time: 0.140 ms
Execution Time: 0.501 ms

The rarely viewed ones, 23,850 rows (80% of the table), don't:

Seq Scan on articles  (cost=0.00..2174.00 rows=23846 width=35) (actual time=0.004..3.243 rows=23850.00 loops=1)
  Filter: (views < 20000)
  Rows Removed by Filter: 6150
  Buffers: shared hit=1799
Planning Time: 0.019 ms
Execution Time: 3.671 ms

That isn't a missed index. Most of the table is the answer, so nearly every page holds a matching row, and every one of those pages has to be read whatever path leads to it. You can check by taking the Seq Scan away for one session (SET enable_seqscan = off doesn't forbid it: since Postgres 18 the planner counts disabled nodes and takes any plan with fewer of them):

Bitmap Heap Scan on articles  (cost=385.09..2482.17 rows=23846 width=35) (actual time=1.035..3.199 rows=23850.00 loops=1)
  Recheck Cond: (views < 20000)
  Heap Blocks: exact=1799
  Buffers: shared hit=1800 read=43
  ->  Bitmap Index Scan on articles_views  (cost=0.00..379.13 rows=23846 width=0) (actual time=0.906..0.906 rows=23850.00 loops=1)
        Index Cond: (views < 20000)
        Index Searches: 1
        Buffers: shared hit=1 read=43
Planning Time: 0.020 ms
Execution Time: 3.635 ms

The bitmap reads all 1,799 heap pages anyway, plus 44 index pages, for no gain. With bitmaps turned off as well, a plain Index Scan has to fetch rows in index order:

Index Scan using articles_views on articles  (cost=0.29..7811.99 rows=23846 width=35) (actual time=0.016..5.639 rows=23850.00 loops=1)
  Index Cond: (views < 20000)
  Index Searches: 1
  Buffers: shared hit=22917
Planning Time: 0.046 ms
Execution Time: 6.101 ms

22,917 page visits for a 1,799-page table: each page visited 13 times on average, because consecutive index entries point to different pages. The planner's costs said it in advance: 2,174 for the Seq Scan, 2,482 for the bitmap, 7,812 for the Index Scan.

Look at the much-viewed query again: 2.6% of the rows lived on 639 pages, 36% of the table. Rows that match are spread over the heap, so a small share of rows can mean a large share of pages. That is why the planner moves to a Seq Scan well before a query wants half the table. And it is why one statement can get two different plans for two values: a plan is chosen per query and per value. When a query is "sometimes slow", test it with a rare value and a common one.

If a query asks for most of a table, no index will make it cheap. Change the question instead: nobody reads 23,850 results, so ask for the first page of them.


An Index That Serves the Whole Query

Back to the author page, now also returning the date, with the author_id index in place:

Limit  (cost=467.11..467.14 rows=10 width=43) (actual time=0.099..0.100 rows=10.00 loops=1)
  Buffers: shared hit=148
  ->  Sort  (cost=467.11..467.49 rows=151 width=43) (actual time=0.098..0.099 rows=10.00 loops=1)
        Sort Key: published_at DESC
        Sort Method: top-N heapsort  Memory: 26kB
        Buffers: shared hit=148
        ->  Bitmap Heap Scan on articles  (cost=5.46..463.85 rows=151 width=43) (actual time=0.026..0.074 rows=151.00 loops=1)
              Recheck Cond: (author_id = 17)
              Heap Blocks: exact=146
              Buffers: shared hit=148
              ->  Bitmap Index Scan on articles_author_id  (cost=0.00..5.42 rows=151 width=0) (actual time=0.014..0.014 rows=151.00 loops=1)
                    Index Cond: (author_id = 17)
                    Index Searches: 1
                    Buffers: shared hit=2
...
Planning Time: 0.108 ms
Execution Time: 0.117 ms

Better than the 1,802 pages of the first plan, but look at the shape: Postgres finds all 151 of the author's articles, sorts them, and throws away 141. Every new article by this author makes the page more expensive, and the Sort is there because the bitmap returns rows in heap order.

An index on both columns, equality column first, then the sort column:

CREATE INDEX articles_author_published
  ON articles (author_id, published_at DESC);
ANALYZE articles;

SELECT id, title, published_at
FROM articles
WHERE author_id = 17
ORDER BY published_at DESC
LIMIT 10;
Limit  (cost=0.29..39.14 rows=10 width=43) (actual time=0.023..0.033 rows=10.00 loops=1)
  Buffers: shared hit=10 read=2
  ->  Index Scan using articles_author_published on articles  (cost=0.29..586.93 rows=151 width=43) (actual time=0.022..0.031 rows=10.00 loops=1)
        Index Cond: (author_id = 17)
        Index Searches: 1
        Buffers: shared hit=10 read=2
...
Planning Time: 0.153 ms
Execution Time: 0.045 ms

The Sort node is gone, and with it the 141 rows read for nothing: 12 pages instead of 148. Inside the index, all of author 17's entries sit together, already in published_at DESC order. The Index Scan starts at the newest one, fetches rows as it walks, and the Limit stops it after ten. The cost of this query no longer depends on how much the author has written.

Three details make this work:

This is the most useful index pattern I know for paged lists: an author's posts, a tenant's orders, a user's notifications. WHERE x = ? ORDER BY y LIMIT n wants an index on (x, y), and the plan to look for is a Limit directly over an Index Scan, with no Sort.


Reading Any Plan, in Order

  1. Run EXPLAIN (ANALYZE, BUFFERS), inside a rolled-back transaction if the query writes.

  2. Read the root's Buffers and Execution Time: the total.

  3. Go down to the innermost nodes. For each, compare estimated and actual rows, multiplied by loops. Find the lowest node where they part ways.

  4. For each scan, check the type against the share of rows it returns, and Rows Removed by Filter against the rows it kept.

  5. Look for a Sort under a Limit: it often means an index in the right column order is missing.

  6. Change one thing, run it again, compare the pages.


Practice It

The Make search fast lab gives you your own Postgres 18 server with a 30,000-article table like this one, and nine steps through the plans of a search box. You start from a slow ILIKE, move it to full-text search with a GIN index, watch the planner change its mind as words get more common, and finish with a topic page that has to stay within a budget of 50 pages. Each step asks you to predict what Postgres will do before you run it; the plans in this article are what you'll be reading.

Read on Tirraflow