Point-in-time recovery (PITR)
Overview
PITR restores a base backup and replays an unbroken archive of recovery records to a selected target, for example before an accidental change. PostgreSQL requires a suitable base backup, required WAL and recovery configuration; timelines and target inclusivity matter. A logical dump alone is not a physical PITR base backup.
First example
SELECT a.id,a.balance+COALESCE(SUM(e.delta),0) FROM ad_account a LEFT JOIN ad_event e ON a.id=e.account AND e.at_s<=20 GROUP BY a.id,a.balance ORDER BY a.id;Observed SQLite result (rows in JSON):
[ [ 1, 125 ], [ 2, 180 ]]How it works
The fixture models a base state at second zero and three committed application events at seconds 10, 20 and 30. The query reconstructs the account state through second 20 inclusive; the last event is deliberately excluded. This is a logical teaching model, not WAL replay; real recovery targets may use commit time or LSN and require isolated restore and verification before service cutover.
Practical example
SELECT a.id,a.balance+COALESCE(SUM(e.delta),0) FROM ad_account a LEFT JOIN ad_event e ON a.id=e.account AND e.at_s<=30 GROUP BY a.id,a.balance ORDER BY a.id;Observed SQLite result (rows in JSON):
[ [ 1, 75 ], [ 2, 180 ]]Practice and mistakes
Practice: reconstruct the state through second 30 and compare with second 20. Hint: change the event cutoff inside ON, preserving accounts without events. Check: account 1 changes from 125 to 75; account 2 stays 180. Mistake: assuming current WAL files alone provide unlimited recovery history.
PostgreSQL 17 example — NOT RUN
SELECT pg_is_in_recovery(),pg_last_wal_replay_lsn(),pg_last_xact_replay_timestamp();Expected result, not executed: "Recovery status and replay positions; NULL may occur. Not a recovery operation."