Skip to content

Commit 4567596

Browse files
committed
Reduce more the number of calls to GetMaxBackends()
Some of the code paths changed by aa64f23 can reduce the number of times GetMaxBackends() is called. The performance gain is marginal, but most of the code changed by this commit already did that. Hence, let's be clean and apply the same rule everywhere, for consistency. Some of the code paths, like in deadlock.c, involve only assertions. These are left unchanged. Reviewed-by: Nathan Bossart, Robert Haas Discussion: https://postgr.es/m/YgMpGZhPOjNfS7er@paquier.xyz
1 parent f0cd909 commit 4567596

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

src/backend/commands/async.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,6 +1633,7 @@ SignalBackends(void)
16331633
int32 *pids;
16341634
BackendId *ids;
16351635
int count;
1636+
int max_backends = GetMaxBackends();
16361637

16371638
/*
16381639
* Identify backends that we need to signal. We don't want to send
@@ -1642,8 +1643,8 @@ SignalBackends(void)
16421643
* XXX in principle these pallocs could fail, which would be bad. Maybe
16431644
* preallocate the arrays? They're not that large, though.
16441645
*/
1645-
pids = (int32 *) palloc(GetMaxBackends() * sizeof(int32));
1646-
ids = (BackendId *) palloc(GetMaxBackends() * sizeof(BackendId));
1646+
pids = (int32 *) palloc(max_backends * sizeof(int32));
1647+
ids = (BackendId *) palloc(max_backends * sizeof(BackendId));
16471648
count = 0;
16481649

16491650
LWLockAcquire(NotifyQueueLock, LW_EXCLUSIVE);

src/backend/storage/lmgr/lock.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2924,6 +2924,7 @@ GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
29242924
LWLock *partitionLock;
29252925
int count = 0;
29262926
int fast_count = 0;
2927+
int max_backends = GetMaxBackends();
29272928

29282929
if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
29292930
elog(ERROR, "unrecognized lock method: %d", lockmethodid);
@@ -2942,12 +2943,12 @@ GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
29422943
vxids = (VirtualTransactionId *)
29432944
MemoryContextAlloc(TopMemoryContext,
29442945
sizeof(VirtualTransactionId) *
2945-
(GetMaxBackends() + max_prepared_xacts + 1));
2946+
(max_backends + max_prepared_xacts + 1));
29462947
}
29472948
else
29482949
vxids = (VirtualTransactionId *)
29492950
palloc0(sizeof(VirtualTransactionId) *
2950-
(GetMaxBackends() + max_prepared_xacts + 1));
2951+
(max_backends + max_prepared_xacts + 1));
29512952

29522953
/* Compute hash code and partition lock, and look up conflicting modes. */
29532954
hashcode = LockTagHashCode(locktag);
@@ -3104,7 +3105,7 @@ GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
31043105

31053106
LWLockRelease(partitionLock);
31063107

3107-
if (count > GetMaxBackends() + max_prepared_xacts) /* should never happen */
3108+
if (count > max_backends + max_prepared_xacts) /* should never happen */
31083109
elog(PANIC, "too many conflicting locks found");
31093110

31103111
vxids[count].backendId = InvalidBackendId;

src/backend/utils/adt/lockfuncs.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,13 +559,14 @@ pg_safe_snapshot_blocking_pids(PG_FUNCTION_ARGS)
559559
int *blockers;
560560
int num_blockers;
561561
Datum *blocker_datums;
562+
int max_backends = GetMaxBackends();
562563

563564
/* A buffer big enough for any possible blocker list without truncation */
564-
blockers = (int *) palloc(GetMaxBackends() * sizeof(int));
565+
blockers = (int *) palloc(max_backends * sizeof(int));
565566

566567
/* Collect a snapshot of processes waited for by GetSafeSnapshot */
567568
num_blockers =
568-
GetSafeSnapshotBlockingPids(blocked_pid, blockers, GetMaxBackends());
569+
GetSafeSnapshotBlockingPids(blocked_pid, blockers, max_backends);
569570

570571
/* Convert int array to Datum array */
571572
if (num_blockers > 0)

0 commit comments

Comments
 (0)