PostgreSQL Source Code  git master
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.fld += var_to_add.fld)
 
#define WALSTAT_ACC_INSTR_TIME(fld)    (stats_shmem->stats.fld += INSTR_TIME_GET_MICROSEC(PendingWalStats.fld))
 

Functions

void pgstat_report_wal (bool force)
 
PgStat_WalStatspgstat_fetch_stat_wal (void)
 
void pgstat_flush_wal (bool nowait)
 
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

PgStat_PendingWalStats PendingWalStats = {0}
 
static WalUsage prevWalUsage
 

Macro Definition Documentation

◆ WALSTAT_ACC

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

◆ WALSTAT_ACC_INSTR_TIME

#define WALSTAT_ACC_INSTR_TIME (   fld)     (stats_shmem->stats.fld += INSTR_TIME_GET_MICROSEC(PendingWalStats.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:1054
PgStat_LocalState pgStatLocal
Definition: pgstat.c:212
#define PGSTAT_KIND_WAL
Definition: pgstat.h:59
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_flush_wal()

void pgstat_flush_wal ( bool  nowait)

Definition at line 78 of file pgstat_wal.c.

79 {
80  (void) pgstat_wal_flush_cb(nowait);
81 }
bool pgstat_wal_flush_cb(bool nowait)
Definition: pgstat_wal.c:91

References pgstat_wal_flush_cb().

Referenced by pgstat_report_wal().

◆ pgstat_report_wal()

void pgstat_report_wal ( bool  force)

Definition at line 48 of file pgstat_wal.c.

49 {
50  bool nowait;
51 
52  /* like in pgstat.c, don't wait for lock acquisition when !force */
53  nowait = !force;
54 
55  /* flush wal stats */
56  pgstat_flush_wal(nowait);
57 
58  /* flush IO stats */
59  pgstat_flush_io(nowait);
60 }
void pgstat_flush_io(bool nowait)
Definition: pgstat_io.c:177
void pgstat_flush_wal(bool nowait)
Definition: pgstat_wal.c:78

References pgstat_flush_io(), and pgstat_flush_wal().

Referenced by BackgroundWriterMain(), CheckpointerMain(), HandleCheckpointerInterrupts(), and WalWriterMain().

◆ pgstat_wal_flush_cb()

bool pgstat_wal_flush_cb ( bool  nowait)

Definition at line 91 of file pgstat_wal.c.

92 {
93  PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
94  WalUsage wal_usage_diff = {0};
95 
97  Assert(pgStatLocal.shmem != NULL &&
99 
100  /*
101  * This function can be called even if nothing at all has happened. Avoid
102  * taking lock for nothing in that case.
103  */
105  return false;
106 
107  /*
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.
111  */
112  WalUsageAccumDiff(&wal_usage_diff, &pgWalUsage, &prevWalUsage);
113 
114  if (!nowait)
115  LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
116  else if (!LWLockConditionalAcquire(&stats_shmem->lock, LW_EXCLUSIVE))
117  return true;
118 
119 #define WALSTAT_ACC(fld, var_to_add) \
120  (stats_shmem->stats.fld += var_to_add.fld)
121 #define WALSTAT_ACC_INSTR_TIME(fld) \
122  (stats_shmem->stats.fld += INSTR_TIME_GET_MICROSEC(PendingWalStats.fld))
123  WALSTAT_ACC(wal_records, wal_usage_diff);
124  WALSTAT_ACC(wal_fpi, wal_usage_diff);
125  WALSTAT_ACC(wal_bytes, wal_usage_diff);
126  WALSTAT_ACC(wal_buffers_full, PendingWalStats);
127  WALSTAT_ACC(wal_write, PendingWalStats);
128  WALSTAT_ACC(wal_sync, PendingWalStats);
129  WALSTAT_ACC_INSTR_TIME(wal_write_time);
130  WALSTAT_ACC_INSTR_TIME(wal_sync_time);
131 #undef WALSTAT_ACC_INSTR_TIME
132 #undef WALSTAT_ACC
133 
134  LWLockRelease(&stats_shmem->lock);
135 
136  /*
137  * Save the current counters for the subsequent calculation of WAL usage.
138  */
140 
141  /*
142  * Clear out the statistics buffer, so it can be re-used.
143  */
144  MemSet(&PendingWalStats, 0, sizeof(PendingWalStats));
145 
146  return false;
147 }
#define Assert(condition)
Definition: c.h:861
#define MemSet(start, val, len)
Definition: c.h:1023
bool IsUnderPostmaster
Definition: globals.c:119
bool IsPostmasterEnvironment
Definition: globals.c:118
WalUsage pgWalUsage
Definition: instrument.c:22
void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, const WalUsage *sub)
Definition: instrument.c:286
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1339
@ LW_EXCLUSIVE
Definition: lwlock.h:114
#define WALSTAT_ACC(fld, var_to_add)
static WalUsage prevWalUsage
Definition: pgstat_wal.c:32
bool pgstat_wal_have_pending_cb(void)
Definition: pgstat_wal.c:168
PgStat_PendingWalStats PendingWalStats
Definition: pgstat_wal.c:24
#define WALSTAT_ACC_INSTR_TIME(fld)
PgStat_ShmemControl * shmem
PgStatShared_Wal wal

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

Referenced by pgstat_flush_wal().

◆ pgstat_wal_have_pending_cb()

bool pgstat_wal_have_pending_cb ( void  )

◆ pgstat_wal_init_backend_cb()

void pgstat_wal_init_backend_cb ( void  )

Definition at line 150 of file pgstat_wal.c.

151 {
152  /*
153  * Initialize prevWalUsage with pgWalUsage so that pgstat_wal_flush_cb()
154  * can calculate how much pgWalUsage counters are increased by subtracting
155  * prevWalUsage from pgWalUsage.
156  */
158 }

References pgWalUsage, and prevWalUsage.

◆ pgstat_wal_init_shmem_cb()

void pgstat_wal_init_shmem_cb ( void *  stats)

Definition at line 176 of file pgstat_wal.c.

177 {
178  PgStatShared_Wal *stats_shmem = (PgStatShared_Wal *) stats;
179 
181 }
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:707
@ LWTRANCHE_PGSTATS_DATA
Definition: lwlock.h:205

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 184 of file pgstat_wal.c.

185 {
186  PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
187 
188  LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
189  memset(&stats_shmem->stats, 0, sizeof(stats_shmem->stats));
190  stats_shmem->stats.stat_reset_timestamp = ts;
191  LWLockRelease(&stats_shmem->lock);
192 }
PgStat_WalStats stats
TimestampTz stat_reset_timestamp
Definition: pgstat.h:476

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  )

Definition at line 195 of file pgstat_wal.c.

196 {
197  PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
198 
199  LWLockAcquire(&stats_shmem->lock, LW_SHARED);
200  memcpy(&pgStatLocal.snapshot.wal, &stats_shmem->stats,
201  sizeof(pgStatLocal.snapshot.wal));
202  LWLockRelease(&stats_shmem->lock);
203 }
@ LW_SHARED
Definition: lwlock.h:115

References PgStatShared_Wal::lock, LW_SHARED, LWLockAcquire(), LWLockRelease(), pgStatLocal, PgStat_LocalState::shmem, PgStat_LocalState::snapshot, PgStatShared_Wal::stats, PgStat_ShmemControl::wal, and PgStat_Snapshot::wal.

Variable Documentation

◆ PendingWalStats

◆ prevWalUsage

WalUsage prevWalUsage
static