backend type in log_line_prefix?

Started by Peter Eisentrautalmost 6 years ago26 messages
#1Peter Eisentraut
peter.eisentraut@2ndquadrant.com
1 attachment(s)

Attached is a demo patch that adds a placeholder %b for log_line_prefix
(not in the default setting) that contains the backend type, the same
that you see in pg_stat_activity and in the ps status. I would have
found this occasionally useful when analyzing logs, especially if you
have a lot of background workers active. Thoughts?

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachments:

0001-Add-backend-type-placeholder-to-log_line_prefix.patchtext/plain; charset=UTF-8; name=0001-Add-backend-type-placeholder-to-log_line_prefix.patch; x-mac-creator=0; x-mac-type=0Download
From 3cb15882f671c8d37cb0f844ce62d1853870875c Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Wed, 12 Feb 2020 22:04:29 +0100
Subject: [PATCH] Add backend type placeholder to log_line_prefix

---
 doc/src/sgml/config.sgml            | 5 +++++
 src/backend/bootstrap/bootstrap.c   | 2 +-
 src/backend/postmaster/autovacuum.c | 4 ++--
 src/backend/postmaster/bgworker.c   | 2 +-
 src/backend/postmaster/pgarch.c     | 2 +-
 src/backend/postmaster/pgstat.c     | 2 +-
 src/backend/postmaster/postmaster.c | 9 ++++++++-
 src/backend/postmaster/syslogger.c  | 2 +-
 src/backend/utils/error/elog.c      | 6 ++++++
 src/backend/utils/init/globals.c    | 2 ++
 src/include/miscadmin.h             | 2 ++
 src/test/regress/pg_regress.c       | 2 +-
 12 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index c1128f89ec..9c068f3903 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -6470,6 +6470,11 @@ <title>What to Log</title>
              <entry>Application name</entry>
              <entry>yes</entry>
             </row>
+            <row>
+             <entry><literal>%b</literal></entry>
+             <entry>Backend process type</entry>
+             <entry>yes</entry>
+            </row>
             <row>
              <entry><literal>%u</literal></entry>
              <entry>User name</entry>
diff --git a/src/backend/bootstrap/bootstrap.c b/src/backend/bootstrap/bootstrap.c
index bfc629c753..1c559851b4 100644
--- a/src/backend/bootstrap/bootstrap.c
+++ b/src/backend/bootstrap/bootstrap.c
@@ -342,7 +342,7 @@ AuxiliaryProcessMain(int argc, char *argv[])
 				statmsg = "??? process";
 				break;
 		}
-		init_ps_display(statmsg, "", "", "");
+		init_ps_display((backend_type_str = statmsg), "", "", "");
 	}
 
 	/* Acquire configuration parameters, unless inherited from postmaster */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 6d1f28c327..d50d4b9b02 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -434,7 +434,7 @@ AutoVacLauncherMain(int argc, char *argv[])
 	am_autovacuum_launcher = true;
 
 	/* Identify myself via ps */
-	init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_LAUNCHER), "", "", "");
+	init_ps_display((backend_type_str = pgstat_get_backend_desc(B_AUTOVAC_LAUNCHER)), "", "", "");
 
 	ereport(DEBUG1,
 			(errmsg("autovacuum launcher started")));
@@ -1507,7 +1507,7 @@ AutoVacWorkerMain(int argc, char *argv[])
 	am_autovacuum_worker = true;
 
 	/* Identify myself via ps */
-	init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_WORKER), "", "", "");
+	init_ps_display((backend_type_str = pgstat_get_backend_desc(B_AUTOVAC_WORKER)), "", "", "");
 
 	SetProcessingMode(InitProcessing);
 
diff --git a/src/backend/postmaster/bgworker.c b/src/backend/postmaster/bgworker.c
index 75fc0d5d33..105b755c50 100644
--- a/src/backend/postmaster/bgworker.c
+++ b/src/backend/postmaster/bgworker.c
@@ -689,7 +689,7 @@ StartBackgroundWorker(void)
 	IsBackgroundWorker = true;
 
 	/* Identify myself via ps */
-	init_ps_display(worker->bgw_name, "", "", "");
+	init_ps_display((backend_type_str = worker->bgw_name), "", "", "");
 
 	/*
 	 * If we're not supposed to have shared memory access, then detach from
diff --git a/src/backend/postmaster/pgarch.c b/src/backend/postmaster/pgarch.c
index 3ca30badb2..cd4f8b6392 100644
--- a/src/backend/postmaster/pgarch.c
+++ b/src/backend/postmaster/pgarch.c
@@ -241,7 +241,7 @@ PgArchiverMain(int argc, char *argv[])
 	/*
 	 * Identify myself via ps
 	 */
-	init_ps_display("archiver", "", "", "");
+	init_ps_display((backend_type_str = "archiver"), "", "", "");
 
 	pgarch_MainLoop();
 
diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index 7169509a79..8d48abfe1e 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -4447,7 +4447,7 @@ PgstatCollectorMain(int argc, char *argv[])
 	/*
 	 * Identify myself via ps
 	 */
-	init_ps_display("stats collector", "", "", "");
+	init_ps_display((backend_type_str = "stats collector"), "", "", "");
 
 	/*
 	 * Read in existing stats files or initialize the stats to zero.
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index b3986bee75..f0aba28655 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -579,6 +579,8 @@ PostmasterMain(int argc, char *argv[])
 
 	IsPostmasterEnvironment = true;
 
+	backend_type_str = "postmaster";
+
 	/*
 	 * We should not be creating any files or directories before we check the
 	 * data directory (see checkDataDir()), but just in case set the umask to
@@ -2621,6 +2623,8 @@ InitProcessGlobals(void)
 			((uint64) MyStartTimestamp >> 20);
 	}
 	srandom(rseed);
+
+	backend_type_str = "uninitialized";
 }
 
 
@@ -4422,11 +4426,14 @@ BackendInitialize(Port *port)
 	 * init_ps_display() to avoid abusing the parameters like this.
 	 */
 	if (am_walsender)
-		init_ps_display(pgstat_get_backend_desc(B_WAL_SENDER), port->user_name, remote_ps_data,
+		init_ps_display((backend_type_str = pgstat_get_backend_desc(B_WAL_SENDER)), port->user_name, remote_ps_data,
 						update_process_title ? "authentication" : "");
 	else
+	{
+		backend_type_str = "client backend";
 		init_ps_display(port->user_name, port->database_name, remote_ps_data,
 						update_process_title ? "authentication" : "");
+	}
 
 	/*
 	 * Disable the timeout, and prevent SIGTERM/SIGQUIT again.
diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c
index b2b69a7207..0b0ba949dc 100644
--- a/src/backend/postmaster/syslogger.c
+++ b/src/backend/postmaster/syslogger.c
@@ -179,7 +179,7 @@ SysLoggerMain(int argc, char *argv[])
 
 	am_syslogger = true;
 
-	init_ps_display("logger", "", "", "");
+	init_ps_display((backend_type_str = "logger"), "", "", "");
 
 	/*
 	 * If we restarted, our stderr is already redirected into our own input
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index f5b0211f66..2baa7aedff 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -2492,6 +2492,12 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
 										   padding > 0 ? padding : -padding);
 
 				break;
+			case 'b':
+				if (padding != 0)
+					appendStringInfo(buf, "%*s", padding, backend_type_str);
+				else
+					appendStringInfoString(buf, backend_type_str);
+				break;
 			case 'u':
 				if (MyProcPort)
 				{
diff --git a/src/backend/utils/init/globals.c b/src/backend/utils/init/globals.c
index eb19644419..f1f8e61507 100644
--- a/src/backend/utils/init/globals.c
+++ b/src/backend/utils/init/globals.c
@@ -78,6 +78,8 @@ char		postgres_exec_path[MAXPGPATH];	/* full path to backend */
 /* note: currently this is not valid in backend processes */
 #endif
 
+const char *backend_type_str;
+
 BackendId	MyBackendId = InvalidBackendId;
 
 BackendId	ParallelMasterBackendId = InvalidBackendId;
diff --git a/src/include/miscadmin.h b/src/include/miscadmin.h
index f985453ec3..9c9418e08a 100644
--- a/src/include/miscadmin.h
+++ b/src/include/miscadmin.h
@@ -178,6 +178,8 @@ extern char pkglib_path[];
 extern char postgres_exec_path[];
 #endif
 
+extern const char *backend_type_str;
+
 /*
  * done in storage/backendid.h for now.
  *
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index 92bd28dc5a..d9f8a74a19 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -2350,7 +2350,7 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
 		fputs("\n# Configuration added by pg_regress\n\n", pg_conf);
 		fputs("log_autovacuum_min_duration = 0\n", pg_conf);
 		fputs("log_checkpoints = on\n", pg_conf);
-		fputs("log_line_prefix = '%m [%p] %q%a '\n", pg_conf);
+		fputs("log_line_prefix = '%m %b[%p] %q%a '\n", pg_conf);
 		fputs("log_lock_waits = on\n", pg_conf);
 		fputs("log_temp_files = 128kB\n", pg_conf);
 		fputs("max_prepared_transactions = 2\n", pg_conf);
-- 
2.25.0

#2Julien Rouhaud
rjuju123@gmail.com
In reply to: Peter Eisentraut (#1)
Re: backend type in log_line_prefix?

On Thu, Feb 13, 2020 at 09:56:38AM +0100, Peter Eisentraut wrote:

Attached is a demo patch that adds a placeholder %b for log_line_prefix (not
in the default setting) that contains the backend type, the same that you
see in pg_stat_activity and in the ps status. I would have found this
occasionally useful when analyzing logs, especially if you have a lot of
background workers active. Thoughts?

+1, I'd also have been happy to have it multiple times.

#3Fujii Masao
masao.fujii@oss.nttdata.com
In reply to: Julien Rouhaud (#2)
Re: backend type in log_line_prefix?

On 2020/02/13 18:14, Julien Rouhaud wrote:

On Thu, Feb 13, 2020 at 09:56:38AM +0100, Peter Eisentraut wrote:

Attached is a demo patch that adds a placeholder %b for log_line_prefix (not
in the default setting) that contains the backend type, the same that you
see in pg_stat_activity and in the ps status. I would have found this
occasionally useful when analyzing logs, especially if you have a lot of
background workers active. Thoughts?

If we do this, backend type should be also included in csvlog?

Regarding the patch, postgresql.conf.sample needs to be updated.

Regards,

--
Fujii Masao
NTT DATA CORPORATION
Advanced Platform Technology Group
Research and Development Headquarters

#4Andres Freund
andres@anarazel.de
In reply to: Peter Eisentraut (#1)
Re: backend type in log_line_prefix?

Hi,

On 2020-02-13 09:56:38 +0100, Peter Eisentraut wrote:

Attached is a demo patch that adds a placeholder %b for log_line_prefix (not
in the default setting) that contains the backend type, the same that you
see in pg_stat_activity and in the ps status. I would have found this
occasionally useful when analyzing logs, especially if you have a lot of
background workers active. Thoughts?

I wished for this several times.

@@ -342,7 +342,7 @@ AuxiliaryProcessMain(int argc, char *argv[])
statmsg = "??? process";
break;
}
-		init_ps_display(statmsg, "", "", "");
+		init_ps_display((backend_type_str = statmsg), "", "", "");
}

But I'm decidedly not a fan of this.

Greetings,

Andres Freund

#5Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Peter Eisentraut (#1)
4 attachment(s)
Re: backend type in log_line_prefix?

On 2020-02-13 09:56, Peter Eisentraut wrote:

Attached is a demo patch that adds a placeholder %b for log_line_prefix
(not in the default setting) that contains the backend type, the same
that you see in pg_stat_activity and in the ps status. I would have
found this occasionally useful when analyzing logs, especially if you
have a lot of background workers active. Thoughts?

After positive initial feedback, here is a more ambitious patch set. In
particular, I wanted to avoid having to specify the backend type (at
least) twice, once for the ps display and once for this new facility.

I have added a new global variable MyBackendType that uses the existing
BackendType enum that was previously only used by the stats collector.
Then the ps display, the stats collector, the log_line_prefix, and other
places can just refer to this to know "who am I". (There are more
places like that, for example in the autovacuum system, so patch 0004 in
particular could be expanded in analogous ways.)

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachments:

v2-0001-Refactor-ps_status.c-API.patchtext/plain; charset=UTF-8; name=v2-0001-Refactor-ps_status.c-API.patch; x-mac-creator=0; x-mac-type=0Download
From 29f9c92c5cdc7eddb60f69bb984a57e45a600938 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Thu, 20 Feb 2020 18:07:37 +0100
Subject: [PATCH v2 1/4] Refactor ps_status.c API

The init_ps_display() arguments were mostly lies by now, so to match
typical usage, just use one argument and let the caller assemble it
from multiple sources if necessary.  The only user of the additional
arguments is BackendInitialize(), which was already doing string
assembly on the caller side anyway.

Remove the second argument of set_ps_display() ("force") and just
handle that in init_ps_display() internally.

BackendInitialize() also used to set the initial status as
"authentication", but that was very far from where authentication
actually happened.  So now it's set to "initializing" and then
"authentication" just before the actual call to
ClientAuthentication().
---
 src/backend/access/transam/xlog.c     |  4 ++--
 src/backend/bootstrap/bootstrap.c     |  2 +-
 src/backend/commands/async.c          |  4 ++--
 src/backend/postmaster/autovacuum.c   |  6 ++---
 src/backend/postmaster/bgworker.c     |  2 +-
 src/backend/postmaster/pgarch.c       |  8 +++----
 src/backend/postmaster/pgstat.c       |  2 +-
 src/backend/postmaster/postmaster.c   | 32 ++++++++++++---------------
 src/backend/postmaster/syslogger.c    |  2 +-
 src/backend/replication/basebackup.c  |  2 +-
 src/backend/replication/syncrep.c     |  4 ++--
 src/backend/replication/walreceiver.c |  7 +++---
 src/backend/replication/walsender.c   |  2 +-
 src/backend/storage/ipc/standby.c     |  4 ++--
 src/backend/storage/lmgr/lock.c       |  6 ++---
 src/backend/tcop/postgres.c           | 16 +++++++-------
 src/backend/utils/init/postinit.c     |  3 ++-
 src/backend/utils/misc/ps_status.c    | 31 +++++++++++++++-----------
 src/include/utils/ps_status.h         |  5 ++---
 19 files changed, 71 insertions(+), 71 deletions(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 3813eadfb4..b4455799c4 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -3639,7 +3639,7 @@ XLogFileRead(XLogSegNo segno, int emode, TimeLineID tli,
 			/* Report recovery progress in PS display */
 			snprintf(activitymsg, sizeof(activitymsg), "waiting for %s",
 					 xlogfname);
-			set_ps_display(activitymsg, false);
+			set_ps_display(activitymsg);
 
 			restoredFromArchive = RestoreArchivedFile(path, xlogfname,
 													  "RECOVERYXLOG",
@@ -3682,7 +3682,7 @@ XLogFileRead(XLogSegNo segno, int emode, TimeLineID tli,
 		/* Report recovery progress in PS display */
 		snprintf(activitymsg, sizeof(activitymsg), "recovering %s",
 				 xlogfname);
-		set_ps_display(activitymsg, false);
+		set_ps_display(activitymsg);
 
 		/* Track source of data in assorted state variables */
 		readSource = source;
diff --git a/src/backend/bootstrap/bootstrap.c b/src/backend/bootstrap/bootstrap.c
index bfc629c753..4212333737 100644
--- a/src/backend/bootstrap/bootstrap.c
+++ b/src/backend/bootstrap/bootstrap.c
@@ -342,7 +342,7 @@ AuxiliaryProcessMain(int argc, char *argv[])
 				statmsg = "??? process";
 				break;
 		}
-		init_ps_display(statmsg, "", "", "");
+		init_ps_display(statmsg);
 	}
 
 	/* Acquire configuration parameters, unless inherited from postmaster */
diff --git a/src/backend/commands/async.c b/src/backend/commands/async.c
index 9aa2b61600..50275f94ff 100644
--- a/src/backend/commands/async.c
+++ b/src/backend/commands/async.c
@@ -2225,7 +2225,7 @@ ProcessIncomingNotify(void)
 	if (Trace_notify)
 		elog(DEBUG1, "ProcessIncomingNotify");
 
-	set_ps_display("notify interrupt", false);
+	set_ps_display("notify interrupt");
 
 	/*
 	 * We must run asyncQueueReadAllNotifications inside a transaction, else
@@ -2242,7 +2242,7 @@ ProcessIncomingNotify(void)
 	 */
 	pq_flush();
 
-	set_ps_display("idle", false);
+	set_ps_display("idle");
 
 	if (Trace_notify)
 		elog(DEBUG1, "ProcessIncomingNotify: done");
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 6d1f28c327..c461af11b2 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -434,7 +434,7 @@ AutoVacLauncherMain(int argc, char *argv[])
 	am_autovacuum_launcher = true;
 
 	/* Identify myself via ps */
-	init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_LAUNCHER), "", "", "");
+	init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_LAUNCHER));
 
 	ereport(DEBUG1,
 			(errmsg("autovacuum launcher started")));
