PostgreSQL Source Code git master
pgstat_replslot.c
Go to the documentation of this file.
1/* -------------------------------------------------------------------------
2 *
3 * pgstat_replslot.c
4 * Implementation of replication slot statistics.
5 *
6 * This file contains the implementation of replication slot 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 * Replication slot stats work a bit different than other variable-numbered
12 * stats. Slots do not have oids (so they can be created on physical
13 * replicas). Use the slot index as object id while running. However, the slot
14 * index can change when restarting. That is addressed by using the name when
15 * (de-)serializing. After a restart it is possible for slots to have been
16 * dropped while shut down, which is addressed by not restoring stats for
17 * slots that cannot be found by name when starting up.
18 *
19 * Copyright (c) 2001-2025, PostgreSQL Global Development Group
20 *
21 * IDENTIFICATION
22 * src/backend/utils/activity/pgstat_replslot.c
23 * -------------------------------------------------------------------------
24 */
25
26#include "postgres.h"
27
28#include "replication/slot.h"
30
31
32static int get_replslot_index(const char *name, bool need_lock);
33
34
35/*
36 * Reset counters for a single replication slot.
37 *
38 * Permission checking for this function is managed through the normal
39 * GRANT system.
40 */
41void
43{
44 ReplicationSlot *slot;
45
46 Assert(name != NULL);
47
48 LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
49
50 /* Check if the slot exits with the given name. */
51 slot = SearchNamedReplicationSlot(name, false);
52
53 if (!slot)
55 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
56 errmsg("replication slot \"%s\" does not exist",
57 name)));
58
59 /*
60 * Reset stats if it is a logical slot. Nothing to do for physical slots
61 * as we collect stats only for logical slots.
62 */
63 if (SlotIsLogical(slot))
66
67 LWLockRelease(ReplicationSlotControlLock);
68}
69
70/*
71 * Report replication slot statistics.
72 *
73 * We can rely on the stats for the slot to exist and to belong to this
74 * slot. We can only get here if pgstat_create_replslot() or
75 * pgstat_acquire_replslot() have already been called.
76 */
77void
79{
80 PgStat_EntryRef *entry_ref;
81 PgStatShared_ReplSlot *shstatent;
83
85 ReplicationSlotIndex(slot), false);
86 shstatent = (PgStatShared_ReplSlot *) entry_ref->shared_stats;
87 statent = &shstatent->stats;
88
89 /* Update the replication slot statistics */
90#define REPLSLOT_ACC(fld) statent->fld += repSlotStat->fld
91 REPLSLOT_ACC(spill_txns);
92 REPLSLOT_ACC(spill_count);
93 REPLSLOT_ACC(spill_bytes);
94 REPLSLOT_ACC(stream_txns);
95 REPLSLOT_ACC(stream_count);
96 REPLSLOT_ACC(stream_bytes);
97 REPLSLOT_ACC(total_txns);
98 REPLSLOT_ACC(total_bytes);
99#undef REPLSLOT_ACC
100
101 pgstat_unlock_entry(entry_ref);
102}
103
104/*
105 * Report replication slot creation.
106 *
107 * NB: This gets called with ReplicationSlotAllocationLock already held, be
108 * careful about calling back into slot.c.
109 */
110void
112{
113 PgStat_EntryRef *entry_ref;
114 PgStatShared_ReplSlot *shstatent;
115
116 Assert(LWLockHeldByMeInMode(ReplicationSlotAllocationLock, LW_EXCLUSIVE));
117
119 ReplicationSlotIndex(slot), false);
120 shstatent = (PgStatShared_ReplSlot *) entry_ref->shared_stats;
121
122 /*
123 * NB: need to accept that there might be stats from an older slot, e.g.
124 * if we previously crashed after dropping a slot.
125 */
126 memset(&shstatent->stats, 0, sizeof(shstatent->stats));
127
128 pgstat_unlock_entry(entry_ref);
129}
130
131/*
132 * Report replication slot has been acquired.
133 *
134 * This guarantees that a stats entry exists during later
135 * pgstat_report_replslot() calls.
136 *
137 * If we previously crashed, no stats data exists. But if we did not crash,
138 * the stats do belong to this slot:
139 * - the stats cannot belong to a dropped slot, pgstat_drop_replslot() would
140 * have been called
141 * - if the slot was removed while shut down,
142 * pgstat_replslot_from_serialized_name_cb() returning false would have
143 * caused the stats to be dropped
144 */
145void
147{
149 ReplicationSlotIndex(slot), true, NULL);
150}
151
152/*
153 * Report replication slot drop.
154 */
155void
157{
158 Assert(LWLockHeldByMeInMode(ReplicationSlotAllocationLock, LW_EXCLUSIVE));
159
163}
164
165/*
166 * Support function for the SQL-callable pgstat* functions. Returns
167 * a pointer to the replication slot statistics struct.
168 */
171{
172 int idx;
173 PgStat_StatReplSlotEntry *slotentry = NULL;
174
175 LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
176
177 idx = get_replslot_index(NameStr(slotname), false);
178
179 if (idx != -1)
181 InvalidOid, idx);
182
183 LWLockRelease(ReplicationSlotControlLock);
184
185 return slotentry;
186}
187
188void
190{
191 /*
192 * This is only called late during shutdown. The set of existing slots
193 * isn't allowed to change at this point, we can assume that a slot exists
194 * at the offset.
195 */
196 if (!ReplicationSlotName(key->objid, name))
197 elog(ERROR, "could not find name for replication slot index %llu",
198 (unsigned long long) key->objid);
199}
200
201bool
203{
204 int idx = get_replslot_index(NameStr(*name), true);
205
206 /* slot might have been deleted */
207 if (idx == -1)
208 return false;
209
211 key->dboid = InvalidOid;
212 key->objid = idx;
213
214 return true;
215}
216
217void
219{
220 ((PgStatShared_ReplSlot *) header)->stats.stat_reset_timestamp = ts;
221}
222
223static int
224get_replslot_index(const char *name, bool need_lock)
225{
226 ReplicationSlot *slot;
227
228 Assert(name != NULL);
229
230 slot = SearchNamedReplicationSlot(name, need_lock);
231
232 if (!slot)
233 return -1;
234
235 return ReplicationSlotIndex(slot);
236}
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
#define NameStr(name)
Definition: c.h:703
#define Assert(condition)
Definition: c.h:815
int64 TimestampTz
Definition: timestamp.h:39
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1937
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
@ LW_SHARED
Definition: lwlock.h:115
@ LW_EXCLUSIVE
Definition: lwlock.h:114
void pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat.c:870
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat.c:950
#define PGSTAT_KIND_REPLSLOT
Definition: pgstat_kind.h:30
bool pgstat_replslot_from_serialized_name_cb(const NameData *name, PgStat_HashKey *key)
void pgstat_reset_replslot(const char *name)
void pgstat_create_replslot(ReplicationSlot *slot)
PgStat_StatReplSlotEntry * pgstat_fetch_replslot(NameData slotname)
void pgstat_acquire_replslot(ReplicationSlot *slot)
static int get_replslot_index(const char *name, bool need_lock)
void pgstat_report_replslot(ReplicationSlot *slot, const PgStat_StatReplSlotEntry *repSlotStat)
void pgstat_replslot_to_serialized_name_cb(const PgStat_HashKey *key, const PgStatShared_Common *header, NameData *name)
void pgstat_replslot_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void pgstat_drop_replslot(ReplicationSlot *slot)
#define REPLSLOT_ACC(fld)
void pgstat_request_entry_refs_gc(void)
Definition: pgstat_shmem.c:700
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create, bool *created_entry)
Definition: pgstat_shmem.c:444
bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat_shmem.c:953
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:675
PgStat_EntryRef * pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, uint64 objid, bool nowait)
Definition: pgstat_shmem.c:684
#define InvalidOid
Definition: postgres_ext.h:37
int ReplicationSlotIndex(ReplicationSlot *slot)
Definition: slot.c:497
ReplicationSlot * SearchNamedReplicationSlot(const char *name, bool need_lock)
Definition: slot.c:464
bool ReplicationSlotName(int index, Name name)
Definition: slot.c:513
#define SlotIsLogical(slot)
Definition: slot.h:217
PgStat_StatReplSlotEntry stats
PgStatShared_Common * shared_stats
Definition: c.h:698
const char * name