Stefan Petrea <mailto:stefan.pet...@tangoe.com>
Friday, October 27, 2017 7:28 AM
We're encountering some problems with WAL growth in production with
PostgreSQL 9.6.3 and 9.6.2. From what I know a WAL file can either be
recycled(and would be reused) or deleted.
We'd like to have better control over the amount of WAL that is kept
There were a few occasions where we had to resize partitions because
pg_xlog grew as much as it did.
According to the docs  there are some parameters in GUC
(postgresql.conf) about this.
The parameters I've been able to identify are the following:
Our WAL grows a lot around the time of our product upgrades (that is,
when we upgrade to a new version of our database, so not a Pg upgrade,
just a newer version of our db schema, plpgsql code etc).
As part of this upgrade, we add new columns or have some large UPDATEs
on tables as big as 300M (but in one case we also have one with 1.5B
I am seeing the following int he docs 
As long as WAL disk usage stays below this setting, old WAL files are
always recycled for future use at a checkpoint, rather than removed.
This can be used to ensure that enough WAL space is reserved to handle
spikes in WAL usage, for example when running large batch jobs. The
is 80 MB. This parameter can only be set in the postgresql.conf file or
on the server command line.
This sounds very familiar because, that's essentially what we're
are some large jobs that cause a lot of workload and changes and
generate a lot of WAL.
So far, the way I interpret this is min_wal_size is the amount of WAL
recycled (that is kept around to be reused) and max_wal_size is the
total amount of WAL allowed to be kept on disk.
I would also like to interpret the default values of min_wal_size and
So if I run the following query:
psql -c "select name, setting from pg_settings where name like
I get the following:
Do these two values look ok?
Both these values were generated by pgtune , but it seems like pgtune
thinks they're expressed by default in KB.
Looking at the PostgreSQL code, it seems to me that these two are
expressed in MB, at least that's what I understand when I see
GUC_UNIT_MB in the source code .
So maybe the pgtune fork we're using has a bug in the sense that it
produces an incorrect value for those two parameters? (should be in MB
but is expressed in KB, therefore much higher than what it should be).
Another question is, how can I use any of the checkpoint settings
to control the WAL that is kept around?
I actually tried something with these settings on a test environment.
I've used the following settings:
checkpoint_timeout = 40s
min_wal_size = 600MB
max_wal_size = 900MB
Then I've created a db named x1 and ran this on it four or five times.
pgbench -i -s 70 x1
The pg_xlog directory grew to 2.2G and after a few minutes, it
decreased to 2.0G
After about 40 minutes it decreased to 1.4G and it's not going any lower.
I was expecting pg_xlog's size to be 600MB after the first WAL removal
Should I expect that the size will eventually drop to 600MB or will it
just sit there at 1.4G?
I have looked a bit at Pg internals too, I'm seeing four functions
there that are responsible for removing WAL: XLogArchiveIsReady,
RemoveXlogFile, RemoveOldXlogFiles, XLOGfileslop.
All of these belong to /src/backend/access/transam/xlog.c
The only place in the code that seems to take a decision about how much
WAL to recycle and how much to remove is the function XLOGfileslop .
It seems like XLOGfileslop is an estimate for the number of WAL to keep
around(recycled WAL). Both max_wal_size and min_wal_size are used inside
As far as checkpoint_* GUC settings go, they seem to be involved as well.
So far, the only thing I know about checkpoints is that between
checkpoints, many WAL are created. The amount of WAL between checkpoints
can vary. I don't have a good understanding about the interplay between
checkpoints and WAL.
I'd be grateful for any thoughts on how to improve this, and better
the amount of WAL kept in pg_xlog.