PostgreSQL Source Code git master
pgstat_replslot.c File Reference
#include "postgres.h"
#include "replication/slot.h"
#include "utils/pgstat_internal.h"
Include dependency graph for pgstat_replslot.c:

Go to the source code of this file.

Macros

#define REPLSLOT_ACC(fld)   statent->fld += repSlotStat->fld
 

Functions

static int get_replslot_index (const char *name, bool need_lock)
 
void pgstat_reset_replslot (const char *name)
 
void pgstat_report_replslot (ReplicationSlot *slot, const PgStat_StatReplSlotEntry *repSlotStat)
 
void pgstat_report_replslotsync (ReplicationSlot *slot)
 
void pgstat_create_replslot (ReplicationSlot *slot)
 
void pgstat_acquire_replslot (ReplicationSlot *slot)
 
void pgstat_drop_replslot (ReplicationSlot *slot)
 
PgStat_StatReplSlotEntrypgstat_fetch_replslot (NameData slotname)
 
void pgstat_replslot_to_serialized_name_cb (const PgStat_HashKey *key, const PgStatShared_Common *header, NameData *name)
 
bool pgstat_replslot_from_serialized_name_cb (const NameData *name, PgStat_HashKey *key)
 
void pgstat_replslot_reset_timestamp_cb (PgStatShared_Common *header, TimestampTz ts)
 

Macro Definition Documentation

◆ REPLSLOT_ACC

#define REPLSLOT_ACC (   fld)    statent->fld += repSlotStat->fld

Function Documentation

◆ get_replslot_index()

static int get_replslot_index ( const char *  name,
bool  need_lock 
)
static

Definition at line 255 of file pgstat_replslot.c.

256{
257 ReplicationSlot *slot;
258
259 Assert(name != NULL);
260
261 slot = SearchNamedReplicationSlot(name, need_lock);
262
263 if (!slot)
264 return -1;
265
266 return ReplicationSlotIndex(slot);
267}
Assert(PointerIsAligned(start, uint64))
int ReplicationSlotIndex(ReplicationSlot *slot)
Definition: slot.c:579
ReplicationSlot * SearchNamedReplicationSlot(const char *name, bool need_lock)
Definition: slot.c:546
const char * name

References Assert(), name, ReplicationSlotIndex(), and SearchNamedReplicationSlot().

Referenced by pgstat_fetch_replslot(), and pgstat_replslot_from_serialized_name_cb().

◆ pgstat_acquire_replslot()

void pgstat_acquire_replslot ( ReplicationSlot slot)

Definition at line 177 of file pgstat_replslot.c.

178{
180 ReplicationSlotIndex(slot), true, NULL);
181}
#define PGSTAT_KIND_REPLSLOT
Definition: pgstat_kind.h:30
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create, bool *created_entry)
Definition: pgstat_shmem.c:469
#define InvalidOid
Definition: postgres_ext.h:37

References InvalidOid, pgstat_get_entry_ref(), PGSTAT_KIND_REPLSLOT, and ReplicationSlotIndex().

Referenced by ReplicationSlotAcquire().

◆ pgstat_create_replslot()

void pgstat_create_replslot ( ReplicationSlot slot)

Definition at line 142 of file pgstat_replslot.c.

143{
144 PgStat_EntryRef *entry_ref;
145 PgStatShared_ReplSlot *shstatent;
146
147 Assert(LWLockHeldByMeInMode(ReplicationSlotAllocationLock, LW_EXCLUSIVE));
148
150 ReplicationSlotIndex(slot), false);
151 shstatent = (PgStatShared_ReplSlot *) entry_ref->shared_stats;
152
153 /*
154 * NB: need to accept that there might be stats from an older slot, e.g.
155 * if we previously crashed after dropping a slot.
156 */
157 memset(&shstatent->stats, 0, sizeof(shstatent->stats));
158
159 pgstat_unlock_entry(entry_ref);
160}
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:2021
@ LW_EXCLUSIVE
Definition: lwlock.h:112
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:720
PgStat_EntryRef * pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, uint64 objid, bool nowait)
Definition: pgstat_shmem.c:729
PgStat_StatReplSlotEntry stats
PgStatShared_Common * shared_stats

References Assert(), InvalidOid, LW_EXCLUSIVE, LWLockHeldByMeInMode(), pgstat_get_entry_ref_locked(), PGSTAT_KIND_REPLSLOT, pgstat_unlock_entry(), ReplicationSlotIndex(), PgStat_EntryRef::shared_stats, and PgStatShared_ReplSlot::stats.

Referenced by ReplicationSlotCreate().

◆ pgstat_drop_replslot()

void pgstat_drop_replslot ( ReplicationSlot slot)

Definition at line 187 of file pgstat_replslot.c.

188{
189 Assert(LWLockHeldByMeInMode(ReplicationSlotAllocationLock, LW_EXCLUSIVE));
190
194}
void pgstat_request_entry_refs_gc(void)
Definition: pgstat_shmem.c:745
bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid)

References Assert(), InvalidOid, LW_EXCLUSIVE, LWLockHeldByMeInMode(), pgstat_drop_entry(), PGSTAT_KIND_REPLSLOT, pgstat_request_entry_refs_gc(), and ReplicationSlotIndex().

Referenced by ReplicationSlotDropPtr().

◆ pgstat_fetch_replslot()

PgStat_StatReplSlotEntry * pgstat_fetch_replslot ( NameData  slotname)

Definition at line 201 of file pgstat_replslot.c.

