skip to content

Using WAL-G, how do you restore a containerised PostgreSQL 18 from object storage with backup-fetch LATEST and wal-fetch, and what can silently go wrong?

level: middleimportance: must knowfreq 50%

answer

  1. an empty data directory first
  2. a signal file WAL-G does not write
  3. restore_command calls the fetch side
  4. exit code 74 means not found
  5. every other failure looks the same

basics

~10 s

Run wal-g backup-fetch into an empty data directory with LATEST, create recovery.signal, set restore_command = 'wal-g wal-fetch %f %p' and start PostgreSQL. A storage error from wal-fetch can end recovery early, so wrap it.

solid answer

~50 s

Stop the broken server and move its data directory aside, then run `wal-g backup-fetch /var/lib/postgresql/data LATEST` (or a name from `wal-g backup-list --detail`) into an empty directory; WAL-G refuses a non-empty one and pulls any delta chain automatically. WAL-G does not write a signal file, so create `recovery.signal` and set `restore_command = 'wal-g wal-fetch %f %p'`, plus a recovery target if you need one. On start PostgreSQL replays WAL through `wal-fetch`, which prefetches up to `WALG_DOWNLOAD_CONCURRENCY` (10) segments ahead into `.wal-g/prefetch`. The silent failure: `wal-fetch` exits 74 when a segment is not in storage, but any other error (a timeout, expired credentials) exits 1, and PostgreSQL treats every code below 126 as end of archive, so it can finish recovery early and promote at an older point. A wrapper that maps non-74 failures to an exit above 125 makes recovery stop instead.

code

bash · 6 lines
bash
# old data volume moved aside; new empty directory owned by postgres, mode 0700
wal-g backup-fetch /var/lib/postgresql/data LATEST
touch /var/lib/postgresql/data/recovery.signal
# in postgresql.conf:
#   restore_command = '/usr/local/bin/walg-fetch.sh %f %p'
pg_ctl -D /var/lib/postgresql/data start

go deeper

for a junior

Recall the order: backup-fetch into an empty directory, recovery.signal, restore_command calling wal-g wal-fetch, then start PostgreSQL.

for a middle

Explain what backup-fetch restores and what it leaves out, how wal-fetch prefetches, and why exit code 74 is the normal end of an archive.

for a senior

Show you know the exit-code trap: storage errors exit 1 and end recovery early, so wrap wal-fetch and check where recovery stopped before reopening traffic.

for a principal

Plan restore time from object storage: download concurrency, prefetch, a separate prefix for the restored timeline and how often fresh base backups shorten replay.

