Skip to content

Commit 8e8a0be

Browse files
committed
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: Julien Rouhaud <rjuju123@gmail.com> Reviewed-by: Kuntal Ghosh <kuntalghosh.2007@gmail.com> Reviewed-by: Alvaro Herrera <alvherre@2ndquadrant.com> Discussion: https://www.postgresql.org/message-id/flat/c65e5196-4f04-4ead-9353-6088c19615a3@2ndquadrant.com
1 parent 1cc9c24 commit 8e8a0be

File tree

12 files changed

+126
-164
lines changed

12 files changed

+126
-164
lines changed

src/backend/bootstrap/bootstrap.c

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -314,36 +314,28 @@ AuxiliaryProcessMain(int argc, char *argv[])
314314
proc_exit(1);
315315
}
316316

317-
/*
318-
* Identify myself via ps
319-
*/
320-
if (IsUnderPostmaster)
317+
switch (MyAuxProcType)
321318
{
322-
const char *statmsg;
323-
324-
switch (MyAuxProcType)
325-
{
326-
case StartupProcess:
327-
statmsg = pgstat_get_backend_desc(B_STARTUP);
328-
break;
329-
case BgWriterProcess:
330-
statmsg = pgstat_get_backend_desc(B_BG_WRITER);
331-
break;
332-
case CheckpointerProcess:
333-
statmsg = pgstat_get_backend_desc(B_CHECKPOINTER);
334-
break;
335-
case WalWriterProcess:
336-
statmsg = pgstat_get_backend_desc(B_WAL_WRITER);
337-
break;
338-
case WalReceiverProcess:
339-
statmsg = pgstat_get_backend_desc(B_WAL_RECEIVER);
340-
break;
341-
default:
342-
statmsg = "??? process";
343-
break;
344-
}
345-
init_ps_display(statmsg);
319+
case StartupProcess:
320+
MyBackendType = B_STARTUP;
321+
break;
322+
case BgWriterProcess:
323+
MyBackendType = B_BG_WRITER;
324+
break;
325+
case CheckpointerProcess:
326+
MyBackendType = B_CHECKPOINTER;
327+
break;
328+
case WalWriterProcess:
329+
MyBackendType = B_WAL_WRITER;
330+
break;
331+
case WalReceiverProcess:
332+
MyBackendType = B_WAL_RECEIVER;
333+
break;
334+
default:
335+
MyBackendType = B_INVALID;
346336
}
337+
if (IsUnderPostmaster)
338+
init_ps_display(NULL);
347339

348340
/* Acquire configuration parameters, unless inherited from postmaster */
349341
if (!IsUnderPostmaster)

src/backend/postmaster/autovacuum.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,8 @@ AutoVacLauncherMain(int argc, char *argv[])
433433

434434
am_autovacuum_launcher = true;
435435

436-
/* Identify myself via ps */
437-
init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_LAUNCHER));
436+
MyBackendType = B_AUTOVAC_LAUNCHER;
437+
init_ps_display(NULL);
438438

439439
ereport(DEBUG1,
440440
(errmsg("autovacuum launcher started")));
@@ -1506,8 +1506,8 @@ AutoVacWorkerMain(int argc, char *argv[])
15061506

15071507
am_autovacuum_worker = true;
15081508

1509-
/* Identify myself via ps */
1510-
init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_WORKER));
1509+
MyBackendType = B_AUTOVAC_WORKER;
1510+
init_ps_display(NULL);
15111511

15121512
SetProcessingMode(InitProcessing);
15131513

src/backend/postmaster/bgworker.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ StartBackgroundWorker(void)
688688

689689
IsBackgroundWorker = true;
690690

691-
/* Identify myself via ps */
691+
MyBackendType = B_BG_WORKER;
692692
init_ps_display(worker->bgw_name);
693693

694694
/*

src/backend/postmaster/pgarch.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,8 @@ PgArchiverMain(int argc, char *argv[])
238238
pqsignal(SIGCHLD, SIG_DFL);
239239
PG_SETMASK(&UnBlockSig);
240240

241-
/*
242-
* Identify myself via ps
243-
*/
244-
init_ps_display("archiver");
241+
MyBackendType = B_ARCHIVER;
242+
init_ps_display(NULL);
245243

246244
pgarch_MainLoop();
247245

src/backend/postmaster/pgstat.c

