@ -26,6 +26,18 @@
# include "utils/memutils.h"
# 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
* GUC parameters
* - - - - - - - - - -
* - - - - - - - - - -
@ -63,23 +75,8 @@ static MemoryContext backendStatusSnapContext;
static void pgstat_beshutdown_hook ( int code , Datum arg ) ;
static void pgstat_beshutdown_hook ( int code , Datum arg ) ;
static void pgstat_read_current_status ( void ) ;
static void pgstat_read_current_status ( void ) ;
static void pgstat_setup_backend_status_context ( 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 .
* Report shared - memory space needed by CreateSharedBackendStatus .
*/
*/
@ -87,28 +84,27 @@ Size
BackendStatusShmemSize ( void )
BackendStatusShmemSize ( void )
{
{
Size size ;
Size size ;
int numBackendStatSlots = GetNumBackendStatSlots ( ) ;
/* BackendStatusArray: */
/* BackendStatusArray: */
size = mul_size ( sizeof ( PgBackendStatus ) , n umBackendStatSlots) ;
size = mul_size ( sizeof ( PgBackendStatus ) , N umBackendStatSlots) ;
/* BackendAppnameBuffer: */
/* BackendAppnameBuffer: */
size = add_size ( size ,
size = add_size ( size ,
mul_size ( NAMEDATALEN , n umBackendStatSlots) ) ;
mul_size ( NAMEDATALEN , N umBackendStatSlots) ) ;
/* BackendClientHostnameBuffer: */
/* BackendClientHostnameBuffer: */
size = add_size ( size ,
size = add_size ( size ,
mul_size ( NAMEDATALEN , n umBackendStatSlots) ) ;
mul_size ( NAMEDATALEN , N umBackendStatSlots) ) ;
/* BackendActivityBuffer: */
/* BackendActivityBuffer: */
size = add_size ( size ,
size = add_size ( size ,
mul_size ( pgstat_track_activity_query_size , n umBackendStatSlots) ) ;
mul_size ( pgstat_track_activity_query_size , N umBackendStatSlots) ) ;
# ifdef USE_SSL
# ifdef USE_SSL
/* BackendSslStatusBuffer: */
/* BackendSslStatusBuffer: */
size = add_size ( size ,
size = add_size ( size ,
mul_size ( sizeof ( PgBackendSSLStatus ) , n umBackendStatSlots) ) ;
mul_size ( sizeof ( PgBackendSSLStatus ) , N umBackendStatSlots) ) ;
# endif
# endif
# ifdef ENABLE_GSS
# ifdef ENABLE_GSS
/* BackendGssStatusBuffer: */
/* BackendGssStatusBuffer: */
size = add_size ( size ,
size = add_size ( size ,
mul_size ( sizeof ( PgBackendGSSStatus ) , n umBackendStatSlots) ) ;
mul_size ( sizeof ( PgBackendGSSStatus ) , N umBackendStatSlots) ) ;
# endif
# endif
return size ;
return size ;
}
}
@ -124,10 +120,9 @@ CreateSharedBackendStatus(void)
bool found ;
bool found ;
int i ;
int i ;
char * buffer ;
char * buffer ;
int numBackendStatSlots = GetNumBackendStatSlots ( ) ;
/* Create or attach to the shared array */
/* Create or attach to the shared array */
size = mul_size ( sizeof ( PgBackendStatus ) , n umBackendStatSlots) ;
size = mul_size ( sizeof ( PgBackendStatus ) , N umBackendStatSlots) ;
BackendStatusArray = ( PgBackendStatus * )
BackendStatusArray = ( PgBackendStatus * )
ShmemInitStruct ( " Backend Status Array " , size , & found ) ;
ShmemInitStruct ( " Backend Status Array " , size , & found ) ;
@ -140,7 +135,7 @@ CreateSharedBackendStatus(void)
}
}
/* Create or attach to the shared appname buffer */
/* Create or attach to the shared appname buffer */
size = mul_size ( NAMEDATALEN , n umBackendStatSlots) ;
size = mul_size ( NAMEDATALEN , N umBackendStatSlots) ;
BackendAppnameBuffer = ( char * )
BackendAppnameBuffer = ( char * )
ShmemInitStruct ( " Backend Application Name Buffer " , size , & found ) ;
ShmemInitStruct ( " Backend Application Name Buffer " , size , & found ) ;
@ -150,7 +145,7 @@ CreateSharedBackendStatus(void)
/* Initialize st_appname pointers. */
/* Initialize st_appname pointers. */
buffer = BackendAppnameBuffer ;
buffer = BackendAppnameBuffer ;
for ( i = 0 ; i < n umBackendStatSlots; i + + )
for ( i = 0 ; i < N umBackendStatSlots; i + + )
{
{
BackendStatusArray [ i ] . st_appname = buffer ;
BackendStatusArray [ i ] . st_appname = buffer ;
buffer + = NAMEDATALEN ;
buffer + = NAMEDATALEN ;
@ -158,7 +153,7 @@ CreateSharedBackendStatus(void)
}
}
/* Create or attach to the shared client hostname buffer */
/* Create or attach to the shared client hostname buffer */
size = mul_size ( NAMEDATALEN , n umBackendStatSlots) ;
size = mul_size ( NAMEDATALEN , N umBackendStatSlots) ;
BackendClientHostnameBuffer = ( char * )
BackendClientHostnameBuffer = ( char * )
ShmemInitStruct ( " Backend Client Host Name Buffer " , size , & found ) ;
ShmemInitStruct ( " Backend Client Host Name Buffer " , size , & found ) ;
@ -168,7 +163,7 @@ CreateSharedBackendStatus(void)
/* Initialize st_clienthostname pointers. */
/* Initialize st_clienthostname pointers. */
buffer = BackendClientHostnameBuffer ;
buffer = BackendClientHostnameBuffer ;
for ( i = 0 ; i < n umBackendStatSlots; i + + )
for ( i = 0 ; i < N umBackendStatSlots; i + + )
{
{
BackendStatusArray [ i ] . st_clienthostname = buffer ;
BackendStatusArray [ i ] . st_clienthostname = buffer ;
buffer + = NAMEDATALEN ;
buffer + = NAMEDATALEN ;
@ -177,7 +172,7 @@ CreateSharedBackendStatus(void)
/* Create or attach to the shared activity buffer */
/* Create or attach to the shared activity buffer */
BackendActivityBufferSize = mul_size ( pgstat_track_activity_query_size ,
BackendActivityBufferSize = mul_size ( pgstat_track_activity_query_size ,
n umBackendStatSlots) ;
N umBackendStatSlots) ;
BackendActivityBuffer = ( char * )
BackendActivityBuffer = ( char * )
ShmemInitStruct ( " Backend Activity Buffer " ,
ShmemInitStruct ( " Backend Activity Buffer " ,
BackendActivityBufferSize ,
BackendActivityBufferSize ,
@ -189,7 +184,7 @@ CreateSharedBackendStatus(void)
/* Initialize st_activity pointers. */
/* Initialize st_activity pointers. */
buffer = BackendActivityBuffer ;
buffer = BackendActivityBuffer ;
for ( i = 0 ; i < n umBackendStatSlots; i + + )
for ( i = 0 ; i < N umBackendStatSlots; i + + )
{
{
BackendStatusArray [ i ] . st_activity_raw = buffer ;
BackendStatusArray [ i ] . st_activity_raw = buffer ;
buffer + = pgstat_track_activity_query_size ;
buffer + = pgstat_track_activity_query_size ;
@ -198,7 +193,7 @@ CreateSharedBackendStatus(void)
# ifdef USE_SSL
# ifdef USE_SSL
/* Create or attach to the shared SSL status buffer */
/* Create or attach to the shared SSL status buffer */
size = mul_size ( sizeof ( PgBackendSSLStatus ) , n umBackendStatSlots) ;
size = mul_size ( sizeof ( PgBackendSSLStatus ) , N umBackendStatSlots) ;
BackendSslStatusBuffer = ( PgBackendSSLStatus * )
BackendSslStatusBuffer = ( PgBackendSSLStatus * )
ShmemInitStruct ( " Backend SSL Status Buffer " , size , & found ) ;
ShmemInitStruct ( " Backend SSL Status Buffer " , size , & found ) ;
@ -210,7 +205,7 @@ CreateSharedBackendStatus(void)
/* Initialize st_sslstatus pointers. */
/* Initialize st_sslstatus pointers. */
ptr = BackendSslStatusBuffer ;
ptr = BackendSslStatusBuffer ;
for ( i = 0 ; i < n umBackendStatSlots; i + + )
for ( i = 0 ; i < N umBackendStatSlots; i + + )
{
{
BackendStatusArray [ i ] . st_sslstatus = ptr ;
BackendStatusArray [ i ] . st_sslstatus = ptr ;
ptr + + ;
ptr + + ;
@ -220,7 +215,7 @@ CreateSharedBackendStatus(void)
# ifdef ENABLE_GSS
# ifdef ENABLE_GSS
/* Create or attach to the shared GSSAPI status buffer */
/* Create or attach to the shared GSSAPI status buffer */
size = mul_size ( sizeof ( PgBackendGSSStatus ) , n umBackendStatSlots) ;
size = mul_size ( sizeof ( PgBackendGSSStatus ) , N umBackendStatSlots) ;
BackendGssStatusBuffer = ( PgBackendGSSStatus * )
BackendGssStatusBuffer = ( PgBackendGSSStatus * )
ShmemInitStruct ( " Backend GSS Status Buffer " , size , & found ) ;
ShmemInitStruct ( " Backend GSS Status Buffer " , size , & found ) ;
@ -232,7 +227,7 @@ CreateSharedBackendStatus(void)
/* Initialize st_gssstatus pointers. */
/* Initialize st_gssstatus pointers. */
ptr = BackendGssStatusBuffer ;
ptr = BackendGssStatusBuffer ;
for ( i = 0 ; i < n umBackendStatSlots; i + + )
for ( i = 0 ; i < N umBackendStatSlots; i + + )
{
{
BackendStatusArray [ i ] . st_gssstatus = ptr ;
BackendStatusArray [ i ] . st_gssstatus = ptr ;
ptr + + ;
ptr + + ;
@ -256,7 +251,7 @@ pgstat_beinit(void)
/* Initialize MyBEEntry */
/* Initialize MyBEEntry */
if ( MyBackendId ! = InvalidBackendId )
if ( MyBackendId ! = InvalidBackendId )
{
{
Assert ( MyBackendId > = 1 & & MyBackendId < = Get MaxBackends( ) ) ;
Assert ( MyBackendId > = 1 & & MyBackendId < = MaxBackends ) ;
MyBEEntry = & BackendStatusArray [ MyBackendId - 1 ] ;
MyBEEntry = & BackendStatusArray [ MyBackendId - 1 ] ;
}
}
else
else
@ -272,7 +267,7 @@ pgstat_beinit(void)
* MaxBackends + AuxBackendType + 1 as the index of the slot for an
* MaxBackends + AuxBackendType + 1 as the index of the slot for an
* auxiliary process .
* auxiliary process .
*/
*/
MyBEEntry = & BackendStatusArray [ Get MaxBackends( ) + MyAuxProcType ] ;
MyBEEntry = & BackendStatusArray [ MaxBackends + MyAuxProcType ] ;
}
}
/* Set up a process-exit hook to clean up */
/* Set up a process-exit hook to clean up */
@ -744,7 +739,6 @@ pgstat_read_current_status(void)
PgBackendGSSStatus * localgssstatus ;
PgBackendGSSStatus * localgssstatus ;
# endif
# endif
int i ;
int i ;
int numBackendStatSlots = GetNumBackendStatSlots ( ) ;
if ( localBackendStatusTable )
if ( localBackendStatusTable )
return ; /* already done */
return ; /* already done */
@ -761,32 +755,32 @@ pgstat_read_current_status(void)
*/
*/
localtable = ( LocalPgBackendStatus * )
localtable = ( LocalPgBackendStatus * )
MemoryContextAlloc ( backendStatusSnapContext ,
MemoryContextAlloc ( backendStatusSnapContext ,
sizeof ( LocalPgBackendStatus ) * n umBackendStatSlots) ;
sizeof ( LocalPgBackendStatus ) * N umBackendStatSlots) ;
localappname = ( char * )
localappname = ( char * )
MemoryContextAlloc ( backendStatusSnapContext ,
MemoryContextAlloc ( backendStatusSnapContext ,
NAMEDATALEN * n umBackendStatSlots) ;
NAMEDATALEN * N umBackendStatSlots) ;
localclienthostname = ( char * )
localclienthostname = ( char * )
MemoryContextAlloc ( backendStatusSnapContext ,
MemoryContextAlloc ( backendStatusSnapContext ,
NAMEDATALEN * n umBackendStatSlots) ;
NAMEDATALEN * N umBackendStatSlots) ;
localactivity = ( char * )
localactivity = ( char * )
MemoryContextAllocHuge ( backendStatusSnapContext ,
MemoryContextAllocHuge ( backendStatusSnapContext ,
pgstat_track_activity_query_size * n umBackendStatSlots) ;
pgstat_track_activity_query_size * N umBackendStatSlots) ;
# ifdef USE_SSL
# ifdef USE_SSL
localsslstatus = ( PgBackendSSLStatus * )
localsslstatus = ( PgBackendSSLStatus * )
MemoryContextAlloc ( backendStatusSnapContext ,
MemoryContextAlloc ( backendStatusSnapContext ,
sizeof ( PgBackendSSLStatus ) * n umBackendStatSlots) ;
sizeof ( PgBackendSSLStatus ) * N umBackendStatSlots) ;
# endif
# endif
# ifdef ENABLE_GSS
# ifdef ENABLE_GSS
localgssstatus = ( PgBackendGSSStatus * )
localgssstatus = ( PgBackendGSSStatus * )
MemoryContextAlloc ( backendStatusSnapContext ,
MemoryContextAlloc ( backendStatusSnapContext ,
sizeof ( PgBackendGSSStatus ) * n umBackendStatSlots) ;
sizeof ( PgBackendGSSStatus ) * N umBackendStatSlots) ;
# endif
# endif
localNumBackends = 0 ;
localNumBackends = 0 ;
beentry = BackendStatusArray ;
beentry = BackendStatusArray ;
localentry = localtable ;
localentry = localtable ;
for ( i = 1 ; i < = n umBackendStatSlots; i + + )
for ( i = 1 ; i < = N umBackendStatSlots; i + + )
{
{
/*
/*
* Follow the protocol of retrying if st_changecount changes while we
* 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 ;
PgBackendStatus * beentry ;
int i ;
int i ;
int max_backends = GetMaxBackends ( ) ;
beentry = BackendStatusArray ;
beentry = BackendStatusArray ;
for ( i = 1 ; i < = max_b ackends; i + + )
for ( i = 1 ; i < = MaxB ackends; i + + )
{
{
/*
/*
* Although we expect the target backend ' s entry to be stable , that
* 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 ;
volatile PgBackendStatus * beentry ;
int i ;
int i ;
int max_backends = GetMaxBackends ( ) ;
beentry = BackendStatusArray ;
beentry = BackendStatusArray ;
@ -989,7 +981,7 @@ pgstat_get_crashed_backend_activity(int pid, char *buffer, int buflen)
if ( beentry = = NULL | | BackendActivityBuffer = = NULL )
if ( beentry = = NULL | | BackendActivityBuffer = = NULL )
return NULL ;
return NULL ;
for ( i = 1 ; i < = max_b ackends; i + + )
for ( i = 1 ; i < = MaxB ackends; i + + )
{
{
if ( beentry - > st_procpid = = pid )
if ( beentry - > st_procpid = = pid )
{
{