PostgreSQL indexes begin with a query
Use an order-history example to reason about selective lookups and the cost of maintaining an index.
Write down the access pattern
Suppose a dashboard repeatedly looks up orders by customer ID. Without a suitable index, PostgreSQL may scan the table to find matches. An index offers another way to locate rows, particularly when the query returns a small fraction of the table. Start with the query people actually run, rather than indexing every column.
Try one focused change
A simple customer lookup gives you a concrete candidate. Compare the query plan before and after adding the index, using data with a realistic distribution. The existence of an index does not guarantee the planner will choose it.
CREATE INDEX orders_customer_idx
ON orders (customer_id);
EXPLAIN
SELECT id, status
FROM orders
WHERE customer_id = 42;Account for the other workload
Indexes consume storage and add maintenance work when indexed data changes. A dashboard with many reads and a busy ingestion pipeline have different tradeoffs. Keep the index when measurements justify it, and review it as query patterns change. An index is a maintained data structure, not a free acceleration switch.
Use a realistic dashboard scenario
Imagine that a support dashboard opens the latest orders for one customer. Test a customer with very few orders and another with a long history. A dataset where every customer has exactly one row may conceal the behavior that matters in production. Write down the selected columns, filter, ordering, and page size before changing the schema. Keep the application query parameterized. An index experiment should improve a known access pattern; it should not require the application to retrieve thousands of rows and discard most of them afterward.
Compare evidence, not just plan names
EXPLAIN ANALYZE executes the statement and reports observed behavior alongside estimates. For a read query, compare the estimated and actual row counts and inspect buffer information to understand the work performed. Avoid declaring success merely because the plan contains the word Index. Run representative requests under similar conditions and record the data size and environment. One warm-cache execution is an observation, not a universal latency guarantee. Be deliberate with EXPLAIN ANALYZE on writes because the statement really runs.
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, status
FROM orders
WHERE customer_id = 42;Keep a change log for the index
For a proposed index, record the query it supports, the reason it was added, and the measurements that justified it. Include a migration and a rollback plan, and arrange production changes around the team's deployment process. After release, monitor both the dashboard and the write-heavy flows that maintain the same table. Revisit the decision when the product adds new filtering or sorting. A documented small improvement is easier to maintain than a growing collection of indexes whose original purpose nobody remembers.