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 bool lock_not_acquired = false;
56
57 if (!have_lockstats)
58 return false;
59
60 for (int i = 0; i <= LOCKTAG_LAST_TYPE; i++)
61 {
65
66 if (!nowait)
69 {
70 lock_not_acquired = true;
71 continue;
72 }
73
74#define LOCKSTAT_ACC(fld) \
75 (lck_shstats->fld += PendingLockStats.stats[i].fld)
76 LOCKSTAT_ACC(waits);
77 LOCKSTAT_ACC(wait_time);
78 LOCKSTAT_ACC(fastpath_exceeded);
79#undef LOCKSTAT_ACC
80
82 }
83
85
86 have_lockstats = false;
87
88 return lock_not_acquired;
89}
90
91
92void
100
101void
103{
104 for (int i = 0; i <= LOCKTAG_LAST_TYPE; i++)
105 {
108
110
111 /*
112 * Use the lock in the first lock type PgStat_LockEntry to protect the
113 * reset timestamp as well.
114 */
115 if (i == 0)
117
118 memset(lck_shstats, 0, sizeof(*lck_shstats));
120 }
121}
122
123void
125{
126 for (int i = 0; i <= LOCKTAG_LAST_TYPE; i++)
127 {
131
133
134 /*
135 * Use the lock in the first lock type PgStat_LockEntry to protect the
136 * reset timestamp as well.
137 */
138 if (i == 0)
141
142 /* using struct assignment due to better type safety */
145 }
146}
147
148/*
149 * Increment counter for lock not acquired with the fast-path, per lock
150 * type, due to the fast-path slot limit reached.
151 *
152 * Note: This function should not be called in performance-sensitive paths,
153 * like lock acquisitions.
154 */
155void
157{
158 Assert(locktag_type <= LOCKTAG_LAST_TYPE);
160 have_lockstats = true;
161 pgstat_report_fixed = true;
162}
163
164/*
165 * Increment the number of waits and wait time, per lock type.
166 *
167 * Note: This function should not be called in performance-sensitive paths,
168 * like lock acquisitions.
169 */
170void
172{
173 Assert(locktag_type <= LOCKTAG_LAST_TYPE);
174 PendingLockStats.stats[locktag_type].waits++;
176 have_lockstats = true;
177 pgstat_report_fixed = true;
178}
uint8_t uint8
Definition c.h:616
#define Assert(condition)
Definition c.h:945
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:1177
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1794
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition lwlock.c:699
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1348
@ LW_SHARED
Definition lwlock.h:113
@ LW_EXCLUSIVE
Definition lwlock.h:112
void pgstat_snapshot_fixed(PgStat_Kind kind)
Definition pgstat.c:1088
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:93
#define LOCKSTAT_ACC(fld)
void pgstat_lock_reset_all_cb(TimestampTz ts)
void pgstat_count_lock_fastpath_exceeded(uint8 locktag_type)
void pgstat_lock_snapshot_cb(void)
static int fb(int x)
LWLock locks[LOCKTAG_LAST_TYPE+1]
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