From 16a9da60cdf7b8eaf2f5fed82e4ab79cfdf33a6a Mon Sep 17 00:00:00 2001
From: Craig Ringer <craig.ringer@2ndquadrant.com>
Date: Mon, 11 Jan 2021 10:33:10 +0800
Subject: [PATCH 4/4] SDT tracepoints for backend type and postmaster pid on
 startup

For collecting stats on things like LWLock activity it's good to be able
to identify a backend reliably, even without debuginfo available.
---
 src/backend/utils/init/miscinit.c  | 5 +++++
 src/backend/utils/misc/guc.c       | 3 +++
 src/backend/utils/misc/ps_status.c | 9 +++++++++
 src/backend/utils/probes.d         | 5 +++++
 4 files changed, 22 insertions(+)

diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
index 8b73850d0d..01b5cccf85 100644
--- a/src/backend/utils/init/miscinit.c
+++ b/src/backend/utils/init/miscinit.c
@@ -233,6 +233,11 @@ GetBackendTypeDesc(BackendType backendType)
 {
 	const char *backendDesc = "unknown process type";
 
+	if (MyProcPid == PostmasterPid)
+		return "postmaster";
+	else if (!IsUnderPostmaster)
+		return "standalone";
+
 	switch (backendType)
 	{
 		case B_INVALID:
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index e337df42cb..b34cfcefdb 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -95,6 +95,7 @@
 #include "utils/pg_lsn.h"
 #include "utils/plancache.h"
 #include "utils/portal.h"
+#include "utils/probes.h"
 #include "utils/ps_status.h"
 #include "utils/rls.h"
 #include "utils/snapmgr.h"
@@ -11834,6 +11835,8 @@ assign_application_name(const char *newval, void *extra)
 {
 	/* Update the pg_stat_activity view */
 	pgstat_report_appname(newval);
+
+	TRACE_POSTGRESQL_GUC_APPLICATION_NAME_ASSIGNED(newval);
 }
 
 static bool
diff --git a/src/backend/utils/misc/ps_status.c b/src/backend/utils/misc/ps_status.c
index 5819faaf2d..f6ea64a233 100644
--- a/src/backend/utils/misc/ps_status.c
+++ b/src/backend/utils/misc/ps_status.c
@@ -31,6 +31,7 @@
 #include "pgstat.h"
 #include "utils/guc.h"
 #include "utils/ps_status.h"
+#include "utils/probes.h"
 
 extern char **environ;
 bool		update_process_title = true;
@@ -265,6 +266,14 @@ init_ps_display(const char *fixed_part)
 	if (!fixed_part)
 		fixed_part = GetBackendTypeDesc(MyBackendType);
 
+	/*
+	 * init_ps_display() is a useful time to report the backend type to
+	 * tracing tools, since otherwise the probe would have to appear in
+	 * many different main loops for different backend types.
+	 */
+	TRACE_POSTGRESQL_BACKEND_TYPE(MyBackendType, fixed_part);
+	TRACE_POSTGRESQL_POSTMASTER_PID(PostmasterPid);
+
 #ifndef PS_USE_NONE
 	/* no ps display for stand-alone backend */
 	if (!IsUnderPostmaster)
diff --git a/src/backend/utils/probes.d b/src/backend/utils/probes.d
index 608a89af91..0157c3432f 100644
--- a/src/backend/utils/probes.d
+++ b/src/backend/utils/probes.d
@@ -101,4 +101,9 @@ provider postgresql {
 	probe wal__switch();
 	probe wal__buffer__write__dirty__start();
 	probe wal__buffer__write__dirty__done();
+
+	/* Useful probes for startup and process info, for when tracing a group of processes */
+	probe backend__type(int backend_type, const char * backend_type_description);
+	probe postmaster__pid(int postmaster_pid);
+	probe guc__application__name__assigned(const char *newval);
 };
-- 
2.29.2

