Skip to content

Commit 78f9506

Browse files
committed
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
1 parent 89c546c commit 78f9506

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

src/backend/postmaster/pgstat.c

+30-14
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ static void pgstat_send_tabstats(TimestampTz now, bool disconnect);
348348
static void pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg, TimestampTz now);
349349
static void pgstat_update_dbstats(PgStat_MsgTabstat *tsmsg, TimestampTz now);
350350
static void pgstat_send_funcstats(void);
351+
static void pgstat_wal_initialize(void);
352+
static bool pgstat_wal_pending(void);
351353
static void pgstat_send_slru(void);
352354
static HTAB *pgstat_collect_oids(Oid catalogid, AttrNumber anum_oid);
353355
static bool pgstat_should_report_connstat(void);
@@ -882,17 +884,10 @@ pgstat_report_stat(bool disconnect)
882884

883885
/*
884886
* Don't expend a clock check if nothing to do.
885-
*
886-
* To determine whether any WAL activity has occurred since last time, not
887-
* only the number of generated WAL records but also the numbers of WAL
888-
* writes and syncs need to be checked. Because even transaction that
889-
* generates no WAL records can write or sync WAL data when flushing the
890-
* data pages.
891887
*/
892888
if (!have_relation_stats &&
893889
pgStatXactCommit == 0 && pgStatXactRollback == 0 &&
894-
pgWalUsage.wal_records == prevWalUsage.wal_records &&
895-
WalStats.m_wal_write == 0 && WalStats.m_wal_sync == 0 &&
890+
!pgstat_wal_pending() &&
896891
!have_function_stats && !disconnect)
897892
return;
898893

@@ -3168,12 +3163,7 @@ pgstat_initialize(void)
31683163
{
31693164
Assert(!pgstat_is_initialized);
31703165

3171-
/*
3172-
* Initialize prevWalUsage with pgWalUsage so that pgstat_send_wal() can
3173-
* calculate how much pgWalUsage counters are increased by subtracting
3174-
* prevWalUsage from pgWalUsage.
3175-
*/
3176-
prevWalUsage = pgWalUsage;
3166+
pgstat_wal_initialize();
31773167

31783168
/* Set up a process-exit hook to clean up */
31793169
before_shmem_exit(pgstat_shutdown_hook, 0);
@@ -3415,6 +3405,32 @@ pgstat_send_wal(bool force)
34153405
MemSet(&WalStats, 0, sizeof(WalStats));
34163406
}
34173407

3408+
static void
3409+
pgstat_wal_initialize(void)
3410+
{
3411+
/*
3412+
* Initialize prevWalUsage with pgWalUsage so that pgstat_send_wal() can
3413+
* calculate how much pgWalUsage counters are increased by subtracting
3414+
* prevWalUsage from pgWalUsage.
3415+
*/
3416+
prevWalUsage = pgWalUsage;
3417+
}
3418+
3419+
/*
3420+
* To determine whether any WAL activity has occurred since last time, not
3421+
* only the number of generated WAL records but also the numbers of WAL
3422+
* writes and syncs need to be checked. Because even transaction that
3423+
* generates no WAL records can write or sync WAL data when flushing the
3424+
* data pages.
3425+
*/
3426+
static bool
3427+
pgstat_wal_pending(void)
3428+
{
3429+
return pgWalUsage.wal_records != prevWalUsage.wal_records ||
3430+
WalStats.m_wal_write != 0 ||
3431+
WalStats.m_wal_sync != 0;
3432+
}
3433+
34183434
/* ----------
34193435
* pgstat_send_slru() -
34203436
*

0 commit comments

Comments
 (0)