Read the plan the advisor summarized#
A query advisor or plan check that reports "sequential scan on orders" has usually read one node of a plan. That node does not say how many rows the scan kept, how many pages it read, or whether the planner expected that many rows. Run the query yourself with EXPLAIN (ANALYZE, BUFFERS) on a copy of production data and read four numbers: the estimated rows= in the cost section, the actual rows= beside it, Rows Removed by Filter, and Buffers. Add the table's size from pg_class.relpages or pg_relation_size.
The PostgreSQL 16 manual names the first comparison as the main thing to check: "The thing that's usually most important to look for is whether the estimated row counts are reasonably close to reality." (Using EXPLAIN, REL_16_15) If they are close, the planner chose a seq scan with the right inputs, and the question is whether an index would be cheaper. If they are far apart, the plan rests on wrong inputs, and an index may not change it.
The runs below used PostgreSQL 16.15 on a local cluster with constructed tables, shared_buffers at 128 MB, and warm caches (each plan was run twice and the second run is shown). Timings on a shared container are noisy; buffer counts are steadier and are the better number to compare.
A small table where the scan wins#
plans holds 200 rows in 2 pages, with an index on code. The advisor's finding is real in the literal sense:
=> EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM plans WHERE code = 'plan-42';
Seq Scan on plans (cost=0.00..4.50 rows=1 width=16) (actual time=0.007..0.015 rows=1 loops=1)
Filter: (code = 'plan-42'::text)
Rows Removed by Filter: 199
Buffers: shared hit=2
Execution Time: 0.021 ms
The index exists, so "missing index" is already wrong. To see what the index would cost, turn sequential scans off for the session:
=> SET enable_seqscan = off;
=> EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM plans WHERE code = 'plan-42';
Index Scan using plans_code_idx on plans (cost=0.14..8.16 rows=1 width=16) (actual time=0.020..0.021 rows=1 loops=1)
Index Cond: (code = 'plan-42'::text)
Buffers: shared hit=2
Execution Time: 0.038 ms
The estimate is exact in both plans, the forced plan reads the same 2 pages, and the planner prices it at 8.16 against 4.50. The setting "discourages the planner from using one if there are other methods available" (enable_seqscan, REL_16_15); it does not forbid the scan, so a forced plan that still scans means no index applies to the condition. The manual describes this case directly: "on a table that only occupies one disk page, you'll nearly always get a sequential scan plan whether indexes are available or not." Dismiss the finding and record the table size and the forced comparison.
A large table with a selective predicate#
orders holds 2,000,000 rows in 14,706 pages (115 MB). Twenty rows belong to customer 4242, and customer_id has no index:
=> EXPLAIN (ANALYZE, BUFFERS) SELECT id, total_cents FROM orders WHERE customer_id = 4242;
Gather (cost=1000.00..26124.67 rows=20 width=12) (actual time=6.369..90.466 rows=20 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=12115 read=2591
-> Parallel Seq Scan on orders (cost=0.00..25122.67 rows=8 width=12) (actual time=19.775..75.894 rows=7 loops=3)
Filter: (customer_id = 4242)
Rows Removed by Filter: 666660
Execution Time: 90.496 ms
The estimate is right (20 expected, 20 returned), and the scan reads every page of the table to keep one row in 100,000. Rows Removed by Filter is per process here: with loops=3, three processes each discarded about 666,660 rows. This is the finding worth fixing. After CREATE INDEX orders_customer_id_idx ON orders (customer_id), which took 1.3 seconds and produced a 15 MB index:
Bitmap Heap Scan on orders (cost=4.58..82.62 rows=20 width=12) (actual time=0.011..0.023 rows=20 loops=1)
Recheck Cond: (customer_id = 4242)
Heap Blocks: exact=20
Buffers: shared hit=23
-> Bitmap Index Scan on orders_customer_id_idx (cost=0.00..4.58 rows=20 width=0) (actual time=0.006..0.006 rows=20 loops=1)
Execution Time: 0.032 ms
The same index does not help every query on the table. With the index in place, WHERE customer_id < 90000 keeps 1,800,000 of the 2,000,000 rows, and the planner still picks Seq Scan on orders with an estimate of 1,797,516 rows. Forced off the scan, it uses the index and reads more:
Plan for customer_id < 90000 | Buffers | Execution time |
|---|---|---|
| Seq Scan (planner's choice) | 14,706 | 368.5 ms |
Bitmap Heap Scan (enable_seqscan = off) | 16,407 | 536.5 ms |
Selectivity decides, and table size alone does not. A finding on a large table with a predicate that keeps most rows is a dismissal, or a question about why the query reads that much.
The disconfirmation: stale statistics#
The third case looks exactly like the second in the advisor's output, and adding an index would not fix it, because the index already exists. jobs holds 1,000,000 rows with an index on status. It was analyzed while every row was pending; then all but 100 rows were updated to done and the table was vacuumed but not analyzed. Autovacuum was disabled on this table so the stale state would hold for the demonstration:
=> SELECT relname, n_live_tup, n_mod_since_analyze, last_analyze, last_autoanalyze
FROM pg_stat_user_tables WHERE relname = 'jobs';
relname | n_live_tup | n_mod_since_analyze | last_analyze | last_autoanalyze
---------+------------+---------------------+-------------------------------+------------------
jobs | 1000000 | 999900 | 2026-11-25 08:34:29.797342+00 |
=> SELECT most_common_vals, most_common_freqs FROM pg_stats WHERE tablename = 'jobs' AND attname = 'status';
most_common_vals | most_common_freqs
------------------+-------------------
{pending} | {1}
=> EXPLAIN (ANALYZE, BUFFERS) SELECT id FROM jobs WHERE status = 'pending';
Seq Scan on jobs (cost=0.00..32155.00 rows=1000000 width=8) (actual time=0.046..80.297 rows=100 loops=1)
Filter: (status = 'pending'::text)
Rows Removed by Filter: 999900
Buffers: shared hit=10625 read=9030
Execution Time: 80.317 ms
The planner expected a million rows and got 100. The statistics still say every row is pending, and n_mod_since_analyze shows nearly the whole table changed since then. After ANALYZE jobs:
Index Scan using jobs_status_idx on jobs (cost=0.42..10.81 rows=67 width=8) (actual time=0.006..0.024 rows=100 loops=1)
Index Cond: (status = 'pending'::text)
Buffers: shared hit=7
Execution Time: 0.035 ms
The fix is the statistics, and the record should say why they went stale. Autovacuum analyzes a table when the rows changed since the last ANALYZE exceed "analyze base threshold + analyze scale factor * number of tuples", (Routine Vacuuming, REL_16_15), and the default scale factor is 0.1 (autovacuum_analyze_scale_factor, REL_16_15). A bulk update can run queries on stale statistics until that triggers, and some tables never trigger it: the same page notes that "autovacuum does not run ANALYZE on partitioned tables", and temporary tables "cannot be accessed by autovacuum." The manual's advice on index tuning starts from the same point: "Examining an application's index usage without having run ANALYZE is therefore a lost cause." (Examining Index Usage, REL_16_15)
Decide from the plan#
| What the finding says | Evidence to collect | Decision |
|---|---|---|
| Seq scan on a table of a few pages | relpages, and the plan with enable_seqscan = off | Dismiss; record the size and that the forced plan cost more |
| Seq scan, estimate close to actual, few rows kept from a large table | rows= estimate and actual, Rows Removed by Filter, buffers | Add an index on the filtered column; compare buffers and time after |
| Seq scan, estimate close to actual, most rows kept | Actual rows against table rows; the forced plan's buffers | Dismiss for indexing; question why the query reads most of the table |
| Seq scan, estimate far from actual | pg_stats for the column, n_mod_since_analyze, last_analyze, last_autoanalyze | Run ANALYZE and re-check; fix the analyze schedule before adding an index |
| Forced plan still scans | The condition against the index definition | The index cannot serve this condition; fix the query or index shape |
A plan checked with a literal value can differ from what the application runs, because the application may bind a parameter of another type. The guide to checking an index against the query the app sends covers that case. When an advisor reports the opposite problem, an index nobody scans, the guide to unused index findings lists what to check before dropping it.