PostgreSQL Source Code  git master
pgstat_archiver.c
Go to the documentation of this file.
1 /* -------------------------------------------------------------------------
2  *
3  * pgstat_archiver.c
4  * Implementation of archiver statistics.
5  *
6  * This file contains the implementation of archiver 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_archiver.c
15  * -------------------------------------------------------------------------
16  */
17 
18 #include "postgres.h"
19 
20 #include "utils/pgstat_internal.h"
21 #include "utils/timestamp.h"
22 
23 
24 /*
25  * Report archiver statistics
26  */
27 void
28 pgstat_report_archiver(const char *xlog, bool failed)
29 {
32 
34 
35  if (failed)
36  {
37  ++stats_shmem->stats.failed_count;
38  memcpy(&stats_shmem->stats.last_failed_wal, xlog,
39  sizeof(stats_shmem->stats.last_failed_wal));
40  stats_shmem->stats.last_failed_timestamp = now;
41  }
42  else
43  {
44  ++stats_shmem->stats.archived_count;
45  memcpy(&stats_shmem->stats.last_archived_wal, xlog,
46  sizeof(stats_shmem->stats.last_archived_wal));
47  stats_shmem->stats.last_archived_timestamp = now;
48  }
49 
51 }
52 
53 /*
54  * Support function for the SQL-callable pgstat* functions. Returns
55  * a pointer to the archiver statistics struct.
56  */
59 {
61 
63 }
64 
65 void
67 {
69 
70  /* see explanation above PgStatShared_Archiver for the reset protocol */
71  LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
73  &stats_shmem->stats,
74  sizeof(stats_shmem->stats),
75  &stats_shmem->changecount);
76  stats_shmem->stats.stat_reset_timestamp = ts;
77  LWLockRelease(&stats_shmem->lock);
78 }
79 
80 void
82 {
85  PgStat_ArchiverStats *reset_offset = &stats_shmem->reset_offset;
87 
89  &stats_shmem->stats,
90  sizeof(stats_shmem->stats),
91  &stats_shmem->changecount);
92 
93  LWLockAcquire(&stats_shmem->lock, LW_SHARED);
94  memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
95  LWLockRelease(&stats_shmem->lock);
96 
97  /* compensate by reset offsets */
98  if (stat_snap->archived_count == reset.archived_count)
99  {
100  stat_snap->last_archived_wal[0] = 0;
101  stat_snap->last_archived_timestamp = 0;
102  }
103  stat_snap->archived_count -= reset.archived_count;
104 
105  if (stat_snap->failed_count == reset.failed_count)
106  {
107  stat_snap->last_failed_wal[0] = 0;
108  stat_snap->last_failed_timestamp = 0;
109  }
110  stat_snap->failed_count -= reset.failed_count;
111 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1574
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1538
int64 TimestampTz
Definition: timestamp.h:39
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1196
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1800
@ 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_ARCHIVER
Definition: pgstat.h:48
void pgstat_archiver_reset_all_cb(TimestampTz ts)
void pgstat_report_archiver(const char *xlog, bool failed)
void pgstat_archiver_snapshot_cb(void)
PgStat_ArchiverStats * pgstat_fetch_stat_archiver(void)
static void pgstat_end_changecount_write(uint32 *cc)
static void pgstat_begin_changecount_write(uint32 *cc)
static void pgstat_copy_changecounted_stats(void *dst, void *src, size_t len, uint32 *cc)
void reset(void)
Definition: sql-declare.c:600
PgStat_ArchiverStats reset_offset
PgStat_ArchiverStats stats
TimestampTz last_failed_timestamp
Definition: pgstat.h:256
TimestampTz stat_reset_timestamp
Definition: pgstat.h:257
TimestampTz last_archived_timestamp
Definition: pgstat.h:252
char last_failed_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:254
PgStat_Counter failed_count
Definition: pgstat.h:253
PgStat_Counter archived_count
Definition: pgstat.h:249
char last_archived_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:250
PgStat_Snapshot snapshot
PgStat_ShmemControl * shmem
PgStatShared_Archiver archiver
PgStat_ArchiverStats archiver