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
This commit is contained in:
parent
f0cd9097cf
commit
4567596316
|
@ -1633,6 +1633,7 @@ SignalBackends(void)
|
||||||
int32 *pids;
|
int32 *pids;
|
||||||
BackendId *ids;
|
BackendId *ids;
|
||||||
int count;
|
int count;
|
||||||
|
int max_backends = GetMaxBackends();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Identify backends that we need to signal. We don't want to send
|
* Identify backends that we need to signal. We don't want to send
|
||||||
|
@ -1642,8 +1643,8 @@ SignalBackends(void)
|
||||||
* XXX in principle these pallocs could fail, which would be bad. Maybe
|
* XXX in principle these pallocs could fail, which would be bad. Maybe
|
||||||
* preallocate the arrays? They're not that large, though.
|
* preallocate the arrays? They're not that large, though.
|
||||||
*/
|
*/
|
||||||
pids = (int32 *) palloc(GetMaxBackends() * sizeof(int32));
|
pids = (int32 *) palloc(max_backends * sizeof(int32));
|
||||||
ids = (BackendId *) palloc(GetMaxBackends() * sizeof(BackendId));
|
ids = (BackendId *) palloc(max_backends * sizeof(BackendId));
|
||||||
count = 0;
|
count = 0;
|
||||||
|
|
||||||
LWLockAcquire(NotifyQueueLock, LW_EXCLUSIVE);
|
LWLockAcquire(NotifyQueueLock, LW_EXCLUSIVE);
|
||||||
|
|
|
@ -2924,6 +2924,7 @@ GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
|
||||||
LWLock *partitionLock;
|
LWLock *partitionLock;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
int fast_count = 0;
|
int fast_count = 0;
|
||||||
|
int max_backends = GetMaxBackends();
|
||||||
|
|
||||||
if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
|
if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
|
||||||
elog(ERROR, "unrecognized lock method: %d", lockmethodid);
|
elog(ERROR, "unrecognized lock method: %d", lockmethodid);
|
||||||
|
@ -2942,12 +2943,12 @@ GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
|
||||||
vxids = (VirtualTransactionId *)
|
vxids = (VirtualTransactionId *)
|
||||||
MemoryContextAlloc(TopMemoryContext,
|
MemoryContextAlloc(TopMemoryContext,
|
||||||
sizeof(VirtualTransactionId) *
|
sizeof(VirtualTransactionId) *
|
||||||
(GetMaxBackends() + max_prepared_xacts + 1));
|
(max_backends + max_prepared_xacts + 1));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
vxids = (VirtualTransactionId *)
|
vxids = (VirtualTransactionId *)
|
||||||
palloc0(sizeof(VirtualTransactionId) *
|
palloc0(sizeof(VirtualTransactionId) *
|
||||||
(GetMaxBackends() + max_prepared_xacts + 1));
|
(max_backends + max_prepared_xacts + 1));
|
||||||
|
|
||||||
/* Compute hash code and partition lock, and look up conflicting modes. */
|
/* Compute hash code and partition lock, and look up conflicting modes. */
|
||||||
hashcode = LockTagHashCode(locktag);
|
hashcode = LockTagHashCode(locktag);
|
||||||
|
@ -3104,7 +3105,7 @@ GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
|
||||||
|
|
||||||
LWLockRelease(partitionLock);
|
LWLockRelease(partitionLock);
|
||||||
|
|
||||||
if (count > GetMaxBackends() + max_prepared_xacts) /* should never happen */
|
if (count > max_backends + max_prepared_xacts) /* should never happen */
|
||||||
elog(PANIC, "too many conflicting locks found");
|
elog(PANIC, "too many conflicting locks found");
|
||||||
|
|
||||||
vxids[count].backendId = InvalidBackendId;
|
vxids[count].backendId = InvalidBackendId;
|
||||||
|
|
|
@ -559,13 +559,14 @@ pg_safe_snapshot_blocking_pids(PG_FUNCTION_ARGS)
|
||||||
int *blockers;
|
int *blockers;
|
||||||
int num_blockers;
|
int num_blockers;
|
||||||
Datum *blocker_datums;
|
Datum *blocker_datums;
|
||||||
|
int max_backends = GetMaxBackends();
|
||||||
|
|
||||||
/* A buffer big enough for any possible blocker list without truncation */
|
/* A buffer big enough for any possible blocker list without truncation */
|
||||||
blockers = (int *) palloc(GetMaxBackends() * sizeof(int));
|
blockers = (int *) palloc(max_backends * sizeof(int));
|
||||||
|
|
||||||
/* Collect a snapshot of processes waited for by GetSafeSnapshot */
|
/* Collect a snapshot of processes waited for by GetSafeSnapshot */
|
||||||
num_blockers =
|
num_blockers =
|
||||||
GetSafeSnapshotBlockingPids(blocked_pid, blockers, GetMaxBackends());
|
GetSafeSnapshotBlockingPids(blocked_pid, blockers, max_backends);
|
||||||
|
|
||||||
/* Convert int array to Datum array */
|
/* Convert int array to Datum array */
|
||||||
if (num_blockers > 0)
|
if (num_blockers > 0)
|
||||||
|
|
Loading…
Reference in New Issue