PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pgstat_wal.c File Reference
#include "postgres.h"
#include "executor/instrument.h"
#include "utils/pgstat_internal.h"
Include dependency graph for pgstat_wal.c:

Go to the source code of this file.

Macros

#define WALSTAT_ACC(fld, var_to_add)    (stats_shmem->stats.wal_counters.fld += var_to_add.fld)
 

Functions

void pgstat_report_wal (bool force)
 
PgStat_WalStatspgstat_fetch_stat_wal (void)
 
bool pgstat_wal_flush_cb (bool nowait)
 
void pgstat_wal_init_backend_cb (void)
 
bool pgstat_wal_have_pending_cb (void)
 
void pgstat_wal_init_shmem_cb (void *stats)
 
void pgstat_wal_reset_all_cb (TimestampTz ts)
 
void pgstat_wal_snapshot_cb (void)
 

Variables

static WalUsage prevWalUsage
 

Macro Definition Documentation

◆ WALSTAT_ACC

#define WALSTAT_ACC (   fld,
  var_to_add 
)     (stats_shmem->stats.wal_counters.fld += var_to_add.fld)

Function Documentation

◆ pgstat_fetch_stat_wal()

PgStat_WalStats * pgstat_fetch_stat_wal ( void  )

Definition at line 67 of file pgstat_wal.c.

68{
70
71 return &pgStatLocal.snapshot.wal;
72}
void pgstat_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:1078
PgStat_LocalState pgStatLocal
Definition: pgstat.c:212
#define PGSTAT_KIND_WAL
Definition: pgstat_kind.h:40
PgStat_Snapshot snapshot
PgStat_WalStats wal

References PGSTAT_KIND_WAL, pgstat_snapshot_fixed(), pgStatLocal, PgStat_LocalState::snapshot, and PgStat_Snapshot::wal.

Referenced by pg_stat_get_wal().

◆ pgstat_report_wal()

void pgstat_report_wal ( bool  force)

Definition at line 46 of file pgstat_wal.c.

47{
48 bool nowait;
49
50 /* like in pgstat.c, don't wait for lock acquisition when !force */
51 nowait = !force;
52
53 /* flush wal stats */
54 (void) pgstat_wal_flush_cb(nowait);
56
57 /* flush IO stats */
58 pgstat_flush_io(nowait);
60}
bool pgstat_flush_backend(bool nowait, bits32 flags)
#define PGSTAT_BACKEND_FLUSH_IO
#define PGSTAT_BACKEND_FLUSH_WAL
void pgstat_flush_io(bool nowait)
Definition: pgstat_io.c:183
bool pgstat_wal_flush_cb(bool nowait)
Definition: pgstat_wal.c:82

References PGSTAT_BACKEND_FLUSH_IO, PGSTAT_BACKEND_FLUSH_WAL, pgstat_flush_backend(), pgstat_flush_io(), and pgstat_wal_flush_cb().

Referenced by BackgroundWriterMain(), CheckpointerMain(), summarizer_wait_for_wal(), WalReceiverMain(), and WalWriterMain().

◆ pgstat_wal_flush_cb()

bool pgstat_wal_flush_cb ( bool  nowait)

Definition at line 82 of file pgstat_wal.c.

