pgstat: split out WAL handling from pgstat_{initialize,report_stat}.

A later commit will move the handling of the different kinds of stats into
separate files.  By splitting out WAL handling in this commit that later move
will just move code around without other changes.

Author: Andres Freund <andres@anarazel.de>
Discussion: https://postgr.es/m/20220303021600.hs34ghqcw6zcokdh@alap3.anarazel.de
This commit is contained in:
Andres Freund 2022-03-19 11:35:37 -07:00
parent 89c546c294
commit 78f9506b38
1 changed files with 30 additions and 14 deletions

View File

@ -348,6 +348,8 @@ static void pgstat_send_tabstats(TimestampTz now, bool disconnect);
static void pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg, TimestampTz now);
static void pgstat_update_dbstats(PgStat_MsgTabstat *tsmsg, TimestampTz now);
static void pgstat_send_funcstats(void);
static void pgstat_wal_initialize(void);
static bool pgstat_wal_pending(void);
static void pgstat_send_slru(void);
static HTAB *pgstat_collect_oids(Oid catalogid, AttrNumber anum_oid);
static bool pgstat_should_report_connstat(void);
@ -882,17 +884,10 @@ pgstat_report_stat(bool disconnect)
/*
* Don't expend a clock check if nothing to do.
*
* To determine whether any WAL activity has occurred since last time, not
* only the number of generated WAL records but also the numbers of WAL
* writes and syncs need to be checked. Because even transaction that
* generates no WAL records can write or sync WAL data when flushing the
* data pages.
*/
if (!have_relation_stats &&
pgStatXactCommit == 0 && pgStatXactRollback == 0 &&
pgWalUsage.wal_records == prevWalUsage.wal_records &&
WalStats.m_wal_write == 0 && WalStats.m_wal_sync == 0 &&
!pgstat_wal_pending() &&
!have_function_stats && !disconnect)
return;
@ -3168,12 +3163,7 @@ pgstat_initialize(void)
{
Assert(!pgstat_is_initialized);
/*
* Initialize prevWalUsage with pgWalUsage so that pgstat_send_wal() can
* calculate how much pgWalUsage counters are increased by subtracting
* prevWalUsage from pgWalUsage.
*/
prevWalUsage = pgWalUsage;
pgstat_wal_initialize();
/* Set up a process-exit hook to clean up */
before_shmem_exit(pgstat_shutdown_hook, 0);
@ -3415,6 +3405,32 @@ pgstat_send_wal(bool force)
MemSet(&WalStats, 0, sizeof(WalStats));
}
static void
pgstat_wal_initialize(void)
{
/*
* Initialize prevWalUsage with pgWalUsage so that pgstat_send_wal() can
* calculate how much pgWalUsage counters are increased by subtracting
* prevWalUsage from pgWalUsage.
*/
prevWalUsage = pgWalUsage;
}
/*
* To determine whether any WAL activity has occurred since last time, not
* only the number of generated WAL records but also the numbers of WAL
* writes and syncs need to be checked. Because even transaction that
* generates no WAL records can write or sync WAL data when flushing the
* data pages.
*/
static bool
pgstat_wal_pending(void)
{
return pgWalUsage.wal_records != prevWalUsage.wal_records ||
WalStats.m_wal_write != 0 ||
WalStats.m_wal_sync != 0;
}
/* ----------
* pgstat_send_slru() -
*