Age Owner Branch data TLA 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
1301 andres@anarazel.de 42 :CBC 4 : pgstat_reset_replslot(const char *name)
43 : : {
44 : : ReplicationSlot *slot;
45 : :
1096 peter@eisentraut.org 46 [ - + ]: 4 : Assert(name != NULL);
47 : :
596 michael@paquier.xyz 48 : 4 : LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
49 : :
50 : : /* Check if the slot exits with the given name. */
51 : 4 : slot = SearchNamedReplicationSlot(name, false);
52 : :
1301 andres@anarazel.de 53 [ + + ]: 4 : if (!slot)
54 [ + - ]: 1 : 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 : : */
596 michael@paquier.xyz 63 [ + - ]: 3 : if (SlotIsLogical(slot))
64 : 3 : pgstat_reset(PGSTAT_KIND_REPLSLOT, InvalidOid,
65 : 3 : ReplicationSlotIndex(slot));
66 : :
67 : 3 : LWLockRelease(ReplicationSlotControlLock);
1317 andres@anarazel.de 68 : 3 : }
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
1301 78 : 5513 : 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 : 5513 : entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_REPLSLOT, InvalidOid,
85 : 5513 : ReplicationSlotIndex(slot), false);
86 : 5513 : shstatent = (PgStatShared_ReplSlot *) entry_ref->shared_stats;
87 : 5513 : statent = &shstatent->stats;
88 : :
89 : : /* Update the replication slot statistics */
90 : : #define REPLSLOT_ACC(fld) statent->fld += repSlotStat->fld
91 : 5513 : REPLSLOT_ACC(spill_txns);
92 : 5513 : REPLSLOT_ACC(spill_count);
93 : 5513 : REPLSLOT_ACC(spill_bytes);
94 : 5513 : REPLSLOT_ACC(stream_txns);
95 : 5513 : REPLSLOT_ACC(stream_count);
96 : 5513 : REPLSLOT_ACC(stream_bytes);
20 msawada@postgresql.o 97 :GNC 5513 : REPLSLOT_ACC(mem_exceeded_count);
1301 andres@anarazel.de 98 :CBC 5513 : REPLSLOT_ACC(total_txns);
99 : 5513 : REPLSLOT_ACC(total_bytes);
100 : : #undef REPLSLOT_ACC
101 : :
102 : 5513 : pgstat_unlock_entry(entry_ref);
1317 103 : 5513 : }
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
1301 112 : 439 : pgstat_create_replslot(ReplicationSlot *slot)
113 : : {
114 : : PgStat_EntryRef *entry_ref;
115 : : PgStatShared_ReplSlot *shstatent;
116 : :
594 michael@paquier.xyz 117 [ - + ]: 439 : Assert(LWLockHeldByMeInMode(ReplicationSlotAllocationLock, LW_EXCLUSIVE));
118 : :
1301 andres@anarazel.de 119 : 439 : entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_REPLSLOT, InvalidOid,
120 : 439 : ReplicationSlotIndex(slot), false);
121 : 439 : 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 : 439 : memset(&shstatent->stats, 0, sizeof(shstatent->stats));
128 : :
129 : 439 : pgstat_unlock_entry(entry_ref);
130 : 439 : }
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 : 1027 : pgstat_acquire_replslot(ReplicationSlot *slot)
148 : : {
1116 149 : 1027 : pgstat_get_entry_ref(PGSTAT_KIND_REPLSLOT, InvalidOid,
150 : 1027 : ReplicationSlotIndex(slot), true, NULL);
1317 151 : 1027 : }
152 : :
153 : : /*
154 : : * Report replication slot drop.
155 : : */
156 : : void
1301 157 : 381 : pgstat_drop_replslot(ReplicationSlot *slot)
158 : : {
594 michael@paquier.xyz 159 [ - + ]: 381 : Assert(LWLockHeldByMeInMode(ReplicationSlotAllocationLock, LW_EXCLUSIVE));
160 : :
509 161 [ + + ]: 381 : if (!pgstat_drop_entry(PGSTAT_KIND_REPLSLOT, InvalidOid,
162 : 381 : ReplicationSlotIndex(slot)))
163 : 48 : pgstat_request_entry_refs_gc();
1301 andres@anarazel.de 164 : 381 : }
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 : 47 : pgstat_fetch_replslot(NameData slotname)
172 : : {
173 : : int idx;
596 michael@paquier.xyz 174 : 47 : PgStat_StatReplSlotEntry *slotentry = NULL;
175 : :
176 : 47 : LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
177 : :
178 : 47 : idx = get_replslot_index(NameStr(slotname), false);
179 : :
180 [ + + ]: 47 : if (idx != -1)
181 : 45 : slotentry = (PgStat_StatReplSlotEntry *) pgstat_fetch_entry(PGSTAT_KIND_REPLSLOT,
182 : : InvalidOid, idx);
183 : :
184 : 47 : LWLockRelease(ReplicationSlotControlLock);
185 : :
186 : 47 : return slotentry;
187 : : }
188 : :
189 : : void
1116 andres@anarazel.de 190 : 89 : 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 : : */
405 michael@paquier.xyz 197 [ - + ]: 89 : if (!ReplicationSlotName(key->objid, name))
213 peter@eisentraut.org 198 [ # # ]:UBC 0 : elog(ERROR, "could not find name for replication slot index %" PRIu64,
199 : : key->objid);
1301 andres@anarazel.de 200 :CBC 89 : }
201 : :
202 : : bool
203 : 61 : pgstat_replslot_from_serialized_name_cb(const NameData *name, PgStat_HashKey *key)
204 : : {
596 michael@paquier.xyz 205 : 61 : int idx = get_replslot_index(NameStr(*name), true);
206 : :
207 : : /* slot might have been deleted */
1301 andres@anarazel.de 208 [ + + ]: 61 : if (idx == -1)
209 : 1 : return false;
210 : :
211 : 60 : key->kind = PGSTAT_KIND_REPLSLOT;
212 : 60 : key->dboid = InvalidOid;
405 michael@paquier.xyz 213 : 60 : key->objid = idx;
214 : :
1301 andres@anarazel.de 215 : 60 : return true;
216 : : }
217 : :
218 : : void
219 : 8 : pgstat_replslot_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
220 : : {
221 : 8 : ((PgStatShared_ReplSlot *) header)->stats.stat_reset_timestamp = ts;
222 : 8 : }
223 : :
224 : : static int
596 michael@paquier.xyz 225 : 108 : get_replslot_index(const char *name, bool need_lock)
226 : : {
227 : : ReplicationSlot *slot;
228 : :
1096 peter@eisentraut.org 229 [ - + ]: 108 : Assert(name != NULL);
230 : :
596 michael@paquier.xyz 231 : 108 : slot = SearchNamedReplicationSlot(name, need_lock);
232 : :
1301 andres@anarazel.de 233 [ + + ]: 108 : if (!slot)
234 : 3 : return -1;
235 : :
236 : 105 : return ReplicationSlotIndex(slot);
237 : : }
|