@@ -1507,7 +1507,7 @@ AutoVacWorkerMain(int argc, char *argv[])
 	am_autovacuum_worker = true;
 
 	/* Identify myself via ps */
-	init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_WORKER), "", "", "");
+	init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_WORKER));
 
 	SetProcessingMode(InitProcessing);
 
@@ -1680,7 +1680,7 @@ AutoVacWorkerMain(int argc, char *argv[])
 		 */
 		InitPostgres(NULL, dbid, NULL, InvalidOid, dbname, false);
 		SetProcessingMode(NormalProcessing);
-		set_ps_display(dbname, false);
+		set_ps_display(dbname);
 		ereport(DEBUG1,
 				(errmsg("autovacuum: processing database \"%s\"", dbname)));
 
diff --git a/src/backend/postmaster/bgworker.c b/src/backend/postmaster/bgworker.c
index 75fc0d5d33..684250984d 100644
--- a/src/backend/postmaster/bgworker.c
+++ b/src/backend/postmaster/bgworker.c
@@ -689,7 +689,7 @@ StartBackgroundWorker(void)
 	IsBackgroundWorker = true;
 
 	/* Identify myself via ps */
-	init_ps_display(worker->bgw_name, "", "", "");
+	init_ps_display(worker->bgw_name);
 
 	/*
 	 * If we're not supposed to have shared memory access, then detach from
diff --git a/src/backend/postmaster/pgarch.c b/src/backend/postmaster/pgarch.c
index 3ca30badb2..58f54544f6 100644
--- a/src/backend/postmaster/pgarch.c
+++ b/src/backend/postmaster/pgarch.c
@@ -241,7 +241,7 @@ PgArchiverMain(int argc, char *argv[])
 	/*
 	 * Identify myself via ps
 	 */
-	init_ps_display("archiver", "", "", "");
+	init_ps_display("archiver");
 
 	pgarch_MainLoop();
 
@@ -584,7 +584,7 @@ pgarch_archiveXlog(char *xlog)
 
 	/* Report archive activity in PS display */
 	snprintf(activitymsg, sizeof(activitymsg), "archiving %s", xlog);
-	set_ps_display(activitymsg, false);
+	set_ps_display(activitymsg);
 
 	rc = system(xlogarchcmd);
 	if (rc != 0)
@@ -634,14 +634,14 @@ pgarch_archiveXlog(char *xlog)
 		}
 
 		snprintf(activitymsg, sizeof(activitymsg), "failed on %s", xlog);
-		set_ps_display(activitymsg, false);
+		set_ps_display(activitymsg);
 
 		return false;
 	}
 	elog(DEBUG1, "archived write-ahead log file \"%s\"", xlog);
 
 	snprintf(activitymsg, sizeof(activitymsg), "last was %s", xlog);
-	set_ps_display(activitymsg, false);
+	set_ps_display(activitymsg);
 
 	return true;
 }
diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index 59dc4f31ab..3934d03396 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -4447,7 +4447,7 @@ PgstatCollectorMain(int argc, char *argv[])
 	/*
 	 * Identify myself via ps
 	 */
-	init_ps_display("stats collector", "", "", "");
+	init_ps_display("stats collector");
 
 	/*
 	 * Read in existing stats files or initialize the stats to zero.
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index b3986bee75..386fc54e6f 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -4274,7 +4274,7 @@ BackendInitialize(Port *port)
 	int			ret;
 	char		remote_host[NI_MAXHOST];
 	char		remote_port[NI_MAXSERV];
-	char		remote_ps_data[NI_MAXHOST];
+	StringInfoData ps_data;
 
 	/* Save port etc. for ps status */
 	MyProcPort = port;
@@ -4335,10 +4335,6 @@ BackendInitialize(Port *port)
 		ereport(WARNING,
 				(errmsg_internal("pg_getnameinfo_all() failed: %s",
 								 gai_strerror(ret))));
-	if (remote_port[0] == '\0')
-		snprintf(remote_ps_data, sizeof(remote_ps_data), "%s", remote_host);
-	else
-		snprintf(remote_ps_data, sizeof(remote_ps_data), "%s(%s)", remote_host, remote_port);
 
 	/*
 	 * Save remote_host and remote_port in port structure (after this, they
@@ -4412,21 +4408,21 @@ BackendInitialize(Port *port)
 	/*
 	 * Now that we have the user and database name, we can set the process
 	 * title for ps.  It's good to do this as early as possible in startup.
-	 *
-	 * For a walsender, the ps display is set in the following form:
-	 *
-	 * postgres: walsender <user> <host> <activity>
-	 *
-	 * To achieve that, we pass "walsender" as username and username as dbname
-	 * to init_ps_display(). XXX: should add a new variant of
-	 * init_ps_display() to avoid abusing the parameters like this.
 	 */
+	initStringInfo(&ps_data);
 	if (am_walsender)
-		init_ps_display(pgstat_get_backend_desc(B_WAL_SENDER), port->user_name, remote_ps_data,
-						update_process_title ? "authentication" : "");
-	else
-		init_ps_display(port->user_name, port->database_name, remote_ps_data,
-						update_process_title ? "authentication" : "");
+		appendStringInfo(&ps_data, "%s ", pgstat_get_backend_desc(B_WAL_SENDER));
+	appendStringInfo(&ps_data, "%s ", port->user_name);
+	if (!am_walsender)
+		appendStringInfo(&ps_data, "%s ", port->database_name);
+	appendStringInfo(&ps_data, "%s", port->remote_host);
+	if (port->remote_port[0] != '\0')
+		appendStringInfo(&ps_data, "(%s)", port->remote_port);
+
+	init_ps_display(ps_data.data);
+	pfree(ps_data.data);
+
+	set_ps_display("initializing");
 
 	/*
 	 * Disable the timeout, and prevent SIGTERM/SIGQUIT again.
diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c
index b2b69a7207..b0ab5d3076 100644
--- a/src/backend/postmaster/syslogger.c
+++ b/src/backend/postmaster/syslogger.c
@@ -179,7 +179,7 @@ SysLoggerMain(int argc, char *argv[])
 
 	am_syslogger = true;
 
-	init_ps_display("logger", "", "", "");
+	init_ps_display("logger");
 
 	/*
 	 * If we restarted, our stderr is already redirected into our own input
diff --git a/src/backend/replication/basebackup.c b/src/backend/replication/basebackup.c
index dea8aab45e..dbde3d359a 100644
--- a/src/backend/replication/basebackup.c
+++ b/src/backend/replication/basebackup.c
@@ -758,7 +758,7 @@ SendBaseBackup(BaseBackupCmd *cmd)
 
 		snprintf(activitymsg, sizeof(activitymsg), "sending backup \"%s\"",
 				 opt.label);
-		set_ps_display(activitymsg, false);
+		set_ps_display(activitymsg);
 	}
 
 	perform_base_backup(&opt);
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index c284103b54..ffd5b31eb2 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -209,7 +209,7 @@ SyncRepWaitForLSN(XLogRecPtr lsn, bool commit)
 		memcpy(new_status, old_status, len);
 		sprintf(new_status + len, " waiting for %X/%X",
 				(uint32) (lsn >> 32), (uint32) lsn);
-		set_ps_display(new_status, false);
+		set_ps_display(new_status);
 		new_status[len] = '\0'; /* truncate off " waiting ..." */
 	}
 
@@ -311,7 +311,7 @@ SyncRepWaitForLSN(XLogRecPtr lsn, bool commit)
 	if (new_status)
 	{
 		/* Reset ps display */
-		set_ps_display(new_status, false);
+		set_ps_display(new_status);
 		pfree(new_status);
 	}
 }
diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c
index 2ab15c3cbb..930cc03e0f 100644
--- a/src/backend/replication/walreceiver.c
+++ b/src/backend/replication/walreceiver.c
@@ -665,8 +665,7 @@ WalRcvWaitForStartPosition(XLogRecPtr *startpoint, TimeLineID *startpointTLI)
 	walrcv->receiveStartTLI = 0;
 	SpinLockRelease(&walrcv->mutex);
 
-	if (update_process_title)
-		set_ps_display("idle", false);
+	set_ps_display("idle");
 
 	/*
 	 * nudge startup process to notice that we've stopped streaming and are
@@ -714,7 +713,7 @@ WalRcvWaitForStartPosition(XLogRecPtr *startpoint, TimeLineID *startpointTLI)
 		snprintf(activitymsg, sizeof(activitymsg), "restarting at %X/%X",
 				 (uint32) (*startpoint >> 32),
 				 (uint32) *startpoint);
-		set_ps_display(activitymsg, false);
+		set_ps_display(activitymsg);
 	}
 }
 
@@ -1027,7 +1026,7 @@ XLogWalRcvFlush(bool dying)
 			snprintf(activitymsg, sizeof(activitymsg), "streaming %X/%X",
 					 (uint32) (LogstreamResult.Write >> 32),
 					 (uint32) LogstreamResult.Write);
-			set_ps_display(activitymsg, false);
+			set_ps_display(activitymsg);
 		}
 
 		/* Also let the master know that we made some progress */
diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index abb533b9d0..f04c9b9496 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -2761,7 +2761,7 @@ XLogSendPhysical(void)
 
 		snprintf(activitymsg, sizeof(activitymsg), "streaming %X/%X",
 				 (uint32) (sentPtr >> 32), (uint32) sentPtr);
-		set_ps_display(activitymsg, false);
+		set_ps_display(activitymsg);
 	}
 }
 
diff --git a/src/backend/storage/ipc/standby.c b/src/backend/storage/ipc/standby.c
index 3090e57fa4..0a53230714 100644
--- a/src/backend/storage/ipc/standby.c
+++ b/src/backend/storage/ipc/standby.c
@@ -254,7 +254,7 @@ ResolveRecoveryConflictWithVirtualXIDs(VirtualTransactionId *waitlist,
 				new_status = (char *) palloc(len + 8 + 1);
 				memcpy(new_status, old_status, len);
 				strcpy(new_status + len, " waiting");
-				set_ps_display(new_status, false);
+				set_ps_display(new_status);
 				new_status[len] = '\0'; /* truncate off " waiting" */
 			}
 
@@ -285,7 +285,7 @@ ResolveRecoveryConflictWithVirtualXIDs(VirtualTransactionId *waitlist,
 	/* Reset ps display if we changed it */
 	if (new_status)
 	{
-		set_ps_display(new_status, false);
+		set_ps_display(new_status);
 		pfree(new_status);
 	}
 }
diff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmgr/lock.c
index 56dba09299..1df7b8e2ab 100644
--- a/src/backend/storage/lmgr/lock.c
+++ b/src/backend/storage/lmgr/lock.c
@@ -1737,7 +1737,7 @@ WaitOnLock(LOCALLOCK *locallock, ResourceOwner owner)
 		new_status = (char *) palloc(len + 8 + 1);
 		memcpy(new_status, old_status, len);
 		strcpy(new_status + len, " waiting");
-		set_ps_display(new_status, false);
+		set_ps_display(new_status);
 		new_status[len] = '\0'; /* truncate off " waiting" */
 	}
 
@@ -1789,7 +1789,7 @@ WaitOnLock(LOCALLOCK *locallock, ResourceOwner owner)
 		/* Report change to non-waiting status */
 		if (update_process_title)
 		{
-			set_ps_display(new_status, false);
+			set_ps_display(new_status);
 			pfree(new_status);
 		}
 
@@ -1803,7 +1803,7 @@ WaitOnLock(LOCALLOCK *locallock, ResourceOwner owner)
 	/* Report change to non-waiting status */
 	if (update_process_title)
 	{
-		set_ps_display(new_status, false);
+		set_ps_display(new_status);
 		pfree(new_status);
 	}
 
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 0a6f80963b..b6a8ffbfde 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -1081,7 +1081,7 @@ exec_simple_query(const char *query_string)
 		 */
 		commandTag = CreateCommandTag(parsetree->stmt);
 
-		set_ps_display(commandTag, false);
+		set_ps_display(commandTag);
 
 		BeginCommand(commandTag, dest);
 
