LCOV - differential code coverage report
Current view: top level - src/backend/utils/activity - pgstat_lock.c (source / functions) Coverage Total Hit UNC GNC
Current: 380a8b2ea024c33a35e7abc8628e7c4f52f9f9f9 vs db5ed03217b9c238703df8b4b286115d6e940488 Lines: 92.5 % 53 49 4 49
Current Date: 2026-05-29 21:51:00 -0400 Functions: 87.5 % 8 7 1 7
Baseline: lcov-20260530-034037-baseline Branches: 75.0 % 12 9 3 9
Baseline Date: 2026-05-29 14:39:03 -0700 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 92.5 % 53 49 4 49
Function coverage date bins:
(30,360] days: 87.5 % 8 7 1 7
Branch coverage date bins:
(30,360] days: 75.0 % 12 9 3 9

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /* -------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * pgstat_lock.c
                                  4                 :                :  *    Implementation of lock statistics.
                                  5                 :                :  *
                                  6                 :                :  * This file contains the implementation of lock statistics.  It is kept
                                  7                 :                :  * separate from pgstat.c to enforce the line between the statistics
                                  8                 :                :  * access / storage implementation and the details about individual types
                                  9                 :                :  * of statistics.
                                 10                 :                :  *
                                 11                 :                :  * Copyright (c) 2021-2026, PostgreSQL Global Development Group
                                 12                 :                :  *
                                 13                 :                :  * IDENTIFICATION
                                 14                 :                :  *    src/backend/utils/activity/pgstat_lock.c
                                 15                 :                :  * -------------------------------------------------------------------------
                                 16                 :                :  */
                                 17                 :                : 
                                 18                 :                : #include "postgres.h"
                                 19                 :                : 
                                 20                 :                : #include "utils/pgstat_internal.h"
                                 21                 :                : 
                                 22                 :                : static PgStat_PendingLock PendingLockStats;
                                 23                 :                : static bool have_lockstats = false;
                                 24                 :                : 
                                 25                 :                : PgStat_Lock *
   67 michael@paquier.xyz        26                 :GNC          12 : pgstat_fetch_stat_lock(void)
                                 27                 :                : {
                                 28                 :             12 :     pgstat_snapshot_fixed(PGSTAT_KIND_LOCK);
                                 29                 :                : 
                                 30                 :             12 :     return &pgStatLocal.snapshot.lock;
                                 31                 :                : }
                                 32                 :                : 
                                 33                 :                : /*
                                 34                 :                :  * Simpler wrapper of pgstat_lock_flush_cb()
                                 35                 :                :  */
                                 36                 :                : void
   67 michael@paquier.xyz        37                 :UNC           0 : pgstat_lock_flush(bool nowait)
                                 38                 :                : {
                                 39                 :              0 :     (void) pgstat_lock_flush_cb(nowait);
                                 40                 :              0 : }
                                 41                 :                : 
                                 42                 :                : /*
                                 43                 :                :  * Flush out locally pending lock statistics
                                 44                 :                :  *
                                 45                 :                :  * If no stats have been recorded, this function returns false.
                                 46                 :                :  *
                                 47                 :                :  * If nowait is true, this function returns true if the lock could not be
                                 48                 :                :  * acquired. Otherwise, return false.
                                 49                 :                :  */
                                 50                 :                : bool
   67 michael@paquier.xyz        51                 :GNC       39381 : pgstat_lock_flush_cb(bool nowait)
                                 52                 :                : {
                                 53                 :                :     LWLock     *lckstat_lock;
                                 54                 :                :     PgStatShared_Lock *shstats;
                                 55                 :                : 
                                 56         [ +  + ]:          39381 :     if (!have_lockstats)
                                 57                 :          39070 :         return false;
                                 58                 :                : 
   54                            59                 :            311 :     shstats = &pgStatLocal.shmem->lock;
                                 60                 :            311 :     lckstat_lock = &shstats->lock;
                                 61                 :                : 
                                 62         [ +  + ]:            311 :     if (!nowait)
                                 63                 :            288 :         LWLockAcquire(lckstat_lock, LW_EXCLUSIVE);
                                 64         [ -  + ]:             23 :     else if (!LWLockConditionalAcquire(lckstat_lock, LW_EXCLUSIVE))
   54 michael@paquier.xyz        65                 :UNC           0 :         return true;
                                 66                 :                : 
   67 michael@paquier.xyz        67         [ +  + ]:GNC        4043 :     for (int i = 0; i <= LOCKTAG_LAST_TYPE; i++)
                                 68                 :                :     {
                                 69                 :                : #define LOCKSTAT_ACC(fld) \
                                 70                 :                :     (shstats->stats.stats[i].fld += PendingLockStats.stats[i].fld)
                                 71                 :           3732 :         LOCKSTAT_ACC(waits);
                                 72                 :           3732 :         LOCKSTAT_ACC(wait_time);
                                 73                 :           3732 :         LOCKSTAT_ACC(fastpath_exceeded);
                                 74                 :                : #undef LOCKSTAT_ACC
                                 75                 :                :     }
                                 76                 :                : 
   54                            77                 :            311 :     LWLockRelease(lckstat_lock);
                                 78                 :                : 
                                 79                 :            311 :     memset(&PendingLockStats, 0, sizeof(PendingLockStats));
   67                            80                 :            311 :     have_lockstats = false;
                                 81                 :                : 
   54                            82                 :            311 :     return false;
                                 83                 :                : }
                                 84                 :                : 
                                 85                 :                : void
   67                            86                 :           1248 : pgstat_lock_init_shmem_cb(void *stats)
                                 87                 :                : {
                                 88                 :           1248 :     PgStatShared_Lock *stat_shmem = (PgStatShared_Lock *) stats;
                                 89                 :                : 
   54                            90                 :           1248 :     LWLockInitialize(&stat_shmem->lock, LWTRANCHE_PGSTATS_DATA);
   67                            91                 :           1248 : }
                                 92                 :                : 
                                 93                 :                : void
                                 94                 :            259 : pgstat_lock_reset_all_cb(TimestampTz ts)
                                 95                 :                : {
   54                            96                 :            259 :     LWLock     *lckstat_lock = &pgStatLocal.shmem->lock.lock;
                                 97                 :                : 
                                 98                 :            259 :     LWLockAcquire(lckstat_lock, LW_EXCLUSIVE);
                                 99                 :                : 
                                100                 :            259 :     pgStatLocal.shmem->lock.stats.stat_reset_timestamp = ts;
                                101                 :                : 
                                102                 :            259 :     memset(pgStatLocal.shmem->lock.stats.stats, 0,
                                103                 :                :            sizeof(pgStatLocal.shmem->lock.stats.stats));
                                104                 :                : 
                                105                 :            259 :     LWLockRelease(lckstat_lock);
   67                           106                 :            259 : }
                                107                 :                : 
                                108                 :                : void
                                109                 :            811 : pgstat_lock_snapshot_cb(void)
                                110                 :                : {
   54                           111                 :            811 :     LWLock     *lckstat_lock = &pgStatLocal.shmem->lock.lock;
                                112                 :                : 
                                113                 :            811 :     LWLockAcquire(lckstat_lock, LW_SHARED);
                                114                 :                : 
                                115                 :            811 :     pgStatLocal.snapshot.lock = pgStatLocal.shmem->lock.stats;
                                116                 :                : 
                                117                 :            811 :     LWLockRelease(lckstat_lock);
   67                           118                 :            811 : }
                                119                 :                : 
                                120                 :                : /*
                                121                 :                :  * Increment counter for lock not acquired with the fast-path, per lock
                                122                 :                :  * type, due to the fast-path slot limit reached.
                                123                 :                :  *
                                124                 :                :  * Note: This function should not be called in performance-sensitive paths,
                                125                 :                :  * like lock acquisitions.
                                126                 :                :  */
                                127                 :                : void
                                128                 :         105495 : pgstat_count_lock_fastpath_exceeded(uint8 locktag_type)
                                129                 :                : {
                                130         [ -  + ]:         105495 :     Assert(locktag_type <= LOCKTAG_LAST_TYPE);
                                131                 :         105495 :     PendingLockStats.stats[locktag_type].fastpath_exceeded++;
                                132                 :         105495 :     have_lockstats = true;
                                133                 :         105495 :     pgstat_report_fixed = true;
                                134                 :         105495 : }
                                135                 :                : 
                                136                 :                : /*
                                137                 :                :  * Increment the number of waits and wait time, per lock type.
                                138                 :                :  *
                                139                 :                :  * Note: This function should not be called in performance-sensitive paths,
                                140                 :                :  * like lock acquisitions.
                                141                 :                :  */
                                142                 :                : void
                                143                 :             40 : pgstat_count_lock_waits(uint8 locktag_type, long msecs)
                                144                 :                : {
                                145         [ -  + ]:             40 :     Assert(locktag_type <= LOCKTAG_LAST_TYPE);
                                146                 :             40 :     PendingLockStats.stats[locktag_type].waits++;
                                147                 :             40 :     PendingLockStats.stats[locktag_type].wait_time += (PgStat_Counter) msecs;
                                148                 :             40 :     have_lockstats = true;
                                149                 :             40 :     pgstat_report_fixed = true;
                                150                 :             40 : }
        

Generated by: LCOV version 2.5.0-beta