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-2023, 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 WAL and IO 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  pgstat_flush_io(force);
48 }
49 
50 /*
51  * Support function for the SQL-callable pgstat* functions. Returns
52  * a pointer to the WAL statistics struct.
53  */
56 {
58 
59  return &pgStatLocal.snapshot.wal;
60 }
61 
62 /*
63  * Calculate how much WAL usage counters have increased by subtracting the
64  * previous counters from the current ones.
65  *
66  * If nowait is true, this function returns true if the lock could not be
67  * acquired. Otherwise return false.
68  */
69 bool
70 pgstat_flush_wal(bool nowait)
71 {
72  PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
73  WalUsage wal_usage_diff = {0};
74 
76  Assert(pgStatLocal.shmem != NULL &&
78 
79  /*
80  * This function can be called even if nothing at all has happened. Avoid
81  * taking lock for nothing in that case.
82  */
84  return false;
85 
86  /*
87  * We don't update the WAL usage portion of the local WalStats elsewhere.
88  * Calculate how much WAL usage counters were increased by subtracting the
89  * previous counters from the current ones.
90  */
91  WalUsageAccumDiff(&wal_usage_diff, &pgWalUsage, &prevWalUsage);
92 
93  if (!nowait)
94  LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
95  else if (!LWLockConditionalAcquire(&stats_shmem->lock, LW_EXCLUSIVE))
96  return true;
97 
98 #define WALSTAT_ACC(fld, var_to_add) \
99  (stats_shmem->stats.fld += var_to_add.fld)
100 #define WALSTAT_ACC_INSTR_TIME(fld) \
101  (stats_shmem->stats.fld += INSTR_TIME_GET_MICROSEC(PendingWalStats.fld))
102  WALSTAT_ACC(wal_records, wal_usage_diff);
103  WALSTAT_ACC(wal_fpi, wal_usage_diff);
104  WALSTAT_ACC(wal_bytes, wal_usage_diff);
105  WALSTAT_ACC(wal_buffers_full, PendingWalStats);
106  WALSTAT_ACC(wal_write, PendingWalStats);
107  WALSTAT_ACC(wal_sync, PendingWalStats);
108  WALSTAT_ACC_INSTR_TIME(wal_write_time);
109  WALSTAT_ACC_INSTR_TIME(wal_sync_time);
110 #undef WALSTAT_ACC_INSTR_TIME
111 #undef WALSTAT_ACC
112 
113  LWLockRelease(&stats_shmem->lock);
114 
115  /*
116  * Save the current counters for the subsequent calculation of WAL usage.
117  */
119 
120  /*
121  * Clear out the statistics buffer, so it can be re-used.
122  */
123  MemSet(&PendingWalStats, 0, sizeof(PendingWalStats));
124 
125  return false;
126 }
127 
128 void
130 {
131  /*
132  * Initialize prevWalUsage with pgWalUsage so that pgstat_flush_wal() can
133  * calculate how much pgWalUsage counters are increased by subtracting
134  * prevWalUsage from pgWalUsage.
135  */
137 }
138 
139 /*
140  * To determine whether any WAL activity has occurred since last time, not
141  * only the number of generated WAL records but also the numbers of WAL
142  * writes and syncs need to be checked. Because even transaction that
143  * generates no WAL records can write or sync WAL data when flushing the
144  * data pages.
145  */
146 bool
148 {
150  PendingWalStats.wal_write != 0 ||
152 }
153 
154 void
156 {
157  PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
158 
159  LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
160  memset(&stats_shmem->stats, 0, sizeof(stats_shmem->stats));
161  stats_shmem->stats.stat_reset_timestamp = ts;
162  LWLockRelease(&stats_shmem->lock);
163 }
164 
165 void
167 {
168  PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
169 
170  LWLockAcquire(&stats_shmem->lock, LW_SHARED);
171  memcpy(&pgStatLocal.snapshot.wal, &stats_shmem->stats,
172  sizeof(pgStatLocal.snapshot.wal));
173  LWLockRelease(&stats_shmem->lock);
174 }
#define MemSet(start, val, len)
Definition: c.h:1004
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:1195
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1366
@ LW_SHARED
Definition: lwlock.h:117
@ LW_EXCLUSIVE
Definition: lwlock.h:116
void pgstat_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:930
PgStat_LocalState pgStatLocal
Definition: pgstat.c:196
@ PGSTAT_KIND_WAL
Definition: pgstat.h:53
bool pgstat_flush_io(bool nowait)
Definition: pgstat_io.c:159
#define WALSTAT_ACC(fld, var_to_add)
void pgstat_wal_reset_all_cb(TimestampTz ts)
Definition: pgstat_wal.c:155
bool pgstat_have_pending_wal(void)
Definition: pgstat_wal.c:147
void pgstat_init_wal(void)
Definition: pgstat_wal.c:129
void pgstat_report_wal(bool force)
Definition: pgstat_wal.c:43
PgStat_WalStats * pgstat_fetch_stat_wal(void)
Definition: pgstat_wal.c:55
bool pgstat_flush_wal(bool nowait)
Definition: pgstat_wal.c:70
static WalUsage prevWalUsage
Definition: pgstat_wal.c:32
void pgstat_wal_snapshot_cb(void)
Definition: pgstat_wal.c:166
PgStat_PendingWalStats PendingWalStats
Definition: pgstat_wal.c:24
#define WALSTAT_ACC_INSTR_TIME(fld)
PgStat_WalStats stats
PgStat_Snapshot snapshot
PgStat_ShmemControl * shmem
PgStat_Counter wal_write
Definition: pgstat.h:451
PgStat_Counter wal_sync
Definition: pgstat.h:452
PgStatShared_Wal wal
PgStat_WalStats wal
TimestampTz stat_reset_timestamp
Definition: pgstat.h:439
int64 wal_records
Definition: instrument.h:51