Lines changed: 3 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -2892,62 +2892,7 @@ pgstat_bestart(void)
28922892
* out-of-line data. Those have to be handled separately, below.
28932893
*/
28942894
lbeentry.st_procpid = MyProcPid;
2895-
2896-
if (MyBackendId != InvalidBackendId)
2897-
{
2898-
if (IsAutoVacuumLauncherProcess())
2899-
{
2900-
/* Autovacuum Launcher */
2901-
lbeentry.st_backendType = B_AUTOVAC_LAUNCHER;
2902-
}
2903-
else if (IsAutoVacuumWorkerProcess())
2904-
{
2905-
/* Autovacuum Worker */
2906-
lbeentry.st_backendType = B_AUTOVAC_WORKER;
2907-
}
2908-
else if (am_walsender)
2909-
{
2910-
/* Wal sender */
2911-
lbeentry.st_backendType = B_WAL_SENDER;
2912-
}
2913-
else if (IsBackgroundWorker)
2914-
{
2915-
/* bgworker */
2916-
lbeentry.st_backendType = B_BG_WORKER;
2917-
}
2918-
else
2919-
{
2920-
/* client-backend */
2921-
lbeentry.st_backendType = B_BACKEND;
2922-
}
2923-
}
2924-
else
2925-
{
2926-
/* Must be an auxiliary process */
2927-
Assert(MyAuxProcType != NotAnAuxProcess);
2928-
switch (MyAuxProcType)
2929-
{
2930-
case StartupProcess:
2931-
lbeentry.st_backendType = B_STARTUP;
2932-
break;
2933-
case BgWriterProcess:
2934-
lbeentry.st_backendType = B_BG_WRITER;
2935-
break;
2936-
case CheckpointerProcess:
2937-
lbeentry.st_backendType = B_CHECKPOINTER;
2938-
break;
2939-
case WalWriterProcess:
2940-
lbeentry.st_backendType = B_WAL_WRITER;
2941-
break;
2942-
case WalReceiverProcess:
2943-
lbeentry.st_backendType = B_WAL_RECEIVER;
2944-
break;
2945-
default:
2946-
elog(FATAL, "unrecognized process type: %d",
2947-
(int) MyAuxProcType);
2948-
}
2949-
}
2950-
2895+
lbeentry.st_backendType = MyBackendType;
29512896
lbeentry.st_proc_start_timestamp = MyStartTimestamp;
29522897
lbeentry.st_activity_start_timestamp = 0;
29532898
lbeentry.st_state_start_timestamp = 0;
@@ -4269,48 +4214,6 @@ pgstat_get_crashed_backend_activity(int pid, char *buffer, int buflen)
42694214
return NULL;
42704215
}
42714216

4272-
const char *
4273-
pgstat_get_backend_desc(BackendType backendType)
4274-
{
4275-
const char *backendDesc = "unknown process type";
4276-
4277-
switch (backendType)
4278-
{
4279-
case B_AUTOVAC_LAUNCHER:
4280-
backendDesc = "autovacuum launcher";
4281-
break;
4282-
case B_AUTOVAC_WORKER:
4283-
backendDesc = "autovacuum worker";
4284-
break;
4285-
case B_BACKEND:
4286-
backendDesc = "client backend";
4287-
break;
4288-
case B_BG_WORKER:
4289-
backendDesc = "background worker";
4290-
break;
4291-
case B_BG_WRITER:
4292-
backendDesc = "background writer";
4293-
break;
4294-
case B_CHECKPOINTER:
4295-
backendDesc = "checkpointer";
4296-
break;
4297-
case B_STARTUP:
4298-
backendDesc = "startup";
4299-
break;
4300-
case B_WAL_RECEIVER:
4301-
backendDesc = "walreceiver";
4302-
break;
4303-
case B_WAL_SENDER:
4304-
backendDesc = "walsender";
4305-
break;
4306-
case B_WAL_WRITER:
4307-
backendDesc = "walwriter";
4308-
break;
4309-
}
4310-
4311-
return backendDesc;
4312-
}
4313-
43144217
/* ------------------------------------------------------------
43154218
* Local support functions follow
43164219
* ------------------------------------------------------------
@@ -4447,10 +4350,8 @@ PgstatCollectorMain(int argc, char *argv[])
44474350
pqsignal(SIGCHLD, SIG_DFL);
44484351
PG_SETMASK(&UnBlockSig);
44494352

4450-
/*
4451-
* Identify myself via ps
4452-
*/
4453-
init_ps_display("stats collector");
4353+
MyBackendType = B_STATS_COLLECTOR;
4354+
init_ps_display(NULL);
44544355

44554356
/*
44564357
* Read in existing stats files or initialize the stats to zero.

src/backend/postmaster/postmaster.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2259,6 +2259,11 @@ ProcessStartupPacket(Port *port, bool secure_done)
22592259
if (strlen(port->user_name) >= NAMEDATALEN)
22602260
port->user_name[NAMEDATALEN - 1] = '\0';
22612261

2262+
if (am_walsender)
2263+
MyBackendType = B_WAL_SENDER;
2264+
else
2265+
MyBackendType = B_BACKEND;
2266+
22622267
/*
22632268
* Normal walsender backends, e.g. for streaming replication, are not
22642269
* connected to a particular database. But walsenders used for logical
@@ -4422,7 +4427,7 @@ BackendInitialize(Port *port)
44224427
*/
44234428
initStringInfo(&ps_data);
44244429
if (am_walsender)
4425-
appendStringInfo(&ps_data, "%s ", pgstat_get_backend_desc(B_WAL_SENDER));
4430+
appendStringInfo(&ps_data, "%s ", GetBackendTypeDesc(B_WAL_SENDER));
44264431
appendStringInfo(&ps_data, "%s ", port->user_name);
44274432
if (!am_walsender)
44284433
appendStringInfo(&ps_data, "%s ", port->database_name);

