Skip to content

PostgreSQL snapshot consistency: pg_start_backup and the WAL chain

Why a raw block snapshot of a PostgreSQL data directory needs pg_backup_start / pg_backup_stop and an unbroken WAL chain to restore cleanly, with the runbook we use on engagements.

A raw block snapshot of a PostgreSQL data directory is not restorable on its own. PostgreSQL’s recovery model requires a snapshot taken between paired pg_backup_start() and pg_backup_stop() calls, plus every WAL segment generated between those two timestamps, plus an archive_command that pushes WAL somewhere durable. Without all three you have crash-consistent bytes, not a backup. This article walks through the quiesce dance for PostgreSQL 15+, the WAL archive setup it presumes, and the runbook we ship to support desks running snapshot-based PostgreSQL backups.

How to verify

Before the runbook, confirm the cluster is in a configuration where snapshot backup is valid:

sudo -u postgres psql -c "SHOW archive_mode;"
sudo -u postgres psql -c "SHOW archive_command;"
sudo -u postgres psql -c "SHOW wal_level;"
sudo -u postgres psql -c "SELECT pg_walfile_name(pg_current_wal_lsn());"
sudo -u postgres psql -c "SELECT * FROM pg_stat_archiver\G"

archive_mode=on, wal_level=replica or logical, a working archive_command, and a pg_stat_archiver row showing recent successful archives — all four are mandatory. Skip any one of them and your snapshot is unrestorable.

What’s happening

PostgreSQL writes data through buffered I/O. On commit, the WAL record is fsync’d (or written if synchronous_commit=off, which is a different conversation) but the data pages in base/ are dirty in shared_buffers and only flushed on checkpoint. A snapshot taken at an arbitrary moment captures:

  • Data files with partially-written pages (torn pages from in-progress 8 KB block writes).
  • WAL segments that hold the recovery information for those torn writes.

PostgreSQL’s startup recovery uses the WAL to apply the redo records and bring the data files back to a consistent state. But it needs the WAL records that match the data file state at snapshot time. If those WAL segments have been recycled in pg_wal/ and not archived, recovery fails.

pg_backup_start('label') (PostgreSQL 15+; pg_start_backup in older versions) does two things: it issues a non-blocking checkpoint, and it records the WAL LSN that begins the “consistent enough” range. From that moment until pg_backup_stop() is called, PostgreSQL keeps generating WAL normally (the cluster stays open for reads and writes), but the recovery semantics are: “start from the LSN of pg_backup_start, replay through pg_backup_stop’s returned LSN, then continue as point-in-time.”

A snapshot taken between these two calls plus all WAL segments generated during the window plus an archive_command that has shipped those segments somewhere = a restorable backup.

The procedure

This is the runbook we automate. It is for PostgreSQL 15+; older versions use pg_start_backup/pg_stop_backup with slightly different arguments.

  1. Confirm the cluster’s archive_command is keeping up. A backlog of un-archived WAL means a snapshot would reference WAL segments that have not yet shipped.

    SELECT * FROM pg_stat_archiver;

    failed_count should be 0; last_archived_time should be recent.

  2. Open a long-running psql session — pg_backup_start registers the backup on that session and pg_backup_stop must be called from the same session.

    sudo -u postgres psql --no-psqlrc --quiet
  3. Begin the backup.

    SELECT pg_backup_start('snapshot-2026-06-01T02:00', false);

    The false argument means “non-exclusive backup mode” — the only supported mode in PostgreSQL 15+.

  4. Outside the psql session, take the block snapshot of the data directory and the WAL directory if it is on a separate volume.

    lvcreate -s -n pg-snap-$(date +%Y%m%dT%H%M%S) vg0/pgdata
    # ZFS:  zfs snapshot tank/pgdata@2026-06-01T02:00
    # btrfs: btrfs subvolume snapshot -r /var/lib/postgresql /snap/...
    # cloud: provider CLI
  5. Back in the psql session, stop the backup and capture the LSN, the labelfile, and the tablespace map.

    SELECT * FROM pg_backup_stop(false);
    -- columns: lsn, labelfile, spcmapfile
  6. Save the labelfile and tablespace map alongside the snapshot. The labelfile content must be written as backup_label in the root of the restored data directory; the tablespace map as tablespace_map. Skipping this step makes the restore fail with “could not locate required checkpoint record.”

    echo "$labelfile" > /backup/2026-06-01/backup_label
    echo "$spcmapfile" > /backup/2026-06-01/tablespace_map
  7. Stream the snapshot content + the labelfile + the WAL archive’s range to the backup destination.

    mount -o ro,nouuid /dev/vg0/pg-snap-... /mnt/pg-snap
    restic -r s3:... backup /mnt/pg-snap /backup/2026-06-01/
  8. Drop the snapshot.

    umount /mnt/pg-snap
    lvremove -f vg0/pg-snap-...
  9. Verify quarterly by restoring to a scratch host: untar the snapshot, place the labelfile and tablespace_map in the data directory, ensure the WAL archive is reachable via restore_command in postgresql.auto.conf, start postgres, watch the log for database system is ready to accept connections.

Common pitfalls

  • Skipping pg_backup_start/stop entirely. The most catastrophic mistake. The snapshot may restore via pg_resetwal and silent data loss, but it is not an actual backup.
  • Closing the psql session before pg_backup_stop. The backup state lives on the session; closing it leaves the cluster in a “backup in progress” state that confuses metrics and can block other backups. Always issue pg_backup_stop cleanly.
  • WAL archive failures. If archive_command fails during the backup window, the WAL segments needed for restore never reach the archive. PostgreSQL accumulates WAL in pg_wal/ and the disk eventually fills.
  • Forgetting the labelfile. A snapshot without backup_label is interpreted as a crash recovery starting from the last checkpoint in pg_control, which is usually wrong and may produce silent data corruption.
  • Snapshotting pg_wal/ and base/ non-atomically. If they are on separate volumes, take both snapshots inside the pg_backup_start window; otherwise the LSN range references segments that may not exist in the snapshot.
  • Tablespaces on separate volumes. PostgreSQL tablespaces outside the default location need their own snapshots in the same window; the tablespace_map tells the restore which mounts to expect.

In the engagements we run, snapshot-based PostgreSQL backups are a fallback we configure when the primary tool (we usually deploy pgBackRest or WAL-G) is not appropriate — typically because the storage layer is a cloud snapshot service the client wants to use directly. The runbook above is what we ship; we never accept “we take VM snapshots, those are our backups” without seeing the pg_backup_start dance. The full operational pattern is at /en/services/managed-operations/.