summaryrefslogtreecommitdiffstats
path: root/src/backend/utils/activity/pgstat_archiver.c
blob: 851726fd50e32868210068a5d1c3f240d1a70337 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* -------------------------------------------------------------------------
 *
 * pgstat_archiver.c
 *	  Implementation of archiver statistics.
 *
 * This file contains the implementation of archiver statistics. It is kept
 * separate from pgstat.c to enforce the line between the statistics access /
 * storage implementation and the details about individual types of
 * statistics.
 *
 * Copyright (c) 2001-2022, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
 *	  src/backend/utils/activity/pgstat_archiver.c
 * -------------------------------------------------------------------------
 */

#include "postgres.h"

#include "utils/pgstat_internal.h"
#include "utils/timestamp.h"


/*
 * Report archiver statistics
 */
void
pgstat_report_archiver(const char *xlog, bool failed)
{
	PgStatShared_Archiver *stats_shmem = &pgStatLocal.shmem->archiver;
	TimestampTz now = GetCurrentTimestamp();

	pgstat_begin_changecount_write(&stats_shmem->changecount);

	if (failed)
	{
		++stats_shmem->stats.failed_count;
		memcpy(&stats_shmem->stats.last_failed_wal, xlog,
			   sizeof(stats_shmem->stats.last_failed_wal));
		stats_shmem->stats.last_failed_timestamp = now;
	}
	else
	{
		++stats_shmem->stats.archived_count;
		memcpy(&stats_shmem->stats.last_archived_wal, xlog,
			   sizeof(stats_shmem->stats.last_archived_wal));
		stats_shmem->stats.last_archived_timestamp = now;
	}

	pgstat_end_changecount_write(&stats_shmem->changecount);
}

/*
 * Support function for the SQL-callable pgstat* functions. Returns
 * a pointer to the archiver statistics struct.
 */
PgStat_ArchiverStats *
pgstat_fetch_stat_archiver(void)
{
	pgstat_snapshot_fixed(PGSTAT_KIND_ARCHIVER);

	return &pgStatLocal.snapshot.archiver;
}

void
pgstat_archiver_reset_all_cb(TimestampTz ts)
{
	PgStatShared_Archiver *stats_shmem = &pgStatLocal.shmem->archiver;

	/* see explanation above PgStatShared_Archiver for the reset protocol */
	LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
	pgstat_copy_changecounted_stats(&stats_shmem->reset_offset,
									&stats_shmem->stats,
									sizeof(stats_shmem->stats),
									&stats_shmem->changecount);
	stats_shmem->stats.stat_reset_timestamp = ts;
	LWLockRelease(&stats_shmem->lock);
}

void
pgstat_archiver_snapshot_cb(void)
{
	PgStatShared_Archiver *stats_shmem = &pgStatLocal.shmem->archiver;
	PgStat_ArchiverStats *stat_snap = &pgStatLocal.snapshot.archiver;
	PgStat_ArchiverStats *reset_offset = &stats_shmem->reset_offset;
	PgStat_ArchiverStats reset;

	pgstat_copy_changecounted_stats(stat_snap,
									&stats_shmem->stats,
									sizeof(stats_shmem->stats),
									&stats_shmem->changecount);

	LWLockAcquire(&stats_shmem->lock, LW_SHARED);
	memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
	LWLockRelease(&stats_shmem->lock);

	/* compensate by reset offsets */
	if (stat_snap->archived_count == reset.archived_count)
	{
		stat_snap->last_archived_wal[0] = 0;
		stat_snap->last_archived_timestamp = 0;
	}
	stat_snap->archived_count -= reset.archived_count;

	if (stat_snap->failed_count == reset.failed_count)
	{
		stat_snap->last_failed_wal[0] = 0;
		stat_snap->last_failed_timestamp = 0;
	}
	stat_snap->failed_count -= reset.failed_count;
}