0%
PostgreSQL Deep Dive: MVCC, Vacuum, Plans, and Replication
Architecture and Storage
MVCC and Vacuum
Indexes and Query Planning
Backups, PITR and Upgrades
A backup is only valuable if it can be restored, quickly enough and to the right point. Postgres offers two kinds: logical backups, which export SQL objects and data, and physical backups, which copy the data files. They differ in speed, flexibility and what they can recover. This section explains both, when to use each and why production systems rely on physical backups plus WAL archiving for recovery.
Logical backups with pg_dump
pg_dump connects to one database and exports its schema and data as SQL or in a custom archive format, using a single consistent snapshot so the dump reflects one moment even while writes continue. pg_dumpall adds roles and tablespaces, which belong to the cluster rather than any database. The custom and directory formats allow parallel dumps with the jobs option and selective restore of individual tables with pg_restore.
Strengths and limits of logical backups
Logical dumps are portable across major versions and architectures, can be restored into a different schema or server, and allow restoring a single table. But restoring them means replaying every row and rebuilding every index, which for a terabyte database takes many hours or days. They capture only the moment of the dump, so anything written after it is lost, and dumping huge databases holds a long snapshot that blocks vacuum cleanup.
Physical backups
A physical backup copies the data directory files of the whole cluster while it runs. Because files are copied over minutes or hours while pages change, the copy is inconsistent on its own; it becomes consistent by replaying the WAL generated during the copy. pg_basebackup does this over a replication connection, starting a backup, streaming the files and the needed WAL and producing a directory or tar files that can be started as a server.
Restore speed
Restoring a physical backup is copying files back and replaying a bounded amount of WAL, so it runs at the speed of storage and network, often hundreds of megabytes per second or more. A terabyte database can be restored in an hour or two rather than the day a logical restore would take. For systems with a recovery time objective in hours or less, physical backups are the only practical basis for restore.
WAL archiving
archive_mode on and an archive_command or, since version 15, an archive_library, copy each completed WAL segment to durable storage, such as object storage. With a base backup plus every WAL segment since, the cluster can be recovered to any moment after the backup, which turns backups from daily snapshots into continuous protection. The archive must succeed reliably: failed archiving makes WAL accumulate in pg_wal until it does.
Backups and replicas
Standbys protect against server failures, but they replicate mistakes and corruption within milliseconds. Only backups, kept independently and for days or weeks, protect against accidental deletes, application bugs that corrupt data, ransomware and operator errors. A delayed standby adds a short window but does not replace backups. Every production database needs both replication for availability and backups for recovery.
Choosing between logical and physical
Use physical backups with WAL archiving as the main recovery mechanism for production clusters. Use logical dumps for migrating data between versions or systems, exporting individual tables or databases, development copies and as an occasional extra safety net, since they also verify that every row can be read. Many teams run nightly physical backups with continuous archiving and a weekly logical dump of critical schemas.
Consistency across databases
pg_dump takes a consistent snapshot of one database. Dumping several databases with separate pg_dump runs gives snapshots at different moments, which matters if they are related. A physical backup covers the whole cluster consistently. For multiple clusters that must be recovered to a matching point, such as services with cross database references, coordinated recovery targets based on time are needed, and exact consistency across clusters is not guaranteed.
Encryption and access
Backups contain every row of the database, so they need protection at least as strong as the database: encryption at rest, encryption in transit, restricted access to the bucket or backup server and separate credentials from the production database, so an attacker who compromises the database cannot also delete its backups. Object storage with versioning or object lock adds protection against deletion.
Backup frequency and retention
Base backup frequency determines how much WAL must be replayed on restore: with weekly full backups, a restore late in the week may replay days of WAL, taking hours. Daily full or incremental backups bound replay time. Retention determines how far back recovery is possible and should cover the time it may take to notice a problem, often 7 to 30 days, plus longer retention of monthly backups for compliance.
Dump formats
pg_dump offers plain SQL, which is readable and restorable with psql but cannot be restored selectively or in parallel; custom format, a compressed archive that pg_restore can restore selectively and in parallel; directory format, one file per table, which also allows parallel dumps; and tar. For anything beyond small databases, custom or directory format is the right choice, since parallel restore and selective restore are often needed during incidents.
Dumping from a replica
A long pg_dump on the primary holds a snapshot that prevents vacuum from removing dead rows across the cluster for the duration of the dump. Running dumps on a standby avoids this, though long queries there may be cancelled by replay conflicts unless max_standby_streaming_delay is raised or hot_standby_feedback is on, which moves the vacuum hold back to the primary. A dedicated replica for dumps with a long delay is a common compromise.
Snapshots at the storage layer
Filesystem or volume snapshots, such as cloud disk snapshots or ZFS snapshots, can serve as physical backups if taken correctly: either of a cleanly stopped server, or atomically across all volumes of a running server, including pg_wal, in which case they restore like a crash and recover with WAL. Snapshots spread across several volumes that are not taken atomically are not safe without the backup start and stop functions.
The low level backup API
pg_backup_start and pg_backup_stop, called pg_start_backup and pg_stop_backup before version 15, let external tools take physical backups with their own copy mechanism: start the backup, copy the files by any means, stop the backup and save the backup label it returns with the copy. Backup tools use this API. Since version 15 only the non exclusive mode exists, which requires the session that started the backup to stay open until it is stopped.
What a backup must include
A complete physical backup includes the data directory, all tablespaces, the WAL needed for consistency and the backup label. Configuration files may live outside the data directory and should be backed up separately. Roles and tablespaces are part of the cluster and included in physical backups, but logical dumps of single databases omit them, so pg_dumpall with the globals only option is needed alongside pg_dump to restore a database into a fresh cluster.
Sizing backup storage
Backup storage holds the retained full backups, incrementals and all WAL for the retention window. WAL often dominates: a cluster generating 500 GB of WAL per day with 30 days of retention needs 15 TB for WAL alone before compression. Compression typically reduces WAL to a third or less of its size. Measuring daily WAL volume from pg_stat_wal or pg_current_wal_lsn deltas gives the numbers needed to plan storage and transfer costs.
archive_timeout
A WAL segment is archived only when it fills, 16 MB by default. On a quiet server, a segment might take an hour to fill, so the last hour of commits exists only in pg_wal on the server and would be lost with it. archive_timeout forces a segment switch after the given number of seconds, so archived WAL is never older than that. Values of 60 seconds are common; very low values create many mostly empty segments.
pg_dump is portable and selective but slow to restore and frozen at one moment. Physical base backups plus continuous WAL archiving restore at storage speed to any point after the backup. Replicas do not replace backups, and backups need encryption, separate credentials and retention matched to how quickly problems are noticed.