Use NameData datatype for slotname in stats.
authorAmit Kapila
Wed, 14 Apr 2021 03:25:03 +0000 (08:55 +0530)
committerAmit Kapila
Wed, 14 Apr 2021 03:25:03 +0000 (08:55 +0530)
This will make it consistent with the other usage of slotname in the code.
In the passing, change pgstat_report_replslot signature to use a structure
rather than multiple parameters.

Reported-by: Andres Freund
Author: Vignesh C
Reviewed-by: Sawada Masahiko, Amit Kapila
Discussion: https://postgr.es/m/20210319185247[email protected]

src/backend/postmaster/pgstat.c
src/backend/replication/logical/logical.c
src/backend/replication/slot.c
src/backend/utils/adt/pgstatfuncs.c
src/include/pgstat.h

index f4467625f7f01ab5be3bb69b1b4ace1d97650309..666ce95d083d8a3bd999d5f815d745c2c650a5e0 100644 (file)
@@ -64,6 +64,7 @@
 #include "storage/pg_shmem.h"
 #include "storage/proc.h"
 #include "storage/procsignal.h"
+#include "utils/builtins.h"
 #include "utils/guc.h"
 #include "utils/memutils.h"
 #include "utils/ps_status.h"
@@ -1539,7 +1540,7 @@ pgstat_reset_replslot_counter(const char *name)
        if (SlotIsPhysical(slot))
            return;
 
-       strlcpy(msg.m_slotname, name, NAMEDATALEN);
+       namestrcpy(&msg.m_slotname, name);
        msg.clearall = false;
    }
    else
@@ -1812,10 +1813,7 @@ pgstat_report_tempfile(size_t filesize)
  * ----------
  */
 void
-pgstat_report_replslot(const char *slotname, PgStat_Counter spilltxns,
-                      PgStat_Counter spillcount, PgStat_Counter spillbytes,
-                      PgStat_Counter streamtxns, PgStat_Counter streamcount,
-                      PgStat_Counter streambytes)
+pgstat_report_replslot(const PgStat_ReplSlotStats *repSlotStat)
 {
    PgStat_MsgReplSlot msg;
 
@@ -1823,14 +1821,14 @@ pgstat_report_replslot(const char *slotname, PgStat_Counter spilltxns,
     * Prepare and send the message
     */
    pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_REPLSLOT);
-   strlcpy(msg.m_slotname, slotname, NAMEDATALEN);
+   namestrcpy(&msg.m_slotname, NameStr(repSlotStat->slotname));
    msg.m_drop = false;
-   msg.m_spill_txns = spilltxns;
-   msg.m_spill_count = spillcount;
-   msg.m_spill_bytes = spillbytes;
-   msg.m_stream_txns = streamtxns;
-   msg.m_stream_count = streamcount;
-   msg.m_stream_bytes = streambytes;
+   msg.m_spill_txns = repSlotStat->spill_txns;
+   msg.m_spill_count = repSlotStat->spill_count;
+   msg.m_spill_bytes = repSlotStat->spill_bytes;
+   msg.m_stream_txns = repSlotStat->stream_txns;
+   msg.m_stream_count = repSlotStat->stream_count;
+   msg.m_stream_bytes = repSlotStat->stream_bytes;
    pgstat_send(&msg, sizeof(PgStat_MsgReplSlot));
 }
 
@@ -1846,7 +1844,7 @@ pgstat_report_replslot_drop(const char *slotname)
    PgStat_MsgReplSlot msg;
 
    pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_REPLSLOT);
-   strlcpy(msg.m_slotname, slotname, NAMEDATALEN);
+   namestrcpy(&msg.m_slotname, slotname);
    msg.m_drop = true;
    pgstat_send(&msg, sizeof(PgStat_MsgReplSlot));
 }
@@ -5202,7 +5200,7 @@ pgstat_recv_resetreplslotcounter(PgStat_MsgResetreplslotcounter *msg,
    else
    {
        /* Get the index of replication slot statistics to reset */
-       idx = pgstat_replslot_index(msg->m_slotname, false);
+       idx = pgstat_replslot_index(NameStr(msg->m_slotname), false);
 
        /*
         * Nothing to do if the given slot entry is not found.  This could
@@ -5538,7 +5536,7 @@ pgstat_recv_replslot(PgStat_MsgReplSlot *msg, int len)
     * Get the index of replication slot statistics.  On dropping, we don't
     * create the new statistics.
     */
-   idx = pgstat_replslot_index(msg->m_slotname, !msg->m_drop);
+   idx = pgstat_replslot_index(NameStr(msg->m_slotname), !msg->m_drop);
 
    /*
     * The slot entry is not found or there is no space to accommodate the new
@@ -5763,7 +5761,7 @@ pgstat_replslot_index(const char *name, bool create_it)
    Assert(nReplSlotStats <= max_replication_slots);
    for (i = 0; i < nReplSlotStats; i++)
    {
-       if (strcmp(replSlotStats[i].slotname, name) == 0)
+       if (namestrcmp(&replSlotStats[i].slotname, name) == 0)
            return i;           /* found */
    }
 
@@ -5776,7 +5774,7 @@ pgstat_replslot_index(const char *name, bool create_it)
 
    /* Register new slot */
    memset(&replSlotStats[nReplSlotStats], 0, sizeof(PgStat_ReplSlotStats));
-   strlcpy(replSlotStats[nReplSlotStats].slotname, name, NAMEDATALEN);
+   namestrcpy(&replSlotStats[nReplSlotStats].slotname, name);
 
    return nReplSlotStats++;
 }
