aboutsummaryrefslogtreecommitdiff
path: root/src/backend/commands/async.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/commands/async.c
parent2da896182ce11240774af6c4d769777f90a09536 (diff)
downloadpostgresql-aa64f23b02924724eafbd9eadbf26d85df30a12b.tar.gz
postgresql-aa64f23b02924724eafbd9eadbf26d85df30a12b.zip
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/commands/async.c')
-rw-r--r--src/backend/commands/async.c11
1 files changed, 6 insertions, 5 deletions
diff --git a/src/backend/commands/async.c b/src/backend/commands/async.c
index 3e1b92df030..d44001a49f3 100644
--- a/src/backend/commands/async.c
+++ b/src/backend/commands/async.c
@@ -518,7 +518,7 @@ AsyncShmemSize(void)
Size size;
/* This had better match AsyncShmemInit */
- size = mul_size(MaxBackends + 1, sizeof(QueueBackendStatus));
+ size = mul_size(GetMaxBackends() + 1, sizeof(QueueBackendStatus));
size = add_size(size, offsetof(AsyncQueueControl, backend));
size = add_size(size, SimpleLruShmemSize(NUM_NOTIFY_BUFFERS, 0));
@@ -534,6 +534,7 @@ AsyncShmemInit(void)
{
bool found;
Size size;
+ int max_backends = GetMaxBackends();
/*
* Create or attach to the AsyncQueueControl structure.
@@ -541,7 +542,7 @@ AsyncShmemInit(void)
* The used entries in the backend[] array run from 1 to MaxBackends; the
* zero'th entry is unused but must be allocated.
*/
- size = mul_size(MaxBackends + 1, sizeof(QueueBackendStatus));
+ size = mul_size(max_backends + 1, sizeof(QueueBackendStatus));
size = add_size(size, offsetof(AsyncQueueControl, backend));
asyncQueueControl = (AsyncQueueControl *)
@@ -556,7 +557,7 @@ AsyncShmemInit(void)
QUEUE_FIRST_LISTENER = InvalidBackendId;
asyncQueueControl->lastQueueFillWarn = 0;
/* zero'th entry won't be used, but let's initialize it anyway */
- for (int i = 0; i <= MaxBackends; i++)
+ for (int i = 0; i <= max_backends; i++)
{
QUEUE_BACKEND_PID(i) = InvalidPid;
QUEUE_BACKEND_DBOID(i) = InvalidOid;
@@ -1641,8 +1642,8 @@ SignalBackends(void)
* XXX in principle these pallocs could fail, which would be bad. Maybe
* preallocate the arrays? They're not that large, though.
*/
- pids = (int32 *) palloc(MaxBackends * sizeof(int32));
- ids = (BackendId *) palloc(MaxBackends * sizeof(BackendId));
+ pids = (int32 *) palloc(GetMaxBackends() * sizeof(int32));
+ ids = (BackendId *) palloc(GetMaxBackends() * sizeof(BackendId));
count = 0;
LWLockAcquire(NotifyQueueLock, LW_EXCLUSIVE);