Pitr_recovery
Chapter 45: Point-in-Time Recovery
Section titled “Chapter 45: Point-in-Time Recovery”Recovering to a Specific Point in Time
Section titled “Recovering to a Specific Point in Time”45.1 Understanding PITR
Section titled “45.1 Understanding PITR”Point-in-Time Recovery (PITR) allows you to recover to any point in time using base backup + WAL archives.
PITR Process ========================================================================
┌─────────────────────────────────────────────────────────────────────┐ │ Point-in-Time Recovery │ │ │ │ Components: │ │ ─────────── │ │ 1. Base backup - Full database snapshot │ │ 2. WAL archives - Transaction logs │ │ 3. Recovery target - Time or transaction ID │ │ │ │ Process: │ │ ─────── │ │ 1. Stop PostgreSQL │ │ 2. Restore base backup │ │ 3. Configure recovery │ │ 4. Start PostgreSQL │ │ 5. PostgreSQL replays WAL up to target │ │ │ └─────────────────────────────────────────────────────────────────────┘45.2 Enabling WAL Archiving
Section titled “45.2 Enabling WAL Archiving”-- postgresql.confwal_level = replicaarchive_mode = onarchive_command = 'cp %p /wal_archive/%f'archive_timeout = 300 -- 5 minutes
-- Or use compressionarchive_command = 'gzip < %p > /wal_archive/%f.gz'45.3 Performing PITR
Section titled “45.3 Performing PITR”# 1. Stop PostgreSQLsudo systemctl stop postgresql
# 2. Clean data directoryrm -rf /var/lib/postgresql/15/main/*
# 3. Restore base backuppg_basebackup -h primary -D /var/lib/postgresql/15/main -P -U replicator
# 4. Create recovery configurationcat > /var/lib/postgresql/15/main/postgresql.auto.conf << EOFrestore_command = 'gunzip < /wal_archive/%f.gz > %p'recovery_target_time = '2024-06-15 14:30:00'recovery_target_action = 'promote'EOF
# 5. Create recovery signal filetouch /var/lib/postgresql/15/main/recovery.signal
# 6. Start PostgreSQLsudo systemctl start postgresql45.4 Monitoring Recovery
Section titled “45.4 Monitoring Recovery”-- Check recovery statusSELECT pg_is_in_recovery();
-- Check recovery progressSELECT now() - pg_last_xact_replay_timestamp() as replication_lag;
-- Check timelineSELECT * FROM pg_current_wal_lsn();Summary
Section titled “Summary”| Component | Purpose |
|---|---|
| Base backup | Initial full copy |
| WAL archives | Transaction logs |
| Recovery target | Point to restore to |