LCOV - code coverage report
Current view: top level - src/backend/utils/activity - pgstat_replslot.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 67 68 98.5 %
Date: 2024-05-09 12:10:51 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-2024, 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/pgstat_internal.h"
      30             : 
      31             : 
      32             : static int  get_replslot_index(const char *name, bool need_lock);
      33             : 
      34             : 
      35             : /*
      36             :  * Reset counters for a single replication slot.
      37             :  *
      38             :  * Permission checking for this function is managed through the normal
      39             :  * GRANT system.
      40             :  */
      41             : void
      42           8 : pgstat_reset_replslot(const char *name)
      43             : {
      44             :     ReplicationSlot *slot;
      45             : 
      46             :     Assert(name != NULL);
      47             : 
      48           8 :     LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
      49             : 
      50             :     /* Check if the slot exits with the given name. */
      51           8 :     slot = SearchNamedReplicationSlot(name, false);
      52             : 
      53           8 :     if (!slot)
      54           2 :         ereport(ERROR,
      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           6 :     if (SlotIsLogical(slot))
      64           6 :         pgstat_reset(PGSTAT_KIND_REPLSLOT, InvalidOid,
      65           6 :                      ReplicationSlotIndex(slot));
      66             : 
      67           6 :     LWLockRelease(ReplicationSlotControlLock);
      68           6 : }
      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       10976 : 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       10976 :     entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_REPLSLOT, InvalidOid,
      85       10976 :                                             ReplicationSlotIndex(slot), false);
      86       10976 :     shstatent = (PgStatShared_ReplSlot *) entry_ref->shared_stats;
      87       10976 :     statent = &shstatent->stats;
      88             : 
      89             :     /* Update the replication slot statistics */
      90             : #define REPLSLOT_ACC(fld) statent->fld += repSlotStat->fld
      91       10976 :     REPLSLOT_ACC(spill_txns);
      92       10976 :     REPLSLOT_ACC(spill_count);
      93       10976 :     REPLSLOT_ACC(spill_bytes);
      94       10976 :     REPLSLOT_ACC(stream_txns);
      95       10976 :     REPLSLOT_ACC(stream_count);
      96       10976 :     REPLSLOT_ACC(stream_bytes);
      97       10976 :     REPLSLOT_ACC(total_txns);
      98       10976 :     REPLSLOT_ACC(total_bytes);
      99             : #undef REPLSLOT_ACC
     100             : 
     101       10976 :     pgstat_unlock_entry(entry_ref);
     102       10976 : }
     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         806 : pgstat_create_replslot(ReplicationSlot *slot)
     112             : {
     113             :     PgStat_EntryRef *entry_ref;
     114             :     PgStatShared_ReplSlot *shstatent;
     115             : 
     116             :     Assert(LWLockHeldByMeInMode(ReplicationSlotAllocationLock, LW_EXCLUSIVE));
     117             : 
     118         806 :     entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_REPLSLOT, InvalidOid,
     119         806 :                                             ReplicationSlotIndex(slot), false);
     120         806 :     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         806 :     memset(&shstatent->stats, 0, sizeof(shstatent->stats));
     127             : 
     128         806 :     pgstat_unlock_entry(entry_ref);
     129         806 : }
     130             : 
     131             : /*
     132             :  * Report replication slot has been acquired.
     133             :  *
     134             :  * This guarantees that a stats entry exists during later
     135             :  * pgstat_report_replslot() calls.
     136             :  *
     137             :  * If we previously crashed, no stats data exists. But if we did not crash,
     138             :  * the stats do belong to this slot:
     139             :  * - the stats cannot belong to a dropped slot, pgstat_drop_replslot() would
     140             :  *   have been called
     141             :  * - if the slot was removed while shut down,
     142             :  *   pgstat_replslot_from_serialized_name_cb() returning false would have
     143             :  *   caused the stats to be dropped
     144             :  */
     145             : void
     146        1772 : pgstat_acquire_replslot(ReplicationSlot *slot)
     147             : {
     148        1772 :     pgstat_get_entry_ref(PGSTAT_KIND_REPLSLOT, InvalidOid,
     149        1772 :                          ReplicationSlotIndex(slot), true, NULL);
     150        1772 : }
     151             : 
     152             : /*
     153             :  * Report replication slot drop.
     154             :  */
     155             : void
     156         684 : pgstat_drop_replslot(ReplicationSlot *slot)
     157             : {
     158             :     Assert(LWLockHeldByMeInMode(ReplicationSlotAllocationLock, LW_EXCLUSIVE));
     159             : 
     160         684 :     pgstat_drop_entry(PGSTAT_KIND_REPLSLOT, InvalidOid,
     161         684 :                       ReplicationSlotIndex(slot));
     162         684 : }
     163             : 
     164             : /*
     165             :  * Support function for the SQL-callable pgstat* functions. Returns
     166             :  * a pointer to the replication slot statistics struct.
     167             :  */
     168             : PgStat_StatReplSlotEntry *
     169          94 : pgstat_fetch_replslot(NameData slotname)
     170             : {
     171             :     int         idx;
     172          94 :     PgStat_StatReplSlotEntry *slotentry = NULL;
     173             : 
     174          94 :     LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
     175             : 
     176          94 :     idx = get_replslot_index(NameStr(slotname), false);
     177             : 
     178          94 :     if (idx != -1)
     179          90 :         slotentry = (PgStat_StatReplSlotEntry *) pgstat_fetch_entry(PGSTAT_KIND_REPLSLOT,
     180             :                                                                     InvalidOid, idx);
     181             : 
     182          94 :     LWLockRelease(ReplicationSlotControlLock);
     183             : 
     184          94 :     return slotentry;
     185             : }
     186             : 
     187             : void
     188         134 : pgstat_replslot_to_serialized_name_cb(const PgStat_HashKey *key, const PgStatShared_Common *header, NameData *name)
     189             : {
     190             :     /*
     191             :      * This is only called late during shutdown. The set of existing slots
     192             :      * isn't allowed to change at this point, we can assume that a slot exists
     193             :      * at the offset.
     194             :      */
     195         134 :     if (!ReplicationSlotName(key->objoid, name))
     196           0 :         elog(ERROR, "could not find name for replication slot index %u",
     197             :              key->objoid);
     198         134 : }
     199             : 
     200             : bool
     201          76 : pgstat_replslot_from_serialized_name_cb(const NameData *name, PgStat_HashKey *key)
     202             : {
     203          76 :     int         idx = get_replslot_index(NameStr(*name), true);
     204             : 
     205             :     /* slot might have been deleted */
     206          76 :     if (idx == -1)
     207           2 :         return false;
     208             : 
     209          74 :     key->kind = PGSTAT_KIND_REPLSLOT;
     210          74 :     key->dboid = InvalidOid;
     211          74 :     key->objoid = idx;
     212             : 
     213          74 :     return true;
     214             : }
     215             : 
     216             : void
     217          16 : pgstat_replslot_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
     218             : {
     219          16 :     ((PgStatShared_ReplSlot *) header)->stats.stat_reset_timestamp = ts;
     220          16 : }
     221             : 
     222             : static int
     223         170 : get_replslot_index(const char *name, bool need_lock)
     224             : {
     225             :     ReplicationSlot *slot;
     226             : 
     227             :     Assert(name != NULL);
     228             : 
     229         170 :     slot = SearchNamedReplicationSlot(name, need_lock);
     230             : 
     231         170 :     if (!slot)
     232           6 :         return -1;
     233             : 
     234         164 :     return ReplicationSlotIndex(slot);
     235             : }

Generated by: LCOV version 1.14