LCOV - differential code coverage report
Current view: top level - src/backend/utils/activity - pgstat_replslot.c (source / functions) Coverage Total Hit UBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 98.6 % 73 72 1 72
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 10 10 10
Baseline: lcov-20250906-005545-baseline Branches: 65.4 % 26 17 9 17
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 66.7 % 3 2 1 2
(360..) days: 100.0 % 70 70 70
Function coverage date bins:
(360..) days: 100.0 % 10 10 10
Branch coverage date bins:
(30,360] days: 25.0 % 4 1 3 1
(360..) days: 72.7 % 22 16 6 16

 Age         Owner                    Branch data    TLA  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-2025, 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
 1249 andres@anarazel.de         42                 :CBC           4 : pgstat_reset_replslot(const char *name)
                                 43                 :                : {
                                 44                 :                :     ReplicationSlot *slot;
                                 45                 :                : 
 1044 peter@eisentraut.org       46         [ -  + ]:              4 :     Assert(name != NULL);
                                 47                 :                : 
  544 michael@paquier.xyz        48                 :              4 :     LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
                                 49                 :                : 
                                 50                 :                :     /* Check if the slot exits with the given name. */
                                 51                 :              4 :     slot = SearchNamedReplicationSlot(name, false);
                                 52                 :                : 
 1249 andres@anarazel.de         53         [ +  + ]:              4 :     if (!slot)
                                 54         [ +  - ]:              1 :         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                 :                :      */
  544 michael@paquier.xyz        63         [ +  - ]:              3 :     if (SlotIsLogical(slot))
                                 64                 :              3 :         pgstat_reset(PGSTAT_KIND_REPLSLOT, InvalidOid,
                                 65                 :              3 :                      ReplicationSlotIndex(slot));
                                 66                 :                : 
                                 67                 :              3 :     LWLockRelease(ReplicationSlotControlLock);
 1265 andres@anarazel.de         68                 :              3 : }
                                 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
 1249                            78                 :           5143 : 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                 :           5143 :     entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_REPLSLOT, InvalidOid,
                                 85                 :           5143 :                                             ReplicationSlotIndex(slot), false);
                                 86                 :           5143 :     shstatent = (PgStatShared_ReplSlot *) entry_ref->shared_stats;
                                 87                 :           5143 :     statent = &shstatent->stats;
                                 88                 :                : 
                                 89                 :                :     /* Update the replication slot statistics */
                                 90                 :                : #define REPLSLOT_ACC(fld) statent->fld += repSlotStat->fld
                                 91                 :           5143 :     REPLSLOT_ACC(spill_txns);
                                 92                 :           5143 :     REPLSLOT_ACC(spill_count);
                                 93                 :           5143 :     REPLSLOT_ACC(spill_bytes);
                                 94                 :           5143 :     REPLSLOT_ACC(stream_txns);
                                 95                 :           5143 :     REPLSLOT_ACC(stream_count);
                                 96                 :           5143 :     REPLSLOT_ACC(stream_bytes);
                                 97                 :           5143 :     REPLSLOT_ACC(total_txns);
                                 98                 :           5143 :     REPLSLOT_ACC(total_bytes);
                                 99                 :                : #undef REPLSLOT_ACC
                                100                 :                : 
                                101                 :           5143 :     pgstat_unlock_entry(entry_ref);
 1265                           102                 :           5143 : }
                                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
 1249                           111                 :            438 : pgstat_create_replslot(ReplicationSlot *slot)
                                112                 :                : {
                                113                 :                :     PgStat_EntryRef *entry_ref;
                                114                 :                :     PgStatShared_ReplSlot *shstatent;
                                115                 :                : 
  542 michael@paquier.xyz       116         [ -  + ]:            438 :     Assert(LWLockHeldByMeInMode(ReplicationSlotAllocationLock, LW_EXCLUSIVE));
                                117                 :                : 
 1249 andres@anarazel.de        118                 :            438 :     entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_REPLSLOT, InvalidOid,
                                119                 :            438 :                                             ReplicationSlotIndex(slot), false);
                                120                 :            438 :     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                 :            438 :     memset(&shstatent->stats, 0, sizeof(shstatent->stats));
                                127                 :                : 
                                128                 :            438 :     pgstat_unlock_entry(entry_ref);
                                129                 :            438 : }
                                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                 :           1023 : pgstat_acquire_replslot(ReplicationSlot *slot)
                                147                 :                : {
 1064                           148                 :           1023 :     pgstat_get_entry_ref(PGSTAT_KIND_REPLSLOT, InvalidOid,
                                149                 :           1023 :                          ReplicationSlotIndex(slot), true, NULL);
 1265                           150                 :           1023 : }
                                151                 :                : 
                                152                 :                : /*
                                153                 :                :  * Report replication slot drop.
                                154                 :                :  */
                                155                 :                : void
 1249                           156                 :            378 : pgstat_drop_replslot(ReplicationSlot *slot)
                                157                 :                : {
  542 michael@paquier.xyz       158         [ -  + ]:            378 :     Assert(LWLockHeldByMeInMode(ReplicationSlotAllocationLock, LW_EXCLUSIVE));
                                159                 :                : 
  457                           160         [ +  + ]:            378 :     if (!pgstat_drop_entry(PGSTAT_KIND_REPLSLOT, InvalidOid,
                                161                 :            378 :                            ReplicationSlotIndex(slot)))
                                162                 :             48 :         pgstat_request_entry_refs_gc();
 1249 andres@anarazel.de        163                 :            378 : }
                                164                 :                : 
                                165                 :                : /*
                                166                 :                :  * Support function for the SQL-callable pgstat* functions. Returns
                                167                 :                :  * a pointer to the replication slot statistics struct.
                                168                 :                :  */
                                169                 :                : PgStat_StatReplSlotEntry *
                                170                 :             47 : pgstat_fetch_replslot(NameData slotname)
                                171                 :                : {
                                172                 :                :     int         idx;
  544 michael@paquier.xyz       173                 :             47 :     PgStat_StatReplSlotEntry *slotentry = NULL;
                                174                 :                : 
                                175                 :             47 :     LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
                                176                 :                : 
                                177                 :             47 :     idx = get_replslot_index(NameStr(slotname), false);
                                178                 :                : 
                                179         [ +  + ]:             47 :     if (idx != -1)
                                180                 :             45 :         slotentry = (PgStat_StatReplSlotEntry *) pgstat_fetch_entry(PGSTAT_KIND_REPLSLOT,
                                181                 :                :                                                                     InvalidOid, idx);
                                182                 :                : 
                                183                 :             47 :     LWLockRelease(ReplicationSlotControlLock);
                                184                 :                : 
                                185                 :             47 :     return slotentry;
                                186                 :                : }
                                187                 :                : 
                                188                 :                : void
 1064 andres@anarazel.de        189                 :             89 : pgstat_replslot_to_serialized_name_cb(const PgStat_HashKey *key, const PgStatShared_Common *header, NameData *name)
                                190                 :                : {
                                191                 :                :     /*
                                192                 :                :      * This is only called late during shutdown. The set of existing slots
                                193                 :                :      * isn't allowed to change at this point, we can assume that a slot exists
                                194                 :                :      * at the offset.
                                195                 :                :      */
  353 michael@paquier.xyz       196         [ -  + ]:             89 :     if (!ReplicationSlotName(key->objid, name))
  161 peter@eisentraut.org      197         [ #  # ]:UBC           0 :         elog(ERROR, "could not find name for replication slot index %" PRIu64,
                                198                 :                :              key->objid);
 1249 andres@anarazel.de        199                 :CBC          89 : }
                                200                 :                : 
                                201                 :                : bool
                                202                 :             61 : pgstat_replslot_from_serialized_name_cb(const NameData *name, PgStat_HashKey *key)
                                203                 :                : {
  544 michael@paquier.xyz       204                 :             61 :     int         idx = get_replslot_index(NameStr(*name), true);
                                205                 :                : 
                                206                 :                :     /* slot might have been deleted */
 1249 andres@anarazel.de        207         [ +  + ]:             61 :     if (idx == -1)
                                208                 :              1 :         return false;
                                209                 :                : 
                                210                 :             60 :     key->kind = PGSTAT_KIND_REPLSLOT;
                                211                 :             60 :     key->dboid = InvalidOid;
  353 michael@paquier.xyz       212                 :             60 :     key->objid = idx;
                                213                 :                : 
 1249 andres@anarazel.de        214                 :             60 :     return true;
                                215                 :                : }
                                216                 :                : 
                                217                 :                : void
                                218                 :              8 : pgstat_replslot_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
                                219                 :                : {
                                220                 :              8 :     ((PgStatShared_ReplSlot *) header)->stats.stat_reset_timestamp = ts;
                                221                 :              8 : }
                                222                 :                : 
                                223                 :                : static int
  544 michael@paquier.xyz       224                 :            108 : get_replslot_index(const char *name, bool need_lock)
                                225                 :                : {
                                226                 :                :     ReplicationSlot *slot;
                                227                 :                : 
 1044 peter@eisentraut.org      228         [ -  + ]:            108 :     Assert(name != NULL);
                                229                 :                : 
  544 michael@paquier.xyz       230                 :            108 :     slot = SearchNamedReplicationSlot(name, need_lock);
                                231                 :                : 
 1249 andres@anarazel.de        232         [ +  + ]:            108 :     if (!slot)
                                233                 :              3 :         return -1;
                                234                 :                : 
                                235                 :            105 :     return ReplicationSlotIndex(slot);
                                236                 :                : }
        

Generated by: LCOV version 2.4-beta