Commit 8e8a0bec authored by Peter Eisentraut's avatar Peter Eisentraut

Unify several ways to tracking backend type

Add a new global variable MyBackendType that uses the same BackendType
enum that was previously only used by the stats collector.  That way
several duplicate ways of checking what type a particular process is
can be simplified.  Since it's no longer just for stats, move to
miscinit.c and rename existing functions to match the expanded
purpose.
Reviewed-by: default avatarJulien Rouhaud <rjuju123@gmail.com>
Reviewed-by: default avatarKuntal Ghosh <kuntalghosh.2007@gmail.com>
Reviewed-by: default avatarAlvaro Herrera <alvherre@2ndquadrant.com>
Discussion: https://www.postgresql.org/message-id/flat/c65e5196-4f04-4ead-9353-6088c19615a3@2ndquadrant.com
parent 1cc9c241
...@@ -314,36 +314,28 @@ AuxiliaryProcessMain(int argc, char *argv[]) ...@@ -314,36 +314,28 @@ AuxiliaryProcessMain(int argc, char *argv[])
proc_exit(1); proc_exit(1);
} }
/*
* Identify myself via ps
*/
if (IsUnderPostmaster)
{
const char *statmsg;
switch (MyAuxProcType) switch (MyAuxProcType)
{ {
case StartupProcess: case StartupProcess:
statmsg = pgstat_get_backend_desc(B_STARTUP); MyBackendType = B_STARTUP;
break; break;
case BgWriterProcess: case BgWriterProcess:
statmsg = pgstat_get_backend_desc(B_BG_WRITER); MyBackendType = B_BG_WRITER;
break; break;
case CheckpointerProcess: case CheckpointerProcess:
statmsg = pgstat_get_backend_desc(B_CHECKPOINTER); MyBackendType = B_CHECKPOINTER;
break; break;
case WalWriterProcess: case WalWriterProcess:
statmsg = pgstat_get_backend_desc(B_WAL_WRITER); MyBackendType = B_WAL_WRITER;
break; break;
case WalReceiverProcess: case WalReceiverProcess:
statmsg = pgstat_get_backend_desc(B_WAL_RECEIVER); MyBackendType = B_WAL_RECEIVER;
break; break;
default: default:
statmsg = "??? process"; MyBackendType = B_INVALID;
break;
}
init_ps_display(statmsg);
} }
if (IsUnderPostmaster)
init_ps_display(NULL);
/* Acquire configuration parameters, unless inherited from postmaster */ /* Acquire configuration parameters, unless inherited from postmaster */
if (!IsUnderPostmaster) if (!IsUnderPostmaster)
......
...@@ -433,8 +433,8 @@ AutoVacLauncherMain(int argc, char *argv[]) ...@@ -433,8 +433,8 @@ AutoVacLauncherMain(int argc, char *argv[])
am_autovacuum_launcher = true; am_autovacuum_launcher = true;
/* Identify myself via ps */ MyBackendType = B_AUTOVAC_LAUNCHER;
init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_LAUNCHER)); init_ps_display(NULL);
ereport(DEBUG1, ereport(DEBUG1,
(errmsg("autovacuum launcher started"))); (errmsg("autovacuum launcher started")));
...@@ -1506,8 +1506,8 @@ AutoVacWorkerMain(int argc, char *argv[]) ...@@ -1506,8 +1506,8 @@ AutoVacWorkerMain(int argc, char *argv[])
am_autovacuum_worker = true; am_autovacuum_worker = true;
/* Identify myself via ps */ MyBackendType = B_AUTOVAC_WORKER;
init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_WORKER)); init_ps_display(NULL);
SetProcessingMode(InitProcessing); SetProcessingMode(InitProcessing);
......
...@@ -688,7 +688,7 @@ StartBackgroundWorker(void) ...@@ -688,7 +688,7 @@ StartBackgroundWorker(void)
IsBackgroundWorker = true; IsBackgroundWorker = true;
/* Identify myself via ps */ MyBackendType = B_BG_WORKER;
init_ps_display(worker->bgw_name); init_ps_display(worker->bgw_name);
/* /*
......
...@@ -238,10 +238,8 @@ PgArchiverMain(int argc, char *argv[]) ...@@ -238,10 +238,8 @@ PgArchiverMain(int argc, char *argv[])
pqsignal(SIGCHLD, SIG_DFL); pqsignal(SIGCHLD, SIG_DFL);
PG_SETMASK(&UnBlockSig); PG_SETMASK(&UnBlockSig);
/* MyBackendType = B_ARCHIVER;
* Identify myself via ps init_ps_display(NULL);
*/
init_ps_display("archiver");
pgarch_MainLoop(); pgarch_MainLoop();
......
...@@ -2892,62 +2892,7 @@ pgstat_bestart(void) ...@@ -2892,62 +2892,7 @@ pgstat_bestart(void)
* out-of-line data. Those have to be handled separately, below. * out-of-line data. Those have to be handled separately, below.
*/ */
lbeentry.st_procpid = MyProcPid; lbeentry.st_procpid = MyProcPid;
lbeentry.st_backendType = MyBackendType;
if (MyBackendId != InvalidBackendId)
{
if (IsAutoVacuumLauncherProcess())
{
/* Autovacuum Launcher */
lbeentry.st_backendType = B_AUTOVAC_LAUNCHER;
}
else if (IsAutoVacuumWorkerProcess())
{
/* Autovacuum Worker */
lbeentry.st_backendType = B_AUTOVAC_WORKER;
}
else if (am_walsender)
{
/* Wal sender */
lbeentry.st_backendType = B_WAL_SENDER;
}
else if (IsBackgroundWorker)
{
/* bgworker */
lbeentry.st_backendType = B_BG_WORKER;
}
else
{
/* client-backend */
lbeentry.st_backendType = B_BACKEND;
}
}
else
{
/* Must be an auxiliary process */
Assert(MyAuxProcType != NotAnAuxProcess);
switch (MyAuxProcType)
{
case StartupProcess:
lbeentry.st_backendType = B_STARTUP;
break;
case BgWriterProcess:
lbeentry.st_backendType = B_BG_WRITER;
break;
case CheckpointerProcess:
lbeentry.st_backendType = B_CHECKPOINTER;
break;
case WalWriterProcess:
lbeentry.st_backendType = B_WAL_WRITER;
break;
case WalReceiverProcess:
lbeentry.st_backendType = B_WAL_RECEIVER;
break;
default:
elog(FATAL, "unrecognized process type: %d",
(int) MyAuxProcType);
}
}
lbeentry.st_proc_start_timestamp = MyStartTimestamp; lbeentry.st_proc_start_timestamp = MyStartTimestamp;
lbeentry.st_activity_start_timestamp = 0; lbeentry.st_activity_start_timestamp = 0;
lbeentry.st_state_start_timestamp = 0; lbeentry.st_state_start_timestamp = 0;
...@@ -4269,48 +4214,6 @@ pgstat_get_crashed_backend_activity(int pid, char *buffer, int buflen) ...@@ -4269,48 +4214,6 @@ pgstat_get_crashed_backend_activity(int pid, char *buffer, int buflen)
return NULL; return NULL;
} }
const char *
pgstat_get_backend_desc(BackendType backendType)
{
const char *backendDesc = "unknown process type";
switch (backendType)
{
case B_AUTOVAC_LAUNCHER:
backendDesc = "autovacuum launcher";
break;
case B_AUTOVAC_WORKER:
backendDesc = "autovacuum worker";
break;
case B_BACKEND:
backendDesc = "client backend";
break;
case B_BG_WORKER:
backendDesc = "background worker";
break;
case B_BG_WRITER:
backendDesc = "background writer";
break;
case B_CHECKPOINTER:
backendDesc = "checkpointer";
break;
case B_STARTUP:
backendDesc = "startup";
break;
case B_WAL_RECEIVER:
backendDesc = "walreceiver";
break;
case B_WAL_SENDER:
backendDesc = "walsender";
break;
case B_WAL_WRITER:
backendDesc = "walwriter";
break;
}
return backendDesc;
}
/* ------------------------------------------------------------ /* ------------------------------------------------------------
* Local support functions follow * Local support functions follow
* ------------------------------------------------------------ * ------------------------------------------------------------
...@@ -4447,10 +4350,8 @@ PgstatCollectorMain(int argc, char *argv[]) ...@@ -4447,10 +4350,8 @@ PgstatCollectorMain(int argc, char *argv[])
pqsignal(SIGCHLD, SIG_DFL); pqsignal(SIGCHLD, SIG_DFL);
PG_SETMASK(&UnBlockSig); PG_SETMASK(&UnBlockSig);
/* MyBackendType = B_STATS_COLLECTOR;
* Identify myself via ps init_ps_display(NULL);
*/
init_ps_display("stats collector");
/* /*
* Read in existing stats files or initialize the stats to zero. * Read in existing stats files or initialize the stats to zero.
......
...@@ -2259,6 +2259,11 @@ retry1: ...@@ -2259,6 +2259,11 @@ retry1:
if (strlen(port->user_name) >= NAMEDATALEN) if (strlen(port->user_name) >= NAMEDATALEN)
port->user_name[NAMEDATALEN - 1] = '\0'; port->user_name[NAMEDATALEN - 1] = '\0';
if (am_walsender)
MyBackendType = B_WAL_SENDER;
else
MyBackendType = B_BACKEND;
/* /*
* Normal walsender backends, e.g. for streaming replication, are not * Normal walsender backends, e.g. for streaming replication, are not
* connected to a particular database. But walsenders used for logical * connected to a particular database. But walsenders used for logical
...@@ -4422,7 +4427,7 @@ BackendInitialize(Port *port) ...@@ -4422,7 +4427,7 @@ BackendInitialize(Port *port)
*/ */
initStringInfo(&ps_data); initStringInfo(&ps_data);
if (am_walsender) if (am_walsender)
appendStringInfo(&ps_data, "%s ", pgstat_get_backend_desc(B_WAL_SENDER)); appendStringInfo(&ps_data, "%s ", GetBackendTypeDesc(B_WAL_SENDER));
appendStringInfo(&ps_data, "%s ", port->user_name); appendStringInfo(&ps_data, "%s ", port->user_name);
if (!am_walsender) if (!am_walsender)
appendStringInfo(&ps_data, "%s ", port->database_name); appendStringInfo(&ps_data, "%s ", port->database_name);
......
...@@ -179,7 +179,8 @@ SysLoggerMain(int argc, char *argv[]) ...@@ -179,7 +179,8 @@ SysLoggerMain(int argc, char *argv[])
am_syslogger = true; am_syslogger = true;
init_ps_display("logger"); MyBackendType = B_LOGGER;
init_ps_display(NULL);
/* /*
* If we restarted, our stderr is already redirected into our own input * If we restarted, our stderr is already redirected into our own input
......
...@@ -841,7 +841,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS) ...@@ -841,7 +841,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
} }
else else
values[17] = values[17] =
CStringGetTextDatum(pgstat_get_backend_desc(beentry->st_backendType)); CStringGetTextDatum(GetBackendTypeDesc(beentry->st_backendType));
/* SSL information */ /* SSL information */
if (beentry->st_ssl) if (beentry->st_ssl)
......
...@@ -57,6 +57,8 @@ ...@@ -57,6 +57,8 @@
ProcessingMode Mode = InitProcessing; ProcessingMode Mode = InitProcessing;
BackendType MyBackendType;
/* List of lock files to be removed at proc exit */ /* List of lock files to be removed at proc exit */
static List *lock_files = NIL; static List *lock_files = NIL;
...@@ -187,6 +189,59 @@ SwitchBackToLocalLatch(void) ...@@ -187,6 +189,59 @@ SwitchBackToLocalLatch(void)
SetLatch(MyLatch); SetLatch(MyLatch);
} }
const char *
GetBackendTypeDesc(BackendType backendType)
{
const char *backendDesc = "unknown process type";
switch (backendType)
{
case B_INVALID:
backendDesc = "not initialized";
break;
case B_AUTOVAC_LAUNCHER:
backendDesc = "autovacuum launcher";
break;
case B_AUTOVAC_WORKER:
backendDesc = "autovacuum worker";
break;
case B_BACKEND:
backendDesc = "client backend";
break;
case B_BG_WORKER:
backendDesc = "background worker";
break;
case B_BG_WRITER:
backendDesc = "background writer";
break;
case B_CHECKPOINTER:
backendDesc = "checkpointer";
break;
case B_STARTUP:
backendDesc = "startup";
break;
case B_WAL_RECEIVER:
backendDesc = "walreceiver";
break;
case B_WAL_SENDER:
backendDesc = "walsender";
break;
case B_WAL_WRITER:
backendDesc = "walwriter";
break;
case B_ARCHIVER:
backendDesc = "archiver";
break;
case B_STATS_COLLECTOR:
backendDesc = "stats collector";
break;
case B_LOGGER:
backendDesc = "logger";
break;
}
return backendDesc;
}
/* ---------------------------------------------------------------- /* ----------------------------------------------------------------
* database path / name support stuff * database path / name support stuff
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include "libpq/libpq.h" #include "libpq/libpq.h"
#include "miscadmin.h" #include "miscadmin.h"
#include "pgstat.h"
#include "utils/guc.h" #include "utils/guc.h"
#include "utils/ps_status.h" #include "utils/ps_status.h"
...@@ -247,14 +248,20 @@ save_ps_display_args(int argc, char **argv) ...@@ -247,14 +248,20 @@ save_ps_display_args(int argc, char **argv)
/* /*
* Call this once during subprocess startup to set the identification * Call this once during subprocess startup to set the identification
* values. At this point, the original argv[] array may be overwritten. * values.
*
* If fixed_part is NULL, a default will be obtained from MyBackendType.
*
* At this point, the original argv[] array may be overwritten.
*/ */
void void
init_ps_display(const char *fixed_part) init_ps_display(const char *fixed_part)
{ {
bool save_update_process_title; bool save_update_process_title;
Assert(fixed_part); Assert(fixed_part || MyBackendType);
if (!fixed_part)
fixed_part = GetBackendTypeDesc(MyBackendType);
#ifndef PS_USE_NONE #ifndef PS_USE_NONE
/* no ps display for stand-alone backend */ /* no ps display for stand-alone backend */
......
...@@ -306,6 +306,28 @@ extern void InitStandaloneProcess(const char *argv0); ...@@ -306,6 +306,28 @@ extern void InitStandaloneProcess(const char *argv0);
extern void SwitchToSharedLatch(void); extern void SwitchToSharedLatch(void);
extern void SwitchBackToLocalLatch(void); extern void SwitchBackToLocalLatch(void);
typedef enum BackendType
{
B_INVALID = 0,
B_AUTOVAC_LAUNCHER,
B_AUTOVAC_WORKER,
B_BACKEND,
B_BG_WORKER,
B_BG_WRITER,
B_CHECKPOINTER,
B_STARTUP,
B_WAL_RECEIVER,
B_WAL_SENDER,
B_WAL_WRITER,
B_ARCHIVER,
B_STATS_COLLECTOR,
B_LOGGER,
} BackendType;
extern BackendType MyBackendType;
extern const char *GetBackendTypeDesc(BackendType backendType);
extern void SetDatabasePath(const char *path); extern void SetDatabasePath(const char *path);
extern void checkDataDir(void); extern void checkDataDir(void);
extern void SetDataDir(const char *dir); extern void SetDataDir(const char *dir);
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "datatype/timestamp.h" #include "datatype/timestamp.h"
#include "libpq/pqcomm.h" #include "libpq/pqcomm.h"
#include "miscadmin.h"
#include "port/atomics.h" #include "port/atomics.h"
#include "portability/instr_time.h" #include "portability/instr_time.h"
#include "postmaster/pgarch.h" #include "postmaster/pgarch.h"
...@@ -712,25 +713,6 @@ typedef struct PgStat_GlobalStats ...@@ -712,25 +713,6 @@ typedef struct PgStat_GlobalStats
} PgStat_GlobalStats; } PgStat_GlobalStats;
/* ----------
* Backend types
* ----------
*/
typedef enum BackendType
{
B_AUTOVAC_LAUNCHER,
B_AUTOVAC_WORKER,
B_BACKEND,
B_BG_WORKER,
B_BG_WRITER,
B_CHECKPOINTER,
B_STARTUP,
B_WAL_RECEIVER,
B_WAL_SENDER,
B_WAL_WRITER
} BackendType;
/* ---------- /* ----------
* Backend states * Backend states
* ---------- * ----------
...@@ -1286,7 +1268,6 @@ extern const char *pgstat_get_wait_event_type(uint32 wait_event_info); ...@@ -1286,7 +1268,6 @@ extern const char *pgstat_get_wait_event_type(uint32 wait_event_info);
extern const char *pgstat_get_backend_current_activity(int pid, bool checkUser); extern const char *pgstat_get_backend_current_activity(int pid, bool checkUser);
extern const char *pgstat_get_crashed_backend_activity(int pid, char *buffer, extern const char *pgstat_get_crashed_backend_activity(int pid, char *buffer,
int buflen); int buflen);
extern const char *pgstat_get_backend_desc(BackendType backendType);
extern void pgstat_progress_start_command(ProgressCommandType cmdtype, extern void pgstat_progress_start_command(ProgressCommandType cmdtype,
Oid relid); Oid relid);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment