LCOV - differential code coverage report
Current view: top level - src/backend/utils/activity - pgstat_replslot.c (source / functions) Coverage Total Hit UNC UBC GNC CBC
Current: 0e5ff9b9b45a657aea12440478dc002e9b01f138 vs 0123ce131fca454009439dfa3b2266d1d40737d7 Lines: 98.8 % 86 85 1 13 72
Current Date: 2026-03-14 14:10:32 -0400 Functions: 100.0 % 11 11 3 8
Baseline: lcov-20260315-024220-baseline Branches: 62.5 % 32 20 3 9 3 17
Baseline Date: 2026-03-14 15:27:56 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 92.9 % 14 13 1 13
(360..) days: 100.0 % 72 72 72
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 100.0 % 10 10 2 8
Branch coverage date bins:
(30,360] days: 37.5 % 8 3 3 2 3
(360..) days: 70.8 % 24 17 7 17

 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-2026, 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
 1439 andres@anarazel.de         42                 :CBC           4 : pgstat_reset_replslot(const char *name)
                                 43                 :                : {
                                 44                 :                :     ReplicationSlot *slot;
                                 45                 :                : 
 1234 peter@eisentraut.org       46         [ -  + ]:              4 :     Assert(name != NULL);
                                 47                 :                : 
  734 michael@paquier.xyz        48                 :              4 :     LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
                                 49                 :                : 
                                 50                 :                :     /* Check if the slot exists with the given name. */
                                 51                 :              4 :     slot = SearchNamedReplicationSlot(name, false);
 1439 andres@anarazel.de         52         [ +  + ]:              4 :     if (!slot)
                                 53         [ +  - ]:              1 :         ereport(ERROR,
                                 54                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                 55                 :                :                  errmsg("replication slot \"%s\" does not exist",
                                 56                 :                :                         name)));
                                 57                 :                : 
                                 58                 :                :     /*
                                 59                 :                :      * Reset stats if it is a logical slot. Nothing to do for physical slots
                                 60                 :                :      * as we collect stats only for logical slots.
                                 61                 :                :      */
  734 michael@paquier.xyz        62         [ +  - ]:              3 :     if (SlotIsLogical(slot))
                                 63                 :              3 :         pgstat_reset(PGSTAT_KIND_REPLSLOT, InvalidOid,
                                 64                 :              3 :                      ReplicationSlotIndex(slot));
                                 65                 :                : 
                                 66                 :              3 :     LWLockRelease(ReplicationSlotControlLock);
 1455 andres@anarazel.de         67                 :              3 : }
                                 68                 :                : 
                                 69                 :                : /*
                                 70                 :                :  * Report replication slot statistics.
                                 71                 :                :  *
                                 72                 :                :  * We can rely on the stats for the slot to exist and to belong to this
                                 73                 :                :  * slot. We can only get here if pgstat_create_replslot() or
                                 74                 :                :  * pgstat_acquire_replslot() have already been called.
                                 75                 :                :  */
                                 76                 :                : void
 1439                            77                 :           6362 : pgstat_report_replslot(ReplicationSlot *slot, const PgStat_StatReplSlotEntry *repSlotStat)
                                 78                 :                : {
                                 79                 :                :     PgStat_EntryRef *entry_ref;
                                 80                 :                :     PgStatShared_ReplSlot *shstatent;
                                 81                 :                :     PgStat_StatReplSlotEntry *statent;
                                 82                 :                : 
                                 83                 :           6362 :     entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_REPLSLOT, InvalidOid,
                                 84                 :           6362 :                                             ReplicationSlotIndex(slot), false);
                                 85                 :           6362 :     shstatent = (PgStatShared_ReplSlot *) entry_ref->shared_stats;
                                 86                 :           6362 :     statent = &shstatent->stats;
                                 87                 :                : 
                                 88                 :                :     /* Update the replication slot statistics */
                                 89                 :                : #define REPLSLOT_ACC(fld) statent->fld += repSlotStat->fld
                                 90                 :           6362 :     REPLSLOT_ACC(spill_txns);
                                 91                 :           6362 :     REPLSLOT_ACC(spill_count);
                                 92                 :           6362 :     REPLSLOT_ACC(spill_bytes);
                                 93                 :           6362 :     REPLSLOT_ACC(stream_txns);
                                 94                 :           6362 :     REPLSLOT_ACC(stream_count);
                                 95                 :           6362 :     REPLSLOT_ACC(stream_bytes);
  158 msawada@postgresql.o       96                 :GNC        6362 :     REPLSLOT_ACC(mem_exceeded_count);
 1439 andres@anarazel.de         97                 :CBC        6362 :     REPLSLOT_ACC(total_txns);
                                 98                 :           6362 :     REPLSLOT_ACC(total_bytes);
                                 99                 :                : #undef REPLSLOT_ACC
                                100                 :                : 
                                101                 :           6362 :     pgstat_unlock_entry(entry_ref);
 1455                           102                 :           6362 : }
                                103                 :                : 
                                104                 :                : /*
                                105                 :                :  * Report replication slot sync skip statistics.
                                106                 :                :  *
                                107                 :                :  * Similar to pgstat_report_replslot(), we can rely on the stats for the
                                108                 :                :  * slot to exist and to belong to this slot.
                                109                 :                :  */
                                110                 :                : void
  110 akapila@postgresql.o      111                 :GNC           4 : pgstat_report_replslotsync(ReplicationSlot *slot)
                                112                 :                : {
                                113                 :                :     PgStat_EntryRef *entry_ref;
                                114                 :                :     PgStatShared_ReplSlot *shstatent;
                                115                 :                :     PgStat_StatReplSlotEntry *statent;
                                116                 :                : 
                                117                 :                :     /* Slot sync stats are valid only for synced logical slots on standby. */
  107                           118         [ -  + ]:              4 :     Assert(slot->data.synced);
  110                           119         [ -  + ]:              4 :     Assert(RecoveryInProgress());
                                120                 :                : 
                                121                 :              4 :     entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_REPLSLOT, InvalidOid,
                                122                 :              4 :                                             ReplicationSlotIndex(slot), false);
                                123         [ -  + ]:              4 :     Assert(entry_ref != NULL);
                                124                 :                : 
                                125                 :              4 :     shstatent = (PgStatShared_ReplSlot *) entry_ref->shared_stats;
                                126                 :              4 :     statent = &shstatent->stats;
                                127                 :                : 
                                128                 :              4 :     statent->slotsync_skip_count += 1;
  100                           129                 :              4 :     statent->slotsync_last_skip = GetCurrentTimestamp();
                                130                 :                : 
  110                           131                 :              4 :     pgstat_unlock_entry(entry_ref);
                                132                 :              4 : }
                                133                 :                : 
                                134                 :                : /*
                                135                 :                :  * Report replication slot creation.
                                136                 :                :  *
                                137                 :                :  * NB: This gets called with ReplicationSlotAllocationLock already held, be
                                138                 :                :  * careful about calling back into slot.c.
                                139                 :                :  */
                                140                 :                : void
 1439 andres@anarazel.de        141                 :CBC         500 : pgstat_create_replslot(ReplicationSlot *slot)
                                142                 :                : {
                                143                 :                :     PgStat_EntryRef *entry_ref;
                                144                 :                :     PgStatShared_ReplSlot *shstatent;
                                145                 :                : 
  732 michael@paquier.xyz       146         [ -  + ]:            500 :     Assert(LWLockHeldByMeInMode(ReplicationSlotAllocationLock, LW_EXCLUSIVE));
                                147                 :                : 
 1439 andres@anarazel.de        148                 :            500 :     entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_REPLSLOT, InvalidOid,
                                149                 :            500 :                                             ReplicationSlotIndex(slot), false);
                                150                 :            500 :     shstatent = (PgStatShared_ReplSlot *) entry_ref->shared_stats;
                                151                 :                : 
                                152                 :                :     /*
                                153                 :                :      * NB: need to accept that there might be stats from an older slot, e.g.
                                154                 :                :      * if we previously crashed after dropping a slot.
                                155                 :                :      */
                                156                 :            500 :     memset(&shstatent->stats, 0, sizeof(shstatent->stats));
                                157                 :                : 
                                158                 :            500 :     pgstat_unlock_entry(entry_ref);
                                159                 :            500 : }
                                160                 :                : 
                                161                 :                : /*
                                162                 :                :  * Report replication slot has been acquired.
                                163                 :                :  *
                                164                 :                :  * This guarantees that a stats entry exists during later
                                165                 :                :  * pgstat_report_replslot() or pgstat_report_replslotsync() calls.
                                166                 :                :  *
                                167                 :                :  * If we previously crashed, no stats data exists. But if we did not crash,
                                168                 :                :  * the stats do belong to this slot:
                                169                 :                :  * - the stats cannot belong to a dropped slot, pgstat_drop_replslot() would
                                170                 :                :  *   have been called
                                171                 :                :  * - if the slot was removed while shut down,
                                172                 :                :  *   pgstat_replslot_from_serialized_name_cb() returning false would have
                                173                 :                :  *   caused the stats to be dropped
                                174                 :                :  */
                                175                 :                : void
                                176                 :           1125 : pgstat_acquire_replslot(ReplicationSlot *slot)
                                177                 :                : {
 1254                           178                 :           1125 :     pgstat_get_entry_ref(PGSTAT_KIND_REPLSLOT, InvalidOid,
                                179                 :           1125 :                          ReplicationSlotIndex(slot), true, NULL);
 1455                           180                 :           1125 : }
                                181                 :                : 
                                182                 :                : /*
                                183                 :                :  * Report replication slot drop.
                                184                 :                :  */
                                185                 :                : void
 1439                           186                 :            431 : pgstat_drop_replslot(ReplicationSlot *slot)
                                187                 :                : {
  732 michael@paquier.xyz       188         [ -  + ]:            431 :     Assert(LWLockHeldByMeInMode(ReplicationSlotAllocationLock, LW_EXCLUSIVE));
                                189                 :                : 
  647                           190         [ +  + ]:            431 :     if (!pgstat_drop_entry(PGSTAT_KIND_REPLSLOT, InvalidOid,
                                191                 :            431 :                            ReplicationSlotIndex(slot)))
                                192                 :             48 :         pgstat_request_entry_refs_gc();
 1439 andres@anarazel.de        193                 :            431 : }
                                194                 :                : 
                                195                 :                : /*
                                196                 :                :  * Support function for the SQL-callable pgstat* functions. Returns
                                197                 :                :  * a pointer to the replication slot statistics struct.
                                198                 :                :  */
                                199                 :                : PgStat_StatReplSlotEntry *
                                200                 :             52 : pgstat_fetch_replslot(NameData slotname)
                                201                 :                : {
                                202                 :                :     int         idx;
  734 michael@paquier.xyz       203                 :             52 :     PgStat_StatReplSlotEntry *slotentry = NULL;
                                204                 :                : 
                                205                 :             52 :     LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
                                206                 :                : 
                                207                 :             52 :     idx = get_replslot_index(NameStr(slotname), false);
                                208                 :                : 
                                209         [ +  + ]:             52 :     if (idx != -1)
                                210                 :             50 :         slotentry = (PgStat_StatReplSlotEntry *) pgstat_fetch_entry(PGSTAT_KIND_REPLSLOT,
                                211                 :                :                                                                     InvalidOid, idx);
                                212                 :                : 
                                213                 :             52 :     LWLockRelease(ReplicationSlotControlLock);
                                214                 :                : 
                                215                 :             52 :     return slotentry;
                                216                 :                : }
                                217                 :                : 
                                218                 :                : void
 1254 andres@anarazel.de        219                 :            107 : pgstat_replslot_to_serialized_name_cb(const PgStat_HashKey *key, const PgStatShared_Common *header, NameData *name)
                                220                 :                : {
                                221                 :                :     /*
                                222                 :                :      * This is only called late during shutdown. The set of existing slots
                                223                 :                :      * isn't allowed to change at this point, we can assume that a slot exists
                                224                 :                :      * at the offset.
                                225                 :                :      */
  543 michael@paquier.xyz       226         [ -  + ]:            107 :     if (!ReplicationSlotName(key->objid, name))
  351 peter@eisentraut.org      227         [ #  # ]:UBC           0 :         elog(ERROR, "could not find name for replication slot index %" PRIu64,
                                228                 :                :              key->objid);
 1439 andres@anarazel.de        229                 :CBC         107 : }
                                230                 :                : 
                                231                 :                : bool
                                232                 :             76 : pgstat_replslot_from_serialized_name_cb(const NameData *name, PgStat_HashKey *key)
                                233                 :                : {
  734 michael@paquier.xyz       234                 :             76 :     int         idx = get_replslot_index(NameStr(*name), true);
                                235                 :                : 
                                236                 :                :     /* slot might have been deleted */
 1439 andres@anarazel.de        237         [ +  + ]:             76 :     if (idx == -1)
                                238                 :              1 :         return false;
                                239                 :                : 
                                240                 :             75 :     key->kind = PGSTAT_KIND_REPLSLOT;
                                241                 :             75 :     key->dboid = InvalidOid;
  543 michael@paquier.xyz       242                 :             75 :     key->objid = idx;
                                243                 :                : 
 1439 andres@anarazel.de        244                 :             75 :     return true;
                                245                 :                : }
                                246                 :                : 
                                247                 :                : void
                                248                 :              8 : pgstat_replslot_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
                                249                 :                : {
                                250                 :              8 :     ((PgStatShared_ReplSlot *) header)->stats.stat_reset_timestamp = ts;
                                251                 :              8 : }
                                252                 :                : 
                                253                 :                : static int
  734 michael@paquier.xyz       254                 :            128 : get_replslot_index(const char *name, bool need_lock)
                                255                 :                : {
                                256                 :                :     ReplicationSlot *slot;
                                257                 :                : 
 1234 peter@eisentraut.org      258         [ -  + ]:            128 :     Assert(name != NULL);
                                259                 :                : 
  734 michael@paquier.xyz       260                 :            128 :     slot = SearchNamedReplicationSlot(name, need_lock);
                                261                 :                : 
 1439 andres@anarazel.de        262         [ +  + ]:            128 :     if (!slot)
                                263                 :              3 :         return -1;
                                264                 :                : 
                                265                 :            125 :     return ReplicationSlotIndex(slot);
                                266                 :                : }
        

Generated by: LCOV version 2.4-beta