@@ -1357,7 +1357,7 @@ exec_parse_message(const char *query_string,	/* string to execute */
 
 	pgstat_report_activity(STATE_RUNNING, query_string);
 
-	set_ps_display("PARSE", false);
+	set_ps_display("PARSE");
 
 	if (save_log_statement_stats)
 		ResetUsage();
@@ -1652,7 +1652,7 @@ exec_bind_message(StringInfo input_message)
 
 	pgstat_report_activity(STATE_RUNNING, psrc->query_string);
 
-	set_ps_display("BIND", false);
+	set_ps_display("BIND");
 
 	if (save_log_statement_stats)
 		ResetUsage();
@@ -2095,7 +2095,7 @@ exec_execute_message(const char *portal_name, long max_rows)
 
 	pgstat_report_activity(STATE_RUNNING, sourceText);
 
-	set_ps_display(portal->commandTag, false);
+	set_ps_display(portal->commandTag);
 
 	if (save_log_statement_stats)
 		ResetUsage();
@@ -4171,7 +4171,7 @@ PostgresMain(int argc, char *argv[],
 		{
 			if (IsAbortedTransactionBlockState())
 			{
-				set_ps_display("idle in transaction (aborted)", false);
+				set_ps_display("idle in transaction (aborted)");
 				pgstat_report_activity(STATE_IDLEINTRANSACTION_ABORTED, NULL);
 
 				/* Start the idle-in-transaction timer */
@@ -4184,7 +4184,7 @@ PostgresMain(int argc, char *argv[],
 			}
 			else if (IsTransactionOrTransactionBlock())
 			{
-				set_ps_display("idle in transaction", false);
+				set_ps_display("idle in transaction");
 				pgstat_report_activity(STATE_IDLEINTRANSACTION, NULL);
 
 				/* Start the idle-in-transaction timer */
@@ -4211,7 +4211,7 @@ PostgresMain(int argc, char *argv[],
 
 				pgstat_report_stat(false);
 
-				set_ps_display("idle", false);
+				set_ps_display("idle");
 				pgstat_report_activity(STATE_IDLE, NULL);
 			}
 
@@ -4361,7 +4361,7 @@ PostgresMain(int argc, char *argv[],
 
 				/* Report query to various monitoring facilities. */
 				pgstat_report_activity(STATE_FASTPATH, NULL);
-				set_ps_display("<FASTPATH>", false);
+				set_ps_display("<FASTPATH>");
 
 				/* start an xact for this function invocation */
 				start_xact_command();
diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c
index 8a47dcdcb1..f4247ea70d 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -236,6 +236,7 @@ PerformAuthentication(Port *port)
 	/*
 	 * Now perform authentication exchange.
 	 */
+	set_ps_display("authentication");
 	ClientAuthentication(port); /* might not return, if failure */
 
 	/*
@@ -303,7 +304,7 @@ PerformAuthentication(Port *port)
 		}
 	}
 
-	set_ps_display("startup", false);
+	set_ps_display("startup");
 
 	ClientAuthInProgress = false;	/* client_min_messages is active now */
 }
diff --git a/src/backend/utils/misc/ps_status.c b/src/backend/utils/misc/ps_status.c
index ed23c840e9..8b160c0b40 100644
--- a/src/backend/utils/misc/ps_status.c
+++ b/src/backend/utils/misc/ps_status.c
@@ -250,12 +250,11 @@ save_ps_display_args(int argc, char **argv)
  * values.  At this point, the original argv[] array may be overwritten.
  */
 void
-init_ps_display(const char *username, const char *dbname,
-				const char *host_info, const char *initial_str)
+init_ps_display(const char *fixed_part)
 {
-	Assert(username);
-	Assert(dbname);
-	Assert(host_info);
+	bool		save_update_process_title;
+
+	Assert(fixed_part);
 
 #ifndef PS_USE_NONE
 	/* no ps display for stand-alone backend */
@@ -309,19 +308,25 @@ init_ps_display(const char *username, const char *dbname,
 	if (*cluster_name == '\0')
 	{
 		snprintf(ps_buffer, ps_buffer_size,
-				 PROGRAM_NAME_PREFIX "%s %s %s ",
-				 username, dbname, host_info);
+				 PROGRAM_NAME_PREFIX "%s ",
+				 fixed_part);
 	}
 	else
 	{
 		snprintf(ps_buffer, ps_buffer_size,
-				 PROGRAM_NAME_PREFIX "%s: %s %s %s ",
-				 cluster_name, username, dbname, host_info);
+				 PROGRAM_NAME_PREFIX "%s: %s ",
+				 cluster_name, fixed_part);
 	}
 
 	ps_buffer_cur_len = ps_buffer_fixed_size = strlen(ps_buffer);
 
-	set_ps_display(initial_str, true);
+	/*
+	 * On the first run, force the update.
+	 */
+	save_update_process_title = update_process_title;
+	update_process_title = true;
+	set_ps_display("");
+	update_process_title = save_update_process_title;
 #endif							/* not PS_USE_NONE */
 }
 
@@ -332,11 +337,11 @@ init_ps_display(const char *username, const char *dbname,
  * indication of what you're currently doing passed in the argument.
  */
 void
-set_ps_display(const char *activity, bool force)
+set_ps_display(const char *activity)
 {
 #ifndef PS_USE_NONE
-	/* update_process_title=off disables updates, unless force = true */
-	if (!force && !update_process_title)
+	/* update_process_title=off disables updates */
+	if (!update_process_title)
 		return;
 
 	/* no ps display for stand-alone backend */
diff --git a/src/include/utils/ps_status.h b/src/include/utils/ps_status.h
index 23f1e59ffc..9f43e1fdf0 100644
--- a/src/include/utils/ps_status.h
+++ b/src/include/utils/ps_status.h
@@ -16,10 +16,9 @@ extern bool update_process_title;
 
 extern char **save_ps_display_args(int argc, char **argv);
 
-extern void init_ps_display(const char *username, const char *dbname,
-							const char *host_info, const char *initial_str);
+extern void init_ps_display(const char *fixed_part);
 
-extern void set_ps_display(const char *activity, bool force);
+extern void set_ps_display(const char *activity);
 
 extern const char *get_ps_display(int *displen);
 
-- 
2.25.0

v2-0002-Unify-several-ways-to-tracking-backend-type.patchtext/plain; charset=UTF-8; name=v2-0002-Unify-several-ways-to-tracking-backend-type.patch; x-mac-creator=0; x-mac-type=0Download
From 6a7f92c36119b345d8ec5b389e35af638cedf934 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Thu, 20 Feb 2020 18:14:33 +0100
Subject: [PATCH v2 2/4] 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.
---
 src/backend/bootstrap/bootstrap.c   | 48 ++++++++----------
 src/backend/postmaster/autovacuum.c |  8 +--
 src/backend/postmaster/bgworker.c   |  2 +-
 src/backend/postmaster/pgarch.c     |  6 +--
 src/backend/postmaster/pgstat.c     | 75 ++++++-----------------------
 src/backend/postmaster/postmaster.c |  5 ++
 src/backend/postmaster/syslogger.c  |  3 +-
 src/backend/utils/init/globals.c    |  2 +
 src/backend/utils/misc/ps_status.c  | 11 ++++-
 src/include/miscadmin.h             | 20 ++++++++
 src/include/pgstat.h                | 20 +-------
 11 files changed, 81 insertions(+), 119 deletions(-)

diff --git a/src/backend/bootstrap/bootstrap.c b/src/backend/bootstrap/bootstrap.c
index 4212333737..d090e68607 100644
--- a/src/backend/bootstrap/bootstrap.c
+++ b/src/backend/bootstrap/bootstrap.c
@@ -314,36 +314,28 @@ AuxiliaryProcessMain(int argc, char *argv[])
 		proc_exit(1);
 	}
 
-	/*
-	 * Identify myself via ps
-	 */
-	if (IsUnderPostmaster)
+	switch (MyAuxProcType)
 	{
-		const char *statmsg;
-
-		switch (MyAuxProcType)
-		{
-			case StartupProcess:
-				statmsg = pgstat_get_backend_desc(B_STARTUP);
-				break;
-			case BgWriterProcess:
-				statmsg = pgstat_get_backend_desc(B_BG_WRITER);
-				break;
-			case CheckpointerProcess:
-				statmsg = pgstat_get_backend_desc(B_CHECKPOINTER);
-				break;
-			case WalWriterProcess:
-				statmsg = pgstat_get_backend_desc(B_WAL_WRITER);
-				break;
-			case WalReceiverProcess:
-				statmsg = pgstat_get_backend_desc(B_WAL_RECEIVER);
-				break;
-			default:
-				statmsg = "??? process";
-				break;
-		}
-		init_ps_display(statmsg);
+		case StartupProcess:
+			MyBackendType = B_STARTUP;
+			break;
+		case BgWriterProcess:
+			MyBackendType = B_BG_WRITER;
+			break;
+		case CheckpointerProcess:
+			MyBackendType = B_CHECKPOINTER;
+			break;
+		case WalWriterProcess:
+			MyBackendType = B_WAL_WRITER;
+			break;
+		case WalReceiverProcess:
+			MyBackendType = B_WAL_RECEIVER;
+			break;
+		default:
+			MyBackendType = B_INVALID;
 	}
+	if (IsUnderPostmaster)
+		init_ps_display(NULL);
 
 	/* Acquire configuration parameters, unless inherited from postmaster */
 	if (!IsUnderPostmaster)
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index c461af11b2..c152b5b83a 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -433,8 +433,8 @@ AutoVacLauncherMain(int argc, char *argv[])
 
 	am_autovacuum_launcher = true;
 
-	/* Identify myself via ps */
-	init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_LAUNCHER));
+	MyBackendType = B_AUTOVAC_LAUNCHER;
+	init_ps_display(NULL);
 
 	ereport(DEBUG1,
 			(errmsg("autovacuum launcher started")));
@@ -1506,8 +1506,8 @@ AutoVacWorkerMain(int argc, char *argv[])
 
 	am_autovacuum_worker = true;
 
-	/* Identify myself via ps */
-	init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_WORKER));
+	MyBackendType = B_AUTOVAC_WORKER;
+	init_ps_display(NULL);
 
 	SetProcessingMode(InitProcessing);
 
diff --git a/src/backend/postmaster/bgworker.c b/src/backend/postmaster/bgworker.c
index 684250984d..6c684b5e12 100644
--- a/src/backend/postmaster/bgworker.c
+++ b/src/backend/postmaster/bgworker.c
@@ -688,7 +688,7 @@ StartBackgroundWorker(void)
 
 	IsBackgroundWorker = true;
 
-	/* Identify myself via ps */
+	MyBackendType = B_BG_WORKER;
 	init_ps_display(worker->bgw_name);
 
 	/*
diff --git a/src/backend/postmaster/pgarch.c b/src/backend/postmaster/pgarch.c
index 58f54544f6..01ffd6513c 100644
--- a/src/backend/postmaster/pgarch.c
+++ b/src/backend/postmaster/pgarch.c
@@ -238,10 +238,8 @@ PgArchiverMain(int argc, char *argv[])
 	pqsignal(SIGCHLD, SIG_DFL);
 	PG_SETMASK(&UnBlockSig);
 
-	/*
-	 * Identify myself via ps
-	 */
-	init_ps_display("archiver");
+	MyBackendType = B_ARCHIVER;
+	init_ps_display(NULL);
 
 	pgarch_MainLoop();
 
diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index 3934d03396..94f0953fd8 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -2889,62 +2889,7 @@ pgstat_bestart(void)
 	 * out-of-line data.  Those have to be handled separately, below.
 	 */
 	lbeentry.st_procpid = MyProcPid;
-
-	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_backendType = MyBackendType;
 	lbeentry.st_proc_start_timestamp = MyStartTimestamp;
 	lbeentry.st_activity_start_timestamp = 0;
 	lbeentry.st_state_start_timestamp = 0;
@@ -4273,6 +4218,9 @@ pgstat_get_backend_desc(BackendType backendType)
 
 	switch (backendType)
 	{
+		case B_INVALID:
+			backendDesc = "not initialized";
+			break;
 		case B_AUTOVAC_LAUNCHER:
 			backendDesc = "autovacuum launcher";
 			break;
@@ -4303,6 +4251,15 @@ pgstat_get_backend_desc(BackendType backendType)
 		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;
@@ -4444,10 +4401,8 @@ PgstatCollectorMain(int argc, char *argv[])
 	pqsignal(SIGCHLD, SIG_DFL);
 	PG_SETMASK(&UnBlockSig);
 
-	/*
-	 * Identify myself via ps
-	 */
-	init_ps_display("stats collector");
+	MyBackendType = B_STATS_COLLECTOR;
+	init_ps_display(NULL);
 
 	/*
 	 * Read in existing stats files or initialize the stats to zero.
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 386fc54e6f..ff51d33b74 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -2259,6 +2259,11 @@ ProcessStartupPacket(Port *port, bool secure_done)
 	if (strlen(port->user_name) >= NAMEDATALEN)
 		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
 	 * connected to a particular database. But walsenders used for logical
diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c
index b0ab5d3076..20c59e9b6a 100644
--- a/src/backend/postmaster/syslogger.c
+++ b/src/backend/postmaster/syslogger.c
@@ -179,7 +179,8 @@ SysLoggerMain(int argc, char *argv[])
 
 	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
diff --git a/src/backend/utils/init/globals.c b/src/backend/utils/init/globals.c
index eb19644419..e75a29def7 100644
--- a/src/backend/utils/init/globals.c
+++ b/src/backend/utils/init/globals.c
@@ -78,6 +78,8 @@ char		postgres_exec_path[MAXPGPATH];	/* full path to backend */
 /* note: currently this is not valid in backend processes */
 #endif
 
+BackendType	MyBackendType;
+
 BackendId	MyBackendId = InvalidBackendId;
 
 BackendId	ParallelMasterBackendId = InvalidBackendId;
diff --git a/src/backend/utils/misc/ps_status.c b/src/backend/utils/misc/ps_status.c
index 8b160c0b40..0685a02a31 100644
--- a/src/backend/utils/misc/ps_status.c
+++ b/src/backend/utils/misc/ps_status.c
@@ -28,6 +28,7 @@
 
 #include "libpq/libpq.h"
 #include "miscadmin.h"
+#include "pgstat.h"
 #include "utils/guc.h"
 #include "utils/ps_status.h"
 
@@ -247,14 +248,20 @@ save_ps_display_args(int argc, char **argv)
 
 /*
  * 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 BackendType.
+ *
+ * At this point, the original argv[] array may be overwritten.
  */
 void
 init_ps_display(const char *fixed_part)
 {
 	bool		save_update_process_title;
 
-	Assert(fixed_part);
+	Assert(fixed_part || MyBackendType);
+	if (!fixed_part)
+		fixed_part = pgstat_get_backend_desc(MyBackendType);
 
 #ifndef PS_USE_NONE
 	/* no ps display for stand-alone backend */
diff --git a/src/include/miscadmin.h b/src/include/miscadmin.h
index f985453ec3..a41b6331da 100644
--- a/src/include/miscadmin.h
+++ b/src/include/miscadmin.h
@@ -178,6 +178,26 @@ extern char pkglib_path[];
 extern char postgres_exec_path[];
 #endif
 
+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;
+
 /*
  * done in storage/backendid.h for now.
  *
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index 3a65a51696..0facf7251b 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -13,6 +13,7 @@
 
 #include "datatype/timestamp.h"
 #include "libpq/pqcomm.h"
+#include "miscadmin.h"
 #include "port/atomics.h"
 #include "portability/instr_time.h"
 #include "postmaster/pgarch.h"
@@ -712,25 +713,6 @@ typedef struct 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
  * ----------
-- 
2.25.0

v2-0003-Add-backend-type-to-csvlog-and-optionally-log_lin.patchtext/plain; charset=UTF-8; name=v2-0003-Add-backend-type-to-csvlog-and-optionally-log_lin.patch; x-mac-creator=0; x-mac-type=0Download
From 75ac8ed0c47801712eb2aa300d9cb29767d2e121 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Thu, 20 Feb 2020 18:16:39 +0100
Subject: [PATCH v2 3/4] Add backend type to csvlog and optionally
 log_line_prefix

---
 doc/src/sgml/config.sgml                      |  8 ++++-
 src/backend/utils/error/elog.c                | 29 +++++++++++++++++++
 src/backend/utils/misc/postgresql.conf.sample |  1 +
 src/test/regress/pg_regress.c                 |  2 +-
 4 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index c1128f89ec..206778b1c3 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -6470,6 +6470,11 @@ <title>What to Log</title>
              <entry>Application name</entry>
              <entry>yes</entry>
             </row>
+            <row>
+             <entry><literal>%b</literal></entry>
+             <entry>Backend process type</entry>
+             <entry>yes</entry>
+            </row>
             <row>
              <entry><literal>%u</literal></entry>
              <entry>User name</entry>
@@ -6785,7 +6790,7 @@ <title>Using CSV-Format Log Output</title>
         character count of the error position therein,
         location of the error in the PostgreSQL source code
         (if <varname>log_error_verbosity</varname> is set to <literal>verbose</literal>),
-        and application name.
+        application name, and backend type.
         Here is a sample table definition for storing CSV-format log output:
 
 <programlisting>
@@ -6814,6 +6819,7 @@ <title>Using CSV-Format Log Output</title>
   query_pos integer,
   location text,
   application_name text,
+  backend_type text,
   PRIMARY KEY (session_id, session_line_num)
 );
 </programlisting>
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index f5b0211f66..bb65eb41ca 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -72,6 +72,8 @@
 #include "libpq/pqformat.h"
 #include "mb/pg_wchar.h"
 #include "miscadmin.h"
+#include "pgstat.h"
+#include "postmaster/bgworker.h"
 #include "postmaster/postmaster.h"
 #include "postmaster/syslogger.h"
 #include "storage/ipc.h"
@@ -2492,6 +2494,23 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
 										   padding > 0 ? padding : -padding);
 
 				break;
+			case 'b':
+				{
+					const char *backend_type_str;
+
+					if (MyProcPid == PostmasterPid)
+						backend_type_str = "postmaster";
+					else if (MyBackendType == B_BG_WORKER)
+						backend_type_str = MyBgworkerEntry->bgw_type;
+					else
+						backend_type_str = pgstat_get_backend_desc(MyBackendType);
+
+					if (padding != 0)
+						appendStringInfo(buf, "%*s", padding, backend_type_str);
+					else
+						appendStringInfoString(buf, backend_type_str);
+					break;
+				}
 			case 'u':
 				if (MyProcPort)
 				{
@@ -2920,6 +2939,16 @@ write_csvlog(ErrorData *edata)
 	if (application_name)
 		appendCSVLiteral(&buf, application_name);
 
+	appendStringInfoChar(&buf, ',');
+
+	/* backend type */
+	if (MyProcPid == PostmasterPid)
+		appendCSVLiteral(&buf, "postmaster");
+	else if (MyBackendType == B_BG_WORKER)
+		appendCSVLiteral(&buf, MyBgworkerEntry->bgw_type);
+	else
+		appendCSVLiteral(&buf, pgstat_get_backend_desc(MyBackendType));
+
 	appendStringInfoChar(&buf, '\n');
 
 	/* If in the syslogger process, try to write messages direct to file */
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
index e1048c0047..323f120edd 100644
--- a/src/backend/utils/misc/postgresql.conf.sample
+++ b/src/backend/utils/misc/postgresql.conf.sample
@@ -524,6 +524,7 @@
 #log_hostname = off
 #log_line_prefix = '%m [%p] '		# special values:
 					#   %a = application name
+					#   %b = backend type
 					#   %u = user name
 					#   %d = database name
 					#   %r = remote host and port
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index 9a4e52bc7b..37fcdaabe2 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -2333,7 +2333,7 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
 		fputs("\n# Configuration added by pg_regress\n\n", pg_conf);
 		fputs("log_autovacuum_min_duration = 0\n", pg_conf);
 		fputs("log_checkpoints = on\n", pg_conf);
-		fputs("log_line_prefix = '%m [%p] %q%a '\n", pg_conf);
+		fputs("log_line_prefix = '%m %b[%p] %q%a '\n", pg_conf);
 		fputs("log_lock_waits = on\n", pg_conf);
 		fputs("log_temp_files = 128kB\n", pg_conf);
 		fputs("max_prepared_transactions = 2\n", pg_conf);
-- 
2.25.0

v2-0004-Remove-am_syslogger-global-variable.patchtext/plain; charset=UTF-8; name=v2-0004-Remove-am_syslogger-global-variable.patch; x-mac-creator=0; x-mac-type=0Download
From 2ffee681f3c17a674f3cab1f512dfbbd79e43357 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Thu, 20 Feb 2020 18:17:28 +0100
Subject: [PATCH v2 4/4] Remove am_syslogger global variable

Use MyBackendType instead.
---
 src/backend/postmaster/syslogger.c | 9 +--------
 src/backend/utils/error/elog.c     | 8 ++++----
 src/include/postmaster/syslogger.h | 2 --
 3 files changed, 5 insertions(+), 14 deletions(-)

diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c
index 20c59e9b6a..cfb189550e 100644
--- a/src/backend/postmaster/syslogger.c
+++ b/src/backend/postmaster/syslogger.c
@@ -74,11 +74,6 @@ char	   *Log_filename = NULL;
 bool		Log_truncate_on_rotation = false;
 int			Log_file_mode = S_IRUSR | S_IWUSR;
 
-/*
- * Globally visible state (used by elog.c)
- */
-bool		am_syslogger = false;
-
 extern bool redirection_done;
 
 /*
@@ -177,8 +172,6 @@ SysLoggerMain(int argc, char *argv[])
 	syslogger_parseArgs(argc, argv);
 #endif							/* EXEC_BACKEND */
 
-	am_syslogger = true;
-
 	MyBackendType = B_LOGGER;
 	init_ps_display(NULL);
 
@@ -1073,7 +1066,7 @@ flush_pipe_input(char *logbuffer, int *bytes_in_logbuffer)
 /*
  * Write text to the currently open logfile
  *
- * This is exported so that elog.c can call it when am_syslogger is true.
+ * This is exported so that elog.c can call it when BackendType is B_LOGGER.
  * This allows the syslogger process to record elog messages of its own,
  * even though its stderr does not point at the syslog pipe.
  */
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index bb65eb41ca..3a6f7f9456 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -2952,7 +2952,7 @@ write_csvlog(ErrorData *edata)
 	appendStringInfoChar(&buf, '\n');
 
 	/* If in the syslogger process, try to write messages direct to file */
-	if (am_syslogger)
+	if (MyBackendType == B_LOGGER)
 		write_syslogger_file(buf.data, buf.len, LOG_DESTINATION_CSVLOG);
 	else
 		write_pipe_chunks(buf.data, buf.len, LOG_DESTINATION_CSVLOG);
@@ -3146,7 +3146,7 @@ send_message_to_server_log(ErrorData *edata)
 		 * catching stderr output, and we are not ourselves the syslogger.
 		 * Otherwise, just do a vanilla write to stderr.
 		 */
-		if (redirection_done && !am_syslogger)
+		if (redirection_done && MyBackendType != B_LOGGER)
 			write_pipe_chunks(buf.data, buf.len, LOG_DESTINATION_STDERR);
 #ifdef WIN32
 
@@ -3165,13 +3165,13 @@ send_message_to_server_log(ErrorData *edata)
 	}
 
 	/* If in the syslogger process, try to write messages direct to file */
-	if (am_syslogger)
+	if (MyBackendType == B_LOGGER)
 		write_syslogger_file(buf.data, buf.len, LOG_DESTINATION_STDERR);
 
 	/* Write to CSV log if enabled */
 	if (Log_destination & LOG_DESTINATION_CSVLOG)
 	{
-		if (redirection_done || am_syslogger)
+		if (redirection_done || MyBackendType == B_LOGGER)
 		{
 			/*
 			 * send CSV data if it's safe to do so (syslogger doesn't need the
diff --git a/src/include/postmaster/syslogger.h b/src/include/postmaster/syslogger.h
index 9b7a386dbd..f611bd1411 100644
--- a/src/include/postmaster/syslogger.h
+++ b/src/include/postmaster/syslogger.h
@@ -70,8 +70,6 @@ extern PGDLLIMPORT char *Log_filename;
 extern bool Log_truncate_on_rotation;
 extern int	Log_file_mode;
 
-extern bool am_syslogger;
-
 #ifndef WIN32
 extern int	syslogPipe[2];
 #else
-- 
2.25.0

#6Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Peter Eisentraut (#5)
4 attachment(s)
Re: backend type in log_line_prefix?

Updated patch set because of conflicts.

On 2020-02-21 10:09, Peter Eisentraut wrote:

After positive initial feedback, here is a more ambitious patch set. In
particular, I wanted to avoid having to specify the backend type (at
least) twice, once for the ps display and once for this new facility.

I have added a new global variable MyBackendType that uses the existing
BackendType enum that was previously only used by the stats collector.
Then the ps display, the stats collector, the log_line_prefix, and other
places can just refer to this to know "who am I". (There are more
places like that, for example in the autovacuum system, so patch 0004 in
particular could be expanded in analogous ways.)

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachments:

v3-0001-Refactor-ps_status.c-API.patchtext/plain; charset=UTF-8; name=v3-0001-Refactor-ps_status.c-API.patch; x-mac-creator=0; x-mac-type=0Download
From bf28870a342871400b6fbf07bbb5eaa71e23d3bd Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Thu, 20 Feb 2020 18:07:37 +0100
Subject: [PATCH v3 1/4] Refactor ps_status.c API

The init_ps_display() arguments were mostly lies by now, so to match
typical usage, just use one argument and let the caller assemble it
from multiple sources if necessary.  The only user of the additional
arguments is BackendInitialize(), which was already doing string
assembly on the caller side anyway.

Remove the second argument of set_ps_display() ("force") and just
handle that in init_ps_display() internally.

BackendInitialize() also used to set the initial status as
"authentication", but that was very far from where authentication
actually happened.  So now it's set to "initializing" and then
"authentication" just before the actual call to
ClientAuthentication().
---
 src/backend/access/transam/xlog.c     |  4 ++--
 src/backend/bootstrap/bootstrap.c     |  2 +-
 src/backend/commands/async.c          |  4 ++--
 src/backend/postmaster/autovacuum.c   |  6 ++---
 src/backend/postmaster/bgworker.c     |  2 +-
 src/backend/postmaster/pgarch.c       |  8 +++----
 src/backend/postmaster/pgstat.c       |  2 +-
 src/backend/postmaster/postmaster.c   | 32 ++++++++++++---------------
 src/backend/postmaster/syslogger.c    |  2 +-
 src/backend/replication/basebackup.c  |  2 +-
 src/backend/replication/syncrep.c     |  4 ++--
 src/backend/replication/walreceiver.c |  7 +++---
 src/backend/replication/walsender.c   |  2 +-
 src/backend/storage/ipc/standby.c     |  4 ++--
 src/backend/storage/lmgr/lock.c       |  6 ++---
 src/backend/tcop/postgres.c           | 16 +++++++-------
 src/backend/utils/init/postinit.c     |  3 ++-
 src/backend/utils/misc/ps_status.c    | 31 +++++++++++++++-----------
 src/include/utils/ps_status.h         |  5 ++---
 19 files changed, 71 insertions(+), 71 deletions(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 4361568882..182e65b2df 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -3648,7 +3648,7 @@ XLogFileRead(XLogSegNo segno, int emode, TimeLineID tli,
 			/* Report recovery progress in PS display */
 			snprintf(activitymsg, sizeof(activitymsg), "waiting for %s",
 					 xlogfname);
-			set_ps_display(activitymsg, false);
+			set_ps_display(activitymsg);
 
 			restoredFromArchive = RestoreArchivedFile(path, xlogfname,
 													  "RECOVERYXLOG",
@@ -3691,7 +3691,7 @@ XLogFileRead(XLogSegNo segno, int emode, TimeLineID tli,
 		/* Report recovery progress in PS display */
 		snprintf(activitymsg, sizeof(activitymsg), "recovering %s",
 				 xlogfname);
-		set_ps_display(activitymsg, false);
+		set_ps_display(activitymsg);
 
 		/* Track source of data in assorted state variables */
 		readSource = source;
diff --git a/src/backend/bootstrap/bootstrap.c b/src/backend/bootstrap/bootstrap.c
index 657b18ecc8..7923d1ec9b 100644
--- a/src/backend/bootstrap/bootstrap.c
+++ b/src/backend/bootstrap/bootstrap.c
@@ -342,7 +342,7 @@ AuxiliaryProcessMain(int argc, char *argv[])
 				statmsg = "??? process";
 				break;
 		}
-		init_ps_display(statmsg, "", "", "");
+		init_ps_display(statmsg);
 	}
 
 	/* Acquire configuration parameters, unless inherited from postmaster */
diff --git a/src/backend/commands/async.c b/src/backend/commands/async.c
index dae939a4ab..0c9d20ebfc 100644
--- a/src/backend/commands/async.c
+++ b/src/backend/commands/async.c
@@ -2225,7 +2225,7 @@ ProcessIncomingNotify(void)
 	if (Trace_notify)
 		elog(DEBUG1, "ProcessIncomingNotify");
 
-	set_ps_display("notify interrupt", false);
+	set_ps_display("notify interrupt");
 
 	/*
 	 * We must run asyncQueueReadAllNotifications inside a transaction, else
@@ -2242,7 +2242,7 @@ ProcessIncomingNotify(void)
 	 */
 	pq_flush();
 
-	set_ps_display("idle", false);
+	set_ps_display("idle");
 
 	if (Trace_notify)
 		elog(DEBUG1, "ProcessIncomingNotify: done");
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index e3a43d3296..a6499fc3da 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -434,7 +434,7 @@ AutoVacLauncherMain(int argc, char *argv[])
 	am_autovacuum_launcher = true;
 
 	/* Identify myself via ps */
-	init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_LAUNCHER), "", "", "");
+	init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_LAUNCHER));
 
 	ereport(DEBUG1,
 			(errmsg("autovacuum launcher started")));
