nbtree: fix read page recheck typo.
[pgsql.git] / src / backend / utils / activity / pgstat_bgwriter.c
blob364a7a2024a3199abcae7e0db0d08e98dad682d3
1 /* -------------------------------------------------------------------------
3 * pgstat_bgwriter.c
4 * Implementation of bgwriter statistics.
6 * This file contains the implementation of bgwriter 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.
11 * Copyright (c) 2001-2024, PostgreSQL Global Development Group
13 * IDENTIFICATION
14 * src/backend/utils/activity/pgstat_bgwriter.c
15 * -------------------------------------------------------------------------
18 #include "postgres.h"
20 #include "utils/pgstat_internal.h"
23 PgStat_BgWriterStats PendingBgWriterStats = {0};
27 * Report bgwriter and IO statistics
29 void
30 pgstat_report_bgwriter(void)
32 PgStatShared_BgWriter *stats_shmem = &pgStatLocal.shmem->bgwriter;
33 static const PgStat_BgWriterStats all_zeroes;
35 Assert(!pgStatLocal.shmem->is_shutdown);
36 pgstat_assert_is_up();
39 * This function can be called even if nothing at all has happened. In
40 * this case, avoid unnecessarily modifying the stats entry.
42 if (memcmp(&PendingBgWriterStats, &all_zeroes, sizeof(all_zeroes)) == 0)
43 return;
45 pgstat_begin_changecount_write(&stats_shmem->changecount);
47 #define BGWRITER_ACC(fld) stats_shmem->stats.fld += PendingBgWriterStats.fld
48 BGWRITER_ACC(buf_written_clean);
49 BGWRITER_ACC(maxwritten_clean);
50 BGWRITER_ACC(buf_alloc);
51 #undef BGWRITER_ACC
53 pgstat_end_changecount_write(&stats_shmem->changecount);
56 * Clear out the statistics buffer, so it can be re-used.
58 MemSet(&PendingBgWriterStats, 0, sizeof(PendingBgWriterStats));
61 * Report IO statistics
63 pgstat_flush_io(false);
67 * Support function for the SQL-callable pgstat* functions. Returns
68 * a pointer to the bgwriter statistics struct.
70 PgStat_BgWriterStats *
71 pgstat_fetch_stat_bgwriter(void)
73 pgstat_snapshot_fixed(PGSTAT_KIND_BGWRITER);
75 return &pgStatLocal.snapshot.bgwriter;
78 void
79 pgstat_bgwriter_init_shmem_cb(void *stats)
81 PgStatShared_BgWriter *stats_shmem = (PgStatShared_BgWriter *) stats;
83 LWLockInitialize(&stats_shmem->lock, LWTRANCHE_PGSTATS_DATA);
86 void
87 pgstat_bgwriter_reset_all_cb(TimestampTz ts)
89 PgStatShared_BgWriter *stats_shmem = &pgStatLocal.shmem->bgwriter;
91 /* see explanation above PgStatShared_BgWriter for the reset protocol */
92 LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
93 pgstat_copy_changecounted_stats(&stats_shmem->reset_offset,
94 &stats_shmem->stats,
95 sizeof(stats_shmem->stats),
96 &stats_shmem->changecount);
97 stats_shmem->stats.stat_reset_timestamp = ts;
98 LWLockRelease(&stats_shmem->lock);
101 void
102 pgstat_bgwriter_snapshot_cb(void)
104 PgStatShared_BgWriter *stats_shmem = &pgStatLocal.shmem->bgwriter;
105 PgStat_BgWriterStats *reset_offset = &stats_shmem->reset_offset;
106 PgStat_BgWriterStats reset;
108 pgstat_copy_changecounted_stats(&pgStatLocal.snapshot.bgwriter,
109 &stats_shmem->stats,
110 sizeof(stats_shmem->stats),
111 &stats_shmem->changecount);
113 LWLockAcquire(&stats_shmem->lock, LW_SHARED);
114 memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
115 LWLockRelease(&stats_shmem->lock);
117 /* compensate by reset offsets */
118 #define BGWRITER_COMP(fld) pgStatLocal.snapshot.bgwriter.fld -= reset.fld;
119 BGWRITER_COMP(buf_written_clean);
120 BGWRITER_COMP(maxwritten_clean);
121 BGWRITER_COMP(buf_alloc);
122 #undef BGWRITER_COMP