PostgreSQL pg_xlog and wal_keep_segments/max_wal_size

The question:

We are running postgresql on centos7
psql (PostgreSQL) 9.6.18

in the conf file wal_keep_segments was commented out which should revert to the default setting of 0, also max_wal_size was set to 2GB.
The pg_xlog dir has 7000+ wal files and the size of the dir is 112GB. Some of the wal files date back to 2017.

to test to see if the default for wal_keep_segments was not working correctly we set it to 200, yet the number of wal files continues to increase

I dont understand why these settings arent working, is there a way that these limits are overruled by other settings?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

max_wal_size:

Maximum size to let the WAL grow during automatic checkpoints. This is a soft limit; WAL size can exceed max_wal_size under special circumstances, like under heavy load, a failing archive_command, or a high wal_keep_segments setting.

This is the amount of WAL that triggers the creation of a checkpoint. Not related to pg_xlog maximum size.

wal_keep_segments is the amount of WAL to keep in excess of what is needed for crash recovery. This is the minimum size of the pg_xlog directory, not the maximum.


If postgresql does not remove old WAL segments, then they are needed for something. In addition to the needs of crash recovery and wal_keep_segments, these can be:

  • active archive_mode without working (or even not defined) archive_command. Check these settings and pg_stat_archiver system view.

  • inactive replication slot. Check pg_replication_slots system view.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment