1/* -------------------------------------------------------------------------
4 * Implementation of WAL statistics.
6 * This file contains the implementation of WAL statistics. It is kept
7 * separate from pgstat.c to enforce the line between the statistics access /
8 * storage implementation and the details about individual types of
11 * Copyright (c) 2001-2025, PostgreSQL Global Development Group
14 * src/backend/utils/activity/pgstat_wal.c
15 * -------------------------------------------------------------------------
25 * WAL usage counters saved from pgWalUsage at the previous call to
26 * pgstat_report_wal(). This is used to calculate how much WAL usage
27 * happens between pgstat_report_wal() calls, by subtracting
28 * the previous counters from the current ones.
34 * Calculate how much WAL usage counters have increased and update
35 * shared WAL and IO statistics.
37 * Must be called by processes that generate WAL, that do not call
38 * pgstat_report_stat(), like walwriter.
40 * "force" set to true ensures that the statistics are flushed; note that
41 * this needs to acquire the pgstat shmem LWLock, waiting on it. When
42 * set to false, the statistics may not be flushed if the lock could not
50 /* like in pgstat.c, don't wait for lock acquisition when !force */
63 * Support function for the SQL-callable pgstat* functions. Returns
64 * a pointer to the WAL statistics struct.
75 * To determine whether WAL usage happened.
84 * Calculate how much WAL usage counters have increased by subtracting the
85 * previous counters from the current ones.
87 * If nowait is true, this function returns true if the lock could not be
88 * acquired. Otherwise return false.
101 * This function can be called even if nothing at all has happened. Avoid
102 * taking lock for nothing in that case.
108 * We don't update the WAL usage portion of the local WalStats elsewhere.
109 * Calculate how much WAL usage counters were increased by subtracting the
110 * previous counters from the current ones.
119#define WALSTAT_ACC(fld, var_to_add) \
120 (stats_shmem->stats.wal_counters.fld += var_to_add.fld)
130 * Save the current counters for the subsequent calculation of WAL usage.
141 * Initialize prevWalUsage with pgWalUsage so that pgstat_wal_flush_cb()
142 * can calculate how much pgWalUsage counters are increased by subtracting
143 * prevWalUsage from pgWalUsage.
162 memset(&stats_shmem->
stats, 0,
sizeof(stats_shmem->
stats));
bool IsPostmasterEnvironment
Assert(PointerIsAligned(start, uint64))
void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, const WalUsage *sub)
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
void LWLockRelease(LWLock *lock)
void LWLockInitialize(LWLock *lock, int tranche_id)
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
void pgstat_snapshot_fixed(PgStat_Kind kind)
PgStat_LocalState pgStatLocal
bool pgstat_flush_backend(bool nowait, bits32 flags)
#define PGSTAT_BACKEND_FLUSH_IO
#define PGSTAT_BACKEND_FLUSH_WAL
void pgstat_flush_io(bool nowait)
#define WALSTAT_ACC(fld, var_to_add)
void pgstat_wal_reset_all_cb(TimestampTz ts)
void pgstat_wal_init_shmem_cb(void *stats)
void pgstat_report_wal(bool force)
void pgstat_wal_init_backend_cb(void)
bool pgstat_wal_flush_cb(bool nowait)
static WalUsage prevWalUsage
void pgstat_wal_snapshot_cb(void)
PgStat_WalStats * pgstat_fetch_stat_wal(void)
static bool pgstat_wal_have_pending(void)
PgStat_ShmemControl * shmem
TimestampTz stat_reset_timestamp