PostgreSQL Source Code  git master
pgstat_replslot.c File Reference
#include "postgres.h"
#include "replication/slot.h"
#include "utils/builtins.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)
 
void pgstat_reset_replslot (const char *name)
 
void pgstat_report_replslot (ReplicationSlot *slot, const PgStat_StatReplSlotEntry *repSlotStat)
 
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 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)
static

Definition at line 216 of file pgstat_replslot.c.

217 {
218  ReplicationSlot *slot;
219 
220  AssertArg(name != NULL);
221 
222  slot = SearchNamedReplicationSlot(name, true);
223 
224  if (!slot)
225  return -1;
226 
227  return ReplicationSlotIndex(slot);
228 }
#define AssertArg(condition)
Definition: c.h:806
const char * name
Definition: encode.c:561
ReplicationSlot * SearchNamedReplicationSlot(const char *name, bool need_lock)
Definition: slot.c:377
int ReplicationSlotIndex(ReplicationSlot *slot)
Definition: slot.c:410

References AssertArg, 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 136 of file pgstat_replslot.c.

137 {
138  PgStat_EntryRef *entry_ref;
139  PgStatShared_ReplSlot *shstatent;
140  PgStat_StatReplSlotEntry *statent;
141 
143  ReplicationSlotIndex(slot), false);
144  shstatent = (PgStatShared_ReplSlot *) entry_ref->shared_stats;
145  statent = &shstatent->stats;
146 
147  /*
148  * NB: need to accept that there might be stats from an older slot, e.g.
149  * if we previously crashed after dropping a slot.
150  */
151  if (NameStr(statent->slotname)[0] == 0 ||
152  namestrcmp(&statent->slotname, NameStr(slot->data.name)) != 0)
153  {
154  memset(statent, 0, sizeof(*statent));
155  namestrcpy(&statent->slotname, NameStr(slot->data.name));
156  }
157 
158  pgstat_unlock_entry(entry_ref);
159 }
#define NameStr(name)
Definition: c.h:681
int namestrcmp(Name name, const char *str)
Definition: name.c:247
void namestrcpy(Name name, const char *str)
Definition: name.c:233
@ PGSTAT_KIND_REPLSLOT
Definition: pgstat.h:44
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:583
PgStat_EntryRef * pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, Oid objoid, bool nowait)
Definition: pgstat_shmem.c:592
#define InvalidOid
Definition: postgres_ext.h:36
PgStat_StatReplSlotEntry stats
PgStatShared_Common * shared_stats
ReplicationSlotPersistentData data
Definition: slot.h:147

References ReplicationSlot::data, if(), InvalidOid, ReplicationSlotPersistentData::name, NameStr, namestrcmp(), namestrcpy(), pgstat_get_entry_ref_locked(), PGSTAT_KIND_REPLSLOT, pgstat_unlock_entry(), ReplicationSlotIndex(), PgStat_EntryRef::shared_stats, PgStat_StatReplSlotEntry::slotname, and PgStatShared_ReplSlot::stats.

Referenced by ReplicationSlotAcquire().

◆ pgstat_create_replslot()

void pgstat_create_replslot ( ReplicationSlot slot)

Definition at line 113 of file pgstat_replslot.c.

114 {
115  PgStat_EntryRef *entry_ref;
116  PgStatShared_ReplSlot *shstatent;
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  namestrcpy(&shstatent->stats.slotname, NameStr(slot->data.name));
128 
129  pgstat_unlock_entry(entry_ref);
130 }

References ReplicationSlot::data, InvalidOid, ReplicationSlotPersistentData::name, NameStr, namestrcpy(), pgstat_get_entry_ref_locked(), PGSTAT_KIND_REPLSLOT, pgstat_unlock_entry(), ReplicationSlotIndex(), PgStat_EntryRef::shared_stats, PgStat_StatReplSlotEntry::slotname, and PgStatShared_ReplSlot::stats.

Referenced by ReplicationSlotCreate().

◆ pgstat_drop_replslot()

void pgstat_drop_replslot ( ReplicationSlot slot)

Definition at line 165 of file pgstat_replslot.c.

166 {
168  ReplicationSlotIndex(slot));
169 }
bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat_shmem.c:838

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

Referenced by ReplicationSlotDropPtr().

◆ pgstat_fetch_replslot()

PgStat_StatReplSlotEntry* pgstat_fetch_replslot ( NameData  slotname)

Definition at line 176 of file pgstat_replslot.c.

