Learn SQL
-- security.backup_pitr_restore

Backups, PITR & Restore Drills in SQL

Untested backups are Schrödinger backups

--concept

Logical dumps (pg_dump) for portability and small DBs; physical base backups + WAL archiving for point-in-time recovery on large systems. RPO/RTO targets pick strategy: dumps for simplicity, PITR for tight windows. Restore drills (not backup logs) prove recoverability — rehearsed restores to staging on cadence.

--how it works
1

Dumps for portable

pg_dump custom format — small DBs, migrations, per-table restores.

2

Base + WAL for PITR

Physical base + streaming WAL — any-moment replay for large systems.

3

Write RPO/RTO

Loss tolerance + speed target per dataset — strategy follows numbers.

4

Drill restores

Monthly staging restores, timed + documented. Keys backed separately.

--visualization
base + replay WAL to '2024-06-01 14:29' (mid-day rescue)RPO minutes — replays to just before DROP
Stage 1 / 1
Untested backups are Schrödinger backups — monthly staging restores, timed.
--example
query.sql
1-- logical:
2pg_dump -Fc app > app.dump
3-- PITR target:
4-- restore base + replay WAL to '2024-06-01 14:30'
Result
strategyfit
dumpsportable, small-scale
PITRbase + WAL, any moment

Dump restores whole snapshots simply. PITR replays the log to just before the accident — the only answer for mid-day drops on large tables.

--variants

Pick by numbers

Targets choose tooling, not habit.

query.sql
1-- RPO 24h + RTO hours → nightly pg_dump
2-- RPO minutes → base + WAL streaming + PITR
Result
strategywindow
dumpssimple, coarse
PITRtight RPO/RTO
1Small blog: dump suffices.
2Payments ledger: minutes matter — WAL required.
3Write both numbers before buying tooling.

Mid-day DROP rescue

Replay to just before the accident.

query.sql
1-- restore base + replay WAL to '2024-06-01 14:29'
Result
targetpoint
PITR targetone minute before DROP
1Base restores the morning snapshot.
2WAL replays to the safe instant.
3Dumps alone could only return last night.
--key facts
RPO/RTOloss + speed targets pick strategy
Drillsrestores tested, not assumed
Keys separateencrypted backups need key custody
--use cases
Ledger DR: tight-PITR with rehearsed runbooks
Migration safety nets: pre-DDL dumps
--under the hood
WAL archiving costs I/O + storage — size from write rate × retention; monitor replay lag.
Encrypted backups need key custody drills — lost keys equal lost backups.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.