Quick revision
The short answer
The nightly rewrite of a 300 GB table produces far more write-ahead log than the cross-region link and the remote replay can carry, so lag climbs to 20 minutes and any customer write made during the batch window could be lost in a disaster. We fix it by changing the job to update only rows that really changed, in small committed chunks paced by the measured lag, by asking whether the derived data needs replicating at all, and by reducing the bytes per change with WAL compression and checkpoint tuning. Then we measure RPO continuously with a heartbeat and alert on it like any other compliance metric.
What to remember
In this answer, we saw that rewriting a 300 GB table floods the WAL stream faster than the cross-region link and the replica can absorb, leaving customer writes exposed for up to 20 minutes. The strongest fixes change the job: update only rows that changed, in small chunks paced by measured lag, and keep derived data out of replication where it can be rebuilt. WAL compression, fewer checkpoints and a replica sized for peak replay cut the rest, and a heartbeat-based lag metric with alerts makes RPO something we measure every minute.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The nightly rewrite of a 300 GB table produces far more write-ahead log than the cross-region link and the remote replay can carry, so lag climbs to 20 minutes and any customer write made during the batch window could be lost in a disaster. We fix it by changing the job to update only rows that really changed, in small committed chunks paced by the measured lag, by asking whether the derived data needs replicating at all, and by reducing the bytes per change with WAL compression and checkpoint tuning. Then we measure RPO continuously with a heartbeat and alert on it like any other compliance metric.
Why this question is asked
A recovery point objective, RPO, is a promise about how much data we may lose in a disaster: here, at most 60 seconds of writes. An asynchronous replica in another region meets it most of the day, but a promise that holds except during the batch window is not a promise. Interviewers ask this scenario because the obvious fixes, a bigger link or a bigger replica, treat the symptom, while the cause is a job that creates enormous replication traffic for little real change. In this answer, we will see why lag builds, change the job first, question whether the data needs replicating, cut the bytes per change, and set up measurement so we never discover a broken RPO during an outage.
1Why the Batch Breaks the RPO
PostgreSQL replicates by shipping its write-ahead log (WAL), the record of every change, to the replica, which replays it. Every updated row produces WAL for the new row version and its index entries, and the first change to each page after a checkpoint also writes a full copy of the 8 KB page, called a full-page image, so that recovery can repair torn pages.
Rewriting a 300 GB table therefore generates WAL in the same order of magnitude, often several hundred gigabytes once indexes and full-page images are counted. If the batch produces WAL at, say, 400 MB per second for 30 minutes, but the link and the replica's replay can handle only 150 MB per second, the backlog grows by 250 MB every second. That is where the 20 minutes of lag comes from.
The danger is not the batch data itself. It is the customer orders written during the window, which sit in the same WAL stream behind the batch. If the primary region is lost at minute 25, those orders have not reached the replica yet, so the actual RPO at that moment is 20 minutes, twenty times the target. And because all that WAL crosses regions, the transfer bill grows with it.
| Minute | WAL produced | WAL replayed remotely | Lag |
|---|---|---|---|
| 0 | 0 GB | 0 GB | seconds |
| 10 | 240 GB | 90 GB | about 6 min of writes |
| 20 | 480 GB | 180 GB | about 13 min |
| 30 (job ends) | 720 GB | 270 GB | about 20 min, then slowly drains |
2Fix 1: Change the Job, Not the Link
Most nightly rewrites rewrite rows that did not change. If the job recomputes 300 GB but only 3 percent of the values differ, writing just those rows cuts WAL by roughly thirty times. The usual technique is to compute the new values into a staging table and update only rows where the value is distinct from the current one.
Then we write in small chunks, each its own transaction of, for example, 10,000 rows, and pace the chunks by measured lag. If the replica falls more than 30 seconds behind, the job pauses until it catches up. This turns a burst the replica cannot absorb into a steady stream it can, and it keeps the RPO inside target throughout. The job takes longer, perhaps two hours instead of thirty minutes, which is usually fine at night.
3Fix 2: Does This Data Need Replicating?
A table that the batch rewrites every night is usually derived data: aggregates, scores or a reporting copy computed from other tables. If it can be rebuilt from source data, it does not need to cross the region link at all. We can move it to a separate database or schema that is not part of the replicated cluster, or rebuild it in the recovery region from the replicated source tables after a failover.
In PostgreSQL, an UNLOGGED table writes no WAL and is not replicated, which fits data we can recompute, with the catch that it is emptied after a crash, so recovery must include rebuilding it. This one decision can remove the whole problem, and it is worth asking before tuning anything.
4Fix 3: Fewer Bytes per Change
Whatever still has to replicate should be compact. wal_compression compresses full-page images in the WAL (PostgreSQL 15 and later support lz4 and zstd as well as pglz), which often shrinks a bulk-update WAL stream substantially. Fewer full-page images helps too: a longer checkpoint_timeout and a larger max_wal_size mean fewer checkpoints during the batch, so each page gets its full-page image less often, at the cost of a longer crash recovery.
On the network side, compressing the replication or WAL-archive path, for example with a compressing tunnel or a backup tool such as pgBackRest that compresses archived WAL, reduces transfer cost. And the remote replica must be sized to replay at peak rate, not average rate: replay is single-threaded per server in PostgreSQL, so fast disks and enough memory on the replica matter.
5Measuring RPO Continuously
The final fix is to stop learning about the RPO during an outage. We insert a heartbeat row on the primary every second, for example the current timestamp into a one-row table, and in the recovery region compare that value with the local clock. The difference is the real data-loss window at that moment, measured end to end, including network and replay.
We alert when it exceeds 60 seconds for more than a minute, graph it next to the batch schedule, and report the share of minutes within target each month as a disaster recovery compliance figure. That makes the cost of a future careless batch visible on the day it ships rather than on the day we need the replica.
- Does the rewritten data need replicating?YesKeep it, go on to reduce volumeNoMove it out or rebuild in DR
- Update only changed rowsstaging table + IS DISTINCT FROM
- Chunk and pace by lagpause when lag exceeds 30 s
- Compress and checkpoint less oftenwal_compression, larger max_wal_size
- Size the remote replica for peak replay
- Heartbeat lag metric and alert at 60 sreport as a DR metric
6 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Why not just make replication synchronous during the batch?
Synchronous cross-region replication would make every commit, including customer orders, wait for a round trip and for the remote server to flush, and during the batch the remote side cannot keep up, so customer commits would slow down badly. It moves the pain from RPO to latency without fixing the cause, which is the volume of WAL.
Why measure with a heartbeat instead of replay_lag?
replay_lag is computed from the primary's view of what the standby has replayed, which is useful but can be misleading when there is little traffic or when the reporting itself is delayed. A heartbeat row written every second and read in the recovery region measures exactly what we would lose, end to end.
What if the batch truly must rewrite everything?
Then pacing is mandatory, and we size the link and the replica for the paced rate. We might also schedule the job when customer traffic is lowest and accept a longer run time, since the RPO target matters more than finishing in thirty minutes.
7Conclusion
In this answer, we saw that rewriting a 300 GB table floods the WAL stream faster than the cross-region link and the replica can absorb, leaving customer writes exposed for up to 20 minutes. The strongest fixes change the job: update only rows that changed, in small chunks paced by measured lag, and keep derived data out of replication where it can be rebuilt. WAL compression, fewer checkpoints and a replica sized for peak replay cut the rest, and a heartbeat-based lag metric with alerts makes RPO something we measure every minute.
Open with
The batch is generating far more WAL than the cross-region link and the remote replay can handle. Rewriting 300 gigabytes, with indexes and full-page images, produces hundreds of gigabytes of WAL in half an hour, the backlog grows, and lag reaches 20 minutes.
Then, if they want more
The real problem is that customer writes during that window sit behind the batch, so if we lost the primary region then, we'd lose up to 20 minutes of orders against a 60-second target. I'd fix the job first.
Usually most rows don't change, so I'd compute into a staging table and update only rows that differ, in chunks of maybe 10,000 rows, each in its own transaction, pausing whenever measured lag goes above 30 seconds.
Then I'd ask whether this table needs replicating at all; if it's derived, it can live outside the replicated database or be rebuilt in the recovery region. For what remains, I'd enable wal_compression, use lz4 or zstd on recent versions, checkpoint less often during the batch, and size the remote replica for peak replay.
Finally, I'd measure RPO with a heartbeat row written every second and compared in the recovery region, alert above 60 seconds, and report it monthly as a DR compliance metric.
About this answer
- Last updated
- 13 November 2026
- Versions it names
- PostgreSQL 10, PostgreSQL 15
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. 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 "How would you bring the RPO back within target during the batch window and control the...": https://scrivio-interview-library.netlify.app/architecture/partitioning-and-replication/292decc682/