Getting Started with pg_hardstorage: Setup, Streaming WAL, and Retention’s

Introduction

Protecting PostgreSQL databases is an important responsibility for every database administrator. A reliable backup solution should protect database information, support recovery during failures, and manage storage efficiently.

pg_hardstorage 1.7 provides a backup and recovery workflow for PostgreSQL databases. It supports full backups, WAL streaming, deduplication, encryption, restoration, verification, and retention management.

In this blog, I will explain my understanding and hands-on experience with pg_hardstorage 1.7 through three important steps:

  1. Backup – Protecting PostgreSQL database data.
  2. Restoration – Recovering and validating database backups.
  3. Retention – Managing old backups and WAL storage.

Understanding the Backup Process

At a high level, the pg_hardstorage workflow includes:

  • Connecting to the PostgreSQL instance and identifying the database cluster.
  • Creating a backup and storing the required data in a repository.
  • Collecting the WAL information required for recovery.
  • Reusing matching data chunks through deduplication where applicable.
  • Restoring the backup and validating the recovered files.

A backup contains the database files captured up to a recovery point. WAL (Write-Ahead Log) records describe ongoing database changes and are essential for restoring the database to a consistent state.

Step 1: Backup – Protecting PostgreSQL Data

1.1. Taking a Full Backup

My first practical task was to take a full backup and inspect its details. I checked the backup ID, PostgreSQL system identifier, timeline, start and stop LSN, backup size, and chunk information.

[root@pgnode3 ~]# sudo -u pgbackup pg_hardstorage backup pg-tutorial \
  --repo file:///srv/hs-patroni-repo
12:17:29 [INFO ] backup.dedup_hints_loaded  deployment=pg-tutorial
  body: {
          "hint_chunks": 2432
        }
12:17:29 [INFO ] backup.pg_probed
  body: {
          "pg_version": 17,
          "raw": "17.10 EE 1.4.5"
        }
12:17:29 [INFO ] backup.identified
  body: {
          "system_id": "7673105448888731611",
          "timeline": 142,
          "xlogpos": "19/41000060"
        }
12:17:29 [INFO ] backup.started  tenant=default deployment=pg-tutorial backup_id=pg-tutorial.full.20261125T121729Z.4154
12:17:30 [INFO ] backup.stream_complete
  body: {
          "bytes_received": 114135241,
          "messages": 6912,
          "tablespaces": 1
        }
12:17:31 [NOTICE] backup.wal_gaps_embedded  deployment=pg-tutorial backup_id=pg-tutorial.full.20261125T121729Z.4154
  body: {
          "gap_count": 1,
          "hint": "this backup carries WAL-gap metadata; restore will refuse PITR within these ranges"
        }
12:17:31 [INFO ] backup.committed  tenant=default deployment=pg-tutorial backup_id=pg-tutorial.full.20261125T121729Z.4154 timeline=142 lsn=19/42000120
  body: {
          "chunks_deduped": 2391,
          "chunks_written": 223,
          "dedup_hit_rate": 0.91469013006886,
          "duration_ms": 2460,
          "file_count": 2286,
          "logical_bytes": 112592615,
          "primary_key": "manifests/pg-tutorial/backups/pg-tutorial.full.20261125T121729Z.4154/manifest.json",
          "total_chunk_refs": 2614,
          "unique_chunk_bytes": 100448802,
          "unique_chunk_count": 2080
        }
✓ Backup committed
  ID:               pg-tutorial.full.20261125T121729Z.4154
  Deployment:       pg-tutorial
  PostgreSQL:       17
  Cluster ID:       7673105448888731611
  Stop LSN / TLI:   19/42000120 / 142
  Files:            2286 in 1 tablespace(s)
  Logical bytes:    107.4 MiB
  Unique chunks:    2080 (95.8 MiB after dedup)
  Dedup ratio:      1.12x
  Duration:         2460 ms
  Encryption:       AES-256-GCM (per-backup DEK, wrapped under local KEK)
  Manifest:         manifests/pg-tutorial/backups/pg-tutorial.full.20261125T121729Z.4154/manifest.json
[root@pgnode3 ~]#

This helped me understand that a successful backup should be identified and inspected.

1.2. WAL Streaming and Deduplication

In this, we will understand how PostgreSQL WAL changes are collected and how repeated data is handled as chunks under the repository.

1.3 Why WAL Streaming Is Required

Some of the questions that occurred to me were: If we already have a full backup, why do we need WAL streaming? When do we need to start WAL streaming? What happens if we start WAL streaming before backup and after backup?

  • If WAL streaming is not started before backup: WAL generated during the backup may not be captured, potentially affecting recovery consistency or leaving a WAL gap.
  • If WAL streaming is not started after backup: Subsequent database changes are not continuously captured, so recovery is limited to the available backup and archived WAL
  • Before backup: Start WAL streaming first to ensure no WAL changes are missed during backup creation.
  • After backup: WAL streaming captures ongoing database changes, allowing recovery beyond the full backup’s timestamp.

