In PostgreSQL 18, how do max_slot_wal_keep_size and idle_replication_slot_timeout each bound what a replication slot retains, and what does each cost?
answer
- one by size, one by time
- both act at checkpoints
- invalidated means lost for good
- wal_status walks toward lost
basics
~20 smax_slot_wal_keep_size invalidates a slot whose restart_lsn trails the current WAL by more than the given size; idle_replication_slot_timeout, new in 18, invalidates a slot unused for longer than the given time. Both act at checkpoints, protect the primary, and force the consumer to resync.
solid answer
~50 s`max_slot_wal_keep_size` (MB if no unit; default `-1`, unlimited; reload) is a **size** cap: at checkpoint time, a slot whose `restart_lsn` is more than that far behind loses its WAL. `wal_status` goes from `reserved` or `extended` to `unreserved`, then `lost`, with `invalidation_reason = 'wal_removed'`; an attached walsender is terminated. `idle_replication_slot_timeout`, new in PostgreSQL 18 (seconds if no unit; default `0`, off; reload), is a **time** cap: a slot inactive longer than that, judged from `inactive_since`, is invalidated at the next checkpoint with reason `idle_timeout`. It skips slots that reserve no WAL and synced slots on a standby. An invalidated slot releases its WAL and its `xmin`/`catalog_xmin` but can never be used again: drop it and rebuild the standby or resync the consumer. The size cap misses a quiet database with an old `catalog_xmin`; the time cap misses a busy one that fills the disk within the timeout. Use both.
code
ini · 3 lines# postgresql.conf on the primary; both apply on reload
max_slot_wal_keep_size = 100GB
idle_replication_slot_timeout = 2dgo deeper
Recall that slots can be limited by size with max_slot_wal_keep_size and, from 18, by idle time with idle_replication_slot_timeout.
Explain the defaults and units, that both act at checkpoints, the wal_status ladder from reserved to lost, and what invalidation_reason records.
Size both limits against pg_wal headroom and planned consumer downtime, and plan the resync path for a consumer whose slot is invalidated.
Decide per consumer whether primary availability or consumer continuity wins, since every limit converts a disk outage into a forced resync.
## Why a slot needs a limit A PostgreSQL **replication slot** keeps WAL from its `restart_lsn` and, for logical slots, system-catalog rows newer than its `catalog_xmin`, until its consumer confirms progress. By default there is no ceiling, so an abandoned or badly lagging consumer can fill `pg_wal` or bloat the catalogs. PostgreSQL 18 offers two ways to make the primary give up on a slot instead. ## max_slot_wal_keep_size: a size cap - **Meaning**: the most WAL that slots may retain in `pg_wal` at checkpoint time. - **Default**: `-1`, no limit. Without a unit the value is in **megabytes**. - **Context**: set in `postgresql.conf` or on the command line; a reload applies it. - **Behaviour**: when a slot's `restart_lsn` falls behind the current LSN by more than the limit, the next checkpoint may remove the WAL it needs. The server logs `invalidating obsolete replication slot`, and if a process is using the slot it logs `terminating process ... to release replication slot` and ends it. - **Monitoring**: `safe_wal_size` in `pg_replication_slots` is the number of bytes that can still be written before the slot is in danger of becoming `lost`; it is NULL when the limit is `-1`. ## idle_replication_slot_timeout: a time cap (new in 18) - **Meaning**: invalidate slots that have not been used by a replication connection for longer than this. - **Default**: `0`, which disables it. Without a unit the value is in **seconds**; `1d` or `12h` are accepted. - **Context**: `postgresql.conf` or the command line; a reload applies it. - **Behaviour**: inactivity is measured from `inactive_since`; invalidation happens at the **next checkpoint** after the timeout passes, so it can lag by up to a checkpoint interval (a manual `CHECKPOINT` forces it). The reason recorded is `idle_timeout`. - **Exemptions**: slots that do not reserve WAL (for example a physical slot created without `immediately_reserve` and never used), and slots on a standby that are being synced from the primary. ## Reading wal_status | `wal_status` | Meaning | Can it recover? | |---|---|---| | `reserved` | the claimed WAL is within `max_wal_size` | yes, normal | | `extended` | beyond `max_wal_size` but still retained by the slot or `wal_keep_size` | yes | | `unreserved` | the slot no longer retains all its WAL; some goes at the next checkpoint | yes, if the consumer catches up first | | `lost` | the slot is no longer usable | no | Any invalidated slot reports `lost`, whatever its `invalidation_reason`: `wal_removed`, `rows_removed`, `wal_level_insufficient` or `idle_timeout`. `invalidation_reason` and `inactive_since` were added in 17. ## What invalidation costs An invalidated slot stops holding anything: its WAL can be removed and its `xmin` and `catalog_xmin` no longer restrain VACUUM. But a consumer that tries to use it gets `can no longer access replication slot`, with a detail naming the reason. Recovery is always manual: 1. Drop the invalidated slot with `pg_drop_replication_slot`. 2. For a physical standby, rebuild it from a new base backup, or let it catch up from a WAL archive through `restore_command` if one exists. 3. For a logical consumer, create a new slot and re-synchronize its data. So each limit converts "the primary runs out of disk" into "one consumer needs a resync". That is usually the right trade, but it must be sized so a healthy consumer is never caught: - Set `max_slot_wal_keep_size` below the free space on the `pg_wal` file system, but above the WAL written during the longest lag you are willing to tolerate (a standby rebooting, a consumer redeploying). - Set `idle_replication_slot_timeout` longer than any planned consumer downtime, such as a weekend maintenance window. ## Why use both - A **busy** database can fill the disk with a few hours of WAL; a one-day idle timeout fires too late, and only the size cap protects it. - A **quiet** database writes little WAL, so the size cap is never reached, while an idle logical slot pins `catalog_xmin` for weeks and bloats the catalogs; only the time cap releases it. - Neither replaces monitoring: alert on `inactive_since`, retained bytes and `safe_wal_size` before either limit fires. - After either limit fires, the invalidated slot still occupies one of the `max_replication_slots` places until someone drops it, so a cleanup step belongs in the runbook too.
- A slot shows wal_status 'unreserved'. Is it already lost?Not yet. `unreserved` means the slot no longer retains all the WAL it needs and some of it will be removed at the next checkpoint. If the consumer catches up past that point before then, the status can return to `reserved` or `extended`. Once the checkpoint removes needed WAL and the slot is invalidated, it reports `lost` and must be dropped and recreated.
- Does wal_keep_size limit what a slot retains?No. `wal_keep_size` (default 0) keeps a minimum amount of extra WAL for standbys that do not use slots; it is a floor, not a ceiling, and it never invalidates a slot. Only `max_slot_wal_keep_size` caps slot retention by size.
- wal_keep_size caps how much WAL a replication slot may keep.
- idle_replication_slot_timeout drops the slot the moment the timeout passes.
- An invalidated slot resumes on its own once its consumer reconnects.
- max_slot_wal_keep_size is in 8 kB pages when written without a unit.
- wal_status 'unreserved' means the slot is already unusable.
Assumes PostgreSQL 18: idle_replication_slot_timeout is new in 18; inactive_since and invalidation_reason exist since 17; on 17 only the size cap is available.