Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e847I-0007FR-DI for pgsql-performance@arkaria.postgresql.org; Fri, 27 Oct 2017 12:52:04 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e847H-00070U-Oy for pgsql-performance@arkaria.postgresql.org; Fri, 27 Oct 2017 12:52:03 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e845T-0003t5-Cv for pgsql-performance@postgresql.org; Fri, 27 Oct 2017 12:50:11 +0000 Received: from mout.perfora.net ([74.208.4.197]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1e845P-0006zg-ON for pgsql-performance@postgresql.org; Fri, 27 Oct 2017 12:50:10 +0000 Received: from [192.168.1.101] ([68.98.128.17]) by mrelay.perfora.net (mreueus001 [74.208.5.2]) with ESMTPSA (Nemesis) id 0McXLC-1dqXCT1i6a-00HqIC; Fri, 27 Oct 2017 14:50:01 +0200 Message-ID: <59F32B79.2070307@sqlexec.com> Date: Fri, 27 Oct 2017 08:50:01 -0400 From: MichaelDBA User-Agent: Postbox 5.0.20 (Windows/20171012) MIME-Version: 1.0 To: Stefan Petrea CC: "pgsql-performance@postgresql.org" Subject: Re: WAL still kept in pg_xlog even long after heavy workload is done References: In-Reply-To: Content-Type: multipart/alternative; boundary="------------090304050605070105070508" X-Provags-ID: V03:K0:L6FM5OxZ6QC/l0Livg1c9Rs55gqIj3B1wrYxcjAiCUC6tGP9vEN /iacDaFW7TNAyv6PlFyUa0hVh7qtEtngoNMlajW1SF7dU26Yg7MLcZ0CBW2Z0k5yDlG3wEl Dv3IA5mScnHkho6Rgyo1dAvE1RBC1tBbspPXCrptMx3LkQMZfnZybL9GX0jmk6igQpjYx+J wTc3gHq5h3eLv1ltEQGAQ== X-UI-Out-Filterresults: notjunk:1;V01:K0:tU1BlZujK0U=:k6hyask1s9WKTmvNf7/GM+ C7RFRVMBM59yL/qusdBJWLFsMp9kUsYHmCHNa7WiicF3tWofSKRWjV8xO2zGMQaR6O5gpajah sBLZdxgPs+vzitMPGC4ScAm4PCV6eCglQcif8ozmTTy9uk5mr/unyccPMNin0ODK4tY9bvAdS F5ue2vTS7piKvLzKmxfk2KwHdkJ5S4EYpyn9gO9O6ECj45yEmqJ/GzeADKNKR6vZupo4cRuGg o5plLmUmCewMZp4PvBbO6hLAZjRQboEDQ0EClZfr0K+CWF0HWSzQL1X2zyW+Bl4gLT8LhtE4D KOVcHz/pVMI4VPT8MO49p4yghubmPUjkwZii04c1j6w7CMfabhPCQDRKs56kppz1xz+iasbKp Y0aJlkqzlsLeDpRdXgeC6VFIV4Wukuhr9ehkztVYpYrLVsk6pTnURjfaW6egDhl/lDI7L1bPL PXPmFN0m6dy9ZtMyqj8WagTYmo8dXiAaoaiqr9dvjQ7TIKVztgdt5hf5NhIHV/b0zj7wJdp7D e7pARNEIVPcXcu5T++KshfWx65MpGLVvKS5odWwbGTwOgxUilJCzyatcSnZpD7BQZo+xFTbkm R4EB8YT2gZIUuwyliCclWVOmLx3Kyiw0R/Vvys6AVOSG5+yFUld558eycMCik4e8l/obxOnzF jWCLQZTeHrvs5NohOWJgEe1KipLKSXs/K4iDO0p7INV4TFXf8/UyDjmNRLfcEV0skCWi/p7m8 hcUkmXNLBE+QHhgsqFZ0AVqMYB9tnYj/bHs4b7McXX9KHfwdMKoPolfIRnw= List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org This is a multi-part message in MIME format. --------------090304050605070105070508 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit To get the values right, you have to consider the "unit" column in pg_settings. On mine, it is 16M for both min and max wal size. So it would be 1024 x 1024 x 16 x (pg_settings.min_wal_size or pg_settings.max_wal_size) The result of this formula should be close to what you specified in postgresql.conf. So modify your SQL a bit: psql -c "select name, setting, unit from pg_settings where name like '%wal_size';" Regards, Michael Vitale > Stefan Petrea > Friday, October 27, 2017 7:28 AM > Hello, > > 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 > around. > There were a few occasions where we had to resize partitions because > pg_xlog grew as much as it did. > > According to the docs [1] there are some parameters in GUC > (postgresql.conf) about this. > The parameters I've been able to identify are the following: > > * wal_keep_segments > * max_wal_size > * min_wal_size > > 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 > rows). > > I am seeing the following int he docs [3] > > min_wal_size (integer) > 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 > default > 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 > doing. There > 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 > max_wal_size. > So if I run the following query: > > psql -c "select name, setting from pg_settings where name like > '%wal_size';" > > I get the following: > > max_wal_size|2097152 > min_wal_size|1048576 > > Do these two values look ok? > > Both these values were generated by pgtune [4], 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 [6]. > > 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? > > * checkpoint_timeout > * checkpoint_completion_target > * checkpoint_flush_after > * checkpoint_warning > > ========= > > 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 > had run. > Should I expect that the size will eventually drop to 600MB or will it > just sit there at 1.4G? > > ========= > > Other thoughts: > > 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 [2]. > > 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 > XLOGfileslop. > > 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 > control > the amount of WAL kept in pg_xlog. > > Thank you, > Stefan > > [1] https://www.postgresql.org/docs/9.6/static/wal-configuration.html > [2] > https://github.com/postgres/postgres/blob/0c5803b450e0cc29b3527df3f352e6f18a038cc6/src/backend/access/transam/xlog.c#L2258 > [3] > https://www.postgresql.org/docs/9.6/static/runtime-config-wal.html#RUNTIME-CONFIG-WAL-CHECKPOINTS > [4] https://github.com/kmatt/pgtune > [5] https://github.com/kmatt/pgtune/blob/master/pgtune#L560 > [6] > https://github.com/postgres/postgres/blob/f49842d1ee31b976c681322f76025d7732e860f3/src/backend/utils/misc/guc.c#L2268 > > > Stefan Petrea > System Engineer > > stefan.petrea@tangoe.com > > > --------------090304050605070105070508 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit To get the values right, you have to consider the "unit" column in pg_settings.  On mine, it is 16M for both min and max wal size.  So it would be
1024 x 1024 x 16 x <value> (pg_settings.min_wal_size or pg_settings.max_wal_size)

The result of this formula should be close to what you specified in postgresql.conf.

So modify your SQL a bit:
psql -c "select name, setting, unit from pg_settings where name like '%wal_size';"

Regards,
Michael Vitale

Friday, October 27, 2017 7:28 AM
Hello,

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 around.
There were a few occasions where we had to resize partitions because
pg_xlog grew as much as it did.

According to the docs [1] there are some parameters in GUC (postgresql.conf) about this.
The parameters I've been able to identify are the following:

* wal_keep_segments
* max_wal_size
* min_wal_size

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 rows).

