Drilling a PostgreSQL Point-in-Time Restore to Just Before a Bad Bulk Approval

Replicas faithfully copy a bad change too. Restore a separate cluster from a base backup and WAL to the second before the incident, verify the restore point with three transactions (a valid approval, a wrong one, a later submission), then decide what goes back to production.

John Baek
John Baek
Founder, CollabOps
2 min read

At 2:03 p.m., a batch job approved every pending change request in one sweep. PostgreSQL's replicas are happily applying the same change. What you need now is a separate copy of the data as it was just before the bad transaction. Until you can tell valid approvals from wrong ones, you can't decide what point to take the production database back to.

Replication protects you from failures; point-in-time restore from a base backup and WAL is what brings you back from a bad change. Replicas follow the incident transaction faithfully.

How do you know you restored to the right point?

You check the restore point with three transactions straddling the incident. Prepare three of them in the drill database. The times below are when each DB transaction committed, not the request time shown on screen, and all are Korea Standard Time.

TimeWhat happenedExpected value at the target point
14:02:10Change request 731 approved correctly731 = approved
14:03:15The batch job wrongly approved pending request 732 too732 = pending
14:04:00New change request 733 submitted correctly733 does not exist yet

Have the base backup completed at 1 p.m. and an unbroken WAL archive running past the target time. WAL is the log in which the database records its changes. With continuous archiving and point-in-time recovery (PITR), PostgreSQL starts from a physical backup, replays WAL and stops at a chosen moment. If you run a Kubernetes PostgreSQL operator, you use this to restore into a new PostgreSQL cluster. In this example, the separate cluster changes-restore-drill gets a recovery target time of 2026-10-20T14:03:14+09:00, which corresponds to PostgreSQL's recovery_target_time. Record the base backup ID and the WAL range you needed alongside it.

Don't connect deployment workers or notification senders to the restored copy. Someone first reads the three rows, read-only. 731 approved, 732 pending, 733 absent. Only when all three match can you say you've recovered the state at the intended moment. Record both the time the database started accepting connections and the time these three checks finished.

Why not just promote the restored copy to production?

Cutting production over to the restored copy wholesale would erase the valid requests that arrived after the incident. That's why 733 is in the drill. The final decision goes a step beyond "the database is up": whether to apply only the correct state of 732 to production, or, if a full cutover is unavoidable, how to reconcile and recover 733 and every valid transaction after it.

How is this different from a node-failure test?

Node failure and data corruption are different tests. Switching off a node is covered separately in the 3-node HA failure test. (Times and request numbers are illustrative.)

  • #postgresql
  • #backup
  • #disaster-recovery
  • #on-prem
  • #sre