Skip to content
dbexplore

What an index costs on every write

Every index is paid for on every write, in WAL, in replica replay and in storage. A way to put a price on one before you build it, or before you keep it.

6 min read

Ask a team what an index costs and you will be told its size. That is the easiest number to get and the least interesting one. A gigabyte of index on a table nobody writes to costs a gigabyte. A gigabyte of index on the busiest table in the cluster costs a gigabyte plus a share of every insert and update that table will ever receive, billed by whichever parts of your platform happen to meter writes.

We think the right unit for pricing an index is not bytes at rest. It is bytes written per day.

What a write does to each index

An insert adds an entry to the table and then one entry to every index on it. An update does the same unless it qualifies as a heap-only tuple, which requires that no indexed column changed and that there is room on the page. Each of those entries is a change to an index page, and every change to a page is written to the write-ahead log first.

There is an amplifier on top. With full_page_writes on, which is the default, the first modification of a page after a checkpoint writes the whole page into WAL. Index pages touched at random are very likely to be touched for the first time since the last checkpoint, so a write that scatters across indexes pays for page images as well as for the entry. wal_compression can shrink those images, and it is off by default.

We measured the shape on a scratch table so you can reproduce it. PostgreSQL 18 with default settings, 500,000 rows of random keys, a checkpoint before each run, and the WAL position read before and after. Against a table with only a primary key, the same insert wrote roughly twice the WAL with two more indexes on it and nearly three times with four. Treat the ratio as an illustration rather than a constant. It depends on your key shapes and on how often the same index pages are touched between checkpoints.

You can take the same measurement on your own staging copy:

-- psql: read the WAL position, run the write load, read it again
SELECT pg_current_wal_lsn() AS before \gset
-- ... replay a representative slice of the write workload here ...
SELECT pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), :'before')) AS wal_written;

For the production view, pg_stat_statements carries wal_bytes and wal_fpi per statement, so the write statements that dominate your WAL are one ORDER BY away.

Who bills a WAL byte

The same byte is charged in four different places, and which of them are on your invoice depends on the platform.

Storage. An index occupies space, and what dropping it returns depends on how the platform bills. Aurora charges for the space you use in the cluster volume, and its documentation lists dropping indexes among the ways to cut that charge. On Amazon RDS you choose an allocated size and the documentation says you cannot deallocate space, so a dropped index frees room inside a volume you keep paying for until you move to a smaller one.

Write I/O. On Aurora’s Standard configuration you pay per million I/O requests, and the WriteIOPS metric is documented as roughly the number of log records the database generates. More index maintenance means more log records. On the I/O-Optimized configuration there is no per-request I/O charge, so this line is gone, and whether that configuration is cheaper overall is a separate break-even calculation.

Replicas. A physical standby applies all the WAL it receives from the primary, including every index change. Each replica pays for the replay in CPU and in lag, and a read replica sized up to keep pace with an index-heavy write load is a cost with no line item that says so.

Distance. If WAL leaves the region, the bill follows. For an Aurora global database, the primary region’s billing includes a metric for write I/O replicated to the secondary regions. Aurora also meters the backup storage that supports point-in-time restore within the retention window as a line of its own.

Notice what this does to the economics of dropping an unused index. On RDS the storage line may not move at all. The saving is in the other three, and those scale with how often the table is written, not with how big the index is.

Pricing one

Three numbers, all obtainable without a vendor.

First, the size, which you get by building it on a copy. Second, the number of index entries it will absorb per day. Postgres has no per-index write counter, so this is an estimate from the table’s counters: inserts plus the updates that were not heap-only.

SELECT relname,
       n_tup_ins + n_tup_upd - n_tup_hot_upd AS entries_per_index
FROM pg_stat_user_tables
ORDER BY 2 DESC
LIMIT 10;

That overcounts for partial indexes, which skip rows outside their predicate, and it ignores the vacuum work that removes dead entries later. The counters are cumulative, so run it twice a day apart and subtract to get a daily rate; a reset or a crash in between makes the second reading smaller, and then you start again. Third, the WAL the entries produce, which the staging measurement above gives you.

Then multiply by the chain that applies to you. With made-up rates, if an index adds 15 GB of WAL a day and the combined price of replaying, replicating and archiving it comes to 0.02 dollars per GB, the index costs 9 dollars a month in write overhead. Those are hypothetical numbers, so use your own, but notice how small the result can be. For a table with modest writes the maintenance bill is close to rounding error, and the right answer to “should we keep it” is usually yes.

The side of the ledger people forget

Everything above is the price. The value is the reads it saves, and the honest comparison needs both. An index that turns a scan of a large table into a lookup, run every few seconds, pays for itself many times over even on a write-heavy table. An index that served one report a year and sits on the hottest table you own does not.

That is where the opinion comes from. An unused index is clutter on a cold table and a running cost on a hot one, and the only thing that separates the two is write rate. Treating all unused indexes as equally urgent wastes the week you could spend on the three that matter.

The trade-off we take is accuracy for reach. Because Postgres exposes no per-index write counter, what we compute is an estimate from table counters and a measured WAL sample, and we say so beside the number. The alternative is to wait for a precise figure that the catalog will not provide. We would rather act on a labelled estimate that gets the ranking right. Finding the candidates is a different job, and the checks that stop you dropping something a quarterly job needs are set out in unused and missing indexes.

The table that takes the most writes

Find the table in your cluster that takes the most writes and count its indexes. Then, for each one, find the date it was last read. If you cannot say, that is the useful part. Somewhere on that table is an index whose cost has been rising with every release, and nobody has looked at its receipt.

Keep reading

· 6 min read

Your Aurora Serverless floor is billed all night

A minimum capacity set for the busiest hour is billed through the quiet ones too. How to see the floor you pay for, how to pick it, and what lowering it costs.

Read the post →

· 6 min read

The slot was lost at the last checkpoint

Postgres says a replication slot is in danger long before it dies, in a column almost nothing reads. By the time it says lost, the subscriber is gone.

Read the post →

· 7 min read

The invoice names an instance, not a query

A cloud invoice names an instance. Postgres can name the queries. How to split a bill across them with pg_stat_statements, and where the split is wrong.

Read the post →

Run this on your own fleet.

DBExplore watches every Postgres you run and acts only inside the guardrails you set. Early access is open in small batches.