1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-06 07:49:08 +03:00

Remove MaxBackends variable in favor of GetMaxBackends() function.

Previously, it was really easy to write code that accessed MaxBackends
before we'd actually initialized it, especially when coding up an
extension. To make this less error-prune, introduce a new function
GetMaxBackends() which should be used to obtain the correct value.
This will ERROR if called too early. Demote the global variable to
a file-level static, so that nobody can peak at it directly.

Nathan Bossart. Idea by Andres Freund. Review by Greg Sabino Mullane,
by Michael Paquier (who had doubts about the approach), and by me.

Discussion: http://postgr.es/m/20210802224204.bckcikl45uezv5e4@alap3.anarazel.de
This commit is contained in:
Robert Haas
2022-02-08 15:52:40 -05:00
parent 2da896182c
commit aa64f23b02
19 changed files with 218 additions and 133 deletions

View File

@@ -55,7 +55,7 @@
int max_locks_per_xact; /* set by guc.c */
#define NLOCKENTS() \
mul_size(max_locks_per_xact, add_size(MaxBackends, max_prepared_xacts))
mul_size(max_locks_per_xact, add_size(GetMaxBackends(), max_prepared_xacts))
/*
@@ -2942,12 +2942,12 @@ GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
vxids = (VirtualTransactionId *)
MemoryContextAlloc(TopMemoryContext,
sizeof(VirtualTransactionId) *
(MaxBackends + max_prepared_xacts + 1));
(GetMaxBackends() + max_prepared_xacts + 1));
}
else
vxids = (VirtualTransactionId *)
palloc0(sizeof(VirtualTransactionId) *
(MaxBackends + max_prepared_xacts + 1));
(GetMaxBackends() + max_prepared_xacts + 1));
/* Compute hash code and partition lock, and look up conflicting modes. */
hashcode = LockTagHashCode(locktag);
@@ -3104,7 +3104,7 @@ GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
LWLockRelease(partitionLock);
if (count > MaxBackends + max_prepared_xacts) /* should never happen */
if (count > GetMaxBackends() + max_prepared_xacts) /* should never happen */
elog(PANIC, "too many conflicting locks found");
vxids[count].backendId = InvalidBackendId;
@@ -3651,11 +3651,12 @@ GetLockStatusData(void)
int els;
int el;
int i;
int max_backends = GetMaxBackends();
data = (LockData *) palloc(sizeof(LockData));
/* Guess how much space we'll need. */
els = MaxBackends;
els = max_backends;
el = 0;
data->locks = (LockInstanceData *) palloc(sizeof(LockInstanceData) * els);
@@ -3689,7 +3690,7 @@ GetLockStatusData(void)
if (el >= els)
{
els += MaxBackends;
els += max_backends;
data->locks = (LockInstanceData *)
repalloc(data->locks, sizeof(LockInstanceData) * els);
}
@@ -3721,7 +3722,7 @@ GetLockStatusData(void)
if (el >= els)
{
els += MaxBackends;
els += max_backends;
data->locks = (LockInstanceData *)
repalloc(data->locks, sizeof(LockInstanceData) * els);
}
@@ -3850,7 +3851,7 @@ GetBlockerStatusData(int blocked_pid)
* for the procs[] array; the other two could need enlargement, though.)
*/
data->nprocs = data->nlocks = data->npids = 0;
data->maxprocs = data->maxlocks = data->maxpids = MaxBackends;
data->maxprocs = data->maxlocks = data->maxpids = GetMaxBackends();
data->procs = (BlockedProcData *) palloc(sizeof(BlockedProcData) * data->maxprocs);
data->locks = (LockInstanceData *) palloc(sizeof(LockInstanceData) * data->maxlocks);
data->waiter_pids = (int *) palloc(sizeof(int) * data->maxpids);
@@ -3925,6 +3926,7 @@ GetSingleProcBlockerStatusData(PGPROC *blocked_proc, BlockedProcsData *data)
PGPROC *proc;
int queue_size;
int i;
int max_backends = GetMaxBackends();
/* Nothing to do if this proc is not blocked */
if (theLock == NULL)
@@ -3953,7 +3955,7 @@ GetSingleProcBlockerStatusData(PGPROC *blocked_proc, BlockedProcsData *data)
if (data->nlocks >= data->maxlocks)
{
data->maxlocks += MaxBackends;
data->maxlocks += max_backends;
data->locks = (LockInstanceData *)
repalloc(data->locks, sizeof(LockInstanceData) * data->maxlocks);
}
@@ -3982,7 +3984,7 @@ GetSingleProcBlockerStatusData(PGPROC *blocked_proc, BlockedProcsData *data)
if (queue_size > data->maxpids - data->npids)
{
data->maxpids = Max(data->maxpids + MaxBackends,
data->maxpids = Max(data->maxpids + max_backends,
data->npids + queue_size);
data->waiter_pids = (int *) repalloc(data->waiter_pids,
sizeof(int) * data->maxpids);