Design for the query
The endpoint filters by tenant and orders by creation time. Build an index that supports both operations, then check what it costs.
You will learn to compare an index's search condition with a residual filter, choose useful column order for a query with an equality filter and a sort, and distinguish index access from index-only access. Read query plans first.
The page loads a fresh order dataset automatically. Select Run SQL in each numbered editor in order. The editors share one database, so later experiments use changes from earlier ones.
Sections on this page
Establish the baseline
Run the plan before creating any indexes. Keep the output visible so you can compare the scan, sort, and rows inspected with the next experiment.
Save the scan type, Rows Removed by Filter, and whether a Sort appears. This baseline shows what the primary key alone can do for this query.
Index the filter
Predict: If the index contains only tenant_id, which work disappears and which work remains?
Look for Index Cond identifying the tenant. An index condition constrains the entries read
through the index. A filter tests candidate rows after retrieval. A bitmap index scan may locate
matching tuple positions, then a bitmap heap scan obtains their table pages. An ordinary index scan
is another valid path. The planner chooses between them using its estimates.
The index helps find the tenant, but it does not store that tenant's rows in creation-time order. Look for the remaining sort. PostgreSQL still needs the qualifying rows before choosing the newest twenty.
Match the filter and order
A B-tree stores entries in key order. In a multicolumn B-tree, the first key orders groups and the
next key orders entries within each group. With equality on tenant_id, the following index puts
that tenant's orders in the requested creation-time order.
Predict: Where can execution stop now? Compare the rows emitted by the access node before and after creating the index.
Look for the tenant in Index Cond, no explicit sort, and an index scan stopped by the limit.
Those observations explain the improvement without depending on a timing threshold. If your
output differs, inspect indexes and restore a fresh database before replaying the steps.
The DESC spelling documents the intended order. For this query, an ascending index on both
columns can also work: PostgreSQL can scan a B-tree backward within the equality-constrained tenant.
Mixed sort directions across multiple unconstrained keys require more care.
Column order is a workload decision. An index beginning with creation time could provide global time order, then filter out other tenants. That can inspect many entries before finding enough matches. Later PostgreSQL versions can use additional strategies, including skip scans in suitable cases; the useful question remains how much work the observed plan performs.
Try a covering index
An ordinary index scan usually retrieves table rows to return columns missing from the index.
INCLUDE adds non-key columns as payload without changing the index's search order. An index with
all the query's columns is often called a covering index.
Predict: Is containing every selected column sufficient to avoid every heap visit? The heap is the table storage holding row versions. Index entries do not carry all transaction visibility information.
Select Run SQL in the editor containing only VACUUM. PostgreSQL requires this command
outside a transaction, and several statements sent together can share a transaction.
Now inspect the plan against the vacuumed table.
VACUUM can mark heap pages all-visible when their row versions are visible to all relevant
transactions. This information lives in the visibility map. An index-only scan uses those bits
to decide when it can skip heap visibility checks. Find Heap Fetches in the output if the planner
chooses an index-only scan. On the fresh, vacuumed fixture, heap fetches should be absent or low.
The next experiment shows why that can change.
Updates create row versions and clear all-visible bits on affected pages. An index-only plan may now need heap fetches. A planner may also switch paths. Containing all returned columns makes index-only access possible; visibility and cost determine how much table access it avoids.
Inspect the storage bill
Every extra index occupies space and must be maintained when its indexed values change. Including
total_cents makes updates to that value update the index too. It can also prevent HOT updates,
a PostgreSQL optimization that avoids new index entries under particular conditions.
These values describe your current fixture, including the primary-key index in the total. They are not a production capacity estimate. Compare the index's purpose with its storage and write cost before recommending it.
Explain the recommendation
Recommend an index for the support endpoint. State the equality predicate, required ordering, expected early stop, and whether the extra payload is worth testing on the real write workload. Then explain why the tenant-only index left a sort in the plan.
Hint: separate three jobs
Ask which keys find the tenant, which keys order its matches, and where the selected values live. Then ask how PostgreSQL knows those values are visible to the transaction.
Worked recommendation
The (tenant_id, created_at DESC) index supports the equality search and requested order. The
executor can return twenty entries and stop without sorting all the tenant's orders. The tenant-only
index narrows the input but leaves ordering work.
Including id and total_cents allows index-only access because all selected columns are present.
The vacuumed fixture can skip heap fetches where visibility-map bits permit it. Frequent updates
can reduce that benefit while increasing index maintenance, so a covering index needs workload
validation. Keep the smaller composite index as a candidate rather than assuming the larger one wins.
Go deeper: how the engine works
What work does an ordered index avoid, and why can a top-N sort still consume all candidates? Read Sorting and top-k (opens in a new tab) in Query Engine Book. Its DuckDB examples and teaching engine explain the general principle; PostgreSQL's operators and implementation details can differ. This reading is optional.
Transfer the idea
Use the indexes already built on this page. In SQL 01, replace tenant_id = 42
with status = 'pending', then select Run SQL. Record the scan and whether the plan sorts.
In the same editor, insert this line before EXPLAIN and run it again.
CREATE INDEX orders_pending_time ON orders (created_at DESC) WHERE status = 'pending';
Compare the scan and sort again. Does the index let PostgreSQL find the newest pending orders without sorting them? Each change to an indexed order can add write work.
What this cannot tell you
A single-user browser experiment does not measure lock contention, write throughput, cache pressure across many queries, or production disk behavior. The next chapter, Pay for your indexes, investigates index alternatives and maintenance in more depth.
Takeaway: Match an index to the query's search and ordering requirements, then account for visibility, storage, and writes. A useful index still depends on useful estimates. Diagnose an estimate →.
Primary sources: Multicolumn indexes, indexes and ordering, and index-only scans.