PostgreSQL Replication: wal_keep_size vs. Slots

Updated

PostgreSQL database replication illustration

On PostgreSQL 13 and later, WAL retention for replication is set with wal_keep_size, measured in megabytes (e.g. wal_keep_size = 64MB), not wal_keep_segments — a server given the removed parameter refuses to start. The real decision is between that floor and a replication slot: too small a wal_keep_size leaves a lagging replica unable to resume; a slot keeps the replica safe but lets WAL pile up on the primary’s disk. They fail in opposite directions.

The wal_keep_segments parameter was removed in PostgreSQL 13 (released 2020) and replaced by wal_keep_size, which is measured in megabytes, not segment count. Paste wal_keep_segments = 64 into a current server and it refuses to start. Below is the real failure, the fix, and the tradeoff between wal_keep_size and a replication slot — demonstrated on two throwaway PostgreSQL 17.10 clusters, not described in the abstract.

The removed setting, tried on a real PostgreSQL 17.10 server

These three lines, unedited, on a fresh initdb cluster:

wal_level = replica
max_wal_senders = 3
wal_keep_segments = 64
$ pg_ctl -D primary start
waiting for server to start.... stopped waiting
pg_ctl: could not start server
Examine the log output.

2026-08-11 primary log:
LOG:  unrecognized configuration parameter "wal_keep_segments" in file ".../postgresql.conf" line 851
FATAL:  configuration file ".../postgresql.conf" contains errors

The server does not start. Not a warning, not a fallback — a hard startup failure, on 17.10 (the current release in August 2026) and every release since 13. The replacement, set in bytes/MB instead of a segment count (segments are 16MB by default):

wal_level = replica
max_wal_senders = 3
wal_keep_size = 64MB
$ psql -p 55601 -c "SHOW wal_keep_size;"
 wal_keep_size
---------------
 64MB

That fixes the startup failure. The full pg_hba.conf entry, pg_basebackup invocation, and standby recovery setup are covered — with real output and four documented failure modes for logical replication too — in physical vs. logical replication. This page won’t repeat that; it covers the one thing that page doesn’t.

What wal_keep_size actually trades against a replication slot

wal_keep_size is a blunt floor: keep at least this many megabytes of WAL on the primary, whether or not anything is reading it — megabytes of 16 MB segments that the primary is free to recycle the moment nothing needs them. A replication slot is a promise instead of a number: never recycle a WAL segment this specific replica still needs, no matter how much accumulates. The difference only shows up when a replica falls behind or disconnects.

Without a slot: the replica breaks and can’t resume

Primary configured with wal_keep_size = 16MB, no slot. Standby built with pg_basebackup -R, confirmed streaming, then stopped to simulate an outage. 250,000 rows inserted on the primary across several transactions, each followed by a checkpoint — enough WAL to push well past the 16MB floor:

$ psql -p 55601 -c "SELECT pg_size_pretty(sum(size)) FROM pg_ls_waldir();"
 96 MB   -- (then kept growing with more inserts)

Restarting the stopped replica after that:

LOG:  entering standby mode
LOG:  consistent recovery state reached at 0/30000A0
LOG:  started streaming WAL from primary at 0/3000000 on timeline 1
FATAL:  could not receive data from WAL stream: ERROR:  requested WAL segment
        000000010000000000000003 has already been removed

The segment the replica needed to resume from was already gone. This replica cannot catch up — the only fix is another full pg_basebackup from scratch. wal_keep_size bought some slack, but a fixed number of megabytes has no idea how far behind any specific replica actually is.

With a slot: the replica survives, but the primary’s disk doesn’t stop growing

Same setup, this time with SELECT pg_create_physical_replication_slot('hw_w20_slot'); on the primary and the standby built with pg_basebackup -R -S hw_w20_slot. Same outage — replica stopped, same volume of writes on the primary:

$ psql -p 55601 -c "SELECT slot_name, active FROM pg_replication_slots;"
 hw_w20_slot | f          -- replica disconnected, slot still held

$ psql -p 55601 -c "SELECT pg_size_pretty(sum(size)) FROM pg_ls_waldir();"
 before the writes:  48 MB
 after the writes:  272 MB

$ psql -p 55601 -c "SELECT slot_name, active, pg_size_pretty(pg_wal_lsn_diff(
                     pg_current_wal_lsn(), restart_lsn)) AS retained_wal
                     FROM pg_replication_slots;"
 hw_w20_slot | f | 255 MB retained, specifically for this slot

Restarting the replica after that, it resumed cleanly and reached the full 250,000 rows with no error — the slot did its job. But the primary held 255MB of WAL it would otherwise have recycled, purely because one replica hadn’t shown up to consume it, and wal_keep_size plays no part in that number — the slot’s promise overrides it. If that replica had stayed down for a week instead of a few seconds, that number keeps climbing until the primary’s disk fills up, at which point the primary itself stops accepting writes. That’s the real trade: a slot protects one replica at the cost of a promise the primary can’t take back from you.

What to actually do with this

Use wal_keep_size alone when a replica falling behind should eventually mean “rebuild it,” and you’d rather that than risk primary disk exhaustion. Use a slot when losing that replica’s sync is worse than the disk risk — and if you do, monitor pg_replication_slots.active and restart_lsn lag, and set max_slot_wal_keep_size (added in PostgreSQL 13) so an abandoned slot has a hard ceiling instead of an unbounded one. Neither setting is “safer” in the abstract; they fail in opposite directions.

What was and wasn’t verified here

Everything above — the startup failure, the fix, both the no-slot WAL-removal error and the with-slot disk growth, and the successful post-outage catch-up in both cases — was executed on two real PostgreSQL 17.10 initdb clusters on this machine and pasted unedited. Without a slot the lagging replica cannot resume, and with one the primary’s disk keeps filling; setup-data-replication-postgresql-ubuntu.sh shows both, after first confirming that wal_keep_segments stops the server, on a primary and two replicas on ports 55701 to 55703, and reports SKIP rather than FAIL when PostgreSQL’s server tools are missing or the timing-dependent no-slot failure does not reproduce. Not tested: behavior on PostgreSQL versions before 13 (where wal_keep_segments is still valid) — that’s a documented historical fact from the release notes, not something the runs above covered, since only 17.10 was installed.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.