I am seeing the following int he docs [3]

min_wal_size (integer)
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 default
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 doing. There
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 max_wal_size.
So if I run the following query:

psql -c "select name, setting from pg_settings where name like '%wal_size';"

I get the following:

max_wal_size|2097152
min_wal_size|1048576

Do these two values look ok?

Both these values were generated by pgtune [4], 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 [6].

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?

* checkpoint_timeout
* checkpoint_completion_target
* checkpoint_flush_after
* checkpoint_warning

=========

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 had run.
Should I expect that the size will eventually drop to 600MB or will it just sit there at 1.4G?

=========

Other thoughts:

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 [2].

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
XLOGfileslop.

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 control
the amount of WAL kept in pg_xlog.

Thank you,
Stefan

[1] https://www.postgresql.org/docs/9.6/static/wal-configuration.html
[2] https://github.com/postgres/postgres/blob/0c5803b450e0cc29b3527df3f352e6f18a038cc6/src/backend/access/transam/xlog.c#L2258
[3] https://www.postgresql.org/docs/9.6/static/runtime-config-wal.html#RUNTIME-CONFIG-WAL-CHECKPOINTS
[4] https://github.com/kmatt/pgtune
[5] https://github.com/kmatt/pgtune/blob/master/pgtune#L560
[6] https://github.com/postgres/postgres/blob/f49842d1ee31b976c681322f76025d7732e860f3/src/backend/utils/misc/guc.c#L2268


Stefan Petrea
System Engineer

stefan.petrea@tangoe.com




--------------090304050605070105070508--