PostgreSQL Source Code  git master
pgstat_wal.c File Reference
#include "postgres.h"
#include "utils/pgstat_internal.h"
#include "executor/instrument.h"
Include dependency graph for pgstat_wal.c:

Go to the source code of this file.

Macros

#define WALSTAT_ACC(fld)   stats_shmem->stats.fld += 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_WalStats PendingWalStats = {0}
 
static WalUsage prevWalUsage
 

Macro Definition Documentation

◆ WALSTAT_ACC

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

Function Documentation

◆ pgstat_fetch_stat_wal()

PgStat_WalStats* pgstat_fetch_stat_wal ( void  )

Definition at line 53 of file pgstat_wal.c.

54 {
56 
57  return &pgStatLocal.snapshot.wal;
58 }
void pgstat_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:899
PgStat_LocalState pgStatLocal
Definition: pgstat.c:195
@ PGSTAT_KIND_WAL
Definition: pgstat.h:52
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 68 of file pgstat_wal.c.

69 {
70  PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
71  WalUsage diff = {0};
72 
74  Assert(pgStatLocal.shmem != NULL &&
76 
77  /*
78  * This function can be called even if nothing at all has happened. Avoid
79  * taking lock for nothing in that case.
80  */
82  return false;
83 
84  /*
85  * We don't update the WAL usage portion of the local WalStats elsewhere.
86  * Calculate how much WAL usage counters were increased by subtracting the
87  * previous counters from the current ones.
88  */
93 
94  if (!nowait)
95  LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
96  else if (!LWLockConditionalAcquire(&stats_shmem->lock, LW_EXCLUSIVE))
97  return true;
98 
99 #define WALSTAT_ACC(fld) stats_shmem->stats.fld += PendingWalStats.fld
100  WALSTAT_ACC(wal_records);
101  WALSTAT_ACC(wal_fpi);
102  WALSTAT_ACC(wal_bytes);
103  WALSTAT_ACC(wal_buffers_full);
104  WALSTAT_ACC(wal_write);
105  WALSTAT_ACC(wal_sync);
106  WALSTAT_ACC(wal_write_time);
107  WALSTAT_ACC(wal_sync_time);
108 #undef WALSTAT_ACC
109 
110  LWLockRelease(&stats_shmem->lock);
111 
112  /*
113  * Save the current counters for the subsequent calculation of WAL usage.
114  */
116 
117  /*
118  * Clear out the statistics buffer, so it can be re-used.
119  */
120  MemSet(&PendingWalStats, 0, sizeof(PendingWalStats));
121 
122  return false;
123 }
#define MemSet(start, val, len)
Definition: c.h:1008
bool IsUnderPostmaster
Definition: globals.c:113
bool IsPostmasterEnvironment
Definition: globals.c:112
WalUsage pgWalUsage
Definition: instrument.c:22
void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, const WalUsage *sub)
Definition: instrument.c:280
Assert(fmt[strlen(fmt) - 1] !='\n')
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1196
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1800
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1367
@ LW_EXCLUSIVE
Definition: lwlock.h:104
#define WALSTAT_ACC(fld)
PgStat_WalStats PendingWalStats
Definition: pgstat_wal.c:24
bool pgstat_have_pending_wal(void)
Definition: pgstat_wal.c:144
static WalUsage prevWalUsage
Definition: pgstat_wal.c:32
PgStat_ShmemControl * shmem
PgStatShared_Wal wal
uint64 wal_bytes
Definition: pgstat.h:389
PgStat_Counter wal_fpi
Definition: pgstat.h:388
PgStat_Counter wal_records
Definition: pgstat.h:387
uint64 wal_bytes
Definition: instrument.h:53
int64 wal_fpi
Definition: instrument.h:52
int64 wal_records
Definition: instrument.h:51

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, WalUsage::wal_bytes, PgStat_WalStats::wal_bytes, WalUsage::wal_fpi, PgStat_WalStats::wal_fpi, WalUsage::wal_records, PgStat_WalStats::wal_records, WALSTAT_ACC, and WalUsageAccumDiff().

Referenced by pgstat_report_stat(), and pgstat_report_wal().

◆ pgstat_have_pending_wal()

bool pgstat_have_pending_wal ( void  )

Definition at line 144 of file pgstat_wal.c.

145 {
147  PendingWalStats.wal_write != 0 ||
149 }
PgStat_Counter wal_write
Definition: pgstat.h:391
PgStat_Counter wal_sync
Definition: pgstat.h:392

References PendingWalStats, pgWalUsage, prevWalUsage, WalUsage::wal_records, PgStat_WalStats::wal_sync, and PgStat_WalStats::wal_write.

Referenced by pgstat_flush_wal(), and pgstat_report_stat().

◆ pgstat_init_wal()

void pgstat_init_wal ( void  )

Definition at line 126 of file pgstat_wal.c.

127 {
128  /*
129  * Initialize prevWalUsage with pgWalUsage so that pgstat_flush_wal() can
130  * calculate how much pgWalUsage counters are increased by subtracting
131  * prevWalUsage from pgWalUsage.
132  */
134 }

References pgWalUsage, and prevWalUsage.

Referenced by pgstat_initialize().

◆ pgstat_report_wal()

void pgstat_report_wal ( bool  force)

Definition at line 43 of file pgstat_wal.c.

44 {
45  pgstat_flush_wal(force);
46 }
bool pgstat_flush_wal(bool nowait)
Definition: pgstat_wal.c:68

References pgstat_flush_wal().

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

◆ pgstat_wal_reset_all_cb()

void pgstat_wal_reset_all_cb ( TimestampTz  ts)

Definition at line 152 of file pgstat_wal.c.

153 {
154  PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
155 
156  LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
157  memset(&stats_shmem->stats, 0, sizeof(stats_shmem->stats));
158  stats_shmem->stats.stat_reset_timestamp = ts;
159  LWLockRelease(&stats_shmem->lock);
160 }
PgStat_WalStats stats
TimestampTz stat_reset_timestamp
Definition: pgstat.h:395

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

164 {
165  PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
166 
167  LWLockAcquire(&stats_shmem->lock, LW_SHARED);
168  memcpy(&pgStatLocal.snapshot.wal, &stats_shmem->stats,
169  sizeof(pgStatLocal.snapshot.wal));
170  LWLockRelease(&stats_shmem->lock);
171 }
@ LW_SHARED
Definition: lwlock.h:105

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().