Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

A PostgreSQL table receives 20,000 updates per second to a last_seen_at column. It has 9 indexes, one of them on last_seen_at. WAL volume and replica lag are high. Explain the mechanics and what you would change.

Because last_seen_at is indexed, no update to it can be a HOT update, so each of the 20,000 updates per second writes a new row version plus a new entry in all 9 indexes, each with its own WAL. That is what drives WAL volume and replica lag. If that index is not essential, we drop it and lower fillfactor so most updates become HOT, we measure the HOT ratio in pg_stat_user_tables, and for more relief we move last_seen_at to a narrow table or batch the updates in memory or Redis.

Why this question is asked

A single timestamp column that records when a user was last seen sounds harmless. At 20,000 updates per second it can dominate the write load of an entire database and its replicas. Interviewers ask this because it tests whether we understand PostgreSQL's update mechanics, especially heap-only tuples, and whether we can pick the cheap fix before the expensive one. In this answer, we will recap how PostgreSQL updates a row, see why the index on last_seen_at multiplies the work by nine, make updates HOT with fillfactor, measure the HOT ratio, and look at structural changes.

1A Quick Recap of PostgreSQL Updates and HOT

PostgreSQL never overwrites a row in place. An UPDATE writes a new version of the whole row, called a tuple, and marks the old one dead for vacuum to remove later. Normally the new version has a new address, so every index on the table needs a new entry pointing to it.

HOT, heap-only tuple, is the important exception. If the update changes no indexed column and the new version fits on the same heap page, PostgreSQL links the old version to the new one inside the page and leaves all indexes untouched. Index lookups arrive at the old slot and follow the chain. A HOT update writes one heap change and its WAL, instead of one heap change plus one entry per index.

2Why One Indexed Column Multiplies the Work by Nine

Because last_seen_at has an index, every update to it changes an indexed column, so HOT is impossible. PostgreSQL must insert a new entry in all 9 indexes, not just in the one on last_seen_at, because the new tuple has a new address that every index must point to. Each index insertion is a B-tree change with its own WAL record, often including a full page image after a checkpoint.

So 20,000 updates per second become 20,000 heap writes and 180,000 index insertions per second. The WAL stream carries all of that to every replica, which must replay it in a single process, so replicas fall behind. Vacuum then has to clean dead tuples and dead index entries in nine indexes, and the indexes bloat.

One update of last_seen_at
Index on last_seen_at (now)Index dropped, page has room
Update typenon-HOTHOT
Heap writes1 new tuple, maybe on another page1 new tuple on the same page
Index entries written90
WAL recordsabout 10about 1
Vacuum workdead heap tuple + 9 dead index entriespruned on the page

3Making Updates HOT Again

The first question is whether the index on last_seen_at is needed. Often it exists for an admin query such as 'users seen in the last 5 minutes', which might be served another way, or is used rarely. We check its scan count in pg_stat_user_indexes. If we can drop it, the updates no longer touch an indexed column.

HOT also needs free space on the same page. A table filled to 100 percent has no room for the new version, so it goes to another page and the update is not HOT. Fillfactor reserves space: with fillfactor 80, inserts fill pages only to 80 percent, leaving 20 percent for updated versions. Old versions on the page are pruned during normal page access, so the space is reused.

SQL
SELECT idx_scan FROM pg_stat_user_indexes WHERE indexrelname = 'users_last_seen_at_idx'; -- 14 in 30 days
DROP INDEX CONCURRENTLY users_last_seen_at_idx;
ALTER TABLE users SET (fillfactor = 80); -- applies to new pages
-- existing pages keep their current fill until rewritten, for example with pg_repack
SELECT n_tup_upd, n_tup_hot_upd,
round(100.0 * n_tup_hot_upd / nullif(n_tup_upd, 0), 1) AS hot_pct
FROM pg_stat_user_tables WHERE relname = 'users';
-- before: n_tup_upd 1200000000 n_tup_hot_upd 3000000 hot_pct 0.3
-- after: hot_pct 93.5 (counters reset or compared over the same interval)