177 {
178  int idx = get_replslot_index(NameStr(slotname));
179 
180  if (idx == -1)
181  return NULL;
182 
183  return (PgStat_StatReplSlotEntry *)
185 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat.c:779
static int get_replslot_index(const char *name)

References get_replslot_index(), idx(), InvalidOid, 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 194 of file pgstat_replslot.c.

195 {
197 
198  /* slot might have been deleted */
199  if (idx == -1)
200  return false;
201 
202  key->kind = PGSTAT_KIND_REPLSLOT;
203  key->dboid = InvalidOid;
204  key->objoid = idx;
205 
206  return true;
207 }

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 210 of file pgstat_replslot.c.

211 {
212  ((PgStatShared_ReplSlot *) header)->stats.stat_reset_timestamp = ts;
213 }
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:212

References header().

◆ pgstat_replslot_to_serialized_name_cb()

void pgstat_replslot_to_serialized_name_cb ( const PgStatShared_Common header,
NameData name 
)

Definition at line 188 of file pgstat_replslot.c.

189 {
190  namestrcpy(name, NameStr(((PgStatShared_ReplSlot *) header)->stats.slotname));
191 }

References header(), name, NameStr, and namestrcpy().

◆ pgstat_report_replslot()

void pgstat_report_replslot ( ReplicationSlot slot,
const PgStat_StatReplSlotEntry repSlotStat 
)

Definition at line 74 of file pgstat_replslot.c.

75 {
76  PgStat_EntryRef *entry_ref;
77  PgStatShared_ReplSlot *shstatent;
78  PgStat_StatReplSlotEntry *statent;
79 
81  ReplicationSlotIndex(slot), false);
82  shstatent = (PgStatShared_ReplSlot *) entry_ref->shared_stats;
83  statent = &shstatent->stats;
84 
85  /*
86  * Any mismatch should have been fixed in pgstat_create_replslot() or
87  * pgstat_acquire_replslot().
88  */
89  Assert(namestrcmp(&statent->slotname, NameStr(slot->data.name)) == 0);
90 
91  /* Update the replication slot statistics */
92 #define REPLSLOT_ACC(fld) statent->fld += repSlotStat->fld
93  REPLSLOT_ACC(spill_txns);
94  REPLSLOT_ACC(spill_count);
95  REPLSLOT_ACC(spill_bytes);
96  REPLSLOT_ACC(stream_txns);
97  REPLSLOT_ACC(stream_count);
98  REPLSLOT_ACC(stream_bytes);
99  REPLSLOT_ACC(total_txns);
100  REPLSLOT_ACC(total_bytes);
101 #undef REPLSLOT_ACC
102 
103  pgstat_unlock_entry(entry_ref);
104 }
Assert(fmt[strlen(fmt) - 1] !='\n')
#define REPLSLOT_ACC(fld)

References Assert(), ReplicationSlot::data, InvalidOid, ReplicationSlotPersistentData::name, NameStr, namestrcmp(), pgstat_get_entry_ref_locked(), PGSTAT_KIND_REPLSLOT, pgstat_unlock_entry(), ReplicationSlotIndex(), REPLSLOT_ACC, PgStat_EntryRef::shared_stats, PgStat_StatReplSlotEntry::slotname, and PgStatShared_ReplSlot::stats.

Referenced by UpdateDecodingStats().

◆ pgstat_reset_replslot()

void pgstat_reset_replslot ( const char *  name)

Definition at line 43 of file pgstat_replslot.c.

44 {
45  ReplicationSlot *slot;
46 
47  AssertArg(name != NULL);
48 
49  /* Check if the slot exits with the given name. */
50  slot = SearchNamedReplicationSlot(name, true);
51 
52  if (!slot)
53  ereport(ERROR,
54  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
55  errmsg("replication slot \"%s\" does not exist",
56  name)));
57 
58  /*
59  * Nothing to do for physical slots as we collect stats only for logical
60  * slots.
61  */
62  if (SlotIsPhysical(slot))
63  return;
64 
65  /* reset this one entry */
67  ReplicationSlotIndex(slot));
68 }
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
void pgstat_reset(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat.c:705
#define SlotIsPhysical(slot)
Definition: slot.h:168

References AssertArg, ereport, errcode(), errmsg(), ERROR, InvalidOid, name, PGSTAT_KIND_REPLSLOT, pgstat_reset(), ReplicationSlotIndex(), SearchNamedReplicationSlot(), and SlotIsPhysical.

Referenced by pg_stat_reset_replication_slot().