LCOV - code coverage report
Current view: top level - src/backend/utils/activity - pgstat_replslot.c (source / functions) Hit Total Coverage
Test: PostgreSQL 16devel Lines: 70 71 98.6 %
Date: 2022-08-17 03:10:30 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 other
      12             :  * variable-numbered stats. Slots do not have oids (so they can be created on
      13             :  * physical replicas). Use the slot index as object id while running. However,
      14             :  * the slot index can change when restarting. That is addressed by using the
      15             :  * name when (de-)serializing. After a restart it is possible for slots to
      16             :  * have been dropped while shut down, which is addressed by not restoring
      17             :  * stats for slots that cannot be found by name when starting up.
      18             :  *
      19             :  * Copyright (c) 2001-2022, 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             :     AssertArg(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             : void
      74        7682 : pgstat_report_replslot(ReplicationSlot *slot, const PgStat_StatReplSlotEntry *repSlotStat)
      75             : {
      76             :     PgStat_EntryRef *entry_ref;
      77             :     PgStatShared_ReplSlot *shstatent;
      78             :     PgStat_StatReplSlotEntry *statent;
      79             : 
      80        7682 :     entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_REPLSLOT, InvalidOid,
      81        7682 :                                             ReplicationSlotIndex(slot), false);
      82        7682 :     shstatent = (PgStatShared_ReplSlot *) entry_ref->shared_stats;
      83        7682 :     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        7682 :     REPLSLOT_ACC(spill_txns);
      94        7682 :     REPLSLOT_ACC(spill_count);
      95        7682 :     REPLSLOT_ACC(spill_bytes);
      96        7682 :     REPLSLOT_ACC(stream_txns);
      97        7682 :     REPLSLOT_ACC(stream_count);
      98        7682 :     REPLSLOT_ACC(stream_bytes);
      99        7682 :     REPLSLOT_ACC(total_txns);
     100        7682 :     REPLSLOT_ACC(total_bytes);
     101             : #undef REPLSLOT_ACC
     102             : 
     103        7682 :     pgstat_unlock_entry(entry_ref);
     104        7682 : }
     105             : 
     106             : /*
     107             :  * Report replication slot creation.
     108             :  *
     109             :  * NB: This gets called with ReplicationSlotAllocationLock already held, be
     110             :  * careful about calling back into slot.c.
     111             :  */
     112             : void
     113         576 : pgstat_create_replslot(ReplicationSlot *slot)
     114             : {
     115             :     PgStat_EntryRef *entry_ref;
     116             :     PgStatShared_ReplSlot *shstatent;
     117             : 
     118         576 :     entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_REPLSLOT, InvalidOid,
     119         576 :                                             ReplicationSlotIndex(slot), false);
     120         576 :     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         576 :     memset(&shstatent->stats, 0, sizeof(shstatent->stats));
     127         576 :     namestrcpy(&shstatent->stats.slotname, NameStr(slot->data.name));
     128             : 
     129         576 :     pgstat_unlock_entry(entry_ref);
     130         576 : }
     131             : 
     132             : /*
     133             :  * Report replication slot has been acquired.
     134             :  */
     135             : void
     136        1284 : pgstat_acquire_replslot(ReplicationSlot *slot)
     137             : {
     138             :     PgStat_EntryRef *entry_ref;
     139             :     PgStatShared_ReplSlot *shstatent;
     140             :     PgStat_StatReplSlotEntry *statent;
     141             : 
     142        1284 :     entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_REPLSLOT, InvalidOid,
     143        1284 :                                             ReplicationSlotIndex(slot), false);
     144        1284 :     shstatent = (PgStatShared_ReplSlot *) entry_ref->shared_stats;
     145        1284 :     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        2554 :     if (NameStr(statent->slotname)[0] == 0 ||
     152        1270 :         namestrcmp(&statent->slotname, NameStr(slot->data.name)) != 0)
     153             :     {
     154          14 :         memset(statent, 0, sizeof(*statent));
     155          14 :         namestrcpy(&statent->slotname, NameStr(slot->data.name));
     156             :     }
     157             : 
     158        1284 :     pgstat_unlock_entry(entry_ref);
     159        1284 : }
     160             : 
     161             : /*
     162             :  * Report replication slot drop.
     163             :  */
     164             : void
     165         496 : pgstat_drop_replslot(ReplicationSlot *slot)
     166             : {
     167         496 :     pgstat_drop_entry(PGSTAT_KIND_REPLSLOT, InvalidOid,
     168         496 :                       ReplicationSlotIndex(slot));
     169         496 : }
     170             : 
     171             : /*
     172             :  * Support function for the SQL-callable pgstat* functions. Returns
     173             :  * a pointer to the replication slot statistics struct.
     174             :  */
     175             : PgStat_StatReplSlotEntry *
     176          86 : pgstat_fetch_replslot(NameData slotname)
     177             : {
     178          86 :     int         idx = get_replslot_index(NameStr(slotname));
     179             : 
     180          86 :     if (idx == -1)
     181           4 :         return NULL;
     182             : 
     183          82 :     return (PgStat_StatReplSlotEntry *)
     184          82 :         pgstat_fetch_entry(PGSTAT_KIND_REPLSLOT, InvalidOid, idx);
     185             : }
     186             : 
     187             : void
     188          80 : pgstat_replslot_to_serialized_name_cb(const PgStatShared_Common *header, NameData *name)
     189             : {
     190          80 :     namestrcpy(name, NameStr(((PgStatShared_ReplSlot *) header)->stats.slotname));
     191          80 : }
     192             : 
     193             : bool
     194          28 : pgstat_replslot_from_serialized_name_cb(const NameData *name, PgStat_HashKey *key)
     195             : {
     196          28 :     int         idx = get_replslot_index(NameStr(*name));
     197             : 
     198             :     /* slot might have been deleted */
     199          28 :     if (idx == -1)
     200           2 :         return false;
     201             : 
     202          26 :     key->kind = PGSTAT_KIND_REPLSLOT;
     203          26 :     key->dboid = InvalidOid;
     204          26 :     key->objoid = idx;
     205             : 
     206          26 :     return true;
     207             : }
     208             : 
     209             : void
     210          16 : pgstat_replslot_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
     211             : {
     212          16 :     ((PgStatShared_ReplSlot *) header)->stats.stat_reset_timestamp = ts;
     213          16 : }
     214             : 
     215             : static int
     216         114 : get_replslot_index(const char *name)
     217             : {
     218             :     ReplicationSlot *slot;
     219             : 
     220             :     AssertArg(name != NULL);
     221             : 
     222         114 :     slot = SearchNamedReplicationSlot(name, true);
     223             : 
     224         114 :     if (!slot)
     225           6 :         return -1;
     226             : 
     227         108 :     return ReplicationSlotIndex(slot);
     228             : }

Generated by: LCOV version 1.14