Menu

I Added an Index and Postgres Still Does a Seq Scan (Here's Why)
📰

I Added an Index and Postgres Still Does a Seq Scan (Here's Why)

DEV Community: backend·Mohamed Hussain S·18 days ago
#0jAPjbFR
#devtobackend#devto#dev#index#rows
Reading 0:00
15s threshold

You have a slow query. You add an index. You run the query again, check the plan, and there it is: Seq Scan .

The usual reaction is "Postgres is ignoring my index." Sometimes that's true, and something is wrong. But a lot of the time, Postgres made the right call, and the real problem is somewhere else.

I reproduced four cases locally on Postgres 17 so we can look at real plans instead of guessing.

The Setup

A table of orders with a million rows. About 95% are completed and 5% are pending :

CREATE TABLE orders ( id serial PRIMARY KEY , customer_id int , status text , created_at timestamptz DEFAULT now () );

INSERT INTO orders ( customer_id , status , created_at ) SELECT ( random () * 10000 ):: int , CASE WHEN random () < 0 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.