Course contents 16 / 19

16 / 19 · Concurrency and Recovery

Backups and restoration

Choose a recoverable point after media loss or a bad commit, then check a restored copy against data and service requirements.

On this page 4

An operator accidentally deletes reservations and commits. Restarting the server recovers the deletion along with all other durable changes. If the disk itself has failed, a restart may have neither the pages nor the local WAL. The first incident needs a prior state. The second needs another copy.

What a backup must contain#

A logical backup records database objects and their contents in a form the database can recreate. PostgreSQL’s pg_dump takes a consistent snapshot of one database while normal reads and writes continue. Restoring that dump can rebuild its schema and rows. Cluster-wide roles and tablespaces require separate handling, for example through pg_dumpall --globals-only. A dump supplies the data at its snapshot. The subsequent physical WAL cannot be replayed on top of it to recover a later PostgreSQL state. PostgreSQL 18, §§25.1 and 25.3

A physical base backup contains the cluster’s files in a form that the engine can recover. For an online PostgreSQL base backup, recovery also needs the continuous WAL sequence beginning no later than the start of the backup. The files may be copied at different times while writes continue. The backup procedure and required WAL let recovery reach a consistent point. Simply copying open files at arbitrary times gives no such boundary. SQLite’s Online Backup API is another engine-specific way to obtain a consistent copy of a live database. PostgreSQL 18, §25.3 SQLite, Online Backup API

The copy and its required WAL must be accessible after the failure we are planning for. A backup on the same lost volume will not help with loss of that volume. A replica can improve availability, but a replica that replays the mistaken deletion also loses the rows. A retained backup with the appropriate history serves a different recovery objective.

Choose how far to replay#

Suppose a PostgreSQL base backup completed at 08:00 and its required WAL is available. The archive has an unbroken sequence through a recoverable commit at 11:55. The primary storage fails at 12:00 and no later WAL can be recovered. Restoring the base and replaying that sequence reaches the committed state at 11:55. The five-minute interval is the portion of history outside the available archive. The number of lost transactions depends on the workload in that interval. PostgreSQL 18, §25.3

A missing WAL segment breaks the chain. Later segments cannot reconstruct the changes that were omitted. The usable restore point can move earlier, or the base backup can become unusable if the gap is within the WAL needed to make it consistent. Archiving must cover the relevant timeline as well. When recovery starts a new history, later log records on that branch cannot be treated as a continuation of a different branch. PostgreSQL 18, §25.3

For the mistaken deletion, suppose the transaction committed at 11:50. Replay all the way to 11:55 and the deletion is present. Point-in-time recovery (PITR) stops replay at a chosen target. In PostgreSQL, when the bad transaction ID is known and the target can be reached, recovery_target_xid with recovery_target_inclusive = off stops before that transaction commits. The restored cluster also excludes later legitimate commits. Restoring into a separate environment allows an operator to extract the needed rows and reconcile them with the current state. A PostgreSQL physical restore operates at cluster scope even when the mistake affected only one table. PostgreSQL 18, §§19.5.6 and 25.3

Measure two different times#

The recovery point objective (RPO) specifies how recent the recovered data must be. A target of ten minutes allows recovery to a point no more than ten minutes before the outage in this media-loss scenario. The recovery time objective (RTO) specifies the target time to restore service. If service is ready at 12:42, the actual recovery took 42 minutes from 12:00. With a ten-minute RPO and a one-hour RTO, this exercise meets both objectives. The objectives are requirements. The five-minute gap and the 42-minute restore are outcomes of the assumed exercise. NIST SP 800-34 Rev. 1, §3.2.1

Figure

Lost history and time offline

Lost history and time offlineThe archive is available through 11:55. Failure is at 12:00 and service is ready again at 12:42.5 min42 min11:55WAL ready12:00Failure12:42Ready
The axis is schematic. The archive lacks five minutes of history. Verified restoration takes 42 minutes from the failure.

The time to restore includes getting a copy and the needed log, replaying the log, checking the data and making the service ready. More frequent backup snapshots or a shorter archive delay can reduce the age of the recovered state. They do not by themselves make the service available sooner.

Restore and check the result#

A restore exercise runs against a separate destination. Check that the backup and WAL can be read, the server reaches the intended point and the application can execute representative operations. PostgreSQL’s pg_verifybackup can verify a base backup against its manifest and check required WAL when applicable. Its successful result is useful evidence about the files. A restore exercise also checks the environment, the recovery target and what the service actually sees. PostgreSQL 18, pg_verifybackup

For the reservation example, a read-only check can compare the restored totals with the expected state at the chosen point. It uses the stock and reservation tables and the six-pack invariant from the toy model.

SELECT s.product, s.available, COALESCE(r.reserved, 0) AS reserved,
       s.available + COALESCE(r.reserved, 0) AS accounted
FROM stock AS s
LEFT JOIN (
    SELECT product, SUM(quantity) AS reserved
    FROM reservations
    GROUP BY product
) AS r ON r.product = s.product
WHERE s.product = 'tea';

At the point before reservation 701 this returns five available, one reserved and six accounted for. After reservation 701 it gives three, three and six. The same total is consistent with both points, so check the individual reservation and the expected commit boundary as well. A real exercise needs similarly specific checks of the restored data and the time at which the service can resume.

Next#

A restored service also needs the roles and access rules its clients expect. A physical backup contains database roles. A logical restore may need a separate copy of their definitions. Roles and permissions examines which operations a role can run and why ownership matters.

References#

  1. PostgreSQL Global Development Group. PostgreSQL 18 documentation. SQL dump, §25.1, continuous archiving and PITR, §25.3, recovery targets, §19.5.6 and pg_verifybackup specify backup scope, WAL coverage and verification.
  2. Marianne Swanson, Pauline Bowen, Amy Wohl Phillips, Dean Gallup and David Lynes. Contingency Planning Guide for Federal Information Systems. NIST SP 800-34 Rev. 1, 2010, §3.2.1. Defines recovery point and recovery time objectives.
  3. SQLite Project. Using the SQLite Online Backup API. Explains how a live database can be copied to a consistent snapshot.