Testing PostgreSQL replication slot WAL retention and slot loss

Testing PostgreSQL replication slot WAL retention and slot loss

Testing PostgreSQL replication slot WAL retention and slot loss

Database writes may continue normally after a replication client disconnects, even as the data disk keeps filling up. Growing application tables are not necessarily the cause: a PostgreSQL replication slot left without a consumer for an extended period can prevent old WAL from being recycled. Direct-attached NVMe storage on a dedicated server provides capacity, but it does not automatically constrain the database’s retention behavior. An investigation needs to answer three questions together: what is retaining WAL, how much is being retained, and whether replication can still resume once the retention budget is exceeded.

1. Define the test boundaries first

This test uses an isolated instance and a physical replication slot. It does not connect a real standby or modify production replication slots. You can follow this approach with PostgreSQL already installed on Ubuntu 24.04 or Debian 12. The version must support wal_status, safe_wal_size, and max_slot_wal_keep_size; confirm support first by querying the system catalogs and configuration parameters.

Connect to psql as the instance administrator. Verify the connection target before proceeding:

\set ON_ERROR_STOP on
SELECT current_database(), inet_server_addr(), inet_server_port();
SHOW data_directory;
SHOW server_version;
SHOW wal_level;
SHOW max_replication_slots;
SHOW max_slot_wal_keep_size;
SHOW wal_keep_size;
SHOW archive_mode;

wal_level must be replica or higher, and a replication slot must be available. Leave ample free disk space on the test instance and record the original settings. Do not run the write-generation steps if there are other replication slots, archiving failures, or real consumers.

Do not delete, move, or truncate files in pg_wal. Slot retention, WAL directory usage, and application data size are three different metrics; none is a substitute for the others.

2. Establish a retention baseline you can interpret

Create a test slot that reserves WAL immediately. This avoids an empty restart_lsn, which would prevent meaningful observation of a slot that has never been connected to:

SELECT *
FROM pg_create_physical_replication_slot('lab_hold', true);

SELECT slot_name, slot_type, active, restart_lsn,
       wal_status, safe_wal_size,
       pg_size_pretty(
         pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)::bigint
       ) AS retained_span
FROM pg_replication_slots
ORDER BY slot_name;

active=false means only that no connection is currently using the slot. It does not mean the slot can be deleted. Its consumer may have disconnected briefly, and the slot still marks the starting point needed for recovery. retained_span estimates the span between the current position and the restart position; it is not the number of disk bytes held exclusively by that slot. Multiple slots may retain the same files.

wal_status=reserved means the required WAL is within the normal retention range. extended means retention has exceeded that range because of a slot or other retention requirements. unreserved means retention is no longer guaranteed, not necessarily that the files have already been deleted. Only lost indicates that the required WAL is no longer available.

safe_wal_size is not free disk space, either. It estimates how much more WAL can be generated before the slot may reach the point of becoming unusable. If the value is null, interpret it alongside any unlimited-retention setting and the slot state. Do not treat it as zero or as a sign of health.

3. Set a limit and generate WAL in a controlled way

Set a small retention limit on the test instance only:

ALTER SYSTEM SET max_slot_wal_keep_size = '64MB';
SELECT pg_reload_conf();

Then open a new connection and run SHOW max_slot_wal_keep_size; to confirm that the setting has taken effect. A successful return from the reload function does not guarantee that the intended value is active; also check the database logs for configuration errors. This parameter applies to the entire instance and cannot be set separately for an individual slot.

Create a regular, persistent table so that each round includes actual WAL activity before the switch:

CREATE SCHEMA slot_lab;
CREATE TABLE slot_lab.probe (
    id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    payload text NOT NULL
);

INSERT INTO slot_lab.probe(payload)
VALUES (repeat('x', 1024));
SELECT pg_switch_wal();

Treat the last two statements as one round and run eight rounds manually, checking the slot state and free disk space after each round. Do not turn this into an infinite loop. The default WAL segment size is usually 16MB, but the instance may use a different size, so run SHOW wal_segment_size; first. Eight rounds define a bounded test procedure, not a fixed threshold for slot loss.

An explicit switch advances to a segment boundary. Inserting a small amount of application data therefore does not mean that only a small amount of WAL space is consumed. Plan the budget around the segment size, not the size of the test table.

4. Validate slot loss with a checkpoint, not a full disk

After completing the bounded writes, run:

CHECKPOINT;

SELECT slot_name, restart_lsn, wal_status, safe_wal_size
FROM pg_replication_slots
WHERE slot_name = 'lab_hold';

SELECT pg_size_pretty(sum(size)::bigint) AS wal_directory_size
FROM pg_ls_waldir();

The retention limit takes effect during checkpoint processing; it is not a real-time disk quota. Exceeding the limit does not guarantee that a query will immediately show lost. Checkpoint recycling progress, wal_keep_size, other slots, and archive retention conditions can all affect what you observe.

What to check if the slot has not been lost

First confirm that the parameter is actually in effect. Then check whether the difference between the current LSN and restart_lsn exceeds the budget. Next, inspect other slots, archiving status, and retention settings. You can issue another checkpoint and save the before-and-after results, but do not generate unbounded writes just to force lost.

Validation should establish three things: an offline slot really does cause the retained span to grow; the limit is not a hard cap on the entire directory; and once the required WAL has been recycled, the slot can no longer serve its original recovery purpose. Keep a record of a normal state as well, so that the test does not leave only a failure screenshot with no way to explain how the state changed.

5. Handle disk-space alerts and replication recovery separately

Production monitoring should collect the slot name, type, activity state, retained span, wal_status, and free filesystem space together. A slot that remains offline while its retained span grows needs investigation. A transition to unreserved or lost requires escalation. Base alert thresholds on the actual WAL generation rate and disk budget rather than copying the test’s 64MB limit.

Recreating a lost slot with the same name does not restore old WAL. If a physical standby can retrieve the missing segments from a continuous WAL archive, follow the established recovery procedure. Otherwise, a new base backup is required. Logical consumers also involve snapshot consistency and consumption positions, so the recovery conclusions for a physical standby do not apply directly.

At the end of the test, save the query results first. Then remove the test objects after confirming that they have no consumers:

SELECT pg_drop_replication_slot('lab_hold');
DROP SCHEMA slot_lab CASCADE;

Finally, restore the original configuration and reload it. If the original value came from a configuration file, you can remove the override added by this test’s ALTER SYSTEM. If a value was already present in the automatic configuration, restore the recorded value rather than simply resetting it. Reconnect to verify the parameter, then observe changes in the directory after subsequent checkpoints.

The test is complete not when “disk usage has dropped,” but when slot ownership is clear, the retention budget is observable, the recovery path after slot loss is justified, and all test objects and temporary settings have been removed.

Frequently asked questions

Does max_slot_wal_keep_size cap the entire WAL directory?

No. It limits WAL a replication slot may retain at checkpoint time, not the size of the entire directory. Checkpoint timing, other retention requirements, and writes still affect disk usage, so free-space monitoring remains necessary.

Can a lost slot be recovered by recreating its name?

Reusing the name does not restore recycled WAL. A physical standby needs the required continuous WAL archive or a new base backup. Logical consumers also require a review of snapshot state, consumption position, and duplicate handling.

AMD EPYC 7543P Dedicated Server

From $459/month, with no setup fee or contract.

From Technical Notes to Full Server Configuration

Check memory, storage, and bandwidth, then choose a billing term that fits the workload.