@@ -1507,7 +1507,7 @@ AutoVacWorkerMain(int argc, char *argv[])
 	am_autovacuum_worker = true;
 
 	/* Identify myself via ps */
-	init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_WORKER), "", "", "");
+	init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_WORKER));
 
 	SetProcessingMode(InitProcessing);
 
@@ -1680,7 +1680,7 @@ AutoVacWorkerMain(int argc, char *argv[])
 		 */
 		InitPostgres(NULL, dbid, NULL, InvalidOid, dbname, false);
 		SetProcessingMode(NormalProcessing);
-		set_ps_display(dbname, false);
+		set_ps_display(dbname);
 		ereport(DEBUG1,
 				(errmsg("autovacuum: processing database \"%s\"", dbname)));
 
diff --git a/src/backend/postmaster/bgworker.c b/src/backend/postmaster/bgworker.c
index 75fc0d5d33..684250984d 100644
--- a/src/backend/postmaster/bgworker.c
+++ b/src/backend/postmaster/bgworker.c
@@ -689,7 +689,7 @@ StartBackgroundWorker(void)
 	IsBackgroundWorker = true;
 
 	/* Identify myself via ps */
-	init_ps_display(worker->bgw_name, "", "", "");
+	init_ps_display(worker->bgw_name);
 
 	/*
 	 * If we're not supposed to have shared memory access, then detach from
diff --git a/src/backend/postmaster/pgarch.c b/src/backend/postmaster/pgarch.c
index 3ca30badb2..58f54544f6 100644
--- a/src/backend/postmaster/pgarch.c
+++ b/src/backend/postmaster/pgarch.c
@@ -241,7 +241,7 @@ PgArchiverMain(int argc, char *argv[])
 	/*
 	 * Identify myself via ps
 	 */
-	init_ps_display("archiver", "", "", "");
+	init_ps_display("archiver");
 
 	pgarch_MainLoop();
 
@@ -584,7 +584,7 @@ pgarch_archiveXlog(char *xlog)
 
 	/* Report archive activity in PS display */
 	snprintf(activitymsg, sizeof(activitymsg), "archiving %s", xlog);
-	set_ps_display(activitymsg, false);
+	set_ps_display(activitymsg);
 
 	rc = system(xlogarchcmd);
 	if (rc != 0)
@@ -634,14 +634,14 @@ pgarch_archiveXlog(char *xlog)
 		}
 
 		snprintf(activitymsg, sizeof(activitymsg), "failed on %s", xlog);
-		set_ps_display(activitymsg, false);
+		set_ps_display(activitymsg);
 
 		return false;
 	}
 	elog(DEBUG1, "archived write-ahead log file \"%s\"", xlog);
 
 	snprintf(activitymsg, sizeof(activitymsg), "last was %s", xlog);
-	set_ps_display(activitymsg, false);
+	set_ps_display(activitymsg);
 
 	return true;
 }
diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index 462b4d7e06..107c965336 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -4450,7 +4450,7 @@ PgstatCollectorMain(int argc, char *argv[])
 	/*
 	 * Identify myself via ps
 	 */
-	init_ps_display("stats collector", "", "", "");
+	init_ps_display("stats collector");
 
 	/*
 	 * Read in existing stats files or initialize the stats to zero.
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 55187eb910..46be78aadb 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -4282,7 +4282,7 @@ BackendInitialize(Port *port)
 	int			ret;
 	char		remote_host[NI_MAXHOST];
 	char		remote_port[NI_MAXSERV];
-	char		remote_ps_data[NI_MAXHOST];
+	StringInfoData ps_data;
 
 	/* Save port etc. for ps status */
 	MyProcPort = port;
@@ -4346,10 +4346,6 @@ BackendInitialize(Port *port)
 		ereport(WARNING,
 				(errmsg_internal("pg_getnameinfo_all() failed: %s",
 								 gai_strerror(ret))));
-	if (remote_port[0] == '\0')
-		snprintf(remote_ps_data, sizeof(remote_ps_data), "%s", remote_host);
-	else
-		snprintf(remote_ps_data, sizeof(remote_ps_data), "%s(%s)", remote_host, remote_port);
 
 	/*
 	 * Save remote_host and remote_port in port structure (after this, they
@@ -4423,21 +4419,21 @@ BackendInitialize(Port *port)
 	/*
 	 * Now that we have the user and database name, we can set the process
 	 * title for ps.  It's good to do this as early as possible in startup.
-	 *
-	 * For a walsender, the ps display is set in the following form:
-	 *
-	 * postgres: walsender <user> <host> <activity>
-	 *
-	 * To achieve that, we pass "walsender" as username and username as dbname
-	 * to init_ps_display(). XXX: should add a new variant of
-	 * init_ps_display() to avoid abusing the parameters like this.
 	 */
+	initStringInfo(&ps_data);
 	if (am_walsender)