## The restore in five steps For a containerised PostgreSQL 18 whose backups and WAL live under `WALG_S3_PREFIX`: 1. **Stop the old server and keep its files.** Move the old data volume aside rather than deleting it; you may want it for forensics. 2. **Prepare an empty data directory** owned by the postgres user with mode `0700`. `backup-fetch` refuses a non-empty target with an error, which protects you from unpacking over live files. 3. **Fetch the base backup**: `wal-g backup-fetch /var/lib/postgresql/data LATEST`, or a specific name from `wal-g backup-list --pretty --detail`. If that backup is a delta, WAL-G fetches the full backup and every delta in its chain by itself. 4. **Tell PostgreSQL to recover.** WAL-G's PostgreSQL fetch does not write any signal file, so create `recovery.signal` in the data directory and set `restore_command = 'wal-g wal-fetch %f %p'`. Since PostgreSQL 12 there is no `recovery.conf`; these two replace it. Recovery targets such as `recovery_target_time` are core PITR settings. 5. **Start PostgreSQL**, follow the log until recovery completes, then check the data before sending traffic. ## What backup-fetch puts on disk | Restored | Not restored | |---|---| | data files of every database and tablespace | the contents of `pg_wal` (the directory is recreated empty) | | `backup_label` and `tablespace_map` from `pg_backup_stop` | `pg_replslot` contents, so replication slots are gone | | `pg_control` | `postmaster.pid`, `standby.signal` | | `postgresql.conf` and `pg_hba.conf` if they live in the data directory | config files kept outside the data directory | The missing slots matter after the restore: streaming replicas and logical subscribers that relied on a slot must have it recreated, and a replica usually has to be rebuilt from the new primary. ## What wal-fetch does during recovery PostgreSQL calls `restore_command` once for every file it needs: segments, and also `.history` files and names that do not exist yet. For each call `wal-fetch`: - downloads and decompresses (and decrypts, if a key is configured) the requested segment into `%p`; - starts prefetching the next segments, up to `WALG_DOWNLOAD_CONCURRENCY` (default 10), into `.wal-g/prefetch` under `pg_wal` (`WALG_PREFETCH_DIR` moves it), so the next call is a local rename instead of a download; - exits **74** (`EX_IOERR`) when the file is simply not in storage, which is how recovery learns it has reached the end of the archive. ## The exit-code trap WAL-G's own docs spell out a sharp edge: - A missing file exits 74. That is correct and expected at the end of the archive. - **Every other error exits 1**: a network timeout, a throttled request, expired credentials, a missing decryption key. - PostgreSQL ends recovery on any nonzero exit below 126 and aborts only when the code is above 125 (the shell's "command not found" range) or the command was killed by a signal. So a five-second storage hiccup in the middle of replay can look exactly like the end of the archive. PostgreSQL finishes recovery at that point, picks a new timeline and opens for writes, missing every transaction after the hiccup. Nothing in the exit status tells you. The fix WAL-G recommends is a small wrapper that passes 74 through and maps every other failure to a code above 125, so PostgreSQL stops and you retry instead of promoting early. ## After the restore 1. **Check where recovery ended.** Compare the last replayed transaction time in the log with what you expected; it is the cheapest proof that the trap above did not fire. 2. **Separate the archives.** The restored server is on a new timeline and will push WAL and `.history` files. If the old primary could still be running, give the restored one its own `WALG_S3_PREFIX`, or `wal-g wal-verify timeline` will report a conflict. 3. **Take a fresh `backup-push`** soon, so the next restore does not have to replay all of today's WAL again. 4. **Rebuild replicas and slots.** ## Choosing which backup to fetch `LATEST` is right when you want the newest state. For a recovery target in the past, fetch the newest backup that **started before** the target, by name from `backup-list --detail` (for example `wal-g backup-fetch /var/lib/postgresql/data base_000000010000000000000042`), or by the JSON you attached at push time with `--target-user-data`. A backup newer than the target cannot be rolled backwards. WAL-G also has an **experimental** partial restore, `--restore-only=<database>`, which unpacks only some databases' files; it needs the file metadata of a backup taken from the local data directory and leaves catalog entries for the skipped databases that you then drop. If restore time matters, raise `WALG_DOWNLOAD_CONCURRENCY` for both base-backup download and WAL prefetch, and remember that object storage throughput, not CPU, is usually what limits replay from a bucket.

  • Why does wal-g wal-fetch exit 74 for a missing segment, and why is that not an error?
    During archive recovery PostgreSQL keeps asking for the next file until the archive has none; a nonzero exit is how it learns it reached the end. WAL-G uses 74 (`EX_IOERR`) for 'not in storage' so a wrapper can tell that normal case apart from real failures, which exit 1.
  • The restored server came up but is missing the last two hours of orders. What do you check first?
    Where recovery stopped: the log shows the last replayed segment and the time recovery ended. If it stopped before the archive's last segment, `wal-fetch` probably hit a storage error and exited 1, which PostgreSQL read as end of archive. Confirm the segment exists with `wal-g wal-show` or `wal-verify integrity`, then restore again with a wrapper that aborts on non-74 failures.

  • wal-g backup-fetch writes recovery.signal for you
  • wal-g backup-fetch can unpack over the old data directory
  • Any wal-fetch failure makes PostgreSQL stop recovery with an error
  • The restored server keeps its replication slots
  • Put restore_command into recovery.conf for the restore