Use MyBackendType in more places to check what process this is

Remove IsBackgroundWorker, IsAutoVacuumLauncherProcess(),
IsAutoVacuumWorkerProcess(), and IsLogicalSlotSyncWorker() in favor of
new Am*Process() macros that use MyBackendType. For consistency with
the existing Am*Process() macros.

Reviewed-by: Andres Freund
Discussion: https://www.postgresql.org/message-id/f3ecd4cb-85ee-4e54-8278-5fabfb3a4ed0@iki.fi
This commit is contained in:
Heikki Linnakangas 2024-03-04 10:25:12 +02:00
parent 067701f577
commit 393b5599e5
20 changed files with 42 additions and 91 deletions

View File

@ -812,7 +812,7 @@ ginInsertCleanup(GinState *ginstate, bool full_clean,
*/
LockPage(index, GIN_METAPAGE_BLKNO, ExclusiveLock);
workMemory =
(IsAutoVacuumWorkerProcess() && autovacuum_work_mem != -1) ?
(AmAutoVacuumWorkerProcess() && autovacuum_work_mem != -1) ?
autovacuum_work_mem : maintenance_work_mem;
}
else

View File

@ -590,7 +590,7 @@ ginbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
/*
* and cleanup any pending inserts
*/
ginInsertCleanup(&gvs.ginstate, !IsAutoVacuumWorkerProcess(),
ginInsertCleanup(&gvs.ginstate, !AmAutoVacuumWorkerProcess(),
false, true, stats);
}
@ -701,7 +701,7 @@ ginvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
*/
if (info->analyze_only)
{
if (IsAutoVacuumWorkerProcess())
if (AmAutoVacuumWorkerProcess())
{
initGinState(&ginstate, index);
ginInsertCleanup(&ginstate, false, true, true, stats);
@ -717,7 +717,7 @@ ginvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
{
stats = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult));
initGinState(&ginstate, index);
ginInsertCleanup(&ginstate, !IsAutoVacuumWorkerProcess(),
ginInsertCleanup(&ginstate, !AmAutoVacuumWorkerProcess(),
false, true, stats);
}

View File

@ -307,7 +307,7 @@ heap_vacuum_rel(Relation rel, VacuumParams *params,
char **indnames = NULL;
verbose = (params->options & VACOPT_VERBOSE) != 0;
instrument = (verbose || (IsAutoVacuumWorkerProcess() &&
instrument = (verbose || (AmAutoVacuumWorkerProcess() &&
params->log_min_duration >= 0));
if (instrument)
{
@ -3087,7 +3087,7 @@ static int
dead_items_max_items(LVRelState *vacrel)
{
int64 max_items;
int vac_work_mem = IsAutoVacuumWorkerProcess() &&
int vac_work_mem = AmAutoVacuumWorkerProcess() &&
autovacuum_work_mem != -1 ?
autovacuum_work_mem : maintenance_work_mem;

View File

@ -351,7 +351,7 @@ do_analyze_rel(Relation onerel, VacuumParams *params,
save_nestlevel = NewGUCNestLevel();
/* measure elapsed time iff autovacuum logging requires it */
if (IsAutoVacuumWorkerProcess() && params->log_min_duration >= 0)
if (AmAutoVacuumWorkerProcess() && params->log_min_duration >= 0)
{
if (track_io_timing)
{
@ -729,7 +729,7 @@ do_analyze_rel(Relation onerel, VacuumParams *params,
vac_close_indexes(nindexes, Irel, NoLock);
/* Log the action if appropriate */
if (IsAutoVacuumWorkerProcess() && params->log_min_duration >= 0)
if (AmAutoVacuumWorkerProcess() && params->log_min_duration >= 0)
{
TimestampTz endtime = GetCurrentTimestamp();

View File

@ -564,7 +564,7 @@ vacuum(List *relations, VacuumParams *params, BufferAccessStrategy bstrategy,
else
{
Assert(params->options & VACOPT_ANALYZE);
if (IsAutoVacuumWorkerProcess())
if (AmAutoVacuumWorkerProcess())
use_own_xacts = true;
else if (in_outer_xact)
use_own_xacts = false;
@ -809,7 +809,7 @@ vacuum_open_relation(Oid relid, RangeVar *relation, bits32 options,
* statements in the permission checks; otherwise, only log if the caller
* so requested.
*/
if (!IsAutoVacuumWorkerProcess())
if (!AmAutoVacuumWorkerProcess())
elevel = WARNING;
else if (verbose)
elevel = LOG;
@ -896,7 +896,7 @@ expand_vacuum_rel(VacuumRelation *vrel, MemoryContext vac_context,
* Since autovacuum workers supply OIDs when calling vacuum(), no
* autovacuum worker should reach this code.
*/
Assert(!IsAutoVacuumWorkerProcess());
Assert(!AmAutoVacuumWorkerProcess());
/*
* We transiently take AccessShareLock to protect the syscache lookup
@ -2336,7 +2336,7 @@ vacuum_delay_point(void)
* [autovacuum_]vacuum_cost_delay to take effect while a table is being
* vacuumed or analyzed.
*/
if (ConfigReloadPending && IsAutoVacuumWorkerProcess())
if (ConfigReloadPending && AmAutoVacuumWorkerProcess())
{
ConfigReloadPending = false;
ProcessConfigFile(PGC_SIGHUP);

View File

@ -136,10 +136,6 @@ int Log_autovacuum_min_duration = 600000;
#define MIN_AUTOVAC_SLEEPTIME 100.0 /* milliseconds */
#define MAX_AUTOVAC_SLEEPTIME 300 /* seconds */
/* Flags to tell if we are in an autovacuum process */
static bool am_autovacuum_launcher = false;
static bool am_autovacuum_worker = false;
/*
* Variables to save the cost-related storage parameters for the current
* relation being vacuumed by this autovacuum worker. Using these, we can
@ -436,8 +432,6 @@ AutoVacLauncherMain(int argc, char *argv[])
{
sigjmp_buf local_sigjmp_buf;
am_autovacuum_launcher = true;
MyBackendType = B_AUTOVAC_LAUNCHER;
init_ps_display(NULL);
@ -1491,8 +1485,6 @@ AutoVacWorkerMain(int argc, char *argv[])
sigjmp_buf local_sigjmp_buf;
Oid dbid;
am_autovacuum_worker = true;
MyBackendType = B_AUTOVAC_WORKER;
init_ps_display(NULL);
@ -3352,24 +3344,6 @@ autovac_init(void)
errhint("Enable the \"track_counts\" option.")));
}
/*
* IsAutoVacuum functions
* Return whether this is either a launcher autovacuum process or a worker
* process.
*/
bool
IsAutoVacuumLauncherProcess(void)
{
return am_autovacuum_launcher;
}
bool
IsAutoVacuumWorkerProcess(void)
{
return am_autovacuum_worker;
}
/*
* AutoVacuumShmemSize
* Compute space needed for autovacuum-related shared memory

View File

@ -731,8 +731,6 @@ BackgroundWorkerMain(void)
if (worker == NULL)
elog(FATAL, "unable to find bgworker entry");
IsBackgroundWorker = true;
MyBackendType = B_BG_WORKER;
init_ps_display(worker->bgw_name);

View File

@ -5000,9 +5000,6 @@ SubPostmasterMain(int argc, char *argv[])
}
if (strcmp(argv[1], "--forkbgworker") == 0)
{
/* do this as early as possible; in particular, before InitProcess() */
IsBackgroundWorker = true;
/* Restore basic shared memory pointers */
InitShmemAccess(UsedShmemSegAddr);

View File

@ -113,9 +113,6 @@ static long sleep_ms = MIN_SLOTSYNC_WORKER_NAPTIME_MS;
/* The restart interval for slot sync work used by postmaster */
#define SLOTSYNC_RESTART_INTERVAL_SEC 10
/* Flag to tell if we are in a slot sync worker process */
static bool am_slotsync_worker = false;
/*
* Flag to tell if we are syncing replication slots. Unlike the 'syncing' flag
* in SlotSyncCtxStruct, this flag is true only if the current process is
@ -491,7 +488,7 @@ synchronize_one_slot(RemoteSlot *remote_slot, Oid remote_dbid)
latestFlushPtr = GetStandbyFlushRecPtr(NULL);
if (remote_slot->confirmed_lsn > latestFlushPtr)
{
ereport(am_slotsync_worker ? LOG : ERROR,
ereport(AmLogicalSlotSyncWorkerProcess() ? LOG : ERROR,
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("skipping slot synchronization as the received slot sync"
" LSN %X/%X for slot \"%s\" is ahead of the standby position %X/%X",
@ -1114,8 +1111,6 @@ ReplSlotSyncWorkerMain(int argc, char *argv[])
sigjmp_buf local_sigjmp_buf;
StringInfoData app_name;
am_slotsync_worker = true;
MyBackendType = B_SLOTSYNC_WORKER;
init_ps_display(NULL);
@ -1438,15 +1433,6 @@ IsSyncingReplicationSlots(void)
return syncing_slots;
}
/*
* Is current process a slot sync worker?
*/
bool
IsLogicalSlotSyncWorker(void)
{
return am_slotsync_worker;
}
/*
* Amount of shared memory required for slot synchronization.
*/

View File

@ -173,7 +173,7 @@ BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows,
natts, vacattrstats);
if (!stats)
{
if (!IsAutoVacuumWorkerProcess())
if (!AmAutoVacuumWorkerProcess())
ereport(WARNING,
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
errmsg("statistics object \"%s.%s\" could not be computed for relation \"%s.%s\"",

View File

@ -136,7 +136,7 @@ proc_exit(int code)
*/
char gprofDirName[32];
if (IsAutoVacuumWorkerProcess())
if (AmAutoVacuumWorkerProcess())
snprintf(gprofDirName, 32, "gprof/avworker");
else
snprintf(gprofDirName, 32, "gprof/%d", (int) getpid());

View File

@ -42,7 +42,6 @@
#include "replication/slot.h"
#include "replication/slotsync.h"
#include "replication/syncrep.h"
#include "replication/walsender.h"
#include "storage/condition_variable.h"
#include "storage/ipc.h"
#include "storage/lmgr.h"
@ -310,11 +309,11 @@ InitProcess(void)
elog(ERROR, "you already exist");
/* Decide which list should supply our PGPROC. */
if (IsAnyAutoVacuumProcess())
if (AmAutoVacuumLauncherProcess() || AmAutoVacuumWorkerProcess())
procgloballist = &ProcGlobal->autovacFreeProcs;
else if (IsBackgroundWorker)
else if (AmBackgroundWorkerProcess())
procgloballist = &ProcGlobal->bgworkerFreeProcs;
else if (am_walsender)
else if (AmWalSenderProcess())
procgloballist = &ProcGlobal->walsenderFreeProcs;
else
procgloballist = &ProcGlobal->freeProcs;
@ -344,7 +343,7 @@ InitProcess(void)
* in the autovacuum case?
*/
SpinLockRelease(ProcStructLock);
if (am_walsender)
if (AmWalSenderProcess())
ereport(FATAL,
(errcode(ERRCODE_TOO_MANY_CONNECTIONS),
errmsg("number of requested standby connections exceeds max_wal_senders (currently %d)",
@ -370,8 +369,8 @@ InitProcess(void)
* Slot sync worker also does not participate in it, see comments atop
* 'struct bkend' in postmaster.c.
*/
if (IsUnderPostmaster && !IsAutoVacuumLauncherProcess() &&
!IsLogicalSlotSyncWorker())
if (IsUnderPostmaster && !AmAutoVacuumLauncherProcess() &&
!AmLogicalSlotSyncWorkerProcess())
MarkPostmasterChildActive();
/*
@ -391,11 +390,11 @@ InitProcess(void)
MyProc->databaseId = InvalidOid;
MyProc->roleId = InvalidOid;
MyProc->tempNamespaceId = InvalidOid;
MyProc->isBackgroundWorker = IsBackgroundWorker;
MyProc->isBackgroundWorker = AmBackgroundWorkerProcess();
MyProc->delayChkptFlags = 0;
MyProc->statusFlags = 0;
/* NB -- autovac launcher intentionally does not set IS_AUTOVACUUM */
if (IsAutoVacuumWorkerProcess())
if (AmAutoVacuumWorkerProcess())
MyProc->statusFlags |= PROC_IS_AUTOVACUUM;
MyProc->lwWaiting = LW_WS_NOT_WAITING;
MyProc->lwWaitMode = 0;
@ -587,7 +586,7 @@ InitAuxiliaryProcess(void)
MyProc->databaseId = InvalidOid;
MyProc->roleId = InvalidOid;
MyProc->tempNamespaceId = InvalidOid;
MyProc->isBackgroundWorker = IsBackgroundWorker;
MyProc->isBackgroundWorker = AmBackgroundWorkerProcess();
MyProc->delayChkptFlags = 0;
MyProc->statusFlags = 0;
MyProc->lwWaiting = LW_WS_NOT_WAITING;
@ -951,8 +950,8 @@ ProcKill(int code, Datum arg)
* Slot sync worker is also not a postmaster child, so skip this shared
* memory related processing here.
*/
if (IsUnderPostmaster && !IsAutoVacuumLauncherProcess() &&
!IsLogicalSlotSyncWorker())
if (IsUnderPostmaster && !AmAutoVacuumLauncherProcess() &&
!AmLogicalSlotSyncWorkerProcess())
MarkPostmasterChildInactive();
/* wake autovac launcher if needed -- see comments in FreeWorkerInfo */

View File

@ -3259,7 +3259,7 @@ ProcessInterrupts(void)
ereport(FATAL,
(errcode(ERRCODE_QUERY_CANCELED),
errmsg("canceling authentication due to timeout")));
else if (IsAutoVacuumWorkerProcess())
else if (AmAutoVacuumWorkerProcess())
ereport(FATAL,
(errcode(ERRCODE_ADMIN_SHUTDOWN),
errmsg("terminating autovacuum process due to administrator command")));
@ -3278,7 +3278,7 @@ ProcessInterrupts(void)
*/
proc_exit(1);
}
else if (IsBackgroundWorker)
else if (AmBackgroundWorkerProcess())
ereport(FATAL,
(errcode(ERRCODE_ADMIN_SHUTDOWN),
errmsg("terminating background worker \"%s\" due to administrator command",
@ -3378,7 +3378,7 @@ ProcessInterrupts(void)
(errcode(ERRCODE_QUERY_CANCELED),
errmsg("canceling statement due to statement timeout")));
}
if (IsAutoVacuumWorkerProcess())
if (AmAutoVacuumWorkerProcess())
{
LockErrorCleanup();
ereport(ERROR,

View File

@ -246,7 +246,7 @@ pgstat_report_vacuum(Oid tableoid, bool shared,
*/
tabentry->ins_since_vacuum = 0;
if (IsAutoVacuumWorkerProcess())
if (AmAutoVacuumWorkerProcess())
{
tabentry->last_autovacuum_time = ts;
tabentry->autovacuum_count++;
@ -337,7 +337,7 @@ pgstat_report_analyze(Relation rel,
if (resetcounter)
tabentry->mod_since_analyze = 0;
if (IsAutoVacuumWorkerProcess())
if (AmAutoVacuumWorkerProcess())
{
tabentry->last_autoanalyze_time = GetCurrentTimestamp();
tabentry->autoanalyze_count++;

View File

@ -115,7 +115,6 @@ pid_t PostmasterPid = 0;
bool IsPostmasterEnvironment = false;
bool IsUnderPostmaster = false;
bool IsBinaryUpgrade = false;
bool IsBackgroundWorker = false;
bool ExitOnAnyError = false;

View File

@ -841,8 +841,8 @@ InitializeSessionUserIdStandalone(void)
* This function should only be called in single-user mode, in autovacuum
* workers, in slot sync worker and in background workers.
*/
Assert(!IsUnderPostmaster || IsAutoVacuumWorkerProcess() ||
IsLogicalSlotSyncWorker() || IsBackgroundWorker);
Assert(!IsUnderPostmaster || AmAutoVacuumWorkerProcess() ||
AmLogicalSlotSyncWorkerProcess() || AmBackgroundWorkerProcess());
/* call only once */
Assert(!OidIsValid(AuthenticatedUserId));

View File

@ -346,7 +346,7 @@ CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connect
*
* We do not enforce them for autovacuum worker processes either.
*/
if (IsUnderPostmaster && !IsAutoVacuumWorkerProcess())
if (IsUnderPostmaster && !AmAutoVacuumWorkerProcess())
{
/*
* Check that the database is currently allowing connections.
@ -828,7 +828,7 @@ InitPostgres(const char *in_dbname, Oid dboid,
before_shmem_exit(ShutdownPostgres, 0);
/* The autovacuum launcher is done here */
if (IsAutoVacuumLauncherProcess())
if (AmAutoVacuumLauncherProcess())
{
/* report this backend in the PgBackendStatus array */
pgstat_bestart();
@ -873,7 +873,7 @@ InitPostgres(const char *in_dbname, Oid dboid,
* process, we use a fixed ID, otherwise we figure it out from the
* authenticated user name.
*/
if (bootstrap || IsAutoVacuumWorkerProcess() || IsLogicalSlotSyncWorker())
if (bootstrap || AmAutoVacuumWorkerProcess() || AmLogicalSlotSyncWorkerProcess())
{
InitializeSessionUserIdStandalone();
am_superuser = true;
@ -889,7 +889,7 @@ InitPostgres(const char *in_dbname, Oid dboid,
errhint("You should immediately run CREATE USER \"%s\" SUPERUSER;.",
username != NULL ? username : "postgres")));
}
else if (IsBackgroundWorker)
else if (AmBackgroundWorkerProcess())
{
if (username == NULL && !OidIsValid(useroid))
{

View File

@ -165,7 +165,6 @@ do { \
extern PGDLLIMPORT pid_t PostmasterPid;
extern PGDLLIMPORT bool IsPostmasterEnvironment;
extern PGDLLIMPORT bool IsUnderPostmaster;
extern PGDLLIMPORT bool IsBackgroundWorker;
extern PGDLLIMPORT bool IsBinaryUpgrade;
extern PGDLLIMPORT bool ExitOnAnyError;
@ -369,6 +368,11 @@ typedef enum BackendType
extern PGDLLIMPORT BackendType MyBackendType;
#define AmAutoVacuumLauncherProcess() (MyBackendType == B_AUTOVAC_LAUNCHER)
#define AmAutoVacuumWorkerProcess() (MyBackendType == B_AUTOVAC_WORKER)
#define AmBackgroundWorkerProcess() (MyBackendType == B_BG_WORKER)
#define AmWalSenderProcess() (MyBackendType == B_WAL_SENDER)
#define AmLogicalSlotSyncWorkerProcess() (MyBackendType == B_SLOTSYNC_WORKER)
#define AmArchiverProcess() (MyBackendType == B_ARCHIVER)
#define AmBackgroundWriterProcess() (MyBackendType == B_BG_WRITER)
#define AmCheckpointerProcess() (MyBackendType == B_CHECKPOINTER)

View File

@ -49,11 +49,6 @@ extern PGDLLIMPORT int Log_autovacuum_min_duration;
/* Status inquiry functions */
extern bool AutoVacuumingActive(void);
extern bool IsAutoVacuumLauncherProcess(void);
extern bool IsAutoVacuumWorkerProcess(void);
#define IsAnyAutoVacuumProcess() \
(IsAutoVacuumLauncherProcess() || IsAutoVacuumWorkerProcess())
/* Functions to start autovacuum process, called from postmaster */
extern void autovac_init(void);

View File

@ -34,7 +34,6 @@ extern int StartSlotSyncWorker(void);
extern void ShutDownSlotSync(void);
extern bool SlotSyncWorkerCanRestart(void);
extern bool IsSyncingReplicationSlots(void);
extern bool IsLogicalSlotSyncWorker(void);
extern Size SlotSyncShmemSize(void);
extern void SlotSyncShmemInit(void);
extern void SyncReplicationSlots(WalReceiverConn *wrconn);