PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pgstat_lock.c
Go to the documentation of this file.
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
21
23static bool have_lockstats = false;
24
32
33/*
34 * Simpler wrapper of pgstat_lock_flush_cb()
35 */
36void
38{
39 (void) pgstat_lock_flush_cb(nowait);
40}
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 */
50bool
52{
55
56 if (!have_lockstats)
57 return false;
58
61
62 if (!nowait)
65 return true;
66
67 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 LOCKSTAT_ACC(waits);
72 LOCKSTAT_ACC(wait_time);
73 LOCKSTAT_ACC(fastpath_exceeded);
74#undef LOCKSTAT_ACC
75 }
76
78
80 have_lockstats = false;
81
82 return false;
83}
84
85void
92
93void
107
108void
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 */
127void
129{
130 Assert(locktag_type <= LOCKTAG_LAST_TYPE);
132 have_lockstats = true;
133 pgstat_report_fixed = true;
134}
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 */
142void
144{
145 Assert(locktag_type <= LOCKTAG_LAST_TYPE);
146 PendingLockStats.stats[locktag_type].waits++;
148 have_lockstats = true;
149 pgstat_report_fixed = true;
150}
uint8_t uint8
Definition c.h:622
#define Assert(condition)
Definition c.h:943
int64 TimestampTz
Definition timestamp.h:39
int i
Definition isn.c:77
#define LOCKTAG_LAST_TYPE
Definition locktag.h:52
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1150
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1767
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition lwlock.c:670
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1321
@ LW_SHARED
Definition lwlock.h:105
@ LW_EXCLUSIVE
Definition lwlock.h:104
void pgstat_snapshot_fixed(PgStat_Kind kind)
Definition pgstat.c:1104
bool pgstat_report_fixed
Definition pgstat.c:219
PgStat_LocalState pgStatLocal
Definition pgstat.c:213
int64 PgStat_Counter
Definition pgstat.h:71
#define PGSTAT_KIND_LOCK
Definition pgstat_kind.h:39
void pgstat_count_lock_waits(uint8 locktag_type, long msecs)
void pgstat_lock_flush(bool nowait)
Definition pgstat_lock.c:37
static bool have_lockstats
Definition pgstat_lock.c:23
static PgStat_PendingLock PendingLockStats
Definition pgstat_lock.c:22
PgStat_Lock * pgstat_fetch_stat_lock(void)
Definition pgstat_lock.c:26
bool pgstat_lock_flush_cb(bool nowait)
Definition pgstat_lock.c:51
void pgstat_lock_init_shmem_cb(void *stats)
Definition pgstat_lock.c:86
#define LOCKSTAT_ACC(fld)
void pgstat_lock_reset_all_cb(TimestampTz ts)
Definition pgstat_lock.c:94
void pgstat_count_lock_fastpath_exceeded(uint8 locktag_type)
void pgstat_lock_snapshot_cb(void)
static int fb(int x)
PgStat_Snapshot snapshot
PgStat_ShmemControl * shmem
PgStat_Counter waits
Definition pgstat.h:351
PgStat_Counter wait_time
Definition pgstat.h:352
PgStat_Counter fastpath_exceeded
Definition pgstat.h:353
PgStat_LockEntry stats[LOCKTAG_LAST_TYPE+1]
Definition pgstat.h:364
TimestampTz stat_reset_timestamp
Definition pgstat.h:363
PgStat_LockEntry stats[LOCKTAG_LAST_TYPE+1]
Definition pgstat.h:358
PgStatShared_Lock lock