summaryrefslogtreecommitdiff
path: root/src/backend/storage/lmgr/deadlock.c
diff options
context:
space:
mode:
authorRobert Haas <rhaas@postgresql.org>2022-02-08 15:52:40 -0500
committerRobert Haas <rhaas@postgresql.org>2022-02-08 15:53:19 -0500
commitaa64f23b02924724eafbd9eadbf26d85df30a12b (patch)
tree54f72bfd7e36c2e879dc87149eb94db220d1cae3 /src/backend/storage/lmgr/deadlock.c
parent2da896182ce11240774af6c4d769777f90a09536 (diff)
downloadpostgresql-aa64f23b02924724eafbd9eadbf26d85df30a12b.tar.gz
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
Diffstat (limited to 'src/backend/storage/lmgr/deadlock.c')
-rw-r--r--src/backend/storage/lmgr/deadlock.c31
1 files changed, 16 insertions, 15 deletions
diff --git a/src/backend/storage/lmgr/deadlock.c b/src/backend/storage/lmgr/deadlock.c
index cd9c0418ec..b5d539ba5d 100644
--- a/src/backend/storage/lmgr/deadlock.c
+++ b/src/backend/storage/lmgr/deadlock.c
@@ -143,6 +143,7 @@ void
InitDeadLockChecking(void)
{
MemoryContext oldcxt;
+ int max_backends = GetMaxBackends();
/* Make sure allocations are permanent */
oldcxt = MemoryContextSwitchTo(TopMemoryContext);
@@ -151,16 +152,16 @@ InitDeadLockChecking(void)
* FindLockCycle needs at most MaxBackends entries in visitedProcs[] and
* deadlockDetails[].
*/
- visitedProcs = (PGPROC **) palloc(MaxBackends * sizeof(PGPROC *));
- deadlockDetails = (DEADLOCK_INFO *) palloc(MaxBackends * sizeof(DEADLOCK_INFO));
+ visitedProcs = (PGPROC **) palloc(max_backends * sizeof(PGPROC *));
+ deadlockDetails = (DEADLOCK_INFO *) palloc(max_backends * sizeof(DEADLOCK_INFO));
/*
* TopoSort needs to consider at most MaxBackends wait-queue entries, and
* it needn't run concurrently with FindLockCycle.
*/
topoProcs = visitedProcs; /* re-use this space */
- beforeConstraints = (int *) palloc(MaxBackends * sizeof(int));
- afterConstraints = (int *) palloc(MaxBackends * sizeof(int));
+ beforeConstraints = (int *) palloc(max_backends * sizeof(int));
+ afterConstraints = (int *) palloc(max_backends * sizeof(int));
/*
* We need to consider rearranging at most MaxBackends/2 wait queues
@@ -169,8 +170,8 @@ InitDeadLockChecking(void)
* MaxBackends total waiters.
*/
waitOrders = (WAIT_ORDER *)
- palloc((MaxBackends / 2) * sizeof(WAIT_ORDER));
- waitOrderProcs = (PGPROC **) palloc(MaxBackends * sizeof(PGPROC *));
+ palloc((max_backends / 2) * sizeof(WAIT_ORDER));
+ waitOrderProcs = (PGPROC **) palloc(max_backends * sizeof(PGPROC *));
/*
* Allow at most MaxBackends distinct constraints in a configuration. (Is
@@ -180,7 +181,7 @@ InitDeadLockChecking(void)
* limits the maximum recursion depth of DeadLockCheckRecurse. Making it
* really big might potentially allow a stack-overflow problem.
*/
- maxCurConstraints = MaxBackends;
+ maxCurConstraints = max_backends;
curConstraints = (EDGE *) palloc(maxCurConstraints * sizeof(EDGE));
/*
@@ -191,7 +192,7 @@ InitDeadLockChecking(void)
* last MaxBackends entries in possibleConstraints[] are reserved as
* output workspace for FindLockCycle.
*/
- maxPossibleConstraints = MaxBackends * 4;
+ maxPossibleConstraints = max_backends * 4;
possibleConstraints =
(EDGE *) palloc(maxPossibleConstraints * sizeof(EDGE));
@@ -327,7 +328,7 @@ DeadLockCheckRecurse(PGPROC *proc)
if (nCurConstraints >= maxCurConstraints)
return true; /* out of room for active constraints? */
oldPossibleConstraints = nPossibleConstraints;
- if (nPossibleConstraints + nEdges + MaxBackends <= maxPossibleConstraints)
+ if (nPossibleConstraints + nEdges + GetMaxBackends() <= maxPossibleConstraints)
{
/* We can save the edge list in possibleConstraints[] */
nPossibleConstraints += nEdges;
@@ -388,7 +389,7 @@ TestConfiguration(PGPROC *startProc)
/*
* Make sure we have room for FindLockCycle's output.
*/
- if (nPossibleConstraints + MaxBackends > maxPossibleConstraints)
+ if (nPossibleConstraints + GetMaxBackends() > maxPossibleConstraints)
return -1;
/*
@@ -486,7 +487,7 @@ FindLockCycleRecurse(PGPROC *checkProc,
* record total length of cycle --- outer levels will now fill
* deadlockDetails[]
*/
- Assert(depth <= MaxBackends);
+ Assert(depth <= GetMaxBackends());
nDeadlockDetails = depth;
return true;
@@ -500,7 +501,7 @@ FindLockCycleRecurse(PGPROC *checkProc,
}
}
/* Mark proc as seen */
- Assert(nVisitedProcs < MaxBackends);
+ Assert(nVisitedProcs < GetMaxBackends());
visitedProcs[nVisitedProcs++] = checkProc;
/*
@@ -698,7 +699,7 @@ FindLockCycleRecurseMember(PGPROC *checkProc,
/*
* Add this edge to the list of soft edges in the cycle
*/
- Assert(*nSoftEdges < MaxBackends);
+ Assert(*nSoftEdges < GetMaxBackends());
softEdges[*nSoftEdges].waiter = checkProcLeader;
softEdges[*nSoftEdges].blocker = leader;
softEdges[*nSoftEdges].lock = lock;
@@ -771,7 +772,7 @@ FindLockCycleRecurseMember(PGPROC *checkProc,
/*
* Add this edge to the list of soft edges in the cycle
*/
- Assert(*nSoftEdges < MaxBackends);
+ Assert(*nSoftEdges < GetMaxBackends());
softEdges[*nSoftEdges].waiter = checkProcLeader;
softEdges[*nSoftEdges].blocker = leader;
softEdges[*nSoftEdges].lock = lock;
@@ -834,7 +835,7 @@ ExpandConstraints(EDGE *constraints,
waitOrders[nWaitOrders].procs = waitOrderProcs + nWaitOrderProcs;
waitOrders[nWaitOrders].nProcs = lock->waitProcs.size;
nWaitOrderProcs += lock->waitProcs.size;
- Assert(nWaitOrderProcs <= MaxBackends);
+ Assert(nWaitOrderProcs <= GetMaxBackends());
/*
* Do the topo sort. TopoSort need not examine constraints after this