{
  "slug": "wal-archiving",
  "term": "WAL archiving",
  "aliases": [
    "continuous WAL archiving"
  ],
  "definition": "WAL archiving preserves write-ahead-log segments for later recovery before normal log recycling can remove them.",
  "note": "PostgreSQL 18 uses WAL segment files, normally 16 MiB (16,777,216 bytes); initdb can select another segment size. With archive_mode on and wal_level at replica or higher, completed segments are passed to archive_command or archive_library. The shell command reports success with exit status zero; the library's archive_file_cb reports success by returning true. PostgreSQL then treats the file as archived and may remove or recycle the original. [pg-archive] [pg-initdb] [pg-initdb-code] [pg-callback]\n\nWAL segment archiving processes completed segments, so a quiet server can leave committed changes in an open segment. Local commit durability therefore does not by itself prove archive coverage. archive_timeout measures elapsed time since the previous segment switch and requires database activity. Forced early switches still produce full-length files. [pg-timeout]\n\nFor scale, a day with exactly 1,440 completed 16 MiB segments produces 24,159,191,040 bytes (22.5 GiB, about 24.16 GB) of uncompressed segment files. This is illustrative file-volume arithmetic, not an upper bound: workload-driven completion or manual pg_switch_wal calls can produce more segments. It is not a measurement of useful WAL records, network traffic or physical archive allocation. [pg-archive] [pg-initdb-code]\n\nArchive steps should refuse to overwrite an existing file with different contents, and should return success for an identical, fully persisted copy, because a crash can make the server archive the same segment twice. File names can be up to 64 characters long and must be preserved as given. [pg-archive]\n\nCleanup ignores timeline prefixes, compares segment positions and retains the cutoff to protect parent-timeline segments. [pg-cleanup16-source] [pg-cleanup18-source]",
  "advantages": [
    {
      "text": "Together with a base backup, the archive allows recovery to replay changes up to a chosen point after that backup, not only to the moment the backup was taken.",
      "citationIds": [
        "pg-archive"
      ]
    }
  ],
  "limitations": [
    {
      "text": "A successful archive command must mean the file is safely preserved, not merely that copying was attempted.",
      "citationIds": [
        "pg-archive"
      ]
    },
    {
      "text": "archive_timeout schedules segment switches, not completed transfers; treating it alone as an end-to-end RPO bound is unjustified.",
      "citationIds": [
        "pg-timeout"
      ]
    },
    {
      "text": "last_archived_wal does not prove all earlier segments were archived: ordering is not guaranteed. Some failures that restart the archiver are not counted in pg_stat_archiver.",
      "citationIds": [
        "pg-stats",
        "pg-archive"
      ]
    },
    {
      "text": "Using pg_archivecleanup with %r as a standby's archive_cleanup_command is intended for that standby's transient staging area, not a long-term archive or one shared by recovering standbys. A standby restart boundary does not establish that WAL needed by every retained backup has been preserved.",
      "citationIds": [
        "pg-cleanup",
        "pg-archive"
      ]
    },
    {
      "text": "A PostgreSQL 16.15 probe covered 23 synthetic files in 10 cases, not valid WAL or restorability. At cutoff 000000020000000000000010, a later segment on timeline 1 remained but an earlier segment on timeline 3 was listed.",
      "citationIds": [
        "hesela-cleanup-probe"
      ]
    },
    {
      "text": "Version 16.15 accepts -n but rejects --dry-run and -b; the reviewed 18.0 source supports both newer options. A backup-history filename used as a cutoff is distinct from a backup-history candidate selected for cleanup.",
      "citationIds": [
        "pg-cleanup16-source",
        "pg-cleanup18-source",
        "hesela-cleanup-probe"
      ]
    }
  ],
  "related": [
    "point-in-time-recovery",
    "recovery-target",
    "write-ahead-log",
    "backup-verification"
  ],
  "citations": [
    {
      "id": "pg-archive",
      "title": "PostgreSQL 18: Continuous Archiving and Point-in-Time Recovery",
      "url": "https://www.postgresql.org/docs/18/continuous-archiving.html",
      "accessed": "2026-11-28"
    },
    {
      "id": "hesela-backup-evidence",
      "title": "Hesela: Backup verification is not a restore test",
      "url": "https://hesela.com/analyses/backup-verification-is-not-a-restore-test/",
      "accessed": "2026-10-14"
    },
    {
      "id": "pg-timeout",
      "title": "PostgreSQL 18: archive_timeout",
      "url": "https://www.postgresql.org/docs/18/runtime-config-wal.html#GUC-ARCHIVE-TIMEOUT",
      "accessed": "2026-11-28"
    },
    {
      "id": "pg-stats",
      "title": "PostgreSQL 18: pg_stat_archiver",
      "url": "https://www.postgresql.org/docs/18/monitoring-stats.html#PG-STAT-ARCHIVER-VIEW",
      "accessed": "2026-10-14"
    },
    {
      "id": "pg-initdb",
      "title": "PostgreSQL 18: initdb WAL segment-size option",
      "url": "https://www.postgresql.org/docs/18/app-initdb.html#APP-INITDB-OPTION-WAL-SEGSIZE",
      "accessed": "2026-11-28"
    },
    {
      "id": "pg-initdb-code",
      "title": "PostgreSQL REL_18_0 initdb.c: binary WAL segment-size conversion",
      "url": "https://github.com/postgres/postgres/blob/REL_18_0/src/bin/initdb/initdb.c#L1518",
      "accessed": "2026-11-28"
    },
    {
      "id": "pg-callback",
      "title": "PostgreSQL 18: archive module callback success and retry",
      "url": "https://www.postgresql.org/docs/18/archive-module-callbacks.html#ARCHIVE-MODULE-ARCHIVE",
      "accessed": "2026-11-28"
    },
    {
      "id": "pg-cleanup",
      "title": "PostgreSQL 18: pg_archivecleanup and standby staging scope",
      "url": "https://www.postgresql.org/docs/18/pgarchivecleanup.html",
      "accessed": "2026-11-28"
    },
    {
      "id": "pg-cleanup16-source",
      "title": "PostgreSQL 16.15: timeline-independent cleanup and short options",
      "url": "https://github.com/postgres/postgres/blob/7d3e000c5961a544302072058a1184e9a588837b/src/bin/pg_archivecleanup/pg_archivecleanup.c",
      "accessed": "2026-11-28"
    },
    {
      "id": "pg-cleanup18-source",
      "title": "PostgreSQL 18.0: cleanup selection and backup-history option",
      "url": "https://github.com/postgres/postgres/blob/3d6a828938a5fa0444275d3d2f67b64ec3199eb7/src/bin/pg_archivecleanup/pg_archivecleanup.c",
      "accessed": "2026-11-28"
    },
    {
      "id": "hesela-cleanup-probe",
      "title": "Hesela: PostgreSQL 16.15 native dry-run filename probe, 10 cases",
      "url": "https://hesela.dev/research/pg-archivecleanup/results.json",
      "accessed": "2026-11-28"
    }
  ],
  "status": "shipping",
  "supersedes": [],
  "supersededBy": [],
  "firstSeen": null,
  "lastSeen": null,
  "updated": "2026-11-28",
  "provenance": [
    "PostgreSQL 18 documentation: continuous archiving section 25.3.1, archive_timeout in section 19.5.3, archive callback section 49.2.3, initdb --wal-segsize and pg_archivecleanup Description; reviewed 2026-11-28. The pg-stats citation retains its earlier access date.",
    "REL_18_0 initdb.c passes the segment-size option multiplied by 1024 * 1024 to the backend; MiB makes that binary unit explicit.",
    "Worked example: 1,440 * 16 * 1,048,576 = 24,159,191,040 bytes; divide by 1,073,741,824 for 22.5 GiB, or by 1,000,000,000 for 24.15919104 GB. The segment count is an assumption, not a bound implied by a 60-second timeout. No PostgreSQL timing or recovery experiment was performed.",
    "Correction on 2026-11-28: removed the earlier 1,440-segment upper-bound claim and ambiguous 23 GB figure; separated shell exit status from library boolean success.",
    "Native probe: Ubuntu pg_archivecleanup 16.15-0ubuntu0.24.04.1, executed 2026-11-28. Seven selection cases plus three rejection controls used -n in a disposable synthetic directory; every filename, size and SHA256 stayed unchanged.",
    "Results publish commands, output and binary/probe hashes. PostgreSQL 18.0 was source-reviewed, not executed. No recovery or deletion experiment was performed."
  ]
}