83{
84 PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
85 WalUsage wal_usage_diff = {0};
86
88 Assert(pgStatLocal.shmem != NULL &&
90
91 /*
92 * This function can be called even if nothing at all has happened. Avoid
93 * taking lock for nothing in that case.
94 */
96 return false;
97
98 /*
99 * We don't update the WAL usage portion of the local WalStats elsewhere.
100 * Calculate how much WAL usage counters were increased by subtracting the
101 * previous counters from the current ones.
102 */
103 WalUsageAccumDiff(&wal_usage_diff, &pgWalUsage, &prevWalUsage);
104
105 if (!nowait)
106 LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
107 else if (!LWLockConditionalAcquire(&stats_shmem->lock, LW_EXCLUSIVE))
108 return true;
109
110#define WALSTAT_ACC(fld, var_to_add) \
111 (stats_shmem->stats.wal_counters.fld += var_to_add.fld)
112 WALSTAT_ACC(wal_records, wal_usage_diff);
113 WALSTAT_ACC(wal_fpi, wal_usage_diff);
114 WALSTAT_ACC(wal_bytes, wal_usage_diff);
115 WALSTAT_ACC(wal_buffers_full, wal_usage_diff);
116#undef WALSTAT_ACC
117
118 LWLockRelease(&stats_shmem->lock);
119
120 /*
121 * Save the current counters for the subsequent calculation of WAL usage.
122 */
124
125 return false;
126}
bool IsUnderPostmaster
Definition: globals.c:121
bool IsPostmasterEnvironment
Definition: globals.c:120
Assert(PointerIsAligned(start, uint64))
WalUsage pgWalUsage
Definition: instrument.c:22
void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, const WalUsage *sub)
Definition: instrument.c:287
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1182
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1902
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1353
@ LW_EXCLUSIVE
Definition: lwlock.h:114
#define WALSTAT_ACC(fld, var_to_add)
static WalUsage prevWalUsage
Definition: pgstat_wal.c:30
bool pgstat_wal_have_pending_cb(void)
Definition: pgstat_wal.c:143
PgStat_ShmemControl * shmem
PgStatShared_Wal wal

References Assert(), PgStat_ShmemControl::is_shutdown, IsPostmasterEnvironment, IsUnderPostmaster, PgStatShared_Wal::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockConditionalAcquire(), LWLockRelease(), pgstat_wal_have_pending_cb(), pgStatLocal, pgWalUsage, prevWalUsage, PgStat_LocalState::shmem, PgStat_ShmemControl::wal, WALSTAT_ACC, and WalUsageAccumDiff().

Referenced by pgstat_report_wal().

◆ pgstat_wal_have_pending_cb()

bool pgstat_wal_have_pending_cb ( void  )

Definition at line 143 of file pgstat_wal.c.

144{
146}
int64 wal_records
Definition: instrument.h:53

References pgWalUsage, prevWalUsage, and WalUsage::wal_records.

Referenced by pgstat_wal_flush_cb().

◆ pgstat_wal_init_backend_cb()

void pgstat_wal_init_backend_cb ( void  )

Definition at line 129 of file pgstat_wal.c.

130{
131 /*
132 * Initialize prevWalUsage with pgWalUsage so that pgstat_wal_flush_cb()
133 * can calculate how much pgWalUsage counters are increased by subtracting
134 * prevWalUsage from pgWalUsage.
135 */
137}

References pgWalUsage, and prevWalUsage.

◆ pgstat_wal_init_shmem_cb()

void pgstat_wal_init_shmem_cb ( void *  stats)

Definition at line 149 of file pgstat_wal.c.

150{
151 PgStatShared_Wal *stats_shmem = (PgStatShared_Wal *) stats;
152
154}
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:721
@ LWTRANCHE_PGSTATS_DATA
Definition: lwlock.h:210

References PgStatShared_Wal::lock, LWLockInitialize(), and LWTRANCHE_PGSTATS_DATA.

◆ pgstat_wal_reset_all_cb()

void pgstat_wal_reset_all_cb ( TimestampTz  ts)

Definition at line 157 of file pgstat_wal.c.

158{
159 PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
160
161 LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
162 memset(&stats_shmem->stats, 0, sizeof(stats_shmem->stats));
163 stats_shmem->stats.stat_reset_timestamp = ts;
164 LWLockRelease(&stats_shmem->lock);
165}
PgStat_WalStats stats
TimestampTz stat_reset_timestamp
Definition: pgstat.h:481

References PgStatShared_Wal::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), pgStatLocal, PgStat_LocalState::shmem, PgStat_WalStats::stat_reset_timestamp, PgStatShared_Wal::stats, and PgStat_ShmemControl::wal.

◆ pgstat_wal_snapshot_cb()

void pgstat_wal_snapshot_cb ( void  )

Variable Documentation

◆ prevWalUsage

WalUsage prevWalUsage
static