PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
lwlock.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * lwlock.h
4 * Lightweight lock manager
5 *
6 *
7 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
9 *
10 * src/include/storage/lwlock.h
11 *
12 *-------------------------------------------------------------------------
13 */
14#ifndef LWLOCK_H
15#define LWLOCK_H
16
17#ifdef FRONTEND
18#error "lwlock.h may not be included from frontend code"
19#endif
20
21#include "port/atomics.h"
22#include "storage/lwlocknames.h"
24
25struct PGPROC;
26
27/* what state of the wait process is a backend in */
28typedef enum LWLockWaitState
29{
30 LW_WS_NOT_WAITING, /* not currently waiting / woken up */
31 LW_WS_WAITING, /* currently waiting */
32 LW_WS_PENDING_WAKEUP, /* removed from waitlist, but not yet
33 * signalled */
35
36/*
37 * Code outside of lwlock.c should not manipulate the contents of this
38 * structure directly, but we have to declare it here to allow LWLocks to be
39 * incorporated into other data structures.
40 */
41typedef struct LWLock
42{
43 uint16 tranche; /* tranche ID */
44 pg_atomic_uint32 state; /* state of exclusive/nonexclusive lockers */
45 proclist_head waiters; /* list of waiting PGPROCs */
46#ifdef LOCK_DEBUG
47 pg_atomic_uint32 nwaiters; /* number of waiters */
48 struct PGPROC *owner; /* last exclusive owner of the lock */
49#endif
51
52/*
53 * In most cases, it's desirable to force each tranche of LWLocks to be aligned
54 * on a cache line boundary and make the array stride a power of 2. This saves
55 * a few cycles in indexing, but more importantly ensures that individual
56 * LWLocks don't cross cache line boundaries. This reduces cache contention
57 * problems, especially on AMD Opterons. In some cases, it's useful to add
58 * even more padding so that each LWLock takes up an entire cache line; this is
59 * useful, for example, in the main LWLock array, where the overall number of
60 * locks is small but some are heavily contended.
61 */
62#define LWLOCK_PADDED_SIZE PG_CACHE_LINE_SIZE
63
65 "Miscalculated LWLock padding");
66
67/* LWLock, padded to a full cache line size */
68typedef union LWLockPadded
69{
73
75
76/* struct for storing named tranche information */
77typedef struct NamedLWLockTranche
78{
82
85
86/*
87 * It's a bit odd to declare NUM_BUFFER_PARTITIONS and NUM_LOCK_PARTITIONS
88 * here, but we need them to figure out offsets within MainLWLockArray, and
89 * having this file include lock.h or bufmgr.h would be backwards.
90 */
91
92/* Number of partitions of the shared buffer mapping hashtable */
93#define NUM_BUFFER_PARTITIONS 128
94
95/* Number of partitions the shared lock tables are divided into */
96#define LOG2_NUM_LOCK_PARTITIONS 4
97#define NUM_LOCK_PARTITIONS (1 << LOG2_NUM_LOCK_PARTITIONS)
98
99/* Number of partitions the shared predicate lock tables are divided into */
100#define LOG2_NUM_PREDICATELOCK_PARTITIONS 4
101#define NUM_PREDICATELOCK_PARTITIONS (1 << LOG2_NUM_PREDICATELOCK_PARTITIONS)
102
103/* Offsets for various chunks of preallocated lwlocks. */
104#define BUFFER_MAPPING_LWLOCK_OFFSET NUM_INDIVIDUAL_LWLOCKS
105#define LOCK_MANAGER_LWLOCK_OFFSET \
106 (BUFFER_MAPPING_LWLOCK_OFFSET + NUM_BUFFER_PARTITIONS)
107#define PREDICATELOCK_MANAGER_LWLOCK_OFFSET \
108 (LOCK_MANAGER_LWLOCK_OFFSET + NUM_LOCK_PARTITIONS)
109#define NUM_FIXED_LWLOCKS \
110 (PREDICATELOCK_MANAGER_LWLOCK_OFFSET + NUM_PREDICATELOCK_PARTITIONS)
111
112typedef enum LWLockMode
113{
116 LW_WAIT_UNTIL_FREE, /* A special mode used in PGPROC->lwWaitMode,
117 * when waiting for lock to become free. Not
118 * to be used as LWLockAcquire argument */
120
121
122#ifdef LOCK_DEBUG
123extern PGDLLIMPORT bool Trace_lwlocks;
124#endif
125
126extern bool LWLockAcquire(LWLock *lock, LWLockMode mode);
128extern bool LWLockAcquireOrWait(LWLock *lock, LWLockMode mode);
129extern void LWLockRelease(LWLock *lock);
130extern void LWLockReleaseClearVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val);
131extern void LWLockReleaseAll(void);
132extern void LWLockDisown(LWLock *l);
134extern bool LWLockHeldByMe(LWLock *lock);
135extern bool LWLockAnyHeldByMe(LWLock *lock, int nlocks, size_t stride);
136extern bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode);
137
138extern bool LWLockWaitForVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 oldval, uint64 *newval);
139extern void LWLockUpdateVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val);
140
141extern Size LWLockShmemSize(void);
142extern void CreateLWLocks(void);
143extern void InitLWLockAccess(void);
144
145extern const char *GetLWLockIdentifier(uint32 classId, uint16 eventId);
146
147/*
148 * Extensions (or core code) can obtain an LWLocks by calling
149 * RequestNamedLWLockTranche() during postmaster startup. Subsequently,
150 * call GetNamedLWLockTranche() to obtain a pointer to an array containing
151 * the number of LWLocks requested.
152 */
153extern void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks);
154extern LWLockPadded *GetNamedLWLockTranche(const char *tranche_name);
155
156/*
157 * There is another, more flexible method of obtaining lwlocks. First, call
158 * LWLockNewTrancheId just once to obtain a tranche ID; this allocates from
159 * a shared counter. Next, each individual process using the tranche should
160 * call LWLockRegisterTranche() to associate that tranche ID with a name.
161 * Finally, LWLockInitialize should be called just once per lwlock, passing
162 * the tranche ID as an argument.
163 *
164 * It may seem strange that each process using the tranche must register it
165 * separately, but dynamic shared memory segments aren't guaranteed to be
166 * mapped at the same address in all coordinating backends, so storing the
167 * registration in the main shared memory segment wouldn't work for that case.
168 */
169extern int LWLockNewTrancheId(void);
170extern void LWLockRegisterTranche(int tranche_id, const char *tranche_name);
171extern void LWLockInitialize(LWLock *lock, int tranche_id);
172
173/*
174 * Every tranche ID less than NUM_INDIVIDUAL_LWLOCKS is reserved; also,
175 * we reserve additional tranche IDs for builtin tranches not included in
176 * the set of individual LWLocks. A call to LWLockNewTrancheId will never
177 * return a value less than LWTRANCHE_FIRST_USER_DEFINED.
178 */
180{
181 LWTRANCHE_XACT_BUFFER = NUM_INDIVIDUAL_LWLOCKS,
223
224/*
225 * Prior to PostgreSQL 9.4, we used an enum type called LWLockId to refer
226 * to LWLocks. New code should instead use LWLock *. However, for the
227 * convenience of third-party code, we include the following typedef.
228 */
230
231#endif /* LWLOCK_H */
#define PGDLLIMPORT
Definition: c.h:1277
uint64_t uint64
Definition: c.h:489
uint16_t uint16
Definition: c.h:487
uint32_t uint32
Definition: c.h:488
size_t Size
Definition: c.h:562
#define newval
long val
Definition: informix.c:689
void LWLockUpdateVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val)
Definition: lwlock.c:1731
union LWLockPadded LWLockPadded
bool LWLockHeldByMe(LWLock *lock)
Definition: lwlock.c:1967
void LWLockReleaseClearVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val)
Definition: lwlock.c:1928
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1179
void CreateLWLocks(void)
Definition: lwlock.c:461
LWLockWaitState
Definition: lwlock.h:29
@ LW_WS_NOT_WAITING
Definition: lwlock.h:30
@ LW_WS_WAITING
Definition: lwlock.h:31
@ LW_WS_PENDING_WAKEUP
Definition: lwlock.h:32
PGDLLIMPORT NamedLWLockTranche * NamedLWLockTrancheArray
Definition: lwlock.c:238
struct NamedLWLockTranche NamedLWLockTranche
#define LWLOCK_PADDED_SIZE
Definition: lwlock.h:62
void LWLockRegisterTranche(int tranche_id, const char *tranche_name)
Definition: lwlock.c:639
LWLockPadded * GetNamedLWLockTranche(const char *tranche_name)
Definition: lwlock.c:584
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:2011
PGDLLIMPORT int NamedLWLockTrancheRequests
Definition: lwlock.c:235
bool LWLockWaitForVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 oldval, uint64 *newval)
Definition: lwlock.c:1595
void LWLockReleaseDisowned(LWLock *l, LWLockMode mode)
Definition: lwlock.c:1919
int LWLockNewTrancheId(void)
Definition: lwlock.c:614
void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks)
Definition: lwlock.c:681
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1899
BuiltinTrancheIds
Definition: lwlock.h:180
@ LWTRANCHE_FIRST_USER_DEFINED
Definition: lwlock.h:221
@ LWTRANCHE_SHARED_TIDBITMAP
Definition: lwlock.h:203
@ LWTRANCHE_SERIAL_SLRU
Definition: lwlock.h:217
@ LWTRANCHE_PER_SESSION_DSA
Definition: lwlock.h:199
@ LWTRANCHE_PARALLEL_QUERY_DSA
Definition: lwlock.h:198
@ LWTRANCHE_COMMITTS_BUFFER
Definition: lwlock.h:182
@ LWTRANCHE_PARALLEL_VACUUM_DSA
Definition: lwlock.h:220
@ LWTRANCHE_PGSTATS_HASH
Definition: lwlock.h:207
@ LWTRANCHE_PARALLEL_BTREE_SCAN
Definition: lwlock.h:197
@ LWTRANCHE_SUBTRANS_BUFFER
Definition: lwlock.h:183
@ LWTRANCHE_PER_SESSION_RECORD_TYPMOD
Definition: lwlock.h:201
@ LWTRANCHE_LAUNCHER_HASH
Definition: lwlock.h:210
@ LWTRANCHE_DSM_REGISTRY_DSA
Definition: lwlock.h:211
@ LWTRANCHE_XACT_BUFFER
Definition: lwlock.h:181
@ LWTRANCHE_DSM_REGISTRY_HASH
Definition: lwlock.h:212
@ LWTRANCHE_NOTIFY_SLRU
Definition: lwlock.h:216
@ LWTRANCHE_REPLICATION_ORIGIN_STATE
Definition: lwlock.h:190
@ LWTRANCHE_MULTIXACTOFFSET_SLRU
Definition: lwlock.h:215
@ LWTRANCHE_PARALLEL_APPEND
Definition: lwlock.h:204
@ LWTRANCHE_REPLICATION_SLOT_IO
Definition: lwlock.h:191
@ LWTRANCHE_SUBTRANS_SLRU
Definition: lwlock.h:218
@ LWTRANCHE_MULTIXACTMEMBER_SLRU
Definition: lwlock.h:214
@ LWTRANCHE_BUFFER_CONTENT
Definition: lwlock.h:189
@ LWTRANCHE_MULTIXACTMEMBER_BUFFER
Definition: lwlock.h:185
@ LWTRANCHE_NOTIFY_BUFFER
Definition: lwlock.h:186
@ LWTRANCHE_PER_SESSION_RECORD_TYPE
Definition: lwlock.h:200
@ LWTRANCHE_PREDICATE_LOCK_MANAGER
Definition: lwlock.h:195
@ LWTRANCHE_BUFFER_MAPPING
Definition: lwlock.h:193
@ LWTRANCHE_SERIAL_BUFFER
Definition: lwlock.h:187
@ LWTRANCHE_LAUNCHER_DSA
Definition: lwlock.h:209
@ LWTRANCHE_PGSTATS_DSA
Definition: lwlock.h:206
@ LWTRANCHE_PARALLEL_HASH_JOIN
Definition: lwlock.h:196
@ LWTRANCHE_COMMITTS_SLRU
Definition: lwlock.h:213
@ LWTRANCHE_PGSTATS_DATA
Definition: lwlock.h:208
@ LWTRANCHE_PER_XACT_PREDICATE_LIST
Definition: lwlock.h:205
@ LWTRANCHE_XACT_SLRU
Definition: lwlock.h:219
@ LWTRANCHE_MULTIXACTOFFSET_BUFFER
Definition: lwlock.h:184
@ LWTRANCHE_WAL_INSERT
Definition: lwlock.h:188
@ LWTRANCHE_LOCK_MANAGER
Definition: lwlock.h:194
@ LWTRANCHE_SHARED_TUPLESTORE
Definition: lwlock.h:202
@ LWTRANCHE_LOCK_FASTPATH
Definition: lwlock.h:192
void LWLockReleaseAll(void)
Definition: lwlock.c:1950
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:718
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1350
bool LWLockAcquireOrWait(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1407
void LWLockDisown(LWLock *l)
Definition: lwlock.c:1888
LWLockMode
Definition: lwlock.h:113
@ LW_SHARED
Definition: lwlock.h:115
@ LW_WAIT_UNTIL_FREE
Definition: lwlock.h:116
@ LW_EXCLUSIVE
Definition: lwlock.h:114
PGDLLIMPORT LWLockPadded * MainLWLockArray
Definition: lwlock.c:199
StaticAssertDecl(sizeof(LWLock)<=LWLOCK_PADDED_SIZE, "Miscalculated LWLock padding")
LWLock * LWLockId
Definition: lwlock.h:229
const char * GetLWLockIdentifier(uint32 classId, uint16 eventId)
Definition: lwlock.c:778
struct LWLock LWLock
Size LWLockShmemSize(void)
Definition: lwlock.c:431
bool LWLockAnyHeldByMe(LWLock *lock, int nlocks, size_t stride)
Definition: lwlock.c:1985
void InitLWLockAccess(void)
Definition: lwlock.c:568
static PgChecksumMode mode
Definition: pg_checksums.c:55
Definition: lwlock.h:42
pg_atomic_uint32 state
Definition: lwlock.h:44
uint16 tranche
Definition: lwlock.h:43
proclist_head waiters
Definition: lwlock.h:45
char * trancheName
Definition: lwlock.h:80
Definition: proc.h:163
LWLock lock
Definition: lwlock.h:70
char pad[LWLOCK_PADDED_SIZE]
Definition: lwlock.h:71