mirror of
https://github.com/postgres/postgres.git
synced 2025-11-19 13:42:17 +03:00
Revert the addition of GetMaxBackends() and related stuff.
This reverts commits0147fc7,4567596,aa64f23, and5ecd018. There is no longer agreement that introducing this function was the right way to address the problem. The consensus now seems to favor trying to make a correct value for MaxBackends available to mdules executing their _PG_init() functions. Nathan Bossart Discussion: http://postgr.es/m/20220323045229.i23skfscdbvrsuxa@jrouhaud
This commit is contained in:
@@ -26,6 +26,18 @@
|
||||
#include "utils/memutils.h"
|
||||
|
||||
|
||||
/* ----------
|
||||
* Total number of backends including auxiliary
|
||||
*
|
||||
* We reserve a slot for each possible BackendId, plus one for each
|
||||
* possible auxiliary process type. (This scheme assumes there is not
|
||||
* more than one of any auxiliary process type at a time.) MaxBackends
|
||||
* includes autovacuum workers and background workers as well.
|
||||
* ----------
|
||||
*/
|
||||
#define NumBackendStatSlots (MaxBackends + NUM_AUXPROCTYPES)
|
||||
|
||||
|
||||
/* ----------
|
||||
* GUC parameters
|
||||
* ----------
|
||||
@@ -63,23 +75,8 @@ static MemoryContext backendStatusSnapContext;
|
||||
static void pgstat_beshutdown_hook(int code, Datum arg);
|
||||
static void pgstat_read_current_status(void);
|
||||
static void pgstat_setup_backend_status_context(void);
|
||||
static inline int GetNumBackendStatSlots(void);
|
||||
|
||||
|
||||
/*
|
||||
* Retrieve the total number of backends including auxiliary
|
||||
*
|
||||
* We reserve a slot for each possible BackendId, plus one for each possible
|
||||
* auxiliary process type. (This scheme assumes there is not more than one of
|
||||
* any auxiliary process type at a time.) MaxBackends includes autovacuum
|
||||
* workers and background workers as well.
|
||||
*/
|
||||
static inline int
|
||||
GetNumBackendStatSlots(void)
|
||||
{
|
||||
return GetMaxBackends() + NUM_AUXPROCTYPES;
|
||||
}
|
||||
|
||||
/*
|
||||
* Report shared-memory space needed by CreateSharedBackendStatus.
|
||||
*/
|
||||
@@ -87,28 +84,27 @@ Size
|
||||
BackendStatusShmemSize(void)
|
||||
{
|
||||
Size size;
|
||||
int numBackendStatSlots = GetNumBackendStatSlots();
|
||||
|
||||
/* BackendStatusArray: */
|
||||
size = mul_size(sizeof(PgBackendStatus), numBackendStatSlots);
|
||||
size = mul_size(sizeof(PgBackendStatus), NumBackendStatSlots);
|
||||
/* BackendAppnameBuffer: */
|
||||
size = add_size(size,
|
||||
mul_size(NAMEDATALEN, numBackendStatSlots));
|
||||
mul_size(NAMEDATALEN, NumBackendStatSlots));
|
||||
/* BackendClientHostnameBuffer: */
|
||||
size = add_size(size,
|
||||
mul_size(NAMEDATALEN, numBackendStatSlots));
|
||||
mul_size(NAMEDATALEN, NumBackendStatSlots));
|
||||
/* BackendActivityBuffer: */
|
||||
size = add_size(size,
|
||||
mul_size(pgstat_track_activity_query_size, numBackendStatSlots));
|
||||
mul_size(pgstat_track_activity_query_size, NumBackendStatSlots));
|
||||
#ifdef USE_SSL
|
||||
/* BackendSslStatusBuffer: */
|
||||
size = add_size(size,
|
||||
mul_size(sizeof(PgBackendSSLStatus), numBackendStatSlots));
|
||||
mul_size(sizeof(PgBackendSSLStatus), NumBackendStatSlots));
|
||||
#endif
|
||||
#ifdef ENABLE_GSS
|
||||
/* BackendGssStatusBuffer: */
|
||||
size = add_size(size,
|
||||
mul_size(sizeof(PgBackendGSSStatus), numBackendStatSlots));
|
||||
mul_size(sizeof(PgBackendGSSStatus), NumBackendStatSlots));
|
||||
#endif
|
||||
return size;
|
||||
}
|
||||
@@ -124,10 +120,9 @@ CreateSharedBackendStatus(void)
|
||||
bool found;
|
||||
int i;
|
||||
char *buffer;
|
||||
int numBackendStatSlots = GetNumBackendStatSlots();
|
||||
|
||||
/* Create or attach to the shared array */
|
||||
size = mul_size(sizeof(PgBackendStatus), numBackendStatSlots);
|
||||
size = mul_size(sizeof(PgBackendStatus), NumBackendStatSlots);
|
||||
BackendStatusArray = (PgBackendStatus *)
|
||||
ShmemInitStruct("Backend Status Array", size, &found);
|
||||
|
||||
@@ -140,7 +135,7 @@ CreateSharedBackendStatus(void)
|
||||
}
|
||||
|
||||
/* Create or attach to the shared appname buffer */
|
||||
size = mul_size(NAMEDATALEN, numBackendStatSlots);
|
||||
size = mul_size(NAMEDATALEN, NumBackendStatSlots);
|
||||
BackendAppnameBuffer = (char *)
|
||||
ShmemInitStruct("Backend Application Name Buffer", size, &found);
|
||||
|
||||
@@ -150,7 +145,7 @@ CreateSharedBackendStatus(void)
|
||||
|
||||
/* Initialize st_appname pointers. */
|
||||
buffer = BackendAppnameBuffer;
|
||||
for (i = 0; i < numBackendStatSlots; i++)
|
||||
for (i = 0; i < NumBackendStatSlots; i++)
|
||||
{
|
||||
BackendStatusArray[i].st_appname = buffer;
|
||||
buffer += NAMEDATALEN;
|
||||
@@ -158,7 +153,7 @@ CreateSharedBackendStatus(void)
|
||||
}
|
||||
|
||||
/* Create or attach to the shared client hostname buffer */
|
||||
size = mul_size(NAMEDATALEN, numBackendStatSlots);
|
||||
size = mul_size(NAMEDATALEN, NumBackendStatSlots);
|
||||
BackendClientHostnameBuffer = (char *)
|
||||
ShmemInitStruct("Backend Client Host Name Buffer", size, &found);
|
||||
|
||||
@@ -168,7 +163,7 @@ CreateSharedBackendStatus(void)
|
||||
|
||||
/* Initialize st_clienthostname pointers. */
|
||||
buffer = BackendClientHostnameBuffer;
|
||||
for (i = 0; i < numBackendStatSlots; i++)
|
||||
for (i = 0; i < NumBackendStatSlots; i++)
|
||||
{
|
||||
BackendStatusArray[i].st_clienthostname = buffer;
|
||||
buffer += NAMEDATALEN;
|
||||
@@ -177,7 +172,7 @@ CreateSharedBackendStatus(void)
|
||||
|
||||
/* Create or attach to the shared activity buffer */
|
||||
BackendActivityBufferSize = mul_size(pgstat_track_activity_query_size,
|
||||
numBackendStatSlots);
|
||||
NumBackendStatSlots);
|
||||
BackendActivityBuffer = (char *)
|
||||
ShmemInitStruct("Backend Activity Buffer",
|
||||
BackendActivityBufferSize,
|
||||
@@ -189,7 +184,7 @@ CreateSharedBackendStatus(void)
|
||||
|
||||
/* Initialize st_activity pointers. */
|
||||
buffer = BackendActivityBuffer;
|
||||
for (i = 0; i < numBackendStatSlots; i++)
|
||||
for (i = 0; i < NumBackendStatSlots; i++)
|
||||
{
|
||||
BackendStatusArray[i].st_activity_raw = buffer;
|
||||
buffer += pgstat_track_activity_query_size;
|
||||
@@ -198,7 +193,7 @@ CreateSharedBackendStatus(void)
|
||||
|
||||
#ifdef USE_SSL
|
||||
/* Create or attach to the shared SSL status buffer */
|
||||
size = mul_size(sizeof(PgBackendSSLStatus), numBackendStatSlots);
|
||||
size = mul_size(sizeof(PgBackendSSLStatus), NumBackendStatSlots);
|
||||
BackendSslStatusBuffer = (PgBackendSSLStatus *)
|
||||
ShmemInitStruct("Backend SSL Status Buffer", size, &found);
|
||||
|
||||
@@ -210,7 +205,7 @@ CreateSharedBackendStatus(void)
|
||||
|
||||
/* Initialize st_sslstatus pointers. */
|
||||
ptr = BackendSslStatusBuffer;
|
||||
for (i = 0; i < numBackendStatSlots; i++)
|
||||
for (i = 0; i < NumBackendStatSlots; i++)
|
||||
{
|
||||
BackendStatusArray[i].st_sslstatus = ptr;
|
||||
ptr++;
|
||||
@@ -220,7 +215,7 @@ CreateSharedBackendStatus(void)
|
||||
|
||||
#ifdef ENABLE_GSS
|
||||
/* Create or attach to the shared GSSAPI status buffer */
|
||||
size = mul_size(sizeof(PgBackendGSSStatus), numBackendStatSlots);
|
||||
size = mul_size(sizeof(PgBackendGSSStatus), NumBackendStatSlots);
|
||||
BackendGssStatusBuffer = (PgBackendGSSStatus *)
|
||||
ShmemInitStruct("Backend GSS Status Buffer", size, &found);
|
||||
|
||||
@@ -232,7 +227,7 @@ CreateSharedBackendStatus(void)
|
||||
|
||||
/* Initialize st_gssstatus pointers. */
|
||||
ptr = BackendGssStatusBuffer;
|
||||
for (i = 0; i < numBackendStatSlots; i++)
|
||||
for (i = 0; i < NumBackendStatSlots; i++)
|
||||
{
|
||||
BackendStatusArray[i].st_gssstatus = ptr;
|
||||
ptr++;
|
||||
@@ -256,7 +251,7 @@ pgstat_beinit(void)
|
||||
/* Initialize MyBEEntry */
|
||||
if (MyBackendId != InvalidBackendId)
|
||||
{
|
||||
Assert(MyBackendId >= 1 && MyBackendId <= GetMaxBackends());
|
||||
Assert(MyBackendId >= 1 && MyBackendId <= MaxBackends);
|
||||
MyBEEntry = &BackendStatusArray[MyBackendId - 1];
|
||||
}
|
||||
else
|
||||
@@ -272,7 +267,7 @@ pgstat_beinit(void)
|
||||
* MaxBackends + AuxBackendType + 1 as the index of the slot for an
|
||||
* auxiliary process.
|
||||
*/
|
||||
MyBEEntry = &BackendStatusArray[GetMaxBackends() + MyAuxProcType];
|
||||
MyBEEntry = &BackendStatusArray[MaxBackends + MyAuxProcType];
|
||||
}
|
||||
|
||||
/* Set up a process-exit hook to clean up */
|
||||
@@ -744,7 +739,6 @@ pgstat_read_current_status(void)
|
||||
PgBackendGSSStatus *localgssstatus;
|
||||
#endif
|
||||
int i;
|
||||
int numBackendStatSlots = GetNumBackendStatSlots();
|
||||
|
||||
if (localBackendStatusTable)
|
||||
return; /* already done */
|
||||
@@ -761,32 +755,32 @@ pgstat_read_current_status(void)
|
||||
*/
|
||||
localtable = (LocalPgBackendStatus *)
|
||||
MemoryContextAlloc(backendStatusSnapContext,
|
||||
sizeof(LocalPgBackendStatus) * numBackendStatSlots);
|
||||
sizeof(LocalPgBackendStatus) * NumBackendStatSlots);
|
||||
localappname = (char *)
|
||||
MemoryContextAlloc(backendStatusSnapContext,
|
||||
NAMEDATALEN * numBackendStatSlots);
|
||||
NAMEDATALEN * NumBackendStatSlots);
|
||||
localclienthostname = (char *)
|
||||
MemoryContextAlloc(backendStatusSnapContext,
|
||||
NAMEDATALEN * numBackendStatSlots);
|
||||
NAMEDATALEN * NumBackendStatSlots);
|
||||
localactivity = (char *)
|
||||
MemoryContextAllocHuge(backendStatusSnapContext,
|
||||
pgstat_track_activity_query_size * numBackendStatSlots);
|
||||
pgstat_track_activity_query_size * NumBackendStatSlots);
|
||||
#ifdef USE_SSL
|
||||
localsslstatus = (PgBackendSSLStatus *)
|
||||
MemoryContextAlloc(backendStatusSnapContext,
|
||||
sizeof(PgBackendSSLStatus) * numBackendStatSlots);
|
||||
sizeof(PgBackendSSLStatus) * NumBackendStatSlots);
|
||||
#endif
|
||||
#ifdef ENABLE_GSS
|
||||
localgssstatus = (PgBackendGSSStatus *)
|
||||
MemoryContextAlloc(backendStatusSnapContext,
|
||||
sizeof(PgBackendGSSStatus) * numBackendStatSlots);
|
||||
sizeof(PgBackendGSSStatus) * NumBackendStatSlots);
|
||||
#endif
|
||||
|
||||
localNumBackends = 0;
|
||||
|
||||
beentry = BackendStatusArray;
|
||||
localentry = localtable;
|
||||
for (i = 1; i <= numBackendStatSlots; i++)
|
||||
for (i = 1; i <= NumBackendStatSlots; i++)
|
||||
{
|
||||
/*
|
||||
* Follow the protocol of retrying if st_changecount changes while we
|
||||
@@ -899,10 +893,9 @@ pgstat_get_backend_current_activity(int pid, bool checkUser)
|
||||
{
|
||||
PgBackendStatus *beentry;
|
||||
int i;
|
||||
int max_backends = GetMaxBackends();
|
||||
|
||||
beentry = BackendStatusArray;
|
||||
for (i = 1; i <= max_backends; i++)
|
||||
for (i = 1; i <= MaxBackends; i++)
|
||||
{
|
||||
/*
|
||||
* Although we expect the target backend's entry to be stable, that
|
||||
@@ -978,7 +971,6 @@ pgstat_get_crashed_backend_activity(int pid, char *buffer, int buflen)
|
||||
{
|
||||
volatile PgBackendStatus *beentry;
|
||||
int i;
|
||||
int max_backends = GetMaxBackends();
|
||||
|
||||
beentry = BackendStatusArray;
|
||||
|
||||
@@ -989,7 +981,7 @@ pgstat_get_crashed_backend_activity(int pid, char *buffer, int buflen)
|
||||
if (beentry == NULL || BackendActivityBuffer == NULL)
|
||||
return NULL;
|
||||
|
||||
for (i = 1; i <= max_backends; i++)
|
||||
for (i = 1; i <= MaxBackends; i++)
|
||||
{
|
||||
if (beentry->st_procpid == pid)
|
||||
{
|
||||
|
||||
@@ -559,14 +559,13 @@ pg_safe_snapshot_blocking_pids(PG_FUNCTION_ARGS)
|
||||
int *blockers;
|
||||
int num_blockers;
|
||||
Datum *blocker_datums;
|
||||
int max_backends = GetMaxBackends();
|
||||
|
||||
/* A buffer big enough for any possible blocker list without truncation */
|
||||
blockers = (int *) palloc(max_backends * sizeof(int));
|
||||
blockers = (int *) palloc(MaxBackends * sizeof(int));
|
||||
|
||||
/* Collect a snapshot of processes waited for by GetSafeSnapshot */
|
||||
num_blockers =
|
||||
GetSafeSnapshotBlockingPids(blocked_pid, blockers, max_backends);
|
||||
GetSafeSnapshotBlockingPids(blocked_pid, blockers, MaxBackends);
|
||||
|
||||
/* Convert int array to Datum array */
|
||||
if (num_blockers > 0)
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
#include "access/session.h"
|
||||
#include "access/sysattr.h"
|
||||
#include "access/tableam.h"
|
||||
#include "access/twophase.h"
|
||||
#include "access/xact.h"
|
||||
#include "access/xlog.h"
|
||||
#include "access/xloginsert.h"
|
||||
@@ -68,9 +67,6 @@
|
||||
#include "utils/syscache.h"
|
||||
#include "utils/timeout.h"
|
||||
|
||||
static int MaxBackends = 0;
|
||||
static int MaxBackendsInitialized = false;
|
||||
|
||||
static HeapTuple GetDatabaseTuple(const char *dbname);
|
||||
static HeapTuple GetDatabaseTupleByOid(Oid dboid);
|
||||
static void PerformAuthentication(Port *port);
|
||||
@@ -542,8 +538,9 @@ pg_split_opts(char **argv, int *argcp, const char *optstr)
|
||||
/*
|
||||
* Initialize MaxBackends value from config options.
|
||||
*
|
||||
* This must be called after modules have had the chance to alter GUCs in
|
||||
* shared_preload_libraries and before shared memory size is determined.
|
||||
* This must be called after modules have had the chance to register background
|
||||
* workers in shared_preload_libraries, and before shared memory size is
|
||||
* determined.
|
||||
*
|
||||
* Note that in EXEC_BACKEND environment, the value is passed down from
|
||||
* postmaster to subprocesses via BackendParameters in SubPostmasterMain; only
|
||||
@@ -553,49 +550,15 @@ pg_split_opts(char **argv, int *argcp, const char *optstr)
|
||||
void
|
||||
InitializeMaxBackends(void)
|
||||
{
|
||||
Assert(MaxBackends == 0);
|
||||
|
||||
/* the extra unit accounts for the autovacuum launcher */
|
||||
SetMaxBackends(MaxConnections + autovacuum_max_workers + 1 +
|
||||
max_worker_processes + max_wal_senders);
|
||||
}
|
||||
|
||||
/*
|
||||
* Safely retrieve the value of MaxBackends.
|
||||
*
|
||||
* Previously, MaxBackends was externally visible, but it was often used before
|
||||
* it was initialized (e.g., in preloaded libraries' _PG_init() functions).
|
||||
* Unfortunately, we cannot initialize MaxBackends before processing
|
||||
* shared_preload_libraries because the libraries sometimes alter GUCs that are
|
||||
* used to calculate its value. Instead, we provide this function for accessing
|
||||
* MaxBackends, and we ERROR if someone calls it before it is initialized.
|
||||
*/
|
||||
int
|
||||
GetMaxBackends(void)
|
||||
{
|
||||
if (unlikely(!MaxBackendsInitialized))
|
||||
elog(ERROR, "MaxBackends not yet initialized");
|
||||
|
||||
return MaxBackends;
|
||||
}
|
||||
|
||||
/*
|
||||
* Set the value of MaxBackends.
|
||||
*
|
||||
* This should only be used by InitializeMaxBackends() and
|
||||
* restore_backend_variables(). If MaxBackends is already initialized or the
|
||||
* specified value is greater than the maximum, this will ERROR.
|
||||
*/
|
||||
void
|
||||
SetMaxBackends(int max_backends)
|
||||
{
|
||||
if (MaxBackendsInitialized)
|
||||
elog(ERROR, "MaxBackends already initialized");
|
||||
MaxBackends = MaxConnections + autovacuum_max_workers + 1 +
|
||||
max_worker_processes + max_wal_senders;
|
||||
|
||||
/* internal error because the values were all checked previously */
|
||||
if (max_backends > MAX_BACKENDS)
|
||||
if (MaxBackends > MAX_BACKENDS)
|
||||
elog(ERROR, "too many backends configured");
|
||||
|
||||
MaxBackends = max_backends;
|
||||
MaxBackendsInitialized = true;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -707,7 +670,7 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
|
||||
|
||||
SharedInvalBackendInit(false);
|
||||
|
||||
if (MyBackendId > GetMaxBackends() || MyBackendId <= 0)
|
||||
if (MyBackendId > MaxBackends || MyBackendId <= 0)
|
||||
elog(FATAL, "bad backend ID: %d", MyBackendId);
|
||||
|
||||
/* Now that we have a BackendId, we can participate in ProcSignal */
|
||||
|
||||
Reference in New Issue
Block a user