max_slot_wal_keep_size

A configuration parameter to limit the amount of storage used by replication slots

max_slot_wal_keep_size is a configuration parameter for limiting the amount of storage used by replication slots.

max_slot_wal_keep_size was added in PostgreSQL 13.

Default value

The default value for max_slot_wal_keep_size is: -1 (no limit).

Applying changes

Changes to max_slot_wal_keep_size can be applied with pg_reload_conf(), pg_ctl reload or SIGHUP.

Definition by PostgreSQL version

PostgreSQL 13

postgres=# SELECT * FROM pg_settings WHERE name='max_slot_wal_keep_size';
-[ RECORD 1 ]---+------------------------------------------------------------------------
name            | max_slot_wal_keep_size
setting         | -1
unit            | MB
category        | Replication / Sending Servers
short_desc      | Sets the maximum WAL size that can be reserved by replication slots.
extra_desc      | Replication slots will be marked as failed, and segments released for
                | deletion or recycling, if this much space is occupied by WAL on disk.
context         | sighup
vartype         | integer
source          | default
min_val         | -1
max_val         | 2147483647
enumvals        |
boot_val        | -1
reset_val       | -1
sourcefile      |
sourceline      |
pending_restart | f

Change history

Bug fixes

  • 2021-07-16: Advance old-segment horizon properly after slot invalidation (commit ead9e51e, backpatched to PostgreSQL 14 and 13)

Examples

If max_slot_wal_keep_size is set to a positive value, and a replication slot becomes inactive, the column safe_wal_size in pg_replication_slots will contain the number of bytes which can be written to WAL before the slot is invalidated, i.e. can no longer be used by the downstream connection to retrieve the required WAL:

postgres=# SELECT slot_name, slot_type, active, restart_lsn, wal_status, safe_wal_size
             FROM pg_replication_slots;
 slot_name | slot_type | active | restart_lsn | wal_status | safe_wal_size 
-----------+-----------+--------+-------------+------------+---------------
 node2     | physical  | f      | 0/301B830   | reserved   |      15057816
(1 row)

If the difference between the slot's restart_lsn and the current LSN exceeds the amount set in max_slot_wal_keep_size, the replication slot will be invalidated:  wal_status will be set to unreserved and (as of PostgreSQL 14) safe_wal_size will show a negative value:

postgres=# SELECT slot_name, slot_type, active, restart_lsn, wal_status, safe_wal_size
             FROM pg_replication_slots;
 slot_name | slot_type | active | restart_lsn | wal_status | safe_wal_size 
-----------+-----------+--------+-------------+------------+---------------
 node2     | physical  | f      | 0/301B830   | unreserved |     -80436096
(1 row)

At the next checkpoint, a log line like the following will be emitted:

[2021-08-15 23:41:21 UTC]    LOG:  00000: checkpoint starting: immediate force wait
[2021-08-15 23:41:21 UTC]    LOG:  00000: invalidating slot "node2" because its restart_lsn 0/301B830 exceeds max_slot_wal_keep_size

after which wal_status will be set to lost, and safe_wal_size will be set to NULL:

postgres=# SELECT slot_name, slot_type, active, restart_lsn, wal_status, safe_wal_size
             FROM pg_replication_slots;
 slot_name | slot_type | active | restart_lsn | wal_status | safe_wal_size 
-----------+-----------+--------+-------------+------------+---------------
 node2     | physical  | f      |             | lost       |              
(1 row)

Categories

GUC configuration item, Management / adminstration, Replication, Storage, WAL

See also

max_wal_size, wal_keep_size