-		init_ps_display(pgstat_get_backend_desc(B_WAL_SENDER), port->user_name, remote_ps_data,
-						update_process_title ? "authentication" : "");
-	else
-		init_ps_display(port->user_name, port->database_name, remote_ps_data,
-						update_process_title ? "authentication" : "");
+		appendStringInfo(&ps_data, "%s ", pgstat_get_backend_desc(B_WAL_SENDER));
+	appendStringInfo(&ps_data, "%s ", port->user_name);
+	if (!am_walsender)
+		appendStringInfo(&ps_data, "%s ", port->database_name);
+	appendStringInfo(&ps_data, "%s", port->remote_host);
+	if (port->remote_port[0] != '\0')
+		appendStringInfo(&ps_data, "(%s)", port->remote_port);
+
+	init_ps_display(ps_data.data);
+	pfree(ps_data.data);
+
+	set_ps_display("initializing");
 
 	/*
 	 * Disable the timeout, and prevent SIGTERM/SIGQUIT again.
diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c
index cf7b535e4e..b394599236 100644
--- a/src/backend/postmaster/syslogger.c
+++ b/src/backend/postmaster/syslogger.c
@@ -179,7 +179,7 @@ SysLoggerMain(int argc, char *argv[])
 
 	am_syslogger = true;
 
-	init_ps_display("logger", "", "", "");
+	init_ps_display("logger");
 
 	/*
 	 * If we restarted, our stderr is already redirected into our own input
diff --git a/src/backend/replication/basebackup.c b/src/backend/replication/basebackup.c
index f66cbc2428..806d013108 100644
--- a/src/backend/replication/basebackup.c
+++ b/src/backend/replication/basebackup.c
@@ -828,7 +828,7 @@ SendBaseBackup(BaseBackupCmd *cmd)
 
 		snprintf(activitymsg, sizeof(activitymsg), "sending backup \"%s\"",
 				 opt.label);
-		set_ps_display(activitymsg, false);
+		set_ps_display(activitymsg);
 	}
 
 	perform_base_backup(&opt);
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index c284103b54..ffd5b31eb2 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -209,7 +209,7 @@ SyncRepWaitForLSN(XLogRecPtr lsn, bool commit)
 		memcpy(new_status, old_status, len);
 		sprintf(new_status + len, " waiting for %X/%X",
 				(uint32) (lsn >> 32), (uint32) lsn);
-		set_ps_display(new_status, false);
+		set_ps_display(new_status);
 		new_status[len] = '\0'; /* truncate off " waiting ..." */
 	}
 
@@ -311,7 +311,7 @@ SyncRepWaitForLSN(XLogRecPtr lsn, bool commit)
 	if (new_status)
 	{
 		/* Reset ps display */
-		set_ps_display(new_status, false);
+		set_ps_display(new_status);
 		pfree(new_status);
 	}
 }
diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c
index 2ab15c3cbb..930cc03e0f 100644
--- a/src/backend/replication/walreceiver.c
+++ b/src/backend/replication/walreceiver.c
@@ -665,8 +665,7 @@ WalRcvWaitForStartPosition(XLogRecPtr *startpoint, TimeLineID *startpointTLI)
 	walrcv->receiveStartTLI = 0;
 	SpinLockRelease(&walrcv->mutex);
 
-	if (update_process_title)
-		set_ps_display("idle", false);
+	set_ps_display("idle");
 
 	/*
 	 * nudge startup process to notice that we've stopped streaming and are
@@ -714,7 +713,7 @@ WalRcvWaitForStartPosition(XLogRecPtr *startpoint, TimeLineID *startpointTLI)
 		snprintf(activitymsg, sizeof(activitymsg), "restarting at %X/%X",
 				 (uint32) (*startpoint >> 32),
 				 (uint32) *startpoint);
-		set_ps_display(activitymsg, false);
+		set_ps_display(activitymsg);
 	}
 }
 
@@ -1027,7 +1026,7 @@ XLogWalRcvFlush(bool dying)
 			snprintf(activitymsg, sizeof(activitymsg), "streaming %X/%X",
 					 (uint32) (LogstreamResult.Write >> 32),
 					 (uint32) LogstreamResult.Write);
-			set_ps_display(activitymsg, false);
+			set_ps_display(activitymsg);
 		}
 
 		/* Also let the master know that we made some progress */
diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index ae4a9cbe11..4a598b906b 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -2768,7 +2768,7 @@ XLogSendPhysical(void)
 
 		snprintf(activitymsg, sizeof(activitymsg), "streaming %X/%X",
 				 (uint32) (sentPtr >> 32), (uint32) sentPtr);
-		set_ps_display(activitymsg, false);
+		set_ps_display(activitymsg);
 	}
 }
 
diff --git a/src/backend/storage/ipc/standby.c b/src/backend/storage/ipc/standby.c
index 3090e57fa4..0a53230714 100644
--- a/src/backend/storage/ipc/standby.c
+++ b/src/backend/storage/ipc/standby.c
@@ -254,7 +254,7 @@ ResolveRecoveryConflictWithVirtualXIDs(VirtualTransactionId *waitlist,
 				new_status = (char *) palloc(len + 8 + 1);
 				memcpy(new_status, old_status, len);
 				strcpy(new_status + len, " waiting");
-				set_ps_display(new_status, false);
+				set_ps_display(new_status);
 				new_status[len] = '\0'; /* truncate off " waiting" */
 			}
 
@@ -285,7 +285,7 @@ ResolveRecoveryConflictWithVirtualXIDs(VirtualTransactionId *waitlist,
 	/* Reset ps display if we changed it */
 	if (new_status)
 	{
-		set_ps_display(new_status, false);
+		set_ps_display(new_status);
 		pfree(new_status);
 	}
 }
diff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmgr/lock.c
index 56dba09299..1df7b8e2ab 100644
--- a/src/backend/storage/lmgr/lock.c
+++ b/src/backend/storage/lmgr/lock.c
@@ -1737,7 +1737,7 @@ WaitOnLock(LOCALLOCK *locallock, ResourceOwner owner)
 		new_status = (char *) palloc(len + 8 + 1);
 		memcpy(new_status, old_status, len);
 		strcpy(new_status + len, " waiting");
-		set_ps_display(new_status, false);
+		set_ps_display(new_status);
 		new_status[len] = '\0'; /* truncate off " waiting" */
 	}
 
@@ -1789,7 +1789,7 @@ WaitOnLock(LOCALLOCK *locallock, ResourceOwner owner)
 		/* Report change to non-waiting status */
 		if (update_process_title)
 		{
-			set_ps_display(new_status, false);
+			set_ps_display(new_status);
 			pfree(new_status);
 		}
 
@@ -1803,7 +1803,7 @@ WaitOnLock(LOCALLOCK *locallock, ResourceOwner owner)
 	/* Report change to non-waiting status */
 	if (update_process_title)
 	{
-		set_ps_display(new_status, false);
+		set_ps_display(new_status);
 		pfree(new_status);
 	}
 
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 9dba3b0566..00c77b66c7 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -1081,7 +1081,7 @@ exec_simple_query(const char *query_string)
 		 */
 		commandTag = CreateCommandTag(parsetree->stmt);
 
-		set_ps_display(GetCommandTagName(commandTag), false);
+		set_ps_display(GetCommandTagName(commandTag));
 
 		BeginCommand(commandTag, dest);
 
