Databases / Performance
Read a PostgreSQL plan before adding an index
Use EXPLAIN ANALYZE and buffer counts to test a multicolumn B-tree against a realistic event-booking query.

A registration dashboard has to list the newest bookings for a single event. Adding an index on every column would make inserts more expensive and would still leave the database with several possible execution plans. Start with the query that matters and use its plan to decide what one index should accomplish.
Build a test with uneven traffic
The example below creates a test table. Three in every four rows belong to the same event. The rest go to a hundred smaller events. The data is synthetic so you can change its distribution without depending on somebody else's timings.
CREATE TEMP TABLE booking_probe (
id bigint PRIMARY KEY,
event_id integer NOT NULL,
created_at timestamptz NOT NULL,
display_name text NOT NULL
);
INSERT INTO booking_probe
SELECT n,
CASE WHEN n % 4 = 0 THEN 2 + ((n / 4) % 100) ELSE 1 END,
timestamptz '2026-01-01T00:00:00Z' + n * interval '1 second',
'Attendee ' || n
FROM generate_series(1, 120000) AS sample(n);
ANALYZE booking_probe;
Find the latest 30 registrations for a small event. Sorting by ID after the timestamp makes the order deterministic even when two real registrations have the same timestamp.
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, created_at, display_name
FROM booking_probe
WHERE event_id = 42
ORDER BY created_at DESC, id DESC
LIMIT 30;
Read the tree from the bottom up
The scan at the bottom determines how PostgreSQL gets candidate rows. A sequential scan can be perfectly reasonable for a small table or a query that reads most of it. A Filter applies to rows a node has already pulled in. An Index Cond can instead constrain the index scan. A sort node tells you the incoming rows were not already in the requested order.
Compare estimated rows to actual rows. The values are per execution of the node, so inspect loops when a child runs repeatedly. An estimate that misses by a large factor can point to stale statistics or a correlation the planner does not model. Planner costs represent units the optimizer uses to compare plans. Actual execution times are milliseconds. These are different measures. PostgreSQL guide to EXPLAIN
Running EXPLAIN ANALYZE executes the statement. Use ordinary EXPLAIN to inspect a write without making changes, or reproduce it in an isolated database. Look for buffer reads and hits as well as elapsed time. A second run can hit warmer caches, so label both runs when recording results.
Make an index answer a question
CREATE INDEX booking_probe_recent_event_idx
ON booking_probe (event_id, created_at DESC, id DESC);
Rerun the same plan. An equality constraint on the leading event_id identifies the relevant region of the B-tree. Within that region, the next two index keys match the requested order. An index scan can stop after finding the 30 rows without sorting all bookings for that event. display_name still needs to be fetched from the table, which is a reasonable trade for a short result. PostgreSQL multicolumn indexes
Change the query to return all bookings for event 1 without a limit. Predict which result will favour sequential access before measuring. Then test a query that requests all events, sorted by timestamp alone. The new index orders timestamps within each event, so it cannot directly supply that global order. PostgreSQL indexes and ORDER BY
Write down the decision
Record the query, parameters, representative data volume, plan and buffer observations. Check a common parameter as well as a rare one. If the index ships, record the read it supports so future maintainers can judge whether the added write and storage costs remain worthwhile.
Cover illustration by ACM MPSTME.