LCOV - code coverage report
Current view: top level - src/backend/utils/activity - pgstat_replslot.c (source / functions) Hit Total Coverage
Test: PostgreSQL 16beta1 Lines: 62 64 96.9 %
Date: 2023-06-02 18:12:27 Functions: 10 10 100.0 %
Legend: Lines: hit not hit

          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-2023, 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/builtins.h"       /* for namestrcpy() */
      30             : #include "utils/pgstat_internal.h"
      31             : 
      32             : 
      33             : static int  get_replslot_index(const char *name);
      34             : 
      35             : 
      36             : /*
      37             :  * Reset counters for a single replication slot.
      38             :  *
      39             :  * Permission checking for this function is managed through the normal
      40             :  * GRANT system.
      41             :  */
      42             : void
      43           8 : pgstat_reset_replslot(const char *name)
      44             : {
      45             :     ReplicationSlot *slot;
      46             : 
      47             :     Assert(name != NULL);
      48             : 
      49             :     /* Check if the slot exits with the given name. */
      50           8 :     slot = SearchNamedReplicationSlot(name, true);
      51             : 
      52           8 :     if (!slot)
      53           2 :         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           6 :     if (SlotIsPhysical(slot))
      63           0 :         return;
      64             : 
      65             :     /* reset this one entry */
      66           6 :     pgstat_reset(PGSTAT_KIND_REPLSLOT, InvalidOid,
      67           6 :                  ReplicationSlotIndex(slot));
      68             : }
      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
      78        9850 : 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        9850 :     entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_REPLSLOT, InvalidOid,
      85        9850 :                                             ReplicationSlotIndex(slot), false);
      86        9850 :     shstatent = (PgStatShared_ReplSlot *) entry_ref->shared_stats;
      87        9850 :     statent = &shstatent->stats;
      88             : 
      89             :     /* Update the replication slot statistics */
      90             : #define REPLSLOT_ACC(fld) statent->fld += repSlotStat->fld
      91        9850 :     REPLSLOT_ACC(spill_txns);
      92        9850 :     REPLSLOT_ACC(spill_count);
      93        9850 :     REPLSLOT_ACC(spill_bytes);
      94        9850 :     REPLSLOT_ACC(stream_txns);
      95        9850 :     REPLSLOT_ACC(stream_count);
      96        9850 :     REPLSLOT_ACC(stream_bytes);
      97        9850 :     REPLSLOT_ACC(total_txns);
      98        9850 :     REPLSLOT_ACC(total_bytes);
      99             : #undef REPLSLOT_ACC
     100             : 
     101        9850 :     pgstat_unlock_entry(entry_ref);
     102        9850 : }
     103             : 
     104             : /*
     105             :  * Report replication slot creation.
     106             :  *
     107             :  * NB: This gets called with ReplicationSlotAllocationLock already held, be
     108             :  * careful about calling back into slot.c.
     109             :  */
     110             : void
     111         694 : pgstat_create_replslot(ReplicationSlot *slot)
     112             : {
     113             :     PgStat_EntryRef *entry_ref;
     114             :     PgStatShared_ReplSlot *shstatent;
     115             : 
     116         694 :     entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_REPLSLOT, InvalidOid,
     117         694 :                                             ReplicationSlotIndex(slot), false);
     118         694 :     shstatent = (PgStatShared_ReplSlot *) entry_ref->shared_stats;
     119             : 
     120             :     /*
     121             :      * NB: need to accept that there might be stats from an older slot, e.g.
     122             :      * if we previously crashed after dropping a slot.
     123             :      */
     124         694 :     memset(&shstatent->stats, 0, sizeof(shstatent->stats));
     125             : 
     126         694 :     pgstat_unlock_entry(entry_ref);
     127         694 : }
     128             : 
     129             : /*
     130             :  * Report replication slot has been acquired.
     131             :  *
     132             :  * This guarantees that a stats entry exists during later
     133             :  * pgstat_report_replslot() calls.
     134             :  *
     135             :  * If we previously crashed, no stats data exists. But if we did not crash,
     136             :  * the stats do belong to this slot:
     137             :  * - the stats cannot belong to a dropped slot, pgstat_drop_replslot() would
     138             :  *   have been called
     139             :  * - if the slot was removed while shut down,
     140             :  *   pgstat_replslot_from_serialized_name_cb() returning false would have
     141             :  *   caused the stats to be dropped
     142             :  */
     143             : void
     144        1548 : pgstat_acquire_replslot(ReplicationSlot *slot)
     145             : {
     146        1548 :     pgstat_get_entry_ref(PGSTAT_KIND_REPLSLOT, InvalidOid,
     147        1548 :                          ReplicationSlotIndex(slot), true, NULL);
     148        1548 : }
     149             : 
     150             : /*
     151             :  * Report replication slot drop.
     152             :  */
     153             : void
     154         628 : pgstat_drop_replslot(ReplicationSlot *slot)
     155             : {
     156         628 :     pgstat_drop_entry(PGSTAT_KIND_REPLSLOT, InvalidOid,
     157         628 :                       ReplicationSlotIndex(slot));
     158         628 : }
     159             : 
     160             : /*
     161             :  * Support function for the SQL-callable pgstat* functions. Returns
     162             :  * a pointer to the replication slot statistics struct.
     163             :  */
     164             : PgStat_StatReplSlotEntry *
     165          86 : pgstat_fetch_replslot(NameData slotname)
     166             : {
     167          86 :     int         idx = get_replslot_index(NameStr(slotname));
     168             : 
     169          86 :     if (idx == -1)
     170           4 :         return NULL;
     171             : 
     172          82 :     return (PgStat_StatReplSlotEntry *)
     173          82 :         pgstat_fetch_entry(PGSTAT_KIND_REPLSLOT, InvalidOid, idx);
     174             : }
     175             : 
     176             : void
     177          86 : pgstat_replslot_to_serialized_name_cb(const PgStat_HashKey *key, const PgStatShared_Common *header, NameData *name)
     178             : {
     179             :     /*
     180             :      * This is only called late during shutdown. The set of existing slots
     181             :      * isn't allowed to change at this point, we can assume that a slot exists
     182             :      * at the offset.
     183             :      */
     184          86 :     if (!ReplicationSlotName(key->objoid, name))
     185           0 :         elog(ERROR, "could not find name for replication slot index %u",
     186             :              key->objoid);
     187          86 : }
     188             : 
     189             : bool
     190          34 : pgstat_replslot_from_serialized_name_cb(const NameData *name, PgStat_HashKey *key)
     191             : {
     192          34 :     int         idx = get_replslot_index(NameStr(*name));
     193             : 
     194             :     /* slot might have been deleted */
     195          34 :     if (idx == -1)
     196           2 :         return false;
     197             : 
     198          32 :     key->kind = PGSTAT_KIND_REPLSLOT;
     199          32 :     key->dboid = InvalidOid;
     200          32 :     key->objoid = idx;
     201             : 
     202          32 :     return true;
     203             : }
     204             : 
     205             : void
     206          16 : pgstat_replslot_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
     207             : {
     208          16 :     ((PgStatShared_ReplSlot *) header)->stats.stat_reset_timestamp = ts;
     209          16 : }
     210             : 
     211             : static int
     212         120 : get_replslot_index(const char *name)
     213             : {
     214             :     ReplicationSlot *slot;
     215             : 
     216             :     Assert(name != NULL);
     217             : 
     218         120 :     slot = SearchNamedReplicationSlot(name, true);
     219             : 
     220         120 :     if (!slot)
     221           6 :         return -1;
     222             : 
     223         114 :     return ReplicationSlotIndex(slot);
     224             : }

Generated by: LCOV version 1.14