Line data Source code
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"
29 : #include "utils/pgstat_internal.h"
30 :
31 :
32 : static 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 : */
41 : void
42 8 : pgstat_reset_replslot(const char *name)
43 : {
44 : ReplicationSlot *slot;
45 :
46 : Assert(name != NULL);
47 :
48 8 : LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
49 :
50 : /* Check if the slot exits with the given name. */
51 8 : slot = SearchNamedReplicationSlot(name, false);
52 :
53 8 : if (!slot)
54 2 : ereport(ERROR,
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 6 : if (SlotIsLogical(slot))
64 6 : pgstat_reset(PGSTAT_KIND_REPLSLOT, InvalidOid,
65 6 : ReplicationSlotIndex(slot));
66 :
67 6 : LWLockRelease(ReplicationSlotControlLock);
68 6 : }
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 : */
77 : void
78 10252 : pgstat_report_replslot(ReplicationSlot *slot, const PgStat_StatReplSlotEntry *repSlotStat)
79 : {
80 : PgStat_EntryRef *entry_ref;
81 : PgStatShared_ReplSlot *shstatent;
82 : PgStat_StatReplSlotEntry *statent;
83 :
84 10252 : entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_REPLSLOT, InvalidOid,
85 10252 : ReplicationSlotIndex(slot), false);
86 10252 : shstatent = (PgStatShared_ReplSlot *) entry_ref->shared_stats;
87 10252 : statent = &shstatent->stats;
88 :
89 : /* Update the replication slot statistics */
90 : #define REPLSLOT_ACC(fld) statent->fld += repSlotStat->fld
91 10252 : REPLSLOT_ACC(spill_txns);
92 10252 : REPLSLOT_ACC(spill_count);
93 10252 : REPLSLOT_ACC(spill_bytes);
94 10252 : REPLSLOT_ACC(stream_txns);
95 10252 : REPLSLOT_ACC(stream_count);
96 10252 : REPLSLOT_ACC(stream_bytes);
97 10252 : REPLSLOT_ACC(mem_exceeded_count);
98 10252 : REPLSLOT_ACC(total_txns);
99 10252 : REPLSLOT_ACC(total_bytes);
100 : #undef REPLSLOT_ACC
101 :
102 10252 : pgstat_unlock_entry(entry_ref);
103 10252 : }
104 :
105 : /*
106 : * Report replication slot creation.
107 : *
108 : * NB: This gets called with ReplicationSlotAllocationLock already held, be
109 : * careful about calling back into slot.c.
110 : */
111 : void
112 928 : pgstat_create_replslot(ReplicationSlot *slot)
113 : {
114 : PgStat_EntryRef *entry_ref;
115 : PgStatShared_ReplSlot *shstatent;
116 :
117 : Assert(LWLockHeldByMeInMode(ReplicationSlotAllocationLock, LW_EXCLUSIVE));
118 :
119 928 : entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_REPLSLOT, InvalidOid,
120 928 : ReplicationSlotIndex(slot), false);
121 928 : shstatent = (PgStatShared_ReplSlot *) entry_ref->shared_stats;
122 :
123 : /*
124 : * NB: need to accept that there might be stats from an older slot, e.g.
125 : * if we previously crashed after dropping a slot.
126 : */
127 928 : memset(&shstatent->stats, 0, sizeof(shstatent->stats));
128 :
129 928 : pgstat_unlock_entry(entry_ref);
130 928 : }
131 :
132 : /*
133 : * Report replication slot has been acquired.
134 : *
135 : * This guarantees that a stats entry exists during later
136 : * pgstat_report_replslot() calls.
137 : *
138 : * If we previously crashed, no stats data exists. But if we did not crash,
139 : * the stats do belong to this slot:
140 : * - the stats cannot belong to a dropped slot, pgstat_drop_replslot() would
141 : * have been called
142 : * - if the slot was removed while shut down,
143 : * pgstat_replslot_from_serialized_name_cb() returning false would have
144 : * caused the stats to be dropped
145 : */
146 : void
147 2110 : pgstat_acquire_replslot(ReplicationSlot *slot)
148 : {
149 2110 : pgstat_get_entry_ref(PGSTAT_KIND_REPLSLOT, InvalidOid,
150 2110 : ReplicationSlotIndex(slot), true, NULL);
151 2110 : }
152 :
153 : /*
154 : * Report replication slot drop.
155 : */
156 : void
157 800 : pgstat_drop_replslot(ReplicationSlot *slot)
158 : {
159 : Assert(LWLockHeldByMeInMode(ReplicationSlotAllocationLock, LW_EXCLUSIVE));
160 :
161 800 : if (!pgstat_drop_entry(PGSTAT_KIND_REPLSLOT, InvalidOid,
162 800 : ReplicationSlotIndex(slot)))
163 96 : pgstat_request_entry_refs_gc();
164 800 : }
165 :
166 : /*
167 : * Support function for the SQL-callable pgstat* functions. Returns
168 : * a pointer to the replication slot statistics struct.
169 : */
170 : PgStat_StatReplSlotEntry *
171 102 : pgstat_fetch_replslot(NameData slotname)
172 : {
173 : int idx;
174 102 : PgStat_StatReplSlotEntry *slotentry = NULL;
175 :
176 102 : LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
177 :
178 102 : idx = get_replslot_index(NameStr(slotname), false);
179 :
180 102 : if (idx != -1)
181 98 : slotentry = (PgStat_StatReplSlotEntry *) pgstat_fetch_entry(PGSTAT_KIND_REPLSLOT,
182 : InvalidOid, idx);
183 :
184 102 : LWLockRelease(ReplicationSlotControlLock);
185 :
186 102 : return slotentry;
187 : }
188 :
189 : void
190 190 : pgstat_replslot_to_serialized_name_cb(const PgStat_HashKey *key, const PgStatShared_Common *header, NameData *name)
191 : {
192 : /*
193 : * This is only called late during shutdown. The set of existing slots
194 : * isn't allowed to change at this point, we can assume that a slot exists
195 : * at the offset.
196 : */
197 190 : if (!ReplicationSlotName(key->objid, name))
198 0 : elog(ERROR, "could not find name for replication slot index %" PRIu64,
199 : key->objid);
200 190 : }
201 :
202 : bool
203 134 : pgstat_replslot_from_serialized_name_cb(const NameData *name, PgStat_HashKey *key)
204 : {
205 134 : int idx = get_replslot_index(NameStr(*name), true);
206 :
207 : /* slot might have been deleted */
208 134 : if (idx == -1)
209 2 : return false;
210 :
211 132 : key->kind = PGSTAT_KIND_REPLSLOT;
212 132 : key->dboid = InvalidOid;
213 132 : key->objid = idx;
214 :
215 132 : return true;
216 : }
217 :
218 : void
219 16 : pgstat_replslot_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
220 : {
221 16 : ((PgStatShared_ReplSlot *) header)->stats.stat_reset_timestamp = ts;
222 16 : }
223 :
224 : static int
225 236 : get_replslot_index(const char *name, bool need_lock)
226 : {
227 : ReplicationSlot *slot;
228 :
229 : Assert(name != NULL);
230 :
231 236 : slot = SearchNamedReplicationSlot(name, need_lock);
232 :
233 236 : if (!slot)
234 6 : return -1;
235 :
236 230 : return ReplicationSlotIndex(slot);
237 : }
|