Skip to content

Commit e41aed6

Browse files
committed
pgstat: revise replication slot API in preparation for shared memory stats.
Previously the pgstat <-> replication slots API was done with on the basis of names. However, the upcoming move to storing stats in shared memory makes it more convenient to use a integer as key. Change the replication slot functions to take the slot rather than the slot name, and expose ReplicationSlotIndex() to compute the index of an replication slot. Special handling will be required for restarts, as the index is not stable across restarts. For now pgstat internally still uses names. Rename pgstat_report_replslot_{create,drop}() to pgstat_{create,drop}_replslot() to match the functions for other kinds of stats. Reviewed-By: Kyotaro Horiguchi <[email protected]> Discussion: https://postgr.es/m/[email protected]
1 parent 8b1dccd commit e41aed6

File tree

6 files changed

+41
-12
lines changed

6 files changed

+41
-12
lines changed

src/backend/postmaster/pgstat.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,15 @@ pgstat_vacuum_stat(void)
862862
CHECK_FOR_INTERRUPTS();
863863

864864
if (SearchNamedReplicationSlot(NameStr(slotentry->slotname), true) == NULL)
865-
pgstat_report_replslot_drop(NameStr(slotentry->slotname));
865+
{
866+
PgStat_MsgReplSlot msg;
867+
868+
pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_REPLSLOT);
869+
namestrcpy(&msg.m_slotname, NameStr(slotentry->slotname));
870+
msg.m_create = false;
871+
msg.m_drop = true;
872+
pgstat_send(&msg, sizeof(PgStat_MsgReplSlot));
873+
}
866874
}
867875
}
868876

src/backend/replication/logical/logical.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1921,7 +1921,7 @@ UpdateDecodingStats(LogicalDecodingContext *ctx)
19211921
repSlotStat.total_txns = rb->totalTxns;
19221922
repSlotStat.total_bytes = rb->totalBytes;
19231923

1924-
pgstat_report_replslot(&repSlotStat);
1924+
pgstat_report_replslot(ctx->slot, &repSlotStat);
19251925

19261926
rb->spillTxns = 0;
19271927
rb->spillCount = 0;

src/backend/replication/slot.c

+18-2
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ ReplicationSlotCreate(const char *name, bool db_specific,
356356
* ReplicationSlotAllocationLock.
357357
*/
358358
if (SlotIsLogical(slot))
359-
pgstat_report_replslot_create(NameStr(slot->data.name));
359+
pgstat_create_replslot(slot);
360360

361361
/*
362362
* Now that the slot has been marked as in_use and active, it's safe to
@@ -399,6 +399,22 @@ SearchNamedReplicationSlot(const char *name, bool need_lock)
399399
return slot;
400400
}
401401

402+
/*
403+
* Return the index of the replication slot in
404+
* ReplicationSlotCtl->replication_slots.
405+
*
406+
* This is mainly useful to have an efficient key for storing replication slot
407+
* stats.
408+
*/
409+
int
410+
ReplicationSlotIndex(ReplicationSlot *slot)
411+
{
412+
Assert(slot >= ReplicationSlotCtl->replication_slots &&
413+
slot < ReplicationSlotCtl->replication_slots + max_replication_slots);
414+
415+
return slot - ReplicationSlotCtl->replication_slots;
416+
}
417+
402418
/*
403419
* Find a previously created slot and mark it as used by this process.
404420
*
@@ -746,7 +762,7 @@ ReplicationSlotDropPtr(ReplicationSlot *slot)
746762
* doesn't seem worth doing as in practice this won't happen frequently.
747763
*/
748764
if (SlotIsLogical(slot))
749-
pgstat_report_replslot_drop(NameStr(slot->data.name));
765+
pgstat_drop_replslot(slot);
750766

751767
/*
752768
* We release this at the very end, so that nobody starts trying to create

src/backend/utils/activity/pgstat_replslot.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pgstat_reset_replslot(const char *name)
6969
* Report replication slot statistics.
7070
*/
7171
void
72-
pgstat_report_replslot(const PgStat_StatReplSlotEntry *repSlotStat)
72+
pgstat_report_replslot(ReplicationSlot *slot, const PgStat_StatReplSlotEntry *repSlotStat)
7373
{
7474
PgStat_MsgReplSlot msg;
7575

@@ -93,14 +93,17 @@ pgstat_report_replslot(const PgStat_StatReplSlotEntry *repSlotStat)
9393

9494
/*
9595
* Report replication slot creation.
96+
*
97+
* NB: This gets called with ReplicationSlotAllocationLock already held, be
98+
* careful about calling back into slot.c.
9699
*/
97100
void
98-
pgstat_report_replslot_create(const char *slotname)
101+
pgstat_create_replslot(ReplicationSlot *slot)
99102
{
100103
PgStat_MsgReplSlot msg;
101104

102105
pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_REPLSLOT);
103-
namestrcpy(&msg.m_slotname, slotname);
106+
namestrcpy(&msg.m_slotname, NameStr(slot->data.name));
104107
msg.m_create = true;
105108
msg.m_drop = false;
106109
pgstat_send(&msg, sizeof(PgStat_MsgReplSlot));
@@ -110,12 +113,12 @@ pgstat_report_replslot_create(const char *slotname)
110113
* Report replication slot drop.
111114
*/
112115
void
113-
pgstat_report_replslot_drop(const char *slotname)
116+
pgstat_drop_replslot(ReplicationSlot *slot)
114117
{
115118
PgStat_MsgReplSlot msg;
116119

117120
pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_REPLSLOT);
118-
namestrcpy(&msg.m_slotname, slotname);
121+
namestrcpy(&msg.m_slotname, NameStr(slot->data.name));
119122
msg.m_create = false;
120123
msg.m_drop = true;
121124
pgstat_send(&msg, sizeof(PgStat_MsgReplSlot));

src/include/pgstat.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -1137,9 +1137,10 @@ extern PgStat_TableStatus *find_tabstat_entry(Oid rel_id);
11371137
*/
11381138

11391139
extern void pgstat_reset_replslot(const char *name);
1140-
extern void pgstat_report_replslot(const PgStat_StatReplSlotEntry *repSlotStat);
1141-
extern void pgstat_report_replslot_create(const char *slotname);
1142-
extern void pgstat_report_replslot_drop(const char *slotname);
1140+
struct ReplicationSlot;
1141+
extern void pgstat_report_replslot(struct ReplicationSlot *slot, const PgStat_StatReplSlotEntry *repSlotStat);
1142+
extern void pgstat_create_replslot(struct ReplicationSlot *slot);
1143+
extern void pgstat_drop_replslot(struct ReplicationSlot *slot);
11431144

11441145

11451146
/*

src/include/replication/slot.h

+1
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ extern bool ReplicationSlotsCountDBSlots(Oid dboid, int *nslots, int *nactive);
216216
extern void ReplicationSlotsDropDBSlots(Oid dboid);
217217
extern bool InvalidateObsoleteReplicationSlots(XLogSegNo oldestSegno);
218218
extern ReplicationSlot *SearchNamedReplicationSlot(const char *name, bool need_lock);
219+
extern int ReplicationSlotIndex(ReplicationSlot *slot);
219220
extern void ReplicationSlotNameForTablesync(Oid suboid, Oid relid, char *syncslotname, int szslot);
220221
extern void ReplicationSlotDropAtPubNode(WalReceiverConn *wrconn, char *slotname, bool missing_ok);
221222

0 commit comments

Comments
 (0)