Fix behavior of "force" in pgstat_report_wal()

As implemented in 5891c7a8ed, setting "force" to true in
pgstat_report_wal() causes the routine to not wait for the pgstat
shmem lock if it cannot be acquired, in which case the WAL and I/O
statistics finish by not being flushed.  The origin of the confusion
comes from pgstat_flush_wal() and pgstat_flush_io(), that use "nowait"
as sole argument.  The I/O stats are new in v16.

This is the opposite behavior of what has been used in
pgstat_report_stat(), where "force" is the opposite of "nowait".  In
this case, when "force" is true, the routine sets "nowait" to false,
which would cause the routine to wait for the pgstat shmem lock,
ensuring that the stats are always flushed.  When "force" is false,
"nowait" is set to true, and the stats would only not be flushed if the
pgstat shmem lock can be acquired, returning immediately without
flushing the stats if the lock cannot be acquired.

This commit changes pgstat_report_wal() so as "force" has the same
behavior as in pgstat_report_stat().  There are currently three callers
of pgstat_report_wal():
- Two in the checkpointer where force=true during a shutdown and the
main checkpointer loop.  Now the code behaves so as the stats are always
flushed.
- One in the main loop of the bgwriter, where force=false.  Now the code
behaves so as the stats would not be flushed if the pgstat shmem lock
could not be acquired.

Before this commit, some stats on WAL and I/O could have been lost after
a shutdown, for example.

Reported-by: Ryoga Yoshida
Author: Ryoga Yoshida, Michael Paquier
Discussion: https://postgr.es/m/f87a4d7be70530606b864fd1df91718c@oss.nttdata.com
Backpatch-through: 15
This commit is contained in:
Michael Paquier 2023-09-26 09:30:39 +09:00
parent 3c065d5a7e
commit 802fcb9ed1
1 changed files with 12 additions and 1 deletions

View File

@ -38,11 +38,22 @@ static WalUsage prevWalUsage;
*
* Must be called by processes that generate WAL, that do not call
* pgstat_report_stat(), like walwriter.
*
* "force" set to true ensures that the statistics are flushed; note that
* this needs to acquire the pgstat shmem LWLock, waiting on it. When
* set to false, the statistics may not be flushed if the lock could not
* be acquired.
*/
void
pgstat_report_wal(bool force)
{
pgstat_flush_wal(force);
bool nowait;
/* like in pgstat.c, don't wait for lock acquisition when !force */
nowait = !force;
/* flush wal stats */
pgstat_flush_wal(nowait);
}
/*