So the HOT percentage is the measurement that tells us the change worked. We compare it over the same interval before and after, for example by sampling the counters an hour apart. We also expect WAL generation per second, visible through pg_stat_wal or the WAL position over time, and replica lag to drop sharply.

4Changing the Shape of the Data

If the index is needed, or if even HOT updates are too many, we change the design. One option is to move last_seen_at into its own narrow table, user_activity(user_id, last_seen_at), with only a primary key. Updates then rewrite a tiny tuple and touch one index, and the wide users table with its 9 indexes is left alone. The admin query gets its index on the small table, where it is cheap.

A stronger option is to stop writing every event. Nobody needs last_seen_at to the millisecond. The service can record activity in memory or in Redis and flush the latest value per user once a minute in a batch. If 20,000 updates per second come from 200,000 active users, a one-minute flush writes 200,000 rows a minute, about 3,300 per second, and often far fewer, because many updates are the same user again.

Step throughChoosing the fix
  1. Measure HOT ratio and index scanspg_stat_user_tables, pg_stat_user_indexes
  2. Index on last_seen_at needed?
    YesMove the column to a narrow tableNoDrop it, set fillfactor 80
  3. Still too many writes?
    YesBatch in memory or Redis, flush each minuteNoDone
  4. Re-measure WAL rate and replica lagand vacuum time

4 steps in order. Play them, or step through them one at a time.

5Side Benefits of Fewer Indexes

Every index we remove also reduces vacuum work, because vacuum scans each index to remove dead entries. It reduces bloat, backup size and replica storage, and it gives the buffer cache more room for the data that queries read. Replica lag improves both because there is less WAL and because replay is simpler.

6Follow-up questions

What is fillfactor and what does it cost?

Fillfactor is a per-table setting, from 10 to 100 percent, for how full PostgreSQL fills each heap page on insert. The default is 100. A lower value leaves room on every page so updated versions can stay on the same page, which is a condition for HOT updates. The cost is space: with fillfactor 80, the table needs about 25 percent more pages for the same rows, so sequential scans read more pages and the cache holds fewer rows. It pays off for tables with frequent updates and is wasted on append-only tables.

Does HOT help if the page is full but no indexed column changed?

No. The new version must go to another page, which needs new index entries, so the update is not HOT. That is why dropping the index alone is not always enough and fillfactor matters. Page pruning frees space from old versions during normal access, so a moderately lowered fillfactor usually keeps the HOT ratio high.

7Conclusion

In this answer, we saw that an update to an indexed column can never be HOT, so each of the 20,000 updates per second wrote new entries in all 9 indexes, plus WAL for each, which drove WAL volume and replica lag. Dropping the index on last_seen_at if it is not essential, and lowering fillfactor so new versions fit on the same page, makes most updates HOT, and n_tup_hot_upd versus n_tup_upd shows the result. For more relief, we can move the column to a narrow table or batch the updates in memory or Redis, and fewer indexes also reduce vacuum, bloat and replica lag.

What to say in the interviewAbout 90 seconds aloud

Open with

In PostgreSQL an update always writes a new row version. If no indexed column changed and there is room on the same page, it can be a HOT update, and then no index is touched.

Then, if they want more

Here last_seen_at itself is indexed, so none of these updates can be HOT, and each one inserts a new entry in all nine indexes, not just the timestamp index, because the new version has a new location. That is 180,000 index insertions per second plus WAL for each, which is exactly what fills the WAL stream and makes replicas lag.

My first step is to check whether that index is used, in pg_stat_user_indexes. If it is not essential, I drop it concurrently and set the table's fillfactor to around 80 so new versions fit on the same page.

Then I measure the HOT ratio, n_tup_hot_upd over n_tup_upd in pg_stat_user_tables, over the same interval, and expect it to go from near zero to well over 90 percent, with WAL rate and lag falling. If the index is needed, I would move last_seen_at into a narrow side table with just user_id and the timestamp.

And because nobody needs it exact to the millisecond, the strongest fix is to collect it in memory or Redis and flush the latest value per user once a minute. Fewer indexes also mean less vacuum work and less bloat.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A PostgreSQL table receives 20,000 updates per second to a last_seen_at column. It has 9...": https://scrivio-interview-library.netlify.app/databases/indexing/1faeed449d/