LCOV - code coverage report
Current view: top level - src/backend/utils/activity - pgstat_archiver.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 73.1 % 52 38
Test Date: 2026-01-26 10:56:24 Functions: 80.0 % 5 4
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 33.3 % 6 2

             Branch data     Line data    Source code
       1                 :             : /* -------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * pgstat_archiver.c
       4                 :             :  *        Implementation of archiver statistics.
       5                 :             :  *
       6                 :             :  * This file contains the implementation of archiver 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                 :             :  * Copyright (c) 2001-2026, PostgreSQL Global Development Group
      12                 :             :  *
      13                 :             :  * IDENTIFICATION
      14                 :             :  *        src/backend/utils/activity/pgstat_archiver.c
      15                 :             :  * -------------------------------------------------------------------------
      16                 :             :  */
      17                 :             : 
      18                 :             : #include "postgres.h"
      19                 :             : 
      20                 :             : #include "utils/pgstat_internal.h"
      21                 :             : #include "utils/timestamp.h"
      22                 :             : 
      23                 :             : 
      24                 :             : /*
      25                 :             :  * Report archiver statistics
      26                 :             :  */
      27                 :             : void
      28                 :           0 : pgstat_report_archiver(const char *xlog, bool failed)
      29                 :             : {
      30                 :           0 :         PgStatShared_Archiver *stats_shmem = &pgStatLocal.shmem->archiver;
      31                 :           0 :         TimestampTz now = GetCurrentTimestamp();
      32                 :             : 
      33                 :           0 :         pgstat_begin_changecount_write(&stats_shmem->changecount);
      34                 :             : 
      35         [ #  # ]:           0 :         if (failed)
      36                 :             :         {
      37                 :           0 :                 ++stats_shmem->stats.failed_count;
      38                 :           0 :                 memcpy(&stats_shmem->stats.last_failed_wal, xlog,
      39                 :             :                            sizeof(stats_shmem->stats.last_failed_wal));
      40                 :           0 :                 stats_shmem->stats.last_failed_timestamp = now;
      41                 :           0 :         }
      42                 :             :         else
      43                 :             :         {
      44                 :           0 :                 ++stats_shmem->stats.archived_count;
      45                 :           0 :                 memcpy(&stats_shmem->stats.last_archived_wal, xlog,
      46                 :             :                            sizeof(stats_shmem->stats.last_archived_wal));
      47                 :           0 :                 stats_shmem->stats.last_archived_timestamp = now;
      48                 :             :         }
      49                 :             : 
      50                 :           0 :         pgstat_end_changecount_write(&stats_shmem->changecount);
      51                 :           0 : }
      52                 :             : 
      53                 :             : /*
      54                 :             :  * Support function for the SQL-callable pgstat* functions. Returns
      55                 :             :  * a pointer to the archiver statistics struct.
      56                 :             :  */
      57                 :             : PgStat_ArchiverStats *
      58                 :           2 : pgstat_fetch_stat_archiver(void)
      59                 :             : {
      60                 :           2 :         pgstat_snapshot_fixed(PGSTAT_KIND_ARCHIVER);
      61                 :             : 
      62                 :           2 :         return &pgStatLocal.snapshot.archiver;
      63                 :             : }
      64                 :             : 
      65                 :             : void
      66                 :           6 : pgstat_archiver_init_shmem_cb(void *stats)
      67                 :             : {
      68                 :           6 :         PgStatShared_Archiver *stats_shmem = (PgStatShared_Archiver *) stats;
      69                 :             : 
      70                 :           6 :         LWLockInitialize(&stats_shmem->lock, LWTRANCHE_PGSTATS_DATA);
      71                 :           6 : }
      72                 :             : 
      73                 :             : void
      74                 :           2 : pgstat_archiver_reset_all_cb(TimestampTz ts)
      75                 :             : {
      76                 :           2 :         PgStatShared_Archiver *stats_shmem = &pgStatLocal.shmem->archiver;
      77                 :             : 
      78                 :             :         /* see explanation above PgStatShared_Archiver for the reset protocol */
      79                 :           2 :         LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
      80                 :           4 :         pgstat_copy_changecounted_stats(&stats_shmem->reset_offset,
      81                 :           2 :                                                                         &stats_shmem->stats,
      82                 :             :                                                                         sizeof(stats_shmem->stats),
      83                 :           2 :                                                                         &stats_shmem->changecount);
      84                 :           2 :         stats_shmem->stats.stat_reset_timestamp = ts;
      85                 :           2 :         LWLockRelease(&stats_shmem->lock);
      86                 :           2 : }
      87                 :             : 
      88                 :             : void
      89                 :           9 : pgstat_archiver_snapshot_cb(void)
      90                 :             : {
      91                 :           9 :         PgStatShared_Archiver *stats_shmem = &pgStatLocal.shmem->archiver;
      92                 :           9 :         PgStat_ArchiverStats *stat_snap = &pgStatLocal.snapshot.archiver;
      93                 :           9 :         PgStat_ArchiverStats *reset_offset = &stats_shmem->reset_offset;
      94                 :           9 :         PgStat_ArchiverStats reset;
      95                 :             : 
      96                 :          18 :         pgstat_copy_changecounted_stats(stat_snap,
      97                 :           9 :                                                                         &stats_shmem->stats,
      98                 :             :                                                                         sizeof(stats_shmem->stats),
      99                 :           9 :                                                                         &stats_shmem->changecount);
     100                 :             : 
     101                 :           9 :         LWLockAcquire(&stats_shmem->lock, LW_SHARED);
     102                 :           9 :         memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
     103                 :           9 :         LWLockRelease(&stats_shmem->lock);
     104                 :             : 
     105                 :             :         /* compensate by reset offsets */
     106         [ -  + ]:           9 :         if (stat_snap->archived_count == reset.archived_count)
     107                 :             :         {
     108                 :           9 :                 stat_snap->last_archived_wal[0] = 0;
     109                 :           9 :                 stat_snap->last_archived_timestamp = 0;
     110                 :           9 :         }
     111                 :           9 :         stat_snap->archived_count -= reset.archived_count;
     112                 :             : 
     113         [ -  + ]:           9 :         if (stat_snap->failed_count == reset.failed_count)
     114                 :             :         {
     115                 :           9 :                 stat_snap->last_failed_wal[0] = 0;
     116                 :           9 :                 stat_snap->last_failed_timestamp = 0;
     117                 :           9 :         }
     118                 :           9 :         stat_snap->failed_count -= reset.failed_count;
     119                 :           9 : }
        

Generated by: LCOV version 2.3.2-1