Point-in-Time Recovery (PITR) allows you to restore a database to any moment in time, providing protection against data loss from accidental deletions, corruption, or application bugs.

How PITR Works

PITR relies on three components working together:

  1. Base Backup: A full copy of the database cluster
  2. WAL Archives: Continuous stream of all changes since the backup
  3. Recovery Target: The exact point you want to restore to
[Base Backup] → [WAL1] → [WAL2] → ... → [WALn] → [Recovery Target]
     ↑                                              ↑
  Starting point                               Destination

Recovery Granularity

PITR supports multiple recovery targets:

Target TypeUse CasePrecision
recovery_target_timeRestore to specific timestampMicrosecond
recovery_target_xidRestore to transaction IDTransaction
recovery_target_nameRestore to named pointCustom
recovery_target_lsnRestore to WAL positionByte-level

Prerequisites for PITR

ini
# postgresql.conf
wal_level = replica
archive_mode = on
archive_command = 'cp %p /archive/%f'

Creating Named Restore Points

sql
-- Create a named restore point before risky operations
SELECT pg_create_restore_point('before_schema_migration');

-- Perform migration...
ALTER TABLE orders ADD COLUMN new_field TEXT;

-- If something goes wrong, you can restore to this point

PITR vs Traditional Backup

FeatureTraditional BackupPITR
Recovery PointBackup time onlyAny point in time
Data Loss WindowHours/daysSeconds
StorageFull backupsBase + incremental WAL
ComplexitySimpleModerate

📖 Continuous Archiving and PITR

✓ Completed