@@ -1365,7 +1365,7 @@ exec_parse_message(const char *query_string,	/* string to execute */
 
 	pgstat_report_activity(STATE_RUNNING, query_string);
 
-	set_ps_display("PARSE", false);
+	set_ps_display("PARSE");
 
 	if (save_log_statement_stats)
 		ResetUsage();
@@ -1656,7 +1656,7 @@ exec_bind_message(StringInfo input_message)
 
 	pgstat_report_activity(STATE_RUNNING, psrc->query_string);
 
-	set_ps_display("BIND", false);
+	set_ps_display("BIND");
 
 	if (save_log_statement_stats)
 		ResetUsage();
@@ -2099,7 +2099,7 @@ exec_execute_message(const char *portal_name, long max_rows)
 
 	pgstat_report_activity(STATE_RUNNING, sourceText);
 
-	set_ps_display(GetCommandTagName(portal->commandTag), false);
+	set_ps_display(GetCommandTagName(portal->commandTag));
 
 	if (save_log_statement_stats)
 		ResetUsage();
@@ -4175,7 +4175,7 @@ PostgresMain(int argc, char *argv[],
 		{
 			if (IsAbortedTransactionBlockState())
 			{
-				set_ps_display("idle in transaction (aborted)", false);
+				set_ps_display("idle in transaction (aborted)");
 				pgstat_report_activity(STATE_IDLEINTRANSACTION_ABORTED, NULL);
 
 				/* Start the idle-in-transaction timer */
@@ -4188,7 +4188,7 @@ PostgresMain(int argc, char *argv[],
 			}
 			else if (IsTransactionOrTransactionBlock())
 			{
-				set_ps_display("idle in transaction", false);
+				set_ps_display("idle in transaction");
 				pgstat_report_activity(STATE_IDLEINTRANSACTION, NULL);
 
 				/* Start the idle-in-transaction timer */
@@ -4215,7 +4215,7 @@ PostgresMain(int argc, char *argv[],
 
 				pgstat_report_stat(false);
 
-				set_ps_display("idle", false);
+				set_ps_display("idle");
 				pgstat_report_activity(STATE_IDLE, NULL);
 			}
 
@@ -4365,7 +4365,7 @@ PostgresMain(int argc, char *argv[],
 
 				/* Report query to various monitoring facilities. */
 				pgstat_report_activity(STATE_FASTPATH, NULL);
-				set_ps_display("<FASTPATH>", false);
+				set_ps_display("<FASTPATH>");
 
 				/* start an xact for this function invocation */
 				start_xact_command();
diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c
index 8a47dcdcb1..f4247ea70d 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -236,6 +236,7 @@ PerformAuthentication(Port *port)
 	/*
 	 * Now perform authentication exchange.
 	 */
+	set_ps_display("authentication");
 	ClientAuthentication(port); /* might not return, if failure */
 
 	/*
@@ -303,7 +304,7 @@ PerformAuthentication(Port *port)
 		}
 	}
 
-	set_ps_display("startup", false);
+	set_ps_display("startup");
 
 	ClientAuthInProgress = false;	/* client_min_messages is active now */
 }
diff --git a/src/backend/utils/misc/ps_status.c b/src/backend/utils/misc/ps_status.c
index ed23c840e9..8b160c0b40 100644
--- a/src/backend/utils/misc/ps_status.c
+++ b/src/backend/utils/misc/ps_status.c
@@ -250,12 +250,11 @@ save_ps_display_args(int argc, char **argv)
  * values.  At this point, the original argv[] array may be overwritten.
  */
 void
-init_ps_display(const char *username, const char *dbname,
-				const char *host_info, const char *initial_str)
+init_ps_display(const char *fixed_part)
 {
-	Assert(username);
-	Assert(dbname);
-	Assert(host_info);
+	bool		save_update_process_title;
+
+	Assert(fixed_part);
 
 #ifndef PS_USE_NONE
 	/* no ps display for stand-alone backend */
@@ -309,19 +308,25 @@ init_ps_display(const char *username, const char *dbname,
 	if (*cluster_name == '\0')
 	{
 		snprintf(ps_buffer, ps_buffer_size,
-				 PROGRAM_NAME_PREFIX "%s %s %s ",
-				 username, dbname, host_info);
+				 PROGRAM_NAME_PREFIX "%s ",
+				 fixed_part);
 	}
 	else
 	{
 		snprintf(ps_buffer, ps_buffer_size,
-				 PROGRAM_NAME_PREFIX "%s: %s %s %s ",
-				 cluster_name, username, dbname, host_info);
+				 PROGRAM_NAME_PREFIX "%s: %s ",
+				 cluster_name, fixed_part);
 	}
 
 	ps_buffer_cur_len = ps_buffer_fixed_size = strlen(ps_buffer);
 
-	set_ps_display(initial_str, true);
+	/*
+	 * On the first run, force the update.
+	 */
+	save_update_process_title = update_process_title;
+	update_process_title = true;
+	set_ps_display("");
+	update_process_title = save_update_process_title;
 #endif							/* not PS_USE_NONE */
 }
 
@@ -332,11 +337,11 @@ init_ps_display(const char *username, const char *dbname,
  * indication of what you're currently doing passed in the argument.
  */
 void
-set_ps_display(const char *activity, bool force)
+set_ps_display(const char *activity)
 {
 #ifndef PS_USE_NONE
-	/* update_process_title=off disables updates, unless force = true */
-	if (!force && !update_process_title)
+	/* update_process_title=off disables updates */
+	if (!update_process_title)
 		return;
 
 	/* no ps display for stand-alone backend */
diff --git a/src/include/utils/ps_status.h b/src/include/utils/ps_status.h
index 23f1e59ffc..9f43e1fdf0 100644
--- a/src/include/utils/ps_status.h
+++ b/src/include/utils/ps_status.h
@@ -16,10 +16,9 @@ extern bool update_process_title;
 
 extern char **save_ps_display_args(int argc, char **argv);
 
-extern void init_ps_display(const char *username, const char *dbname,
-							const char *host_info, const char *initial_str);
+extern void init_ps_display(const char *fixed_part);
 
-extern void set_ps_display(const char *activity, bool force);
+extern void set_ps_display(const char *activity);
 
 extern const char *get_ps_display(int *displen);
 

base-commit: 7e39b968f118c6444bd3a3bd59c3e9d73e652e0c
-- 
2.25.0

v3-0002-Unify-several-ways-to-tracking-backend-type.patchtext/plain; charset=UTF-8; name=v3-0002-Unify-several-ways-to-tracking-backend-type.patch; x-mac-creator=0; x-mac-type=0Download
From ca31388319903484ef5bec3e9fc71cb9de321525 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Thu, 20 Feb 2020 18:14:33 +0100
Subject: [PATCH v3 2/4] 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.
---
 src/backend/bootstrap/bootstrap.c   | 48 ++++++++----------
 src/backend/postmaster/autovacuum.c |  8 +--
 src/backend/postmaster/bgworker.c   |  2 +-
 src/backend/postmaster/pgarch.c     |  6 +--
 src/backend/postmaster/pgstat.c     | 75 ++++++-----------------------
 src/backend/postmaster/postmaster.c |  5 ++
 src/backend/postmaster/syslogger.c  |  3 +-
 src/backend/utils/init/globals.c    |  2 +
 src/backend/utils/misc/ps_status.c  | 11 ++++-
 src/include/miscadmin.h             | 20 ++++++++
 src/include/pgstat.h                | 20 +-------
 11 files changed, 81 insertions(+), 119 deletions(-)

diff --git a/src/backend/bootstrap/bootstrap.c b/src/backend/bootstrap/bootstrap.c
index 7923d1ec9b..5480a024e0 100644
--- a/src/backend/bootstrap/bootstrap.c
+++ b/src/backend/bootstrap/bootstrap.c
@@ -314,36 +314,28 @@ AuxiliaryProcessMain(int argc, char *argv[])
 		proc_exit(1);
 	}
 
-	/*
-	 * Identify myself via ps
-	 */
-	if (IsUnderPostmaster)
+	switch (MyAuxProcType)
 	{
-		const char *statmsg;
-
-		switch (MyAuxProcType)
-		{
-			case StartupProcess:
-				statmsg = pgstat_get_backend_desc(B_STARTUP);
-				break;
-			case BgWriterProcess:
-				statmsg = pgstat_get_backend_desc(B_BG_WRITER);
-				break;
-			case CheckpointerProcess:
-				statmsg = pgstat_get_backend_desc(B_CHECKPOINTER);
-				break;
-			case WalWriterProcess:
-				statmsg = pgstat_get_backend_desc(B_WAL_WRITER);
-				break;
-			case WalReceiverProcess:
-				statmsg = pgstat_get_backend_desc(B_WAL_RECEIVER);
-				break;
-			default:
-				statmsg = "??? process";
-				break;
-		}
-		init_ps_display(statmsg);
+		case StartupProcess:
+			MyBackendType = B_STARTUP;
+			break;
+		case BgWriterProcess:
+			MyBackendType = B_BG_WRITER;
+			break;
+		case CheckpointerProcess:
+			MyBackendType = B_CHECKPOINTER;
+			break;
+		case WalWriterProcess:
+			MyBackendType = B_WAL_WRITER;
+			break;
+		case WalReceiverProcess:
+			MyBackendType = B_WAL_RECEIVER;
+			break;
+		default:
+			MyBackendType = B_INVALID;
 	}
+	if (IsUnderPostmaster)
+		init_ps_display(NULL);
 
 	/* Acquire configuration parameters, unless inherited from postmaster */
 	if (!IsUnderPostmaster)
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index a6499fc3da..da75e755f0 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -433,8 +433,8 @@ AutoVacLauncherMain(int argc, char *argv[])
 
 	am_autovacuum_launcher = true;
 
-	/* Identify myself via ps */
-	init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_LAUNCHER));
+	MyBackendType = B_AUTOVAC_LAUNCHER;
+	init_ps_display(NULL);
 
 	ereport(DEBUG1,
 			(errmsg("autovacuum launcher started")));
@@ -1506,8 +1506,8 @@ AutoVacWorkerMain(int argc, char *argv[])
 
 	am_autovacuum_worker = true;
 
-	/* Identify myself via ps */
-	init_ps_display(pgstat_get_backend_desc(B_AUTOVAC_WORKER));
+	MyBackendType = B_AUTOVAC_WORKER;
+	init_ps_display(NULL);
 
 	SetProcessingMode(InitProcessing);
 
diff --git a/src/backend/postmaster/bgworker.c b/src/backend/postmaster/bgworker.c
index 684250984d..6c684b5e12 100644
--- a/src/backend/postmaster/bgworker.c
+++ b/src/backend/postmaster/bgworker.c
@@ -688,7 +688,7 @@ StartBackgroundWorker(void)
 
 	IsBackgroundWorker = true;
 
-	/* Identify myself via ps */
+	MyBackendType = B_BG_WORKER;
 	init_ps_display(worker->bgw_name);
 
 	/*
diff --git a/src/backend/postmaster/pgarch.c b/src/backend/postmaster/pgarch.c
index 58f54544f6..01ffd6513c 100644
--- a/src/backend/postmaster/pgarch.c
+++ b/src/backend/postmaster/pgarch.c
@@ -238,10 +238,8 @@ PgArchiverMain(int argc, char *argv[])
 	pqsignal(SIGCHLD, SIG_DFL);
 	PG_SETMASK(&UnBlockSig);
 
-	/*
-	 * Identify myself via ps
-	 */
-	init_ps_display("archiver");
+	MyBackendType = B_ARCHIVER;
+	init_ps_display(NULL);
 
 	pgarch_MainLoop();
 
diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index 107c965336..d39e21494e 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -2892,62 +2892,7 @@ pgstat_bestart(void)
 	 * out-of-line data.  Those have to be handled separately, below.
 	 */
 	lbeentry.st_procpid = MyProcPid;
-
-	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_backendType = MyBackendType;
 	lbeentry.st_proc_start_timestamp = MyStartTimestamp;
 	lbeentry.st_activity_start_timestamp = 0;
 	lbeentry.st_state_start_timestamp = 0;
@@ -4276,6 +4221,9 @@ pgstat_get_backend_desc(BackendType backendType)
 
 	switch (backendType)
 	{
+		case B_INVALID:
+			backendDesc = "not initialized";
+			break;
 		case B_AUTOVAC_LAUNCHER:
 			backendDesc = "autovacuum launcher";
 			break;
@@ -4306,6 +4254,15 @@ pgstat_get_backend_desc(BackendType backendType)
 		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;
@@ -4447,10 +4404,8 @@ PgstatCollectorMain(int argc, char *argv[])
 	pqsignal(SIGCHLD, SIG_DFL);
 	PG_SETMASK(&UnBlockSig);
 
-	/*
-	 * Identify myself via ps
-	 */
-	init_ps_display("stats collector");
+	MyBackendType = B_STATS_COLLECTOR;
+	init_ps_display(NULL);
 
 	/*
 	 * Read in existing stats files or initialize the stats to zero.
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 46be78aadb..c586b825ed 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -2259,6 +2259,11 @@ ProcessStartupPacket(Port *port, bool secure_done)
 	if (strlen(port->user_name) >= NAMEDATALEN)
 		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
 	 * connected to a particular database. But walsenders used for logical
diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c
index b394599236..3b708c3f67 100644
--- a/src/backend/postmaster/syslogger.c
+++ b/src/backend/postmaster/syslogger.c
@@ -179,7 +179,8 @@ SysLoggerMain(int argc, char *argv[])
 
 	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
diff --git a/src/backend/utils/init/globals.c b/src/backend/utils/init/globals.c
index eb19644419..e75a29def7 100644
--- a/src/backend/utils/init/globals.c
+++ b/src/backend/utils/init/globals.c
@@ -78,6 +78,8 @@ char		postgres_exec_path[MAXPGPATH];	/* full path to backend */
 /* note: currently this is not valid in backend processes */
 #endif
 
+BackendType	MyBackendType;
+
 BackendId	MyBackendId = InvalidBackendId;
 
 BackendId	ParallelMasterBackendId = InvalidBackendId;
diff --git a/src/backend/utils/misc/ps_status.c b/src/backend/utils/misc/ps_status.c
index 8b160c0b40..0685a02a31 100644
--- a/src/backend/utils/misc/ps_status.c
+++ b/src/backend/utils/misc/ps_status.c
@@ -28,6 +28,7 @@
 
 #include "libpq/libpq.h"
 #include "miscadmin.h"
+#include "pgstat.h"
 #include "utils/guc.h"
 #include "utils/ps_status.h"
 
@@ -247,14 +248,20 @@ save_ps_display_args(int argc, char **argv)
 
 /*
  * 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 BackendType.
+ *
+ * At this point, the original argv[] array may be overwritten.
  */
 void
 init_ps_display(const char *fixed_part)
 {
 	bool		save_update_process_title;
 
-	Assert(fixed_part);
+	Assert(fixed_part || MyBackendType);
+	if (!fixed_part)
+		fixed_part = pgstat_get_backend_desc(MyBackendType);
 
 #ifndef PS_USE_NONE
 	/* no ps display for stand-alone backend */
diff --git a/src/include/miscadmin.h b/src/include/miscadmin.h
index f985453ec3..a41b6331da 100644
--- a/src/include/miscadmin.h
+++ b/src/include/miscadmin.h
@@ -178,6 +178,26 @@ extern char pkglib_path[];
 extern char postgres_exec_path[];
 #endif
 
+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;
+
 /*
  * done in storage/backendid.h for now.
  *
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index 7bc36c6583..80a0d7df59 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -13,6 +13,7 @@
 
 #include "datatype/timestamp.h"
 #include "libpq/pqcomm.h"
+#include "miscadmin.h"
 #include "port/atomics.h"
 #include "portability/instr_time.h"
 #include "postmaster/pgarch.h"
@@ -712,25 +713,6 @@ typedef struct 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
  * ----------
-- 
2.25.0

v3-0003-Add-backend-type-to-csvlog-and-optionally-log_lin.patchtext/plain; charset=UTF-8; name=v3-0003-Add-backend-type-to-csvlog-and-optionally-log_lin.patch; x-mac-creator=0; x-mac-type=0Download
From ec2b614282f5f661708413b72a94586d95ea0366 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Thu, 20 Feb 2020 18:16:39 +0100
Subject: [PATCH v3 3/4] Add backend type to csvlog and optionally
 log_line_prefix

---
 doc/src/sgml/config.sgml                      |  8 ++++-
 src/backend/utils/error/elog.c                | 29 +++++++++++++++++++
 src/backend/utils/misc/postgresql.conf.sample |  1 +
 src/test/regress/pg_regress.c                 |  2 +-
 4 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index c1128f89ec..206778b1c3 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -6470,6 +6470,11 @@ <title>What to Log</title>
              <entry>Application name</entry>
              <entry>yes</entry>
             </row>
+            <row>
+             <entry><literal>%b</literal></entry>
+             <entry>Backend process type</entry>
+             <entry>yes</entry>
+            </row>
             <row>
              <entry><literal>%u</literal></entry>
              <entry>User name</entry>
@@ -6785,7 +6790,7 @@ <title>Using CSV-Format Log Output</title>
         character count of the error position therein,
         location of the error in the PostgreSQL source code
         (if <varname>log_error_verbosity</varname> is set to <literal>verbose</literal>),
-        and application name.
+        application name, and backend type.
         Here is a sample table definition for storing CSV-format log output:
 
 <programlisting>
@@ -6814,6 +6819,7 @@ <title>Using CSV-Format Log Output</title>
   query_pos integer,
   location text,
   application_name text,
+  backend_type text,
   PRIMARY KEY (session_id, session_line_num)
 );
 </programlisting>
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index f5b0211f66..bb65eb41ca 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -72,6 +72,8 @@
 #include "libpq/pqformat.h"
 #include "mb/pg_wchar.h"
 #include "miscadmin.h"
+#include "pgstat.h"
+#include "postmaster/bgworker.h"
 #include "postmaster/postmaster.h"
 #include "postmaster/syslogger.h"
 #include "storage/ipc.h"
@@ -2492,6 +2494,23 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
 										   padding > 0 ? padding : -padding);
 
 				break;
+			case 'b':
+				{
+					const char *backend_type_str;
+
+					if (MyProcPid == PostmasterPid)
+						backend_type_str = "postmaster";
+					else if (MyBackendType == B_BG_WORKER)
+						backend_type_str = MyBgworkerEntry->bgw_type;
+					else
+						backend_type_str = pgstat_get_backend_desc(MyBackendType);
+
+					if (padding != 0)
+						appendStringInfo(buf, "%*s", padding, backend_type_str);
+					else
+						appendStringInfoString(buf, backend_type_str);
+					break;
+				}
 			case 'u':
 				if (MyProcPort)
 				{
@@ -2920,6 +2939,16 @@ write_csvlog(ErrorData *edata)
 	if (application_name)
 		appendCSVLiteral(&buf, application_name);
 
+	appendStringInfoChar(&buf, ',');
+
+	/* backend type */
+	if (MyProcPid == PostmasterPid)
+		appendCSVLiteral(&buf, "postmaster");
+	else if (MyBackendType == B_BG_WORKER)
+		appendCSVLiteral(&buf, MyBgworkerEntry->bgw_type);
+	else
+		appendCSVLiteral(&buf, pgstat_get_backend_desc(MyBackendType));
+
 	appendStringInfoChar(&buf, '\n');
 
 	/* If in the syslogger process, try to write messages direct to file */
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
index e58e4788a8..c0e9531f9c 100644
--- a/src/backend/utils/misc/postgresql.conf.sample
+++ b/src/backend/utils/misc/postgresql.conf.sample
@@ -524,6 +524,7 @@
 #log_hostname = off
 #log_line_prefix = '%m [%p] '		# special values:
 					#   %a = application name
+					#   %b = backend type
 					#   %u = user name
 					#   %d = database name
 					#   %r = remote host and port
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index a53e4a6243..f6a5e1b9c7 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -2334,7 +2334,7 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
 		fputs("\n# Configuration added by pg_regress\n\n", pg_conf);
 		fputs("log_autovacuum_min_duration = 0\n", pg_conf);
 		fputs("log_checkpoints = on\n", pg_conf);
-		fputs("log_line_prefix = '%m [%p] %q%a '\n", pg_conf);
+		fputs("log_line_prefix = '%m %b[%p] %q%a '\n", pg_conf);
 		fputs("log_lock_waits = on\n", pg_conf);
 		fputs("log_temp_files = 128kB\n", pg_conf);
 		fputs("max_prepared_transactions = 2\n", pg_conf);
-- 
2.25.0

v3-0004-Remove-am_syslogger-global-variable.patchtext/plain; charset=UTF-8; name=v3-0004-Remove-am_syslogger-global-variable.patch; x-mac-creator=0; x-mac-type=0Download
From a7a8e38021312307da9493bed9ccedba1f1beb41 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Thu, 20 Feb 2020 18:17:28 +0100
Subject: [PATCH v3 4/4] Remove am_syslogger global variable

Use MyBackendType instead.
---
 src/backend/postmaster/syslogger.c | 9 +--------
 src/backend/utils/error/elog.c     | 8 ++++----
 src/include/postmaster/syslogger.h | 2 --
 3 files changed, 5 insertions(+), 14 deletions(-)

diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c
index 3b708c3f67..5343134648 100644
--- a/src/backend/postmaster/syslogger.c
+++ b/src/backend/postmaster/syslogger.c
@@ -74,11 +74,6 @@ char	   *Log_filename = NULL;
 bool		Log_truncate_on_rotation = false;
 int			Log_file_mode = S_IRUSR | S_IWUSR;
 
-/*
- * Globally visible state (used by elog.c)
- */
-bool		am_syslogger = false;
-
 extern bool redirection_done;
 
 /*
@@ -177,8 +172,6 @@ SysLoggerMain(int argc, char *argv[])
 	syslogger_parseArgs(argc, argv);
 #endif							/* EXEC_BACKEND */
 
-	am_syslogger = true;
-
 	MyBackendType = B_LOGGER;
 	init_ps_display(NULL);
 
@@ -1078,7 +1071,7 @@ flush_pipe_input(char *logbuffer, int *bytes_in_logbuffer)
 /*
  * Write text to the currently open logfile
  *
- * This is exported so that elog.c can call it when am_syslogger is true.
+ * This is exported so that elog.c can call it when BackendType is B_LOGGER.
  * This allows the syslogger process to record elog messages of its own,
  * even though its stderr does not point at the syslog pipe.
  */
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index bb65eb41ca..3a6f7f9456 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -2952,7 +2952,7 @@ write_csvlog(ErrorData *edata)
 	appendStringInfoChar(&buf, '\n');
 
 	/* If in the syslogger process, try to write messages direct to file */
-	if (am_syslogger)
+	if (MyBackendType == B_LOGGER)
 		write_syslogger_file(buf.data, buf.len, LOG_DESTINATION_CSVLOG);
 	else
 		write_pipe_chunks(buf.data, buf.len, LOG_DESTINATION_CSVLOG);
@@ -3146,7 +3146,7 @@ send_message_to_server_log(ErrorData *edata)
 		 * catching stderr output, and we are not ourselves the syslogger.
 		 * Otherwise, just do a vanilla write to stderr.
 		 */
-		if (redirection_done && !am_syslogger)
+		if (redirection_done && MyBackendType != B_LOGGER)
 			write_pipe_chunks(buf.data, buf.len, LOG_DESTINATION_STDERR);
 #ifdef WIN32
 
@@ -3165,13 +3165,13 @@ send_message_to_server_log(ErrorData *edata)
 	}
 
 	/* If in the syslogger process, try to write messages direct to file */
-	if (am_syslogger)
+	if (MyBackendType == B_LOGGER)
 		write_syslogger_file(buf.data, buf.len, LOG_DESTINATION_STDERR);
 
 	/* Write to CSV log if enabled */
 	if (Log_destination & LOG_DESTINATION_CSVLOG)
 	{
-		if (redirection_done || am_syslogger)
+		if (redirection_done || MyBackendType == B_LOGGER)
 		{
 			/*
 			 * send CSV data if it's safe to do so (syslogger doesn't need the
diff --git a/src/include/postmaster/syslogger.h b/src/include/postmaster/syslogger.h
index 9b7a386dbd..f611bd1411 100644
--- a/src/include/postmaster/syslogger.h
+++ b/src/include/postmaster/syslogger.h
@@ -70,8 +70,6 @@ extern PGDLLIMPORT char *Log_filename;
 extern bool Log_truncate_on_rotation;
 extern int	Log_file_mode;
 
-extern bool am_syslogger;
-
 #ifndef WIN32
 extern int	syslogPipe[2];
 #else
-- 
2.25.0

#7Kuntal Ghosh
kuntalghosh.2007@gmail.com
In reply to: Peter Eisentraut (#6)
Re: backend type in log_line_prefix?

Hello,

On Sat, Mar 7, 2020 at 8:38 PM Peter Eisentraut
<peter.eisentraut@2ndquadrant.com> wrote:

Updated patch set because of conflicts.

Thank you for the patch. This feature is really helpful. Here are some
minor comments:

In v3-0001-Refactor-ps_status.c-API.patch,

- *
- * For a walsender, the ps display is set in the following form:
- *
- * postgres: walsender <user> <host> <activity>
This part is still valid, right?

+ init_ps_display(ps_data.data);
+ pfree(ps_data.data);
+
+ set_ps_display("initializing");
As per the existing behaviour, if update_process_title is true, we
display "authentication" as the initial string. On the other hand,
this patch, irrespective of the GUC variable, always displays
"initializing" as the initial string and In PerformAuthentication, it
sets the display as "authentication" Is this intended? Should we check
the GUC here as well?

In v3-0002-Unify-several-ways-to-tracking-backend-type.patch,
+ * If fixed_part is NULL, a default will be obtained from BackendType.
s/BackendType/MyBackendType?

In v3-0003-Add-backend-type-to-csvlog-and-optionally-log_lin.patch,
+ <entry>Backend process type</entry>
In other places you've written "backend type".

In v3-0004-Remove-am_syslogger-global-variable.patch,
+ * This is exported so that elog.c can call it when BackendType is B_LOGGER.
s/BackendType/MyBackendType?

Done some basic testing. Working as expected.

--
Thanks & Regards,
Kuntal Ghosh
EnterpriseDB: http://www.enterprisedb.com

#8Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Kuntal Ghosh (#7)
Re: backend type in log_line_prefix?

On 2020-03-09 16:20, Kuntal Ghosh wrote:

In v3-0001-Refactor-ps_status.c-API.patch,

- *
- * For a walsender, the ps display is set in the following form:
- *
- * postgres: walsender <user> <host> <activity>
This part is still valid, right?

Sure but I figured this comment was in the context of the explanation of
how the old API was being abused, so it's no longer necessary.

+ init_ps_display(ps_data.data);
+ pfree(ps_data.data);
+
+ set_ps_display("initializing");
As per the existing behaviour, if update_process_title is true, we
display "authentication" as the initial string. On the other hand,
this patch, irrespective of the GUC variable, always displays
"initializing" as the initial string and In PerformAuthentication, it
sets the display as "authentication" Is this intended? Should we check
the GUC here as well?

set_ps_display() checks update_process_title itself and does nothing if
it's off, so this should work okay.

In v3-0002-Unify-several-ways-to-tracking-backend-type.patch,
+ * If fixed_part is NULL, a default will be obtained from BackendType.
s/BackendType/MyBackendType?

yup

In v3-0003-Add-backend-type-to-csvlog-and-optionally-log_lin.patch,
+ <entry>Backend process type</entry>
In other places you've written "backend type".

ok changed

In v3-0004-Remove-am_syslogger-global-variable.patch,
+ * This is exported so that elog.c can call it when BackendType is B_LOGGER.
s/BackendType/MyBackendType?

ok

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#9Julien Rouhaud
rjuju123@gmail.com
In reply to: Peter Eisentraut (#8)
Re: backend type in log_line_prefix?

On Tue, Mar 10, 2020 at 4:41 PM Peter Eisentraut
<peter.eisentraut@2ndquadrant.com> wrote:

On 2020-03-09 16:20, Kuntal Ghosh wrote:

In v3-0002-Unify-several-ways-to-tracking-backend-type.patch,

In pgstat_get_backend_desc(), the fallback "unknown process type"
description shouldn't be required anymore.

Other than that, it all looks good to me.

#10Kuntal Ghosh
kuntalghosh.2007@gmail.com
In reply to: Peter Eisentraut (#8)
Re: backend type in log_line_prefix?

On Tue, Mar 10, 2020 at 9:11 PM Peter Eisentraut
<peter.eisentraut@2ndquadrant.com> wrote:

On 2020-03-09 16:20, Kuntal Ghosh wrote:

In v3-0001-Refactor-ps_status.c-API.patch,
- * postgres: walsender <user> <host> <activity>
This part is still valid, right?

Sure but I figured this comment was in the context of the explanation of
how the old API was being abused, so it's no longer necessary.

Makes sense.

set_ps_display() checks update_process_title itself and does nothing if
it's off, so this should work okay.

Right.

--
Thanks & Regards,
Kuntal Ghosh
EnterpriseDB: http://www.enterprisedb.com

#11Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Peter Eisentraut (#6)
Re: backend type in log_line_prefix?

I like these patches; the first two are nice cleanup.

My only gripe is that pgstat_get_backend_desc() is not really a pgstat
function; I think it should have a different name with a prototype in
miscadmin.h (next to the enum's new location, which I would put
someplace near the "pmod.h" comment rather than where you put it;
perhaps just above the AuxProcType definition), and implementation
probably in miscinit.c.

--
�lvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#12Justin Pryzby
pryzby@telsasoft.com
In reply to: Fujii Masao (#3)
Re: backend type in log_line_prefix?

On Thu, Feb 13, 2020 at 06:43:32PM +0900, Fujii Masao wrote:

If we do this, backend type should be also included in csvlog?

+1, I've been missing that

Note, this patch seems to correspond to:
b025f32e0b Add leader_pid to pg_stat_activity

I had mentioned privately to Julien missing this info in CSV log.

Should leader_pid be exposed instead (or in addition)? Or backend_type be a
positive number giving the leader's PID if it's a parallel worker, or a some
special negative number like -BackendType to indicate a nonparallel worker.
NULL for a B_BACKEND which is not a parallel worker.

My hope is to answer to questions like these:

. is query (ever? usually?) using parallel paths?
. is query usefully using parallel paths?
. what queries are my max_parallel_workers(_per_process) being used for ?
. Are certain longrunning or frequently running queries which are using
parallel paths using all max_parallel_workers and precluding other queries
from using parallel query ? Or, are semi-short queries sometimes precluding
longrunning queries from using parallelism, when the long queries would
better benefit ?

I think this patch alone wouldn't provide that, and there'd need to either be a
line logged for each worker. Maybe it'd log full query+details (ugh), or just
log "parallel worker of pid...". Or maybe there'd be a new column with which
the leader would log nworkers (workers planned vs workers launched - I would
*not* want to get this out of autoexplain).

--
Justin

#13Justin Pryzby
pryzby@telsasoft.com
In reply to: Justin Pryzby (#12)
Re: backend type in log_line_prefix?

On Tue, Mar 10, 2020 at 02:01:42PM -0500, Justin Pryzby wrote:

On Thu, Feb 13, 2020 at 06:43:32PM +0900, Fujii Masao wrote:

If we do this, backend type should be also included in csvlog?

+1, I've been missing that

Note, this patch seems to correspond to:
b025f32e0b Add leader_pid to pg_stat_activity

I had mentioned privately to Julien missing this info in CSV log.

Should leader_pid be exposed instead (or in addition)? Or backend_type be a

I looked more closely and played with the patch.

Can I suggest:

$ git diff
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 3a6f7f9456..56e0a1437e 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -2945,7 +2945,7 @@ write_csvlog(ErrorData *edata)
        if (MyProcPid == PostmasterPid)
                appendCSVLiteral(&buf, "postmaster");
        else if (MyBackendType == B_BG_WORKER)
-               appendCSVLiteral(&buf, MyBgworkerEntry->bgw_type);
+               appendCSVLiteral(&buf, MyBgworkerEntry->bgw_name);
        else
                appendCSVLiteral(&buf, pgstat_get_backend_desc(MyBackendType));

Then it logs the leader:
|2020-03-11 13:16:05.596 CDT,,,16289,,5e692ae3.3fa1,1,,2020-03-11 13:16:03 CDT,4/3,0,LOG,00000,"temporary file: path ""base/pgsql_tmp/pgsql_tmp16289.0"", size 4276224",,,,,,"explain analyze SELECT * FROM t a JOIN t b USING(i) WHERE i>999 GROUP BY 1;",,,"psql","parallel worker for PID 16210"

It'll be easy enough to extract the leader and join that ON leader=pid.

I think this patch alone wouldn't provide that, and there'd need to either be a
line logged for each worker. Maybe it'd log full query+details (ugh), or just
log "parallel worker of pid...". Or maybe there'd be a new column with which
the leader would log nworkers (workers planned vs workers launched - I would
*not* want to get this out of autoexplain).

I'm still not sure how to do that, though.
I see I can get what's needed at DEBUG1:

|2020-03-11 13:50:58.304 CDT,,,16196,,5e692aa7.3f44,22,,2020-03-11 13:15:03 CDT,,0,DEBUG,00000,"registering background worker ""parallel worker for PID 16210""",,,,,,,,,"","postmaster"

But I don't think it's viable to run for very long with log_statement=all,
log_min_messages=DEBUG.

--
Justin

#14Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Justin Pryzby (#13)
Re: backend type in log_line_prefix?

On 2020-03-11 19:53, Justin Pryzby wrote:

Can I suggest:

$ git diff
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 3a6f7f9456..56e0a1437e 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -2945,7 +2945,7 @@ write_csvlog(ErrorData *edata)
if (MyProcPid == PostmasterPid)
appendCSVLiteral(&buf, "postmaster");
else if (MyBackendType == B_BG_WORKER)
-               appendCSVLiteral(&buf, MyBgworkerEntry->bgw_type);
+               appendCSVLiteral(&buf, MyBgworkerEntry->bgw_name);
else
appendCSVLiteral(&buf, pgstat_get_backend_desc(MyBackendType));

The difference is intentional. bgw_type is so that you can filter and
group by type. The bgw_name could be totally different for each instance.

Having the bgw name available somehow would perhaps also be useful, but
then we should also do this in a consistent way for processes that are
not background workers, such as regular client backends or wal senders
or autovacuum workers. Doing it just for background workers would
create inconsistencies that the introduction of bgw_type some time ago
sought to eliminate.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#15Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Alvaro Herrera (#11)
Re: backend type in log_line_prefix?

On 2020-03-10 19:07, Alvaro Herrera wrote:

I like these patches; the first two are nice cleanup.

My only gripe is that pgstat_get_backend_desc() is not really a pgstat
function; I think it should have a different name with a prototype in
miscadmin.h (next to the enum's new location, which I would put
someplace near the "pmod.h" comment rather than where you put it;
perhaps just above the AuxProcType definition), and implementation
probably in miscinit.c.

I have committed the refactoring patches with adjustments along these
lines. The patch with the log_line_prefix and csvlog enhancements is
still under discussion.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#16Justin Pryzby
pryzby@telsasoft.com
In reply to: Peter Eisentraut (#5)
Re: backend type in log_line_prefix?

On Fri, Feb 21, 2020 at 10:09:38AM +0100, Peter Eisentraut wrote:

From 75ac8ed0c47801712eb2aa300d9cb29767d2e121 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Thu, 20 Feb 2020 18:16:39 +0100
Subject: [PATCH v2 3/4] Add backend type to csvlog and optionally log_line_prefix

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index c1128f89ec..206778b1c3 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -6470,6 +6470,11 @@ <title>What to Log</title>

characters are copied straight to the log line. Some escapes are
only recognized by session processes, and will be treated as empty by
background processes such as the main server process. Status
...
<entry>Escape</entry>
<entry>Effect</entry>
<entry>Session only</entry>

<entry>Application name</entry>
<entry>yes</entry>
</row>
+            <row>
+             <entry><literal>%b</literal></entry>
+             <entry>Backend process type</entry>
+             <entry>yes</entry>

=> should say "no", it's not blank for background processes:

+
+					if (MyProcPid == PostmasterPid)
+						backend_type_str = "postmaster";
+					else if (MyBackendType == B_BG_WORKER)
+						backend_type_str = MyBgworkerEntry->bgw_type;
+					else
+						backend_type_str = pgstat_get_backend_desc(MyBackendType);

--
Justin

#17Justin Pryzby
pryzby@telsasoft.com
In reply to: Peter Eisentraut (#14)
2 attachment(s)
Re: backend type in log_line_prefix?

On Fri, Mar 13, 2020 at 10:22:52PM +0100, Peter Eisentraut wrote:

Can I suggest:

-               appendCSVLiteral(&buf, MyBgworkerEntry->bgw_type);
+               appendCSVLiteral(&buf, MyBgworkerEntry->bgw_name);

The difference is intentional. bgw_type is so that you can filter and group
by type. The bgw_name could be totally different for each instance.

I found 5373bc2a0867048bb78f93aede54ac1309b5e227

Your patch adds bgw_type, which is also in pg_stat_activity, so I agree it's
good to allow include it log_line_prefix and CSV.

I suggest the CSV/log should also have the leader_pid, corresponding to
| b025f32e0b Add leader_pid to pg_stat_activity

With the attached on top of your patch, CSV logs like:

2020-03-14 22:09:39.395 CDT,"pryzbyj","template1",17030,"[local]",5e6d9c69.4286,2,"idle",2020-03-14 22:09:29 CDT,3/23,0,LOG,00000,"statement: explain analyze SELECT COUNT(1), a.a FROM t a JOIN t b ON a.a=b.a GROUP BY 2;",,,,,,,,,"psql","client backend",
2020-03-14 22:09:43.094 CDT,,,17042,,5e6d9c73.4292,1,,2020-03-14 22:09:39 CDT,4/3,0,LOG,00000,"temporary file: path ""base/pgsql_tmp/pgsql_tmp17042.0"", size 4694016",,,,,,"explain analyze SELECT COUNT(1), a.a FROM t a JOIN t b ON a.a=b.a GROUP BY 2;",,,"psql","parallel worker",17030
2020-03-14 22:09:43.094 CDT,,,17043,,5e6d9c73.4293,1,,2020-03-14 22:09:39 CDT,5/3,0,LOG,00000,"temporary file: path ""base/pgsql_tmp/pgsql_tmp17043.0"", size 4694016",,,,,,"explain analyze SELECT COUNT(1), a.a FROM t a JOIN t b ON a.a=b.a GROUP BY 2;",,,"psql","parallel worker",17030

As for my question "what's using/trying/failing to use parallel workers", I was
able to look into that by parsing "Workers Planned/Launched" from autoexplain.
It's not a *good* way to do it, but I don't see how to do better and I don't
see any way this patch can improve that.

--
Justin

Attachments:

v1-0001-Fix-previous-commit.patchtext/x-diff; charset=us-asciiDownload
From 5b111a3a6cd5282dbdf1e504ef648994b1918302 Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Sat, 14 Mar 2020 19:09:54 -0500
Subject: [PATCH v1 1/2] Fix previous commit..

---
 doc/src/sgml/config.sgml       | 2 +-
 src/backend/utils/error/elog.c | 5 ++---
 2 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 53d0c480aa..fc0e2c00c3 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -6460,7 +6460,7 @@ local0.*    /var/log/postgresql
             <row>
              <entry><literal>%b</literal></entry>
              <entry>Backend process type</entry>
-             <entry>yes</entry>
+             <entry>no</entry>
             </row>
             <row>
              <entry><literal>%u</literal></entry>
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 3a6f7f9456..62eef7b71f 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -72,7 +72,6 @@
 #include "libpq/pqformat.h"
 #include "mb/pg_wchar.h"
 #include "miscadmin.h"
-#include "pgstat.h"
 #include "postmaster/bgworker.h"
 #include "postmaster/postmaster.h"
 #include "postmaster/syslogger.h"
@@ -2503,7 +2502,7 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
 					else if (MyBackendType == B_BG_WORKER)
 						backend_type_str = MyBgworkerEntry->bgw_type;
 					else
-						backend_type_str = pgstat_get_backend_desc(MyBackendType);
+						backend_type_str = GetBackendTypeDesc(MyBackendType);
 
 					if (padding != 0)
 						appendStringInfo(buf, "%*s", padding, backend_type_str);
@@ -2947,7 +2946,7 @@ write_csvlog(ErrorData *edata)
 	else if (MyBackendType == B_BG_WORKER)
 		appendCSVLiteral(&buf, MyBgworkerEntry->bgw_type);
 	else
-		appendCSVLiteral(&buf, pgstat_get_backend_desc(MyBackendType));
+		appendCSVLiteral(&buf, GetBackendTypeDesc(MyBackendType));
 
 	appendStringInfoChar(&buf, '\n');
 
-- 
2.17.0

v1-0002-Include-the-leader-PID-in-logfile.patchtext/x-diff; charset=us-asciiDownload
From 30172a4771360fd6ed4f4646651efffa3785dfb7 Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Fri, 13 Mar 2020 22:03:06 -0500
Subject: [PATCH v1 2/2] Include the leader PID in logfile

See also: b025f32e0b, which adds the leader PID to pg_stat_activity
---
 doc/src/sgml/config.sgml                      |  8 +++-
 src/backend/utils/error/elog.c                | 47 +++++++++++++++++++
 src/backend/utils/misc/postgresql.conf.sample |  1 +
 3 files changed, 55 insertions(+), 1 deletion(-)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index fc0e2c00c3..d53b62e2df 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -6487,6 +6487,11 @@ local0.*    /var/log/postgresql
              <entry>Process ID</entry>
              <entry>no</entry>
             </row>
+            <row>
+             <entry><literal>%k</literal></entry>
+             <entry>Leader PID for a parallel process, NULL otherwise</entry>
+             <entry>yes</entry>
+            </row>
             <row>
              <entry><literal>%t</literal></entry>
              <entry>Time stamp without milliseconds</entry>
@@ -6777,7 +6782,7 @@ log_line_prefix = '%m [%p] %q%u@%d/%a '
         character count of the error position therein,
         location of the error in the PostgreSQL source code
         (if <varname>log_error_verbosity</varname> is set to <literal>verbose</literal>),
-        application name, and backend type.
+        application name, backend type, and leader PID.
         Here is a sample table definition for storing CSV-format log output:
 
 <programlisting>
@@ -6807,6 +6812,7 @@ CREATE TABLE postgres_log
   location text,
   application_name text,
   backend_type text,
+  leader_pid integer,
   PRIMARY KEY (session_id, session_line_num)
 );
 </programlisting>
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 62eef7b71f..e6b3caf414 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -77,6 +77,7 @@
 #include "postmaster/syslogger.h"
 #include "storage/ipc.h"
 #include "storage/proc.h"
+#include "storage/procarray.h"
 #include "tcop/tcopprot.h"
 #include "utils/guc.h"
 #include "utils/memutils.h"
@@ -177,6 +178,7 @@ static void write_console(const char *line, int len);
 static void setup_formatted_log_time(void);
 static void setup_formatted_start_time(void);
 static const char *process_log_prefix_padding(const char *p, int *padding);
+static pid_t get_leader_pid();
 static void log_line_prefix(StringInfo buf, ErrorData *edata);
 static void write_csvlog(ErrorData *edata);
 static void send_message_to_server_log(ErrorData *edata);
@@ -2402,6 +2404,24 @@ process_log_prefix_padding(const char *p, int *ppadding)
 	return p;
 }
 
+/* Return PID of leader, or InvalidPid if not a parallel worker */
+static pid_t
+get_leader_pid()
+{
+	PGPROC	*proc;
+	pid_t	leader_pid = InvalidPid;
+
+	if (MyBackendType != B_BG_WORKER)
+		return InvalidPid;
+
+	LWLockAcquire(ProcArrayLock, LW_SHARED);
+	proc = BackendPidGetProcWithLock(MyProcPid);
+	if (proc && proc->lockGroupLeader)
+		leader_pid = proc->lockGroupLeader->pid;
+	LWLockRelease(ProcArrayLock);
+	return leader_pid;
+}
+
 /*
  * Format tag info for log lines; append to the provided buffer.
  */
@@ -2413,6 +2433,10 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
 
 	/* has counter been reset in current process? */
 	static int	log_my_pid = 0;
+
+	/* Leader PID is retrieved only once per process after forking from postmaster */
+	static pid_t	leader_pid = InvalidPid;
+
 	int			padding;
 	const char *p;
 
@@ -2427,6 +2451,7 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
 		log_line_number = 0;
 		log_my_pid = MyProcPid;
 		formatted_start_time[0] = '\0';
+		leader_pid = get_leader_pid();
 	}
 	log_line_number++;
 
@@ -2560,6 +2585,18 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
 				else
 					appendStringInfo(buf, "%d", MyProcPid);
 				break;
+
+			case 'k':
+				if (!MyProcPort)
+					; /* Do nothing */
+				else if (leader_pid == InvalidPid)
+					; /* Do nothing */
+				else if (padding != 0)
+					appendStringInfo(buf, "%*d", padding, leader_pid);
+				else
+					appendStringInfo(buf, "%d", leader_pid);
+				break;
+
 			case 'l':
 				if (padding != 0)
 					appendStringInfo(buf, "%*ld", padding, log_line_number);
@@ -2768,6 +2805,9 @@ write_csvlog(ErrorData *edata)
 	/* has counter been reset in current process? */
 	static int	log_my_pid = 0;
 
+	/* Leader PID is retrieved only once per process after forking from postmaster */
+	static pid_t	leader_pid = InvalidPid;
+
 	/*
 	 * This is one of the few places where we'd rather not inherit a static
 	 * variable's value from the postmaster.  But since we will, reset it when
@@ -2778,6 +2818,7 @@ write_csvlog(ErrorData *edata)
 		log_line_number = 0;
 		log_my_pid = MyProcPid;
 		formatted_start_time[0] = '\0';
+		leader_pid = get_leader_pid();
 	}
 	log_line_number++;
 
@@ -2948,6 +2989,12 @@ write_csvlog(ErrorData *edata)
 	else
 		appendCSVLiteral(&buf, GetBackendTypeDesc(MyBackendType));
 
+	appendStringInfoChar(&buf, ',');
+
+	/* leader PID */
+	if (MyProcPort && leader_pid != InvalidPid)
+		appendStringInfo(&buf, "%d", leader_pid);
+
 	appendStringInfoChar(&buf, '\n');
 
 	/* If in the syslogger process, try to write messages direct to file */
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
index c0e9531f9c..32fc4b18e6 100644
--- a/src/backend/utils/misc/postgresql.conf.sample
+++ b/src/backend/utils/misc/postgresql.conf.sample
@@ -530,6 +530,7 @@
 					#   %r = remote host and port
 					#   %h = remote host
 					#   %p = process ID
+					#   %k = leader PID
 					#   %t = timestamp without milliseconds
 					#   %m = timestamp with milliseconds
 					#   %n = timestamp with milliseconds (as a Unix epoch)
-- 
2.17.0

#18Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Peter Eisentraut (#15)
Re: backend type in log_line_prefix?

On 2020-03-13 22:24, Peter Eisentraut wrote:

On 2020-03-10 19:07, Alvaro Herrera wrote:

I like these patches; the first two are nice cleanup.

My only gripe is that pgstat_get_backend_desc() is not really a pgstat
function; I think it should have a different name with a prototype in
miscadmin.h (next to the enum's new location, which I would put
someplace near the "pmod.h" comment rather than where you put it;
perhaps just above the AuxProcType definition), and implementation
probably in miscinit.c.

I have committed the refactoring patches with adjustments along these
lines. The patch with the log_line_prefix and csvlog enhancements is
still under discussion.

I have committed that last one also, after some corrections.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#19Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Justin Pryzby (#17)
Re: backend type in log_line_prefix?

On 2020-03-15 10:57, Justin Pryzby wrote:

I suggest the CSV/log should also have the leader_pid, corresponding to
| b025f32e0b Add leader_pid to pg_stat_activity

I haven't followed those developments. It sounds interesting, but I
suggest you start a new thread or continue in the thread that added
leader_pid.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#20Fujii Masao
masao.fujii@oss.nttdata.com
In reply to: Peter Eisentraut (#18)
Re: backend type in log_line_prefix?

On 2020/03/15 19:32, Peter Eisentraut wrote:

On 2020-03-13 22:24, Peter Eisentraut wrote:

On 2020-03-10 19:07, Alvaro Herrera wrote:

I like these patches; the first two are nice cleanup.

My only gripe is that pgstat_get_backend_desc() is not really a pgstat
function; I think it should have a different name with a prototype in
miscadmin.h (next to the enum's new location, which I would put
someplace near the "pmod.h" comment rather than where you put it;
perhaps just above the AuxProcType definition), and implementation
probably in miscinit.c.

I have committed the refactoring patches with adjustments along these
lines.  The patch with the log_line_prefix and csvlog enhancements is
still under discussion.

I have committed that last one also, after some corrections.

Thanks for adding this nice feature!

I have one comment; You seem to need to update file-fdw.sgml so that
pglog table in the doc should include backend_type column.

Regards,

--
Fujii Masao
NTT DATA CORPORATION
Advanced Platform Technology Group
Research and Development Headquarters

#21Mike Palmiotto
mike.palmiotto@crunchydata.com
In reply to: Fujii Masao (#20)
Re: backend type in log_line_prefix?

On 2020/03/15 19:32, Peter Eisentraut wrote:

On 2020-03-13 22:24, Peter Eisentraut wrote:

On 2020-03-10 19:07, Alvaro Herrera wrote:

I like these patches; the first two are nice cleanup.

My only gripe is that pgstat_get_backend_desc() is not really a pgstat
function; I think it should have a different name with a prototype in
miscadmin.h (next to the enum's new location, which I would put
someplace near the "pmod.h" comment rather than where you put it;
perhaps just above the AuxProcType definition), and implementation
probably in miscinit.c.

I have committed the refactoring patches with adjustments along these
lines. The patch with the log_line_prefix and csvlog enhancements is
still under discussion.

I have committed that last one also, after some corrections.

Sorry for being late to this thread, but was wondering if anyone had
taken a look at the Process Centralization patchset that I submitted
to this CF:
/messages/by-id/CAMN686HgTVRJBAw6hqFE4Lj8bgPLQqfp1c-+WBGUtEmg6wPVhg@mail.gmail.com

There's quite a bit of that code that is in the same vein as the
MyBackendType changes proposed/merged in this thread.

I think we could reduce a large portion of redundant code (including
the pgstat_get_backend_desc code) while also
centralizing/standardizing process startup. A few useful features
(outside of code reduction) include the ability to identify backends
prior to their Main functions, cleaner control of SubPostmasterMain
logic (including implicit handling of shmem timing considerations).

If others think it's worthwhile, I will work on rebasing those changes
on the changes proposed/merged in this thread (re: MyBackendType).

Thanks,
--
Mike Palmiotto
https://crunchydata.com

#22Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Peter Eisentraut (#18)
1 attachment(s)
Re: backend type in log_line_prefix?

On Sun, Mar 15, 2020 at 7:32 AM Peter Eisentraut <
peter.eisentraut@2ndquadrant.com> wrote:

I have committed that last one also, after some corrections.

IMHO we should also update file_fdw documentation. See attached!

Regards,

--
Fabrízio de Royes Mello Timbira - http://www.timbira.com.br/
PostgreSQL: Consultoria, Desenvolvimento, Suporte 24x7 e Treinamento

Attachments:

fix-file-fdw-doc-csvlog.patchtext/x-patch; charset=US-ASCII; name=fix-file-fdw-doc-csvlog.patchDownload
diff --git a/doc/src/sgml/file-fdw.sgml b/doc/src/sgml/file-fdw.sgml
index 28b61c8f2d..ed028e4ec9 100644
--- a/doc/src/sgml/file-fdw.sgml
+++ b/doc/src/sgml/file-fdw.sgml
@@ -261,7 +261,8 @@ CREATE FOREIGN TABLE pglog (
   query text,
   query_pos integer,
   location text,
-  application_name text
+  application_name text,
+  backend_type text
 ) SERVER pglog
 OPTIONS ( filename '/home/josh/data/log/pglog.csv', format 'csv' );
 </programlisting>
#23Bruce Momjian
bruce@momjian.us
In reply to: Fabrízio de Royes Mello (#22)
Re: backend type in log_line_prefix?

On Thu, Mar 19, 2020 at 01:37:17PM -0300, Fabr�zio de Royes Mello wrote:

On Sun, Mar 15, 2020 at 7:32 AM Peter Eisentraut <
peter.eisentraut@2ndquadrant.com> wrote:

I have committed that last one also, after some corrections.

IMHO we should also update file_fdw documentation. See attached!

Regards,

--
� �Fabr�zio de Royes Mello � � � � Timbira - http://www.timbira.com.br/
� �PostgreSQL: Consultoria, Desenvolvimento, Suporte 24x7 e Treinamento

diff --git a/doc/src/sgml/file-fdw.sgml b/doc/src/sgml/file-fdw.sgml
index 28b61c8f2d..ed028e4ec9 100644
--- a/doc/src/sgml/file-fdw.sgml
+++ b/doc/src/sgml/file-fdw.sgml
@@ -261,7 +261,8 @@ CREATE FOREIGN TABLE pglog (
query text,
query_pos integer,
location text,
-  application_name text
+  application_name text,
+  backend_type text
) SERVER pglog
OPTIONS ( filename '/home/josh/data/log/pglog.csv', format 'csv' );
</programlisting>

Patch applied to master, thanks.

--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EnterpriseDB https://enterprisedb.com

+ As you are, so once was I.  As I am, so you will be. +
+                      Ancient Roman grave inscription +
#24Kyotaro Horiguchi
horikyota.ntt@gmail.com
In reply to: Bruce Momjian (#23)
Re: backend type in log_line_prefix?

Hello.

At Mon, 23 Mar 2020 18:38:53 -0400, Bruce Momjian <bruce@momjian.us> wrote in

Patch applied to master, thanks.

The patch (8e8a0becb3) named archiver process as just "archiver". On
the other hand the discussion in the thread [1]/messages/by-id/20200319195410.icib45bbgjwqb5zn@alap3.anarazel.de was going to name the
process as "WAL/wal archiver". As all other processes related to WAL
are named as walreceiver, walsender, walwriter, wouldn't we name the
process like "wal archiver"?

[1]: /messages/by-id/20200319195410.icib45bbgjwqb5zn@alap3.anarazel.de

regards.

--
Kyotaro Horiguchi
NTT Open Source Software Center

#25Bruce Momjian
bruce@momjian.us
In reply to: Kyotaro Horiguchi (#24)
1 attachment(s)
Re: backend type in log_line_prefix?

On Fri, Mar 27, 2020 at 04:30:07PM +0900, Kyotaro Horiguchi wrote:

Hello.

At Mon, 23 Mar 2020 18:38:53 -0400, Bruce Momjian <bruce@momjian.us> wrote in

Patch applied to master, thanks.

The patch (8e8a0becb3) named archiver process as just "archiver". On
the other hand the discussion in the thread [1] was going to name the
process as "WAL/wal archiver". As all other processes related to WAL
are named as walreceiver, walsender, walwriter, wouldn't we name the
process like "wal archiver"?

[1]: /messages/by-id/20200319195410.icib45bbgjwqb5zn@alap3.anarazel.de

Agreed. I ended up moving "wal" as a separate word, since it looks
cleaner; patch attached. Tools that look for the backend type in
pg_stat_activity would need to be adjusted; it would be an
incompatibility. Maybe changing it would cause too much disruption.

--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EnterpriseDB https://enterprisedb.com

+ As you are, so once was I.  As I am, so you will be. +
+                      Ancient Roman grave inscription +

Attachments:

wal.difftext/x-diff; charset=us-asciiDownload
diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
index a7b7b12249..2d625ee01e 100644
--- a/src/backend/utils/init/miscinit.c
+++ b/src/backend/utils/init/miscinit.c
@@ -221,16 +221,16 @@ GetBackendTypeDesc(BackendType backendType)
 			backendDesc = "startup";
 			break;
 		case B_WAL_RECEIVER:
-			backendDesc = "walreceiver";
+			backendDesc = "wal receiver";
 			break;
 		case B_WAL_SENDER:
-			backendDesc = "walsender";
+			backendDesc = "wal sender";
 			break;
 		case B_WAL_WRITER:
-			backendDesc = "walwriter";
+			backendDesc = "wal writer";
 			break;
 		case B_ARCHIVER:
-			backendDesc = "archiver";
+			backendDesc = "wal archiver";
 			break;
 		case B_STATS_COLLECTOR:
 			backendDesc = "stats collector";
#26Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Bruce Momjian (#25)
Re: backend type in log_line_prefix?

On 2020-04-01 03:55, Bruce Momjian wrote:

Agreed. I ended up moving "wal" as a separate word, since it looks
cleaner; patch attached. Tools that look for the backend type in
pg_stat_activity would need to be adjusted; it would be an
incompatibility. Maybe changing it would cause too much disruption.

Yeah, it's probably not worth the change for that reason. There is no
confusion what the "archiver" is. Also, we have archive_mode,
archive_command, etc. without a wal_ prefix. Let's leave it as is.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services