202{
203 int idx;
204 PgStat_StatReplSlotEntry *slotentry = NULL;
205
206 LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
207
208 idx = get_replslot_index(NameStr(slotname), false);
209
210 if (idx != -1)
212 InvalidOid, idx);
213
214 LWLockRelease(ReplicationSlotControlLock);
215
216 return slotentry;
217}
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:262
#define NameStr(name)
Definition: c.h:754
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1174
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1894
@ LW_SHARED
Definition: lwlock.h:113
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat.c:934
static int get_replslot_index(const char *name, bool need_lock)

References get_replslot_index(), idx(), InvalidOid, LW_SHARED, LWLockAcquire(), LWLockRelease(), NameStr, pgstat_fetch_entry(), and PGSTAT_KIND_REPLSLOT.

Referenced by pg_stat_get_replication_slot().

◆ pgstat_replslot_from_serialized_name_cb()

bool pgstat_replslot_from_serialized_name_cb ( const NameData name,
PgStat_HashKey key 
)

Definition at line 233 of file pgstat_replslot.c.

234{
235 int idx = get_replslot_index(NameStr(*name), true);
236
237 /* slot might have been deleted */
238 if (idx == -1)
239 return false;
240
242 key->dboid = InvalidOid;
243 key->objid = idx;
244
245 return true;
246}

References get_replslot_index(), idx(), InvalidOid, sort-test::key, name, NameStr, and PGSTAT_KIND_REPLSLOT.

◆ pgstat_replslot_reset_timestamp_cb()

void pgstat_replslot_reset_timestamp_cb ( PgStatShared_Common header,
TimestampTz  ts 
)

Definition at line 249 of file pgstat_replslot.c.

250{
251 ((PgStatShared_ReplSlot *) header)->stats.stat_reset_timestamp = ts;
252}

◆ pgstat_replslot_to_serialized_name_cb()

void pgstat_replslot_to_serialized_name_cb ( const PgStat_HashKey key,
const PgStatShared_Common header,
NameData name 
)

Definition at line 220 of file pgstat_replslot.c.

221{
222 /*
223 * This is only called late during shutdown. The set of existing slots
224 * isn't allowed to change at this point, we can assume that a slot exists
225 * at the offset.
226 */
227 if (!ReplicationSlotName(key->objid, name))
228 elog(ERROR, "could not find name for replication slot index %" PRIu64,
229 key->objid);
230}
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
bool ReplicationSlotName(int index, Name name)
Definition: slot.c:595

References elog, ERROR, sort-test::key, name, and ReplicationSlotName().

◆ pgstat_report_replslot()

void pgstat_report_replslot ( ReplicationSlot slot,
const PgStat_StatReplSlotEntry repSlotStat 
)

Definition at line 78 of file pgstat_replslot.c.

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(mem_exceeded_count);
98 REPLSLOT_ACC(total_txns);
99 REPLSLOT_ACC(total_bytes);
100#undef REPLSLOT_ACC
101
102 pgstat_unlock_entry(entry_ref);
103}
#define REPLSLOT_ACC(fld)

References InvalidOid, pgstat_get_entry_ref_locked(), PGSTAT_KIND_REPLSLOT, pgstat_unlock_entry(), ReplicationSlotIndex(), REPLSLOT_ACC, PgStat_EntryRef::shared_stats, and PgStatShared_ReplSlot::stats.

Referenced by UpdateDecodingStats().

◆ pgstat_report_replslotsync()

void pgstat_report_replslotsync ( ReplicationSlot slot)

Definition at line 112 of file pgstat_replslot.c.

113{
114 PgStat_EntryRef *entry_ref;
115 PgStatShared_ReplSlot *shstatent;
117
118 /* Slot sync stats are valid only for synced logical slots on standby. */
119 Assert(slot->data.synced);
121
123 ReplicationSlotIndex(slot), false);
124 Assert(entry_ref != NULL);
125
126 shstatent = (PgStatShared_ReplSlot *) entry_ref->shared_stats;
127 statent = &shstatent->stats;
128
129 statent->slotsync_skip_count += 1;
131
132 pgstat_unlock_entry(entry_ref);
133}
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1645
PgStat_Counter slotsync_skip_count
Definition: pgstat.h:403
TimestampTz slotsync_skip_at
Definition: pgstat.h:404
ReplicationSlotPersistentData data
Definition: slot.h:210
bool RecoveryInProgress(void)
Definition: xlog.c:6406

References Assert(), ReplicationSlot::data, GetCurrentTimestamp(), InvalidOid, pgstat_get_entry_ref_locked(), PGSTAT_KIND_REPLSLOT, pgstat_unlock_entry(), RecoveryInProgress(), ReplicationSlotIndex(), PgStat_EntryRef::shared_stats, PgStat_StatReplSlotEntry::slotsync_skip_at, PgStat_StatReplSlotEntry::slotsync_skip_count, PgStatShared_ReplSlot::stats, and ReplicationSlotPersistentData::synced.

Referenced by update_slotsync_skip_stats().

◆ pgstat_reset_replslot()

void pgstat_reset_replslot ( const char *  name)

Definition at line 42 of file pgstat_replslot.c.

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}
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ereport(elevel,...)
Definition: elog.h:150
void pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat.c:854
#define SlotIsLogical(slot)
Definition: slot.h:285

References Assert(), ereport, errcode(), errmsg(), ERROR, InvalidOid, LW_SHARED, LWLockAcquire(), LWLockRelease(), name, PGSTAT_KIND_REPLSLOT, pgstat_reset(), ReplicationSlotIndex(), SearchNamedReplicationSlot(), and SlotIsLogical.

Referenced by pg_stat_reset_replication_slot().