Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 86c30ce

Browse files
committed
Refactor GetLockStatusData() to skip backends/groups without fast-path locks.
Previously, GetLockStatusData() checked all slots for every backend to gather fast-path lock data, which could be inefficient. This commit refactors it by skipping backends with PID=0 (since they don't hold fast-path locks) and skipping groups with no registered fast-path locks, improving efficiency. This refactoring is particularly beneficial, for example when max_connections and max_locks_per_transaction are set high, as it reduces unnecessary checks across numerous slots. Author: Fujii Masao Reviewed-by: Bertrand Drouvot Discussion: https://postgr.es/m/a0a00c44-31e9-4c67-9846-fb9636213ac9@oss.nttdata.com
1 parent 45188c2 commit 86c30ce

File tree

1 file changed

+39
-28
lines changed
  • src/backend/storage/lmgr

1 file changed

+39
-28
lines changed

src/backend/storage/lmgr/lock.c

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3731,44 +3731,55 @@ GetLockStatusData(void)
37313731
for (i = 0; i < ProcGlobal->allProcCount; ++i)
37323732
{
37333733
PGPROC *proc = &ProcGlobal->allProcs[i];
3734-
uint32 f;
3734+
3735+
/* Skip backends with pid=0, as they don't hold fast-path locks */
3736+
if (proc->pid == 0)
3737+
continue;
37353738

37363739
LWLockAcquire(&proc->fpInfoLock, LW_SHARED);
37373740

3738-
for (f = 0; f < FP_LOCK_SLOTS_PER_BACKEND; ++f)
3741+
for (uint32 g = 0; g < FastPathLockGroupsPerBackend; g++)
37393742
{
3740-
LockInstanceData *instance;
3741-
uint32 lockbits = FAST_PATH_GET_BITS(proc, f);
3742-
3743-
/* Skip unallocated slots. */
3744-
if (!lockbits)
3743+
/* Skip groups without registered fast-path locks */
3744+
if (proc->fpLockBits[g] == 0)
37453745
continue;
37463746

3747-
if (el >= els)
3747+
for (int j = 0; j < FP_LOCK_SLOTS_PER_GROUP; j++)
37483748
{
3749-
els += MaxBackends;
3750-
data->locks = (LockInstanceData *)
3751-
repalloc(data->locks, sizeof(LockInstanceData) * els);
3752-
}
3749+
LockInstanceData *instance;
3750+
uint32 f = FAST_PATH_SLOT(g, j);
3751+
uint32 lockbits = FAST_PATH_GET_BITS(proc, f);
37533752

3754-
instance = &data->locks[el];
3755-
SET_LOCKTAG_RELATION(instance->locktag, proc->databaseId,
3756-
proc->fpRelId[f]);
3757-
instance->holdMask = lockbits << FAST_PATH_LOCKNUMBER_OFFSET;
3758-
instance->waitLockMode = NoLock;
3759-
instance->vxid.procNumber = proc->vxid.procNumber;
3760-
instance->vxid.localTransactionId = proc->vxid.lxid;
3761-
instance->pid = proc->pid;
3762-
instance->leaderPid = proc->pid;
3763-
instance->fastpath = true;
3753+
/* Skip unallocated slots */
3754+
if (!lockbits)
3755+
continue;
37643756

3765-
/*
3766-
* Successfully taking fast path lock means there were no
3767-
* conflicting locks.
3768-
*/
3769-
instance->waitStart = 0;
3757+
if (el >= els)
3758+
{
3759+
els += MaxBackends;
3760+
data->locks = (LockInstanceData *)
3761+
repalloc(data->locks, sizeof(LockInstanceData) * els);
3762+
}
37703763

3771-
el++;
3764+
instance = &data->locks[el];
3765+
SET_LOCKTAG_RELATION(instance->locktag, proc->databaseId,
3766+
proc->fpRelId[f]);
3767+
instance->holdMask = lockbits << FAST_PATH_LOCKNUMBER_OFFSET;
3768+
instance->waitLockMode = NoLock;
3769+
instance->vxid.procNumber = proc->vxid.procNumber;
3770+
instance->vxid.localTransactionId = proc->vxid.lxid;
3771+
instance->pid = proc->pid;
3772+
instance->leaderPid = proc->pid;
3773+
instance->fastpath = true;
3774+
3775+
/*
3776+
* Successfully taking fast path lock means there were no
3777+
* conflicting locks.
3778+
*/
3779+
instance->waitStart = 0;
3780+
3781+
el++;
3782+
}
37723783
}
37733784

37743785
if (proc->fpVXIDLock)

0 commit comments

Comments
 (0)