Imagine we have a PostgreSQL database running in production. At 9:50 AM, we start WAL streaming in pg_hardstorage to continuously capture database changes. At 10:00 AM, we start taking a full backup of the database. While the backup is running, users continue performing INSERT, UPDATE, and DELETE operations. All these changes generate WAL records, which are continuously collected by WAL streaming. At 10:30 AM, the full backup is completed, but WAL streaming continues capturing new changes. Suddenly, at 11:00 AM, the database crashes. Now, we can restore the full backup taken at 10:00 AM and replay the collected WAL records to recover the database changes that happened after the backup. This helps us recover the database closer to 11:00 AM instead of losing all the changes made after 10:00 AM, provided all required WAL records are available.

A full backup represents the database at a particular recovery point. WAL records contain changes that happen as PostgreSQL continues running. Collecting the required WAL helps make recovery possible beyond the backup’s starting point.

1.4 Checking Available WAL

I used the WAL list command to inspect available WAL segments and their timeline and LSN information:

[root@pgnode3 ~]# sudo -u pgbackup pg_hardstorage wal list pg-tutorial \
  --repo file:///srv/hs-patroni-repo
WAL inventory for pg-tutorial
  TLI 122: 11 segments (#5078..#5088)
  TLI 123: 3 segments (#5089..#5091)
  TLI 124: 6 segments (#5092..#5097)
  TLI 125: 1 segments (#5098..#5098)
  TLI 126: 1 segments (#5099..#5099)
  TLI 127: 1 segments (#5100..#5100)
  TLI 128: 1 segments (#5101..#5101)
  TLI 129: 1 segments (#5102..#5102)
  TLI 130: 1 segments (#5103..#5103)
  TLI 131: 2 segments (#5104..#5105)
  TLI 132: 4 segments (#5106..#5109)
  TLI 133: 2 segments (#5110..#5111)
  TLI 134: 1 segments (#5112..#5112)
  TLI 135: 1 segments (#5113..#5113)
  TLI 136: 750 segments (#5114..#5863)
  TLI 137: 436 segments (#5864..#6299)
  TLI 138: 9 segments (#6300..#6308)
  TLI 139: 55 segments (#6309..#6363)
  TLI 140: 43 segments (#6364..#6406)
  TLI 141: 3 segments (#6407..#6409)
  TLI 142: 59 segments (#6410..#6468)
  ✓ no gaps detected
  0000008E0000001900000025  TLI 142  19/25000000 -> 19/26000000
  0000008E0000001900000026  TLI 142  19/26000000 -> 19/27000000
  0000008E0000001900000027  TLI 142  19/27000000 -> 19/28000000
  0000008E0000001900000028  TLI 142  19/28000000 -> 19/29000000
  0000008E0000001900000029  TLI 142  19/29000000 -> 19/2A000000
  0000008E000000190000002A  TLI 142  19/2A000000 -> 19/2B000000
  0000008E000000190000002B  TLI 142  19/2B000000 -> 19/2C000000
  0000008E000000190000002C  TLI 142  19/2C000000 -> 19/2D000000
  0000008E000000190000002D  TLI 142  19/2D000000 -> 19/2E000000
  0000008E000000190000002E  TLI 142  19/2E000000 -> 19/2F000000
  0000008E000000190000002F  TLI 142  19/2F000000 -> 19/30000000
  0000008E0000001900000030  TLI 142  19/30000000 -> 19/31000000
  0000008E0000001900000031  TLI 142  19/31000000 -> 19/32000000
  0000008E0000001900000032  TLI 142  19/32000000 -> 19/33000000
  0000008E0000001900000033  TLI 142  19/33000000 -> 19/34000000
  0000008E0000001900000034  TLI 142  19/34000000 -> 19/35000000
  0000008E0000001900000035  TLI 142  19/35000000 -> 19/36000000
  0000008E0000001900000036  TLI 142  19/36000000 -> 19/37000000
  0000008E0000001900000037  TLI 142  19/37000000 -> 19/38000000
  0000008E0000001900000038  TLI 142  19/38000000 -> 19/39000000
  0000008E0000001900000039  TLI 142  19/39000000 -> 19/3A000000
  0000008E000000190000003A  TLI 142  19/3A000000 -> 19/3B000000
  0000008E000000190000003B  TLI 142  19/3B000000 -> 19/3C000000
  0000008E000000190000003C  TLI 142  19/3C000000 -> 19/3D000000
  0000008E000000190000003D  TLI 142  19/3D000000 -> 19/3E000000
  0000008E000000190000003E  TLI 142  19/3E000000 -> 19/3F000000
  0000008E000000190000003F  TLI 142  19/3F000000 -> 19/40000000
  0000008E0000001900000040  TLI 142  19/40000000 -> 19/41000000
  0000008E0000001900000041  TLI 142  19/41000000 -> 19/42000000
  0000008E0000001900000042  TLI 142  19/42000000 -> 19/43000000
  0000008E0000001900000043  TLI 142  19/43000000 -> 19/44000000
  0000008E0000001900000044  TLI 142  19/44000000 -> 19/45000000

This helped me check which WAL segments were present and whether the expected sequence was available.

1.5 Understanding Replication Slots

A replication slot helps PostgreSQL retain WAL that a consumer may still need. While exploring pg_hardstorage on my three-node Patroni-managed HA cluster, I became curious about how I could check if a replication slot exists and whether it is active or not, and how I could reactivate an inactive slot.

In a three-node PostgreSQL HA cluster using Patroni, pg_hardstorage uses a physical replication slot to continuously collect WAL records from the primary node and store them in the backup repository.

To check the replication slot, connect to the current primary node and run:

postgres=# SELECT slot_name, active, restart_lsn, wal_status
FROM pg_replication_slots;
          slot_name           | active | restart_lsn | wal_status
------------------------------+--------+-------------+------------
 logi                         | f      | 0/5001F50   | extended
 pgnode1                      | t      | 19/45000060 | reserved
 pg_hardstorage_pg_tutorial   | t      | 19/45000000 | reserved
 pg_hardstorage_pg_tutorial11 | f      | 19/41000000 | reserved
 pg_hardstorage_pg_tutorial_1 | f      | 19/20000028 | reserved
(5 rows)

Here, slot_name shows the slot name, active tells whether WAL streaming is happening, and restart_lsn shows the WAL position PostgreSQL needs to retain.

For example, assume pgnode1 is the primary and pgnode2 and pgnode3 are standby nodes. pg_hardstorage collects WAL from pgnode1 using the slot pg_hardstorage_pg_tutorial. If the WAL streaming process stops, the slot may become inactive(active = false). This does not mean the slot is lost. If the required WAL records are still available, pg_hardstorage can reconnect using the same slot and continue collecting WAL.

If the slot is inactive, first check whether it exists and whether the required WAL records are available. If the slot is still valid, restart WAL streaming and reconnect. If the required WAL records are missing, use the archived WAL in the pg_hardstorage repository to repair the gap, if available. Otherwise, a new backup and WAL starting point may be required. The pg_hardstorage repair slot command can help check and repair the slot.

For a three-node Patroni cluster, the recommended approach is to configure the pg_hardstorage slot as a permanent physical replication slot in Patroni. This helps maintain the slot across primary changes. For example, if pgnode1 fails and Patroni promotes pgnode2 as the new primary, you pg_hardstorage can reconnect to pgnode2 and continue WAL streaming, provided the required WAL is available.

1.6 Understanding Deduplication

pg_hardstorage Uses a deduplication method to save storage space on disk. For this, I compared the first backup with a later backup. Deduplication can reuse matching chunks already stored in the repository instead of storing identical content again. Therefore, the logical backup size and the amount of new physical storage used may be different.

12:17:31 [INFO ] backup.committed  tenant=default deployment=pg-tutorial backup_id=pg-tutorial.full.20261125T121729Z.4154 timeline=142 lsn=19/42000120
  body: {
          "chunks_deduped": 2391,
          "chunks_written": 223,
          "dedup_hit_rate": 0.91469013006886,
          "duration_ms": 2460,
          "file_count": 2286,
          "logical_bytes": 112592615,
          "primary_key": "manifests/pg-tutorial/backups/pg-tutorial.full.20261125T121729Z.4154/manifest.json",
          "total_chunk_refs": 2614,
          "unique_chunk_bytes": 100448802,
          "unique_chunk_count": 2080
        }
✓

Step 2: Restore and Validation

Restoring the backup is only the first step, validation is also important

2.1 Restoring a Backup

After taking backups, I restored one into a separate directory. I checked the restored files and PostgreSQL control information to understand whether the database directory had been reconstructed correctly.

Understand the Restoration Process

During restoration, pg_hardstorage:

  • Reads the backup manifest.
  • Identifies the required chunks.
  • Fetches backup chunks from the repository.
  • Decrypts and decompresses data when configured.
  • Reconstructs the PostgreSQL data directory.
  • Retrieves the necessary WAL for recovery when required.
sudo -u pgbackup pg_hardstorage restore pg-tutorial \
  --repo file:///srv/hs-patroni-repo \
  --target /tmp/hs-restore-new

2.2 Validating the Restored Backup

Use PostgreSQL’s pg_verifybackup verification utility:

sudo -u postgres /usr/pgsql-17/bin/pg_verifybackup \
  /tmp/hs-restore

For encrypted backups, provide the required encryption key command:

sudo -u postgres /usr/pgsql-17/bin/pg_verifybackup \
  --encryption-key-command='/etc/pgee/keystore/pg_tde_key.sh' \
  /tmp/hs-restore

A successful verification confirms that the backup files and required WAL data pass the verification checks.

2.3 Start the Restored PostgreSQL Instance

After validation, start the restored instance using a separate port.

sudo -u postgres /usr/pgsql-17/bin/pg_ctl \
  -D /tmp/hs-restore \
  -l /tmp/hs-restore/startup.log \
  -o "-p 5440 -h 127.0.0.1" \
start

Connect to PostgreSQL:

psql -h 127.0.0.1 -p 5440 -U postgres

Check database recovery status:

SELECT version();
SELECT pg_is_in_recovery();

2.4 Starting a Separate PostgreSQL Instance

Start PostgreSQL from a restored data directory on a separate port to verify backup restore validation on a different standby port.

sudo -u postgres /usr/pgsql-17/bin/pg_ctl \
  -D /tmp/hs-restore-new \
  -l /tmp/hs-restore-new/startup.log \
  -o "-p 5440 -h 127.0.0.1" start

Step 3: Retention – Managing Backup and WAL Storage

Retention is the process of deciding how long backups and WAL files should be maintained.

As database backups increase, repository storage consumption also increases. Retention policies help manage storage and preserve the backups required for recovery.

3.1. GFS Retention

GFS stands for Grandfather-Father-Son. It maintains backups at daily, weekly, monthly and yearly intervals.

Example:
retention:
  policy: gfs
  keep_daily: 7
  keep_weekly: 4
  keep_monthly: 12
  keep_yearly: 5

This approach maintains selected backups for short-term and long-term recovery.

3.2. Time-Based Retention

Time-based retention keeps backups for a specified duration.

Example: Keep backups for 14 days.

sudo -u pgbackup pg_hardstorage rotate pg-tutorial \
  --repo file:///srv/hs-patroni-repo \
  --policy simple \
  --keep-for 14d

To apply the policy:

sudo -u pgbackup pg_hardstorage rotate pg-tutorial \
  --repo file:///srv/hs-patroni-repo \
  --policy simple \
  --keep-for 14d \
  --apply

3.3. Count-Based Retention

Count-based retention keeps a specified number of the latest full backups.

Example: Keep the latest five full backups.

sudo -u pgbackup pg_hardstorage rotate pg-tutorial \
  --repo file:///srv/hs-patroni-repo \
  --policy count \
  --keep-fulls 5 \
  --apply

3.4. WAL Retention and Pruning

Backup retention and WAL retention are different.

  • Backup retention controls how many backups are preserved.
  • WAL retention controls which archived WAL segments remain available for recovery.

Example: Keep the recent 14 days of WAL, subject to the recovery requirements of retained backups.

sudo -u pgbackup pg_hardstorage wal prune pg-tutorial \
  --repo file:///srv/hs-patroni-repo \
  --keep-since 14d

Apply the pruning:

sudo -u pgbackup pg_hardstorage wal prune pg-tutorial \
  --repo file:///srv/hs-patroni-repo \
  --keep-since 14d \
  --apply

3.5. Repository Garbage Collection

After backup rotation and WAL pruning, unused chunks can be reclaimed through garbage collection.

sudo -u pgbackup pg_hardstorage repo gc \
  file:///srv/hs-patroni-repo \
  --apply

My Learning: I understood how backup retention, WAL pruning, and garbage collection work together to control repository storage without removing data required for retained recovery points.

What’s Improved in pg_hardstorage 1.7?

Version 1.7 includes improvements in concurrency, performance, and reliability.

Some important improvements include:

  • Improved concurrent backup uploads.
  • Improved concurrent restore fetching and writing.
  • Better WAL streaming and walsender timeout handling.
  • Improved full verification performance.
  • Better handling of interrupted chunk reads.
  • Improved manifest streaming and repository operations.
  • Better compatibility with certain S3-compatible storage systems.

These improvements help make backup, restoration, and verification operations more efficient.

Here is the demo:

https://youtu.be/W_-G-KmW2fs

Conclusion

Working with pg_hardstorage 1.7 helped me understand the complete PostgreSQL backup and recovery lifecycle.

Through implementation of backup using pg_hardstorage, I learned how to take full backups, configure WAL streaming, understand deduplication, restore database files, validate backup integrity, and manage repository storage using retention policies.

The most important lesson I learned is that taking a backup is not enough. We must also ensure that the backup can be successfully restored and validated.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top