5 PostgreSQL Indexing Mistakes That Quietly Slow Down Queries
Adding an index in PostgreSQL feels like an instant performance fix, right up until a query still takes hundreds of milliseconds in production. Creating an index does not guarantee the query planner will use it.
Here are five common indexing mistakes that force PostgreSQL back into sequential scans or unnecessary heap lookups, along with how to spot and fix each one using EXPLAIN ANALYZE. All the runnable SQL from this post is on GitHub at itsmill/postgres-indexing-examples.
Test Table Setup
To see the planner decisions clearly, create an orders table with 200,000 rows where 5% of orders are pending and the rest are completed:
CREATE TABLE orders ( id BIGSERIAL PRIMARY KEY, customer_id INT NOT NULL, email TEXT NOT NULL, status TEXT NOT NULL, total_amount NUMERIC(10, 2) NOT NULL, created_at TIMESTAMPTZ NOT NULL ); INSERT INTO orders (customer_id, email, status, total_amount, created_at) SELECT (random() * 50000)::INT + 1, 'user' || g || '@example.com', CASE WHEN g % 20 = 0 THEN 'pending' ELSE 'completed' END, round((random() * 500)::NUMERIC, 2), NOW() - (random() * INTERVAL '365 days') FROM generate_series(1, 200000) AS g; ANALYZE orders; 1. Wrapping an Indexed Column in a Function
Suppose you index created_at and query all orders from November 2026:
CREATE INDEX idx_orders_created_at ON orders (created_at); EXPLAIN ANALYZE SELECT id, total_amount FROM orders WHERE EXTRACT(YEAR FROM created_at) = 2026 AND EXTRACT(MONTH FROM created_at) = 11; Even with the index in place, EXPLAIN ANALYZE shows a Seq Scan. Because created_at is wrapped inside EXTRACT(), Postgres cannot traverse the B-tree directly and has to evaluate the function on every single row.
Rewrite the filter as an explicit range so the column stays untouched:
EXPLAIN ANALYZE SELECT id, total_amount FROM orders WHERE created_at >= '2026-11-01 00:00:00+00' AND created_at < '2026-12-01 00:00:00+00'; Now the planner switches to an Index Scan (or Bitmap Index Scan) and reads only the matching slice of rows.
2. Case-Insensitive Lookups on a Plain Index
A standard B-tree index on email is case-sensitive. If your application normalizes user input with LOWER(), Postgres ignores the plain index:
CREATE INDEX idx_orders_email ON orders (email); -- Falls back to a Seq Scan EXPLAIN ANALYZE SELECT id, status FROM orders WHERE LOWER(email) = 'user1000@example.com'; Fix this by creating an expression index that matches the exact predicate your queries run:
CREATE INDEX idx_orders_lower_email ON orders (LOWER(email)); EXPLAIN ANALYZE SELECT id, status FROM orders WHERE LOWER(email) = 'user1000@example.com'; With the expression index in place, the query drops from a full table scan to a sub-millisecond index lookup.
3. Putting the Range Column First in a Composite Index
Column order matters in multi-column B-tree indexes. If you filter by exact status and a created_at date range, putting created_at first forces Postgres to scan through every status within that 30-day window:
CREATE INDEX idx_orders_created_status ON orders (created_at, status); EXPLAIN ANALYZE SELECT id, total_amount FROM orders WHERE status = 'pending' AND created_at >= NOW() - INTERVAL '30 days'; Flip the column order so the equality column (status) comes first and the range column (created_at) comes second:
DROP INDEX idx_orders_created_status; CREATE INDEX idx_orders_status_created ON orders (status, created_at); EXPLAIN ANALYZE SELECT id, total_amount FROM orders WHERE status = 'pending' AND created_at >= NOW() - INTERVAL '30 days'; Postgres can now jump straight to the pending section of the B-tree and walk only the recent timestamps.
4. Indexing an Entire Skewed Column
In our sample table, 95% of rows are completed and only 5% are pending. If background workers only ever poll pending orders, indexing all 200,000 rows wastes disk space, slows down writes, and bloats memory.
Use a partial index with a WHERE clause so Postgres only indexes the 5% of rows you actually care about:
CREATE INDEX idx_orders_pending_only ON orders (created_at) WHERE status = 'pending'; EXPLAIN ANALYZE SELECT id, customer_id FROM orders WHERE status = 'pending' AND created_at >= NOW() - INTERVAL '7 days'; As long as the query includes status = 'pending', Postgres uses the tiny partial index instead of maintaining entries for hundreds of thousands of completed orders.
5. Skipping Covering Indexes on Frequent Read Queries
If a high-traffic endpoint looks up total_amount and status by customer_id, a basic index on (customer_id) still forces Postgres to jump back to the main table heap to fetch those two extra columns:
CREATE INDEX idx_orders_customer ON orders (customer_id); EXPLAIN ANALYZE SELECT total_amount, status FROM orders WHERE customer_id = 4200; You can avoid the heap fetches by attaching non-key payload columns with INCLUDE:
DROP INDEX idx_orders_customer; CREATE INDEX idx_orders_customer_cover ON orders (customer_id) INCLUDE (total_amount, status); VACUUM ANALYZE orders; EXPLAIN ANALYZE SELECT total_amount, status FROM orders WHERE customer_id = 4200; Once the visibility map is updated via VACUUM, EXPLAIN ANALYZE reports an Index Only Scan with Heap Fetches: 0, serving the entire query directly out of the index.
Checking Before You Ship
Indexes speed up reads at the cost of slower writes and extra storage. Before shipping a migration, always run EXPLAIN (ANALYZE, BUFFERS) against realistic row counts in staging to confirm the planner actually picks your index and drops total buffer reads.