src/backend/postmaster/syslogger.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ SysLoggerMain(int argc, char *argv[])
179179

180180
am_syslogger = true;
181181

182-
init_ps_display("logger");
182+
MyBackendType = B_LOGGER;
183+
init_ps_display(NULL);
183184

184185
/*
185186
* If we restarted, our stderr is already redirected into our own input

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
841841
}
842842
else
843843
values[17] =
844-
CStringGetTextDatum(pgstat_get_backend_desc(beentry->st_backendType));
844+
CStringGetTextDatum(GetBackendTypeDesc(beentry->st_backendType));
845845

846846
/* SSL information */
847847
if (beentry->st_ssl)

src/backend/utils/init/miscinit.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757

5858
ProcessingMode Mode = InitProcessing;
5959

60+
BackendType MyBackendType;
61+
6062
/* List of lock files to be removed at proc exit */
6163
static List *lock_files = NIL;
6264

@@ -187,6 +189,59 @@ SwitchBackToLocalLatch(void)
187189
SetLatch(MyLatch);
188190
}
189191

192+
const char *
193+
GetBackendTypeDesc(BackendType backendType)
194+
{
195+
const char *backendDesc = "unknown process type";
196+
197+
switch (backendType)
198+
{
199+
case B_INVALID:
200+
backendDesc = "not initialized";
201+
break;
202+
case B_AUTOVAC_LAUNCHER:
203+
backendDesc = "autovacuum launcher";
204+
break;
205+
case B_AUTOVAC_WORKER:
206+
backendDesc = "autovacuum worker";
207+
break;
208+
case B_BACKEND:
209+
backendDesc = "client backend";
210+
break;
211+
case B_BG_WORKER:
212+
backendDesc = "background worker";
213+
break;
214+
case B_BG_WRITER:
215+
backendDesc = "background writer";
216+
break;
217+
case B_CHECKPOINTER:
218+
backendDesc = "checkpointer";
219+
break;
220+
case B_STARTUP:
221+
backendDesc = "startup";
222+
break;
223+
case B_WAL_RECEIVER:
224+
backendDesc = "walreceiver";
225+
break;
226+
case B_WAL_SENDER:
227+
backendDesc = "walsender";
228+
break;
229+
case B_WAL_WRITER:
230+
backendDesc = "walwriter";
231+
break;
232+
case B_ARCHIVER:
233+
backendDesc = "archiver";
234+
break;
235+
case B_STATS_COLLECTOR:
236+
backendDesc = "stats collector";
237+
break;
238+
case B_LOGGER:
239+
backendDesc = "logger";
240+
break;
241+
}
242+
243+
return backendDesc;
244+
}
190245

191246
/* ----------------------------------------------------------------
192247
* database path / name support stuff

src/backend/utils/misc/ps_status.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "libpq/libpq.h"
3030
#include "miscadmin.h"
31+
#include "pgstat.h"
3132
#include "utils/guc.h"
3233
#include "utils/ps_status.h"
3334

@@ -247,14 +248,20 @@ save_ps_display_args(int argc, char **argv)
247248

248249
/*
249250
* Call this once during subprocess startup to set the identification
250-
* values. At this point, the original argv[] array may be overwritten.
251+
* values.
252+
*
253+
* If fixed_part is NULL, a default will be obtained from MyBackendType.
254+
*
255+
* At this point, the original argv[] array may be overwritten.
251256
*/
252257
void
253258
init_ps_display(const char *fixed_part)
254259
{
255260
bool save_update_process_title;
256261

257-
Assert(fixed_part);
262+
Assert(fixed_part || MyBackendType);
263+
if (!fixed_part)
264+
fixed_part = GetBackendTypeDesc(MyBackendType);
258265

259266
#ifndef PS_USE_NONE
260267
/* no ps display for stand-alone backend */

src/include/miscadmin.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,28 @@ extern void InitStandaloneProcess(const char *argv0);
306306
extern void SwitchToSharedLatch(void);
307307
extern void SwitchBackToLocalLatch(void);
308308

309+
typedef enum BackendType
310+
{
311+
B_INVALID = 0,
312+
B_AUTOVAC_LAUNCHER,
313+
B_AUTOVAC_WORKER,
314+
B_BACKEND,
315+
B_BG_WORKER,
316+
B_BG_WRITER,
317+
B_CHECKPOINTER,
318+
B_STARTUP,
319+
B_WAL_RECEIVER,
320+
B_WAL_SENDER,
321+
B_WAL_WRITER,
322+
B_ARCHIVER,
323+
B_STATS_COLLECTOR,
324+
B_LOGGER,
325+
} BackendType;
326+
327+
extern BackendType MyBackendType;
328+
329+
extern const char *GetBackendTypeDesc(BackendType backendType);
330+
309331
extern void SetDatabasePath(const char *path);
310332
extern void checkDataDir(void);
311333
extern void SetDataDir(const char *dir);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy