PostgreSQL Source Code  git master
pgstat_wal.c
Go to the documentation of this file.
1 /* -------------------------------------------------------------------------
2  *
3  * pgstat_wal.c
4  * Implementation of WAL statistics.
5  *
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
9  * statistics.
10  *
11  * Copyright (c) 2001-2022, PostgreSQL Global Development Group
12  *
13  * IDENTIFICATION
14  * src/backend/utils/activity/pgstat_wal.c
15  * -------------------------------------------------------------------------
16  */
17 
18 #include "postgres.h"
19 
20 #include "utils/pgstat_internal.h"
21 #include "executor/instrument.h"
22 
23 
25 
26 /*
27  * WAL usage counters saved from pgWALUsage at the previous call to
28  * pgstat_report_wal(). This is used to calculate how much WAL usage
29  * happens between pgstat_report_wal() calls, by subtracting
30  * the previous counters from the current ones.
31  */
33 
34 
35 /*
36  * Calculate how much WAL usage counters have increased and update
37  * shared statistics.
38  *
39  * Must be called by processes that generate WAL, that do not call
40  * pgstat_report_stat(), like walwriter.
41  */
42 void
43 pgstat_report_wal(bool force)
44 {
45  pgstat_flush_wal(force);
46 }
47 
48 /*
49  * Support function for the SQL-callable pgstat* functions. Returns
50  * a pointer to the WAL statistics struct.
51  */
54 {
56 
57  return &pgStatLocal.snapshot.wal;
58 }
59 
60 /*
61  * Calculate how much WAL usage counters have increased by subtracting the
62  * previous counters from the current ones.
63  *
64  * If nowait is true, this function returns true if the lock could not be
65  * acquired. Otherwise return false.
66  */
67 bool
68 pgstat_flush_wal(bool nowait)
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 }
124 
125 void
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 }
135 
136 /*
137  * To determine whether any WAL activity has occurred since last time, not
138  * only the number of generated WAL records but also the numbers of WAL
139  * writes and syncs need to be checked. Because even transaction that
140  * generates no WAL records can write or sync WAL data when flushing the
141  * data pages.
142  */
143 bool
145 {
147  PendingWalStats.wal_write != 0 ||
149 }
150 
151 void
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 }
161 
162 void
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 }
#define MemSet(start, val, len)
Definition: c.h:1008
int64 TimestampTz
Definition: timestamp.h:39
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_SHARED
Definition: lwlock.h:105
@ LW_EXCLUSIVE
Definition: lwlock.h:104
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
#define WALSTAT_ACC(fld)
PgStat_WalStats PendingWalStats
Definition: pgstat_wal.c:24
void pgstat_wal_reset_all_cb(TimestampTz ts)
Definition: pgstat_wal.c:152
bool pgstat_have_pending_wal(void)
Definition: pgstat_wal.c:144
void pgstat_init_wal(void)
Definition: pgstat_wal.c:126
void pgstat_report_wal(bool force)
Definition: pgstat_wal.c:43
PgStat_WalStats * pgstat_fetch_stat_wal(void)
Definition: pgstat_wal.c:53
bool pgstat_flush_wal(bool nowait)
Definition: pgstat_wal.c:68
static WalUsage prevWalUsage
Definition: pgstat_wal.c:32
void pgstat_wal_snapshot_cb(void)
Definition: pgstat_wal.c:163
PgStat_WalStats stats
PgStat_Snapshot snapshot
PgStat_ShmemControl * shmem
PgStatShared_Wal wal
PgStat_WalStats wal
PgStat_Counter wal_write
Definition: pgstat.h:391
TimestampTz stat_reset_timestamp
Definition: pgstat.h:395
uint64 wal_bytes
Definition: pgstat.h:389
PgStat_Counter wal_fpi
Definition: pgstat.h:388
PgStat_Counter wal_sync
Definition: pgstat.h:392
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