index 4f6e87f18d34cf3a0fe64d2a27938976aed1be71..68e210ce12bceef5c075702e7a83fb38cb189fc5 100644 (file)
@@ -1773,6 +1773,7 @@ void
 UpdateDecodingStats(LogicalDecodingContext *ctx)
 {
    ReorderBuffer *rb = ctx->reorder;
+   PgStat_ReplSlotStats repSlotStat;
 
    /*
     * Nothing to do if we haven't spilled or streamed anything since the last
@@ -1790,9 +1791,15 @@ UpdateDecodingStats(LogicalDecodingContext *ctx)
         (long long) rb->streamCount,
         (long long) rb->streamBytes);
 
-   pgstat_report_replslot(NameStr(ctx->slot->data.name),
-                          rb->spillTxns, rb->spillCount, rb->spillBytes,
-                          rb->streamTxns, rb->streamCount, rb->streamBytes);
+   namestrcpy(&repSlotStat.slotname, NameStr(ctx->slot->data.name));
+   repSlotStat.spill_txns = rb->spillTxns;
+   repSlotStat.spill_count = rb->spillCount;
+   repSlotStat.spill_bytes = rb->spillBytes;
+   repSlotStat.stream_txns = rb->streamTxns;
+   repSlotStat.stream_count = rb->streamCount;
+   repSlotStat.stream_bytes = rb->streamBytes;
+
+   pgstat_report_replslot(&repSlotStat);
    rb->spillTxns = 0;
    rb->spillCount = 0;
    rb->spillBytes = 0;
index 75a087c2f9d27f5f499a50863af8d555f687bb3d..f61b163f78d0e1619914654da4a4217934a1cef3 100644 (file)
@@ -328,7 +328,12 @@ ReplicationSlotCreate(const char *name, bool db_specific,
     * ReplicationSlotAllocationLock.
     */
    if (SlotIsLogical(slot))
-       pgstat_report_replslot(NameStr(slot->data.name), 0, 0, 0, 0, 0, 0);
+   {
+       PgStat_ReplSlotStats repSlotStat;
+       MemSet(&repSlotStat, 0, sizeof(PgStat_ReplSlotStats));
+       namestrcpy(&repSlotStat.slotname, NameStr(slot->data.name));
+       pgstat_report_replslot(&repSlotStat);
+   }
 
    /*
     * Now that the slot has been marked as in_use and active, it's safe to
index 182b15e3f23a8f88c6e8dd46cb7722966f0e9f6f..521ba7361439d65f9fb524e36aec3b6a83bf2d62 100644 (file)
@@ -2328,7 +2328,7 @@ pg_stat_get_replication_slots(PG_FUNCTION_ARGS)
        MemSet(values, 0, sizeof(values));
        MemSet(nulls, 0, sizeof(nulls));
 
-       values[0] = PointerGetDatum(cstring_to_text(s->slotname));
+       values[0] = CStringGetTextDatum(NameStr(s->slotname));
        values[1] = Int64GetDatum(s->spill_txns);
        values[2] = Int64GetDatum(s->spill_count);
        values[3] = Int64GetDatum(s->spill_bytes);
index 9a87e7cd8847b3669332be9d510cbb8364980cbe..8e11215058e801180dcf5039d006ba65a6beaf16 100644 (file)
@@ -393,7 +393,7 @@ typedef struct PgStat_MsgResetslrucounter
 typedef struct PgStat_MsgResetreplslotcounter
 {
    PgStat_MsgHdr m_hdr;
-   char        m_slotname[NAMEDATALEN];
+   NameData      m_slotname;
    bool        clearall;
 } PgStat_MsgResetreplslotcounter;
 
@@ -540,7 +540,7 @@ typedef struct PgStat_MsgSLRU
 typedef struct PgStat_MsgReplSlot
 {
    PgStat_MsgHdr m_hdr;
-   char        m_slotname[NAMEDATALEN];
+   NameData    m_slotname;
    bool        m_drop;
    PgStat_Counter m_spill_txns;
    PgStat_Counter m_spill_count;
@@ -917,7 +917,7 @@ typedef struct PgStat_SLRUStats
  */
 typedef struct PgStat_ReplSlotStats
 {
-   char        slotname[NAMEDATALEN];
+   NameData    slotname;
    PgStat_Counter spill_txns;
    PgStat_Counter spill_count;
    PgStat_Counter spill_bytes;
@@ -1027,10 +1027,7 @@ extern void pgstat_report_recovery_conflict(int reason);
 extern void pgstat_report_deadlock(void);
 extern void pgstat_report_checksum_failures_in_db(Oid dboid, int failurecount);
 extern void pgstat_report_checksum_failure(void);
-extern void pgstat_report_replslot(const char *slotname, PgStat_Counter spilltxns,
-                                  PgStat_Counter spillcount, PgStat_Counter spillbytes,
-                                  PgStat_Counter streamtxns, PgStat_Counter streamcount,
-                                  PgStat_Counter streambytes);
+extern void pgstat_report_replslot(const PgStat_ReplSlotStats *repSlotStat);
 extern void pgstat_report_replslot_drop(const char *slotname);
 
 extern void pgstat_initialize(void);