Learn Labs
WAL, Vacuum & Recovery

Backups and a Point-in-Time Restore

pgBackRest to SeaweedFS: encrypted backups, the WAL archive, scheduled backups, and a restore to one second before a DROP TABLE

A backup you haven't restored is a hope. The cluster backs up with pgBackRest to the pg-backups bucket in SeaweedFS, and labs/backup_pitr.py proves it works by destroying data and getting it back.

The setup

[global]repo1-type=s3repo1-s3-endpoint=s3-gatewayHAProxy terminating TLS in front of SeaweedFSrepo1-storage-port=8443repo1-s3-bucket=pg-backupsrepo1-s3-key=pgbackrestan S3 identity that can only touch this bucketrepo1-cipher-type=aes-256-cbcencrypted on the node: the bucket only sees ciphertextrepo1-retention-full=2keep two full backups and the WAL to restore themcompress-type=zstarchive-async=yarchive_command returns once the segment is queuedstart-fast=ycheckpoint now instead of waiting for the next one

Two things run all the time:

  • The WAL archive. Patroni sets archive_command to pgbackrest archive-push on every node, so whichever node is primary ships each 16 MB WAL segment to the bucket. archive_timeout = 60 forces a segment switch at least once a minute, which bounds how much an idle primary can lose.
  • Scheduled backups. node/backup-loop.sh runs on every node and acts only while its node is primary (it checks Patroni's /primary), so the schedule follows failovers. Daily incremental, weekly full, and after each check it pushes the newest backup's time to the Prometheus Pushgateway, which the PostgresBackupTooOld alert watches. The first time it ran it found an empty repository and took a full backup on its own.

The drill

docker compose -f compose_prod.yaml --profile restore up -d pg-restore
docker compose -f compose_prod.yaml run --rm labs python backup_pitr.py

1. Full and incremental backups
 appdb is 237 MB
 full backup in   2.9s: database 260 MB, changed 260 MB, stored in the repository 77 MB
 incr backup in   2.5s: database 265 MB, changed 226 MB, stored in the repository 75 MB

2. Writes after the last backup, then an accident
 2,001,000 rows (1,000 written after the backup); target time 2026-10-22 19:30:28.745580+00:00
 DROP TABLE ledger
 segment 0000000C0000000100000029 archived 0.5s after the switch

3. Point-in-time restore into pg-restore
 pgbackrest restore (copy the backup files): 1.7s
 WAL replay to the target, then promote:     3.1s
   recovery stopping before commit of transaction 751756, time 2026-10-22 19:30:29.749836+00
   selected new timeline ID: 13
   archive recovery complete
 restored 2,001,000 rows, 1000 of them written after the backup; sum matches: True
targetDROP TABLEbackupsfull 2.9 sincr 2.5 sWAL archiveevery segment, pushed asynchronouslyrestore usedincr filesWAL replayedtime →

The repository holds 77 MB for a 260 MB database. zstd compression does most of that (the data is md5 strings and numbers), and it's encrypted on top. The security lab downloads a WAL file from the bucket and finds 8.000 bits of entropy per byte: indistinguishable from random.

The incremental backup stored 75 MB, barely less than the full. The UPDATE changed 1 row in 20 (5%), but with dozens of rows on every 8 KB page, almost every page held at least one updated row, and pgBackRest's incrementals work on whole files. 5% of the rows was nearly 100% of the files (changed 226 MB of 265). Block-level incrementals (repo1-block=y, not enabled here) copy only changed blocks, but when nearly every page changed that wouldn't help much either: what a backup costs depends on how a change is spread, not how many rows it touches.

The 1,000 rows written after the last backup came back. They were in no backup, only in the WAL archive. The restore copied the incremental backup's files (1.7 s), then replayed archived WAL forward until the target time and stopped just before the transaction that committed the DROP (recovery stopping before commit of transaction 751756). It then promoted onto a new timeline, 13: the restored database is a new history branching off the old one, and its WAL must never mix with the live cluster's.

Rules the drill enforces

  • Restore somewhere else. pg-restore is a separate, empty node. You almost never want to rewind the whole production cluster to undo one bad statement: restore beside it, then copy the table back.
  • The restored copy must not archive. The node image's Postgres config came from the backup, including archive_command. Started as-is, the copy would push its timeline-13 WAL into the production repository. The lab starts it with -c archive_mode=off.
  • Force the WAL out before restoring. The DROP sat in a WAL segment that wasn't full. pg_switch_wal() closed it, and the lab waited until pg_stat_archiver reported that segment archived (0.5 s later) before restoring.

On this page