Sub topic 2
Logical versus physical backups
This chapter assumes you can use psql, know the difference between a database and a cluster, know where the data directory lives, and understand what a transaction is. It explains the two families of PostgreSQL backups and how to choose between them. Later chapters cover the tools in detail.
Two ways to copy a database
A PostgreSQL backup is either logical or physical.
- A logical backup records the contents of the database as SQL statements or as a structured archive of rows and object definitions.
pg_dumpandpg_dumpallproduce logical backups. To restore one, PostgreSQL re-executes the work: it creates tables, loads rows, builds indexes and adds constraints. - A physical backup copies the files PostgreSQL itself uses on disk, which are the data directory and the write-ahead log (WAL).
pg_basebackupplus WAL archiving produce physical backups. To restore one, you put the files back and start the server. PostgreSQL then replays WAL until the files are consistent.
The write-ahead log is a sequential record of every change, written before the change reaches the data files. Physical backups and point-in-time recovery both rely on it.
How each one works inside
Logical backups
pg_dump connects as an ordinary client and runs a single transaction with a snapshot. The snapshot gives it a consistent view of one database as of the moment the dump started. Other sessions can keep reading and writing, because the dump takes only light locks (ACCESS SHARE) that block schema changes but not normal queries. It does not block writers. Changes committed after the snapshot are not in the dump.
The output describes objects and data, not files. This has several consequences:
- The dump is independent of the on-disk format. You can restore it into a newer major version, onto a different CPU architecture or operating system, or into a database with different storage settings.
- You can restore part of it. With the custom or directory format,
pg_restorecan pick a single table or schema. - The restore does real work. Rows are inserted again, indexes are rebuilt and constraints are re-checked. For a large database this is slow.
- The dump is only as recent as the moment it started. Everything after that moment is lost if you restore from it.
Physical backups
pg_basebackup copies the whole cluster's files over a replication connection. The files are not consistent at the moment of copying, because the server keeps writing while the copy runs. The backup therefore also includes the WAL generated during the copy (the -Xs option streams it). On restore, PostgreSQL replays that WAL and reaches a consistent state.
If you also archive WAL continuously, you can replay past the end of the base backup. This lets you stop at any moment you choose, which is point-in-time recovery.
Consequences:
- The backup is a copy of the whole cluster: every database, every role and every tablespace. You cannot restore one table from it directly.
- Restore is fast, because it is mostly file copying plus WAL replay. Indexes are not rebuilt.
- The backup is tied to the same major version of PostgreSQL, and to a compatible architecture and build. A backup from PostgreSQL 15 cannot start under 16.
- With WAL archiving, the recovery point can be seconds before a failure instead of hours.
Comparison
| Property | Logical (`pg_dump`) | Physical (`pg_basebackup` + WAL) |
|---|---|---|
| Unit of backup | One database (`pg_dumpall` for the cluster) | The whole cluster |
| Roles and tablespaces | Not in `pg_dump`; use `pg_dumpall --globals-only` | Included |
| Restore a single table | Yes, with custom or directory format | No, restore the cluster first and copy the table out |
| Cross-major-version restore | Yes | No |
| Cross-architecture restore | Yes | Generally no |
| Restore speed on large data | Slow; indexes rebuilt | Fast; files copied and WAL replayed |
| Point-in-time recovery | No | Yes, with WAL archiving |
| Typical recovery point | Time of the last dump | Seconds, with continuous archiving |
| Output size | Often smaller, especially compressed | Roughly the size of the data directory |
What each one cannot do
Knowing the limits matters more than knowing the features, because the limits are what hurt during an incident.
A logical backup cannot:
- Restore to a moment between two dumps. If you dump nightly, you lose up to a day of changes.
- Include roles or tablespace definitions when made with
pg_dump. Restoring into a fresh cluster without them fails when objects refer to missing owners, or produces objects owned by the wrong role. - Restore quickly when the database is large. A 2 TB database can take many hours to reload.
A physical backup cannot:
- Be restored into a different major version. It is also unsuitable as a migration path across versions.
- Restore a single database or table without restoring everything.
- Be taken from a standby or primary with an incompatible configuration. For example,
wal_levelmust be at leastreplica. - Detect logical damage by itself. If someone runs
DELETE FROM ordersand the transaction commits, the delete is in the WAL and replays faithfully. Only a recovery target set before the mistake avoids it.
Matching the approach to the failure
Different failures need different tools. The table maps common scenarios to the approach that handles them.
| Scenario | Logical dump | Physical backup + WAL |
|---|---|---|
| A developer drops a table an hour ago | Restores the table only if a dump exists from before the drop | Recover to a time just before the drop (PITR) |
| The disk or server is lost | Works, but restore may be slow | Best fit; fast restore, small data loss |
| Upgrade to a new major version | Best fit | Not possible |
| Copy production data to a test system | Good, and you can select tables | Works only for the full cluster and the same version |
| Restore one table from last week | Best fit | Heavy: restore a full cluster, then extract the table |
| Cluster-wide corruption | Fallback if the files are unusable | Restore from a base backup taken before the corruption started |
Many production systems use both. A nightly or weekly physical backup with WAL archiving covers disaster recovery. Periodic logical dumps cover version upgrades, single-table recovery and a last line of defense when the physical files turn out to be damaged.
Choosing using RPO and RTO
The recovery point objective (RPO) is how much data you can afford to lose, measured in time. The recovery time objective (RTO) is how long you can afford to be down. Use them to decide:
- If an RPO of several hours or a day is acceptable and the database is small, scheduled
pg_dumpis enough and is the simplest thing to operate. - If the RPO is minutes or less, you need WAL archiving, which means a physical backup. A dump cannot give you that.
- If the RTO is short and the database is large, reloading a dump is too slow. Restoring a base backup and replaying WAL is faster, and a standby can be faster still.
- If you need to restore individual tables or move between major versions, include logical dumps.
The numbers matter. Measure how long your restore takes with your real data before you commit to an RTO.
Seeing the difference in practice
The same database backed up both ways looks very different on disk. The first command makes a logical backup of one database. The second makes a physical backup of the entire cluster.
pg_dump -h localhost -U postgres -Fc -f mydb.dump mydb
pg_basebackup -h localhost -U replicator -D /backups/base -Ft -z -Xs -PThe file mydb.dump is a single archive that pg_restore understands. You can list what it contains:
pg_restore --list mydb.dump | head -n 20The output is a table of contents of object definitions and table data. The /backups/base directory instead holds tar files of the data directory and a WAL tar file. Those are not meant to be read by SQL tools. They are meant to be unpacked into a data directory.
The replicator role used above needs the REPLICATION attribute, and pg_hba.conf must allow a replication connection for it. pg_dump needs only read access to the objects it dumps, so a superuser is not required. If it cannot read a table, the dump fails and says so.
Common mistakes
- Copying the data directory with
cporrsyncwhile the server runs. The result is inconsistent and usually will not start cleanly. Usepg_basebackup, or stop the server first. - Assuming
pg_dumpsaves roles. It does not. Without roles, a restore on a fresh cluster fails onOWNER TOandGRANTstatements, or you must use--no-ownerand lose ownership information. - Treating a standby or replica as a backup. Replication copies a bad
DROP TABLEto the standby within moments. A replica protects against hardware failure, not against mistakes. - Relying on a physical backup for a version upgrade. It will not start on a newer major version. Use a dump, or
pg_upgrade. - Never restoring. A backup you have not restored is a hope, not a backup.
Summary
Logical backups are portable and selective but slow to restore and limited to the moment the dump started. Physical backups restore fast and support point-in-time recovery but are tied to the whole cluster and the same major version. The next chapters cover pg_dump in detail first, then return to physical backups and WAL archiving.
Exercise
Your database is 1.5 TB, your RPO is five minutes and your RTO is one hour. A colleague proposes a nightly pg_dump. Does it meet the objectives? Which approach does, and why?
Show answer
No. A nightly dump gives an RPO of up to 24 hours, far above five minutes, and reloading 1.5 TB means re-inserting rows and rebuilding indexes, which will almost certainly exceed one hour. A physical base backup plus continuous WAL archiving meets the RPO because archived WAL can be replayed to within minutes of the failure. Restoring files and replaying WAL is also much faster than a reload, though you should measure it on real data to confirm it fits the one-hour RTO.
Exercise
Someone accidentally runs DELETE FROM customers and commits it at 10:15. You have a nightly pg_dump from 02:00 and a base backup with continuous WAL archiving. Describe what each backup lets you recover and what you lose.
Show answer
The dump restores the database as of 02:00, so you lose all changes from 02:00 to the failure unless you merge them by hand. You can restore just the customers table from it if the dump is in custom or directory format. The base backup with WAL lets you do point-in-time recovery to just before 10:15, losing almost nothing, but you restore a whole cluster and then copy the table's rows out or switch over to it.
Exercise
Name three things a pg_dump file does not give you that a physical backup does, and one thing a physical backup does not give you that a dump does.
Show answer
A pgdump file does not give you roles and tablespace definitions (those need pgdumpall --globals-only), point-in-time recovery, or fast restore of a large database. A physical backup does not give you restore into a different major version, and it cannot restore a single table without restoring the whole cluster.