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)
 
bool pgstat_flush_wal (bool nowait)
 
void pgstat_init_wal (void)
 
bool pgstat_have_pending_wal (void)
 
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:938
PgStat_LocalState pgStatLocal
Definition: pgstat.c:193
@ PGSTAT_KIND_WAL
Definition: pgstat.h:53
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()

bool pgstat_flush_wal ( 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.fld += var_to_add.fld)
112 #define WALSTAT_ACC_INSTR_TIME(fld) \
113  (stats_shmem->stats.fld += INSTR_TIME_GET_MICROSEC(PendingWalStats.fld))
114  WALSTAT_ACC(wal_records, wal_usage_diff);
115  WALSTAT_ACC(wal_fpi, wal_usage_diff);
116  WALSTAT_ACC(wal_bytes, wal_usage_diff);
117  WALSTAT_ACC(wal_buffers_full, PendingWalStats);
118  WALSTAT_ACC(wal_write, PendingWalStats);
119  WALSTAT_ACC(wal_sync, PendingWalStats);
120  WALSTAT_ACC_INSTR_TIME(wal_write_time);
121  WALSTAT_ACC_INSTR_TIME(wal_sync_time);
122 #undef WALSTAT_ACC_INSTR_TIME
123 #undef WALSTAT_ACC
124 
125  LWLockRelease(&stats_shmem->lock);
126 
127  /*
128  * Save the current counters for the subsequent calculation of WAL usage.
129  */
131 
132  /*
133  * Clear out the statistics buffer, so it can be re-used.
134  */
135  MemSet(&PendingWalStats, 0, sizeof(PendingWalStats));
136 
137  return false;
138 }
#define Assert(condition)
Definition: c.h:858
#define MemSet(start, val, len)
Definition: c.h:1020
bool IsUnderPostmaster
Definition: globals.c:117
bool IsPostmasterEnvironment
Definition: globals.c:116
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:1170
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1783
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1341
@ LW_EXCLUSIVE
Definition: lwlock.h:114
#define WALSTAT_ACC(fld, var_to_add)
bool pgstat_have_pending_wal(void)
Definition: pgstat_wal.c:159
static WalUsage prevWalUsage
Definition: pgstat_wal.c:32
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_have_pending_wal(), pgStatLocal, pgWalUsage, prevWalUsage, PgStat_LocalState::shmem, PgStat_ShmemControl::wal, WALSTAT_ACC, WALSTAT_ACC_INSTR_TIME, and WalUsageAccumDiff().

Referenced by pgstat_report_stat(), and pgstat_report_wal().

◆ pgstat_have_pending_wal()

bool pgstat_have_pending_wal ( void  )

◆ pgstat_init_wal()

void pgstat_init_wal ( void  )

Definition at line 141 of file pgstat_wal.c.

142 {
143  /*
144  * Initialize prevWalUsage with pgWalUsage so that pgstat_flush_wal() can
145  * calculate how much pgWalUsage counters are increased by subtracting
146  * prevWalUsage from pgWalUsage.
147  */
149 }

References pgWalUsage, and prevWalUsage.

Referenced by pgstat_initialize().

◆ 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 }
bool pgstat_flush_io(bool nowait)
Definition: pgstat_io.c:173
bool pgstat_flush_wal(bool nowait)
Definition: pgstat_wal.c:82

References pgstat_flush_io(), and pgstat_flush_wal().

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

◆ pgstat_wal_reset_all_cb()

void pgstat_wal_reset_all_cb ( TimestampTz  ts)

Definition at line 167 of file pgstat_wal.c.

168 {
169  PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
170 
171  LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
172  memset(&stats_shmem->stats, 0, sizeof(stats_shmem->stats));
173  stats_shmem->stats.stat_reset_timestamp = ts;
174  LWLockRelease(&stats_shmem->lock);
175 }
PgStat_WalStats stats
TimestampTz stat_reset_timestamp
Definition: pgstat.h:441

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

179 {
180  PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
181 
182  LWLockAcquire(&stats_shmem->lock, LW_SHARED);
183  memcpy(&pgStatLocal.snapshot.wal, &stats_shmem->stats,
184  sizeof(pgStatLocal.snapshot.wal));
185  LWLockRelease(&stats_shmem->lock);
186 }
@ 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

Definition at line 32 of file pgstat_wal.c.

Referenced by pgstat_flush_wal(), pgstat_have_pending_wal(), and pgstat_init_wal().