Skip to content

Commit c878de1

Browse files
committed
Make FP_LOCK_SLOTS_PER_BACKEND look like a function
The FP_LOCK_SLOTS_PER_BACKEND macro looks like a constant, but it depends on the max_locks_per_transaction GUC, and thus can change. This is non-obvious and confusing, so make it look more like a function by renaming it to FastPathLockSlotsPerBackend(). While at it, use the macro when initializing fast-path shared memory, instead of using the formula. Reported-by: Andres Freund Discussion: https://postgr.es/m/ffiwtzc6vedo6wb4gbwelon5nefqg675t5c7an2ta7pcz646cg%40qwmkdb3l4ett
1 parent 91ecb5e commit c878de1

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

src/backend/storage/lmgr/lock.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,10 @@ int FastPathLockGroupsPerBackend = 0;
226226
* the FAST_PATH_SLOT macro, split it into group and index (in the group).
227227
*/
228228
#define FAST_PATH_GROUP(index) \
229-
(AssertMacro((uint32) (index) < FP_LOCK_SLOTS_PER_BACKEND), \
229+
(AssertMacro((uint32) (index) < FastPathLockSlotsPerBackend()), \
230230
((index) / FP_LOCK_SLOTS_PER_GROUP))
231231
#define FAST_PATH_INDEX(index) \
232-
(AssertMacro((uint32) (index) < FP_LOCK_SLOTS_PER_BACKEND), \
232+
(AssertMacro((uint32) (index) < FastPathLockSlotsPerBackend()), \
233233
((index) % FP_LOCK_SLOTS_PER_GROUP))
234234

235235
/* Macros for manipulating proc->fpLockBits */
@@ -242,7 +242,7 @@ int FastPathLockGroupsPerBackend = 0;
242242
#define FAST_PATH_BIT_POSITION(n, l) \
243243
(AssertMacro((l) >= FAST_PATH_LOCKNUMBER_OFFSET), \
244244
AssertMacro((l) < FAST_PATH_BITS_PER_SLOT+FAST_PATH_LOCKNUMBER_OFFSET), \
245-
AssertMacro((n) < FP_LOCK_SLOTS_PER_BACKEND), \
245+
AssertMacro((n) < FastPathLockSlotsPerBackend()), \
246246
((l) - FAST_PATH_LOCKNUMBER_OFFSET + FAST_PATH_BITS_PER_SLOT * (FAST_PATH_INDEX(n))))
247247
#define FAST_PATH_SET_LOCKMODE(proc, n, l) \
248248
FAST_PATH_BITS(proc, n) |= UINT64CONST(1) << FAST_PATH_BIT_POSITION(n, l)
@@ -2691,7 +2691,7 @@ static bool
26912691
FastPathGrantRelationLock(Oid relid, LOCKMODE lockmode)
26922692
{
26932693
uint32 i;
2694-
uint32 unused_slot = FP_LOCK_SLOTS_PER_BACKEND;
2694+
uint32 unused_slot = FastPathLockSlotsPerBackend();
26952695

26962696
/* fast-path group the lock belongs to */
26972697
uint32 group = FAST_PATH_REL_GROUP(relid);
@@ -2713,7 +2713,7 @@ FastPathGrantRelationLock(Oid relid, LOCKMODE lockmode)
27132713
}
27142714

27152715
/* If no existing entry, use any empty slot. */
2716-
if (unused_slot < FP_LOCK_SLOTS_PER_BACKEND)
2716+
if (unused_slot < FastPathLockSlotsPerBackend())
27172717
{
27182718
MyProc->fpRelId[unused_slot] = relid;
27192719
FAST_PATH_SET_LOCKMODE(MyProc, unused_slot, lockmode);

src/backend/storage/lmgr/proc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ ProcGlobalShmemSize(void)
116116
* nicely aligned in each backend.
117117
*/
118118
fpLockBitsSize = MAXALIGN(FastPathLockGroupsPerBackend * sizeof(uint64));
119-
fpRelIdSize = MAXALIGN(FastPathLockGroupsPerBackend * sizeof(Oid) * FP_LOCK_SLOTS_PER_GROUP);
119+
fpRelIdSize = MAXALIGN(FastPathLockSlotsPerBackend() * sizeof(Oid));
120120

121121
size = add_size(size, mul_size(TotalProcs, (fpLockBitsSize + fpRelIdSize)));
122122

@@ -231,7 +231,7 @@ InitProcGlobal(void)
231231
* shared memory and then divide that between backends.
232232
*/
233233
fpLockBitsSize = MAXALIGN(FastPathLockGroupsPerBackend * sizeof(uint64));
234-
fpRelIdSize = MAXALIGN(FastPathLockGroupsPerBackend * sizeof(Oid) * FP_LOCK_SLOTS_PER_GROUP);
234+
fpRelIdSize = MAXALIGN(FastPathLockSlotsPerBackend() * sizeof(Oid));
235235

236236
fpPtr = ShmemAlloc(TotalProcs * (fpLockBitsSize + fpRelIdSize));
237237
MemSet(fpPtr, 0, TotalProcs * (fpLockBitsSize + fpRelIdSize));

src/backend/utils/init/postinit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ InitializeFastPathLocks(void)
587587
while (FastPathLockGroupsPerBackend < FP_LOCK_GROUPS_PER_BACKEND_MAX)
588588
{
589589
/* stop once we exceed max_locks_per_xact */
590-
if (FastPathLockGroupsPerBackend * FP_LOCK_SLOTS_PER_GROUP >= max_locks_per_xact)
590+
if (FastPathLockSlotsPerBackend() >= max_locks_per_xact)
591591
break;
592592

593593
FastPathLockGroupsPerBackend *= 2;

src/include/storage/proc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ extern PGDLLIMPORT int FastPathLockGroupsPerBackend;
8888

8989
#define FP_LOCK_GROUPS_PER_BACKEND_MAX 1024
9090
#define FP_LOCK_SLOTS_PER_GROUP 16 /* don't change */
91-
#define FP_LOCK_SLOTS_PER_BACKEND (FP_LOCK_SLOTS_PER_GROUP * FastPathLockGroupsPerBackend)
91+
#define FastPathLockSlotsPerBackend() \
92+
(FP_LOCK_SLOTS_PER_GROUP * FastPathLockGroupsPerBackend)
9293

9394
/*
9495
* Flags for PGPROC.delayChkptFlags

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy