Skip to content

Commit 5373bc2

Browse files
committed
Add background worker type
Add bgw_type field to background worker structure. It is intended to be set to the same value for all workers of the same type, so they can be grouped in pg_stat_activity, for example. The backend_type column in pg_stat_activity now shows bgw_type for a background worker. The ps listing also no longer calls out that a process is a background worker but just show the bgw_type. That way, being a background worker is more of an implementation detail now that is not shown to the user. However, most log messages still refer to 'background worker "%s"'; otherwise constructing sensible and translatable log messages would become tricky. Reviewed-by: Michael Paquier <michael.paquier@gmail.com> Reviewed-by: Daniel Gustafsson <daniel@yesql.se>
1 parent 8b304b8 commit 5373bc2

File tree

10 files changed

+89
-16
lines changed

10 files changed

+89
-16
lines changed

contrib/pg_prewarm/autoprewarm.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,8 @@ apw_start_master_worker(void)
800800
worker.bgw_start_time = BgWorkerStart_ConsistentState;
801801
strcpy(worker.bgw_library_name, "pg_prewarm");
802802
strcpy(worker.bgw_function_name, "autoprewarm_main");
803-
strcpy(worker.bgw_name, "autoprewarm");
803+
strcpy(worker.bgw_name, "autoprewarm master");
804+
strcpy(worker.bgw_type, "autoprewarm master");
804805

805806
if (process_shared_preload_libraries_in_progress)
806807
{
@@ -840,7 +841,8 @@ apw_start_database_worker(void)
840841
worker.bgw_start_time = BgWorkerStart_ConsistentState;
841842
strcpy(worker.bgw_library_name, "pg_prewarm");
842843
strcpy(worker.bgw_function_name, "autoprewarm_database_main");
843-
strcpy(worker.bgw_name, "autoprewarm");
844+
strcpy(worker.bgw_name, "autoprewarm worker");
845+
strcpy(worker.bgw_type, "autoprewarm worker");
844846

845847
/* must set notify PID to wait for shutdown */
846848
worker.bgw_notify_pid = MyProcPid;

doc/src/sgml/bgworker.sgml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ typedef void (*bgworker_main_type)(Datum main_arg);
5151
typedef struct BackgroundWorker
5252
{
5353
char bgw_name[BGW_MAXLEN];
54+
char bgw_type[BGW_MAXLEN];
5455
int bgw_flags;
5556
BgWorkerStartTime bgw_start_time;
5657
int bgw_restart_time; /* in seconds, or BGW_NEVER_RESTART */
@@ -64,8 +65,14 @@ typedef struct BackgroundWorker
6465
</para>
6566

6667
<para>
67-
<structfield>bgw_name</> is a string to be used in log messages, process
68-
listings and similar contexts.
68+
<structfield>bgw_name</> and <structfield>bgw_type</structfield> are
69+
strings to be used in log messages, process listings and similar contexts.
70+
<structfield>bgw_type</structfield> should be the same for all background
71+
workers of the same type, so that it is possible to group such workers in a
72+
process listing, for example. <structfield>bgw_name</structfield> on the
73+
other hand can contain additional information about the specific process.
74+
(Typically, the string for <structfield>bgw_name</structfield> will contain
75+
the type somehow, but that is not strictly required.)
6976
</para>
7077

7178
<para>

src/backend/access/transam/parallel.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ LaunchParallelWorkers(ParallelContext *pcxt)
467467
memset(&worker, 0, sizeof(worker));
468468
snprintf(worker.bgw_name, BGW_MAXLEN, "parallel worker for PID %d",
469469
MyProcPid);
470+
snprintf(worker.bgw_type, BGW_MAXLEN, "parallel worker");
470471
worker.bgw_flags =
471472
BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION
472473
| BGWORKER_CLASS_PARALLEL;

src/backend/postmaster/bgworker.c

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,8 @@ BackgroundWorkerStateChange(void)
344344
*/
345345
ascii_safe_strlcpy(rw->rw_worker.bgw_name,
346346
slot->worker.bgw_name, BGW_MAXLEN);
347+
ascii_safe_strlcpy(rw->rw_worker.bgw_type,
348+
slot->worker.bgw_type, BGW_MAXLEN);
347349
ascii_safe_strlcpy(rw->rw_worker.bgw_library_name,
348350
slot->worker.bgw_library_name, BGW_MAXLEN);
349351
ascii_safe_strlcpy(rw->rw_worker.bgw_function_name,
@@ -630,6 +632,12 @@ SanityCheckBackgroundWorker(BackgroundWorker *worker, int elevel)
630632
return false;
631633
}
632634

635+
/*
636+
* If bgw_type is not filled in, use bgw_name.
637+
*/
638+
if (strcmp(worker->bgw_type, "") == 0)
639+
strcpy(worker->bgw_type, worker->bgw_name);
640+
633641
return true;
634642
}
635643

@@ -671,7 +679,7 @@ bgworker_die(SIGNAL_ARGS)
671679
ereport(FATAL,
672680
(errcode(ERRCODE_ADMIN_SHUTDOWN),
673681
errmsg("terminating background worker \"%s\" due to administrator command",
674-
MyBgworkerEntry->bgw_name)));
682+
MyBgworkerEntry->bgw_type)));
675683
}
676684

677685
/*
@@ -700,7 +708,6 @@ void
700708
StartBackgroundWorker(void)
701709
{
702710
sigjmp_buf local_sigjmp_buf;
703-
char buf[MAXPGPATH];
704711
BackgroundWorker *worker = MyBgworkerEntry;
705712
bgworker_main_type entrypt;
706713

@@ -710,8 +717,7 @@ StartBackgroundWorker(void)
710717
IsBackgroundWorker = true;
711718

712719
/* Identify myself via ps */
713-
snprintf(buf, MAXPGPATH, "bgworker: %s", worker->bgw_name);
714-
init_ps_display(buf, "", "", "");
720+
init_ps_display(worker->bgw_name, "", "", "");
715721

716722
/*
717723
* If we're not supposed to have shared memory access, then detach from
@@ -1233,3 +1239,40 @@ LookupBackgroundWorkerFunction(const char *libraryname, const char *funcname)
12331239
return (bgworker_main_type)
12341240
load_external_function(libraryname, funcname, true, NULL);
12351241
}
1242+
1243+
/*
1244+
* Given a PID, get the bgw_type of the background worker. Returns NULL if
1245+
* not a valid background worker.
1246+
*
1247+
* The return value is in static memory belonging to this function, so it has
1248+
* to be used before calling this function again. This is so that the caller
1249+
* doesn't have to worry about the background worker locking protocol.
1250+
*/
1251+
const char *
1252+
GetBackgroundWorkerTypeByPid(pid_t pid)
1253+
{
1254+
int slotno;
1255+
bool found = false;
1256+
static char result[BGW_MAXLEN];
1257+
1258+
LWLockAcquire(BackgroundWorkerLock, LW_SHARED);
1259+
1260+
for (slotno = 0; slotno < BackgroundWorkerData->total_slots; slotno++)
1261+
{
1262+
BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
1263+
1264+
if (slot->pid > 0 && slot->pid == pid)
1265+
{
1266+
strcpy(result, slot->worker.bgw_type);
1267+
found = true;
1268+
break;
1269+
}
1270+
}
1271+
1272+
LWLockRelease(BackgroundWorkerLock);
1273+
1274+
if (!found)
1275+
return NULL;
1276+
1277+
return result;
1278+
}

src/backend/postmaster/postmaster.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3117,8 +3117,9 @@ CleanupBackgroundWorker(int pid,
31173117
exitstatus = 0;
31183118
#endif
31193119

3120-
snprintf(namebuf, MAXPGPATH, "%s: %s", _("worker process"),
3121-
rw->rw_worker.bgw_name);
3120+
snprintf(namebuf, MAXPGPATH, _("background worker \"%s\""),
3121+
rw->rw_worker.bgw_type);
3122+
31223123

31233124
if (!EXIT_STATUS_0(exitstatus))
31243125
{

src/backend/replication/logical/launcher.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid,
422422
else
423423
snprintf(bgw.bgw_name, BGW_MAXLEN,
424424
"logical replication worker for subscription %u", subid);
425+
snprintf(bgw.bgw_type, BGW_MAXLEN, "logical replication worker");
425426

426427
bgw.bgw_restart_time = BGW_NEVER_RESTART;
427428
bgw.bgw_notify_pid = MyProcPid;
@@ -775,6 +776,8 @@ ApplyLauncherRegister(void)
775776
snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyLauncherMain");
776777
snprintf(bgw.bgw_name, BGW_MAXLEN,
777778
"logical replication launcher");
779+
snprintf(bgw.bgw_type, BGW_MAXLEN,
780+
"logical replication launcher");
778781
bgw.bgw_restart_time = 5;
779782
bgw.bgw_notify_pid = 0;
780783
bgw.bgw_main_arg = (Datum) 0;

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "funcapi.h"
2222
#include "miscadmin.h"
2323
#include "pgstat.h"
24+
#include "postmaster/bgworker_internals.h"
2425
#include "postmaster/postmaster.h"
2526
#include "storage/proc.h"
2627
#include "storage/procarray.h"
@@ -823,8 +824,19 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
823824
}
824825
}
825826
/* Add backend type */
826-
values[17] =
827-
CStringGetTextDatum(pgstat_get_backend_desc(beentry->st_backendType));
827+
if (beentry->st_backendType == B_BG_WORKER)
828+
{
829+
const char *bgw_type;
830+
831+
bgw_type = GetBackgroundWorkerTypeByPid(beentry->st_procpid);
832+
if (bgw_type)
833+
values[17] = CStringGetTextDatum(bgw_type);
834+
else
835+
nulls[17] = true;
836+
}
837+
else
838+
values[17] =
839+
CStringGetTextDatum(pgstat_get_backend_desc(beentry->st_backendType));
828840
}
829841
else
830842
{

src/include/postmaster/bgworker.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ typedef enum
8888
typedef struct BackgroundWorker
8989
{
9090
char bgw_name[BGW_MAXLEN];
91+
char bgw_type[BGW_MAXLEN];
9192
int bgw_flags;
9293
BgWorkerStartTime bgw_start_time;
9394
int bgw_restart_time; /* in seconds, or BGW_NEVER_RESTART */
@@ -122,6 +123,7 @@ extern BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle,
122123
extern BgwHandleStatus WaitForBackgroundWorkerStartup(BackgroundWorkerHandle *handle, pid_t *pid);
123124
extern BgwHandleStatus
124125
WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *);
126+
extern const char *GetBackgroundWorkerTypeByPid(pid_t pid);
125127

126128
/* Terminate a bgworker */
127129
extern void TerminateBackgroundWorker(BackgroundWorkerHandle *handle);

src/test/modules/test_shm_mq/setup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ setup_background_workers(int nworkers, dsm_segment *seg)
219219
worker.bgw_restart_time = BGW_NEVER_RESTART;
220220
sprintf(worker.bgw_library_name, "test_shm_mq");
221221
sprintf(worker.bgw_function_name, "test_shm_mq_main");
222-
snprintf(worker.bgw_name, BGW_MAXLEN, "test_shm_mq");
222+
snprintf(worker.bgw_type, BGW_MAXLEN, "test_shm_mq");
223223
worker.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
224224
/* set bgw_notify_pid, so we can detect if the worker stops */
225225
worker.bgw_notify_pid = MyProcPid;

src/test/modules/worker_spi/worker_spi.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ initialize_worker_spi(worktable *table)
111111
StartTransactionCommand();
112112
SPI_connect();
113113
PushActiveSnapshot(GetTransactionSnapshot());
114-
pgstat_report_activity(STATE_RUNNING, "initializing spi_worker schema");
114+
pgstat_report_activity(STATE_RUNNING, "initializing worker_spi schema");
115115

116116
/* XXX could we use CREATE SCHEMA IF NOT EXISTS? */
117117
initStringInfo(&buf);
@@ -359,7 +359,8 @@ _PG_init(void)
359359
*/
360360
for (i = 1; i <= worker_spi_total_workers; i++)
361361
{
362-
snprintf(worker.bgw_name, BGW_MAXLEN, "worker %d", i);
362+
snprintf(worker.bgw_name, BGW_MAXLEN, "worker_spi worker %d", i);
363+
snprintf(worker.bgw_type, BGW_MAXLEN, "worker_spi");
363364
worker.bgw_main_arg = Int32GetDatum(i);
364365

365366
RegisterBackgroundWorker(&worker);
@@ -385,7 +386,8 @@ worker_spi_launch(PG_FUNCTION_ARGS)
385386
worker.bgw_restart_time = BGW_NEVER_RESTART;
386387
sprintf(worker.bgw_library_name, "worker_spi");
387388
sprintf(worker.bgw_function_name, "worker_spi_main");
388-
snprintf(worker.bgw_name, BGW_MAXLEN, "worker %d", i);
389+
snprintf(worker.bgw_name, BGW_MAXLEN, "worker_spi worker %d", i);
390+
snprintf(worker.bgw_type, BGW_MAXLEN, "worker_spi");
389391
worker.bgw_main_arg = Int32GetDatum(i);
390392
/* set bgw_notify_pid so that we can use WaitForBackgroundWorkerStartup */
391393
worker.bgw_notify_pid = MyProcPid;

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