From 554b83fe9f29259c970d05f88d84e7f91dd4eb83 Mon Sep 17 00:00:00 2001
From: Don Seiler <don@seiler.us>
Date: Wed, 20 Jun 2018 13:46:50 -0500
Subject: [PATCH] Changes to add application_name to Port struct so we can
 display the application name in "connection authorized" log messages.

---
 src/backend/postmaster/postmaster.c |  8 ++++++++
 src/backend/utils/init/postinit.c   | 14 ++++++++++++--
 src/include/libpq/libpq-be.h        |  7 +++++++
 3 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index a4b53b33cd..b950dc47e8 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -2094,6 +2094,14 @@ retry1:
 											pstrdup(nameptr));
 				port->guc_options = lappend(port->guc_options,
 											pstrdup(valptr));
+
+				/*
+				 * Copy application_name to port when we come across it.
+				 * This is used so we can log the application_name upon
+				 * connection authorization.
+				 */
+				if (strcmp(nameptr, "application_name") == 0)
+					port->application_name = pstrdup(valptr);
 			}
 			offset = valoffset + strlen(valptr) + 1;
 		}
diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c
index 09e0df290d..d9624ce3a6 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -266,7 +266,14 @@ PerformAuthentication(Port *port)
 #ifdef USE_SSL
 			if (port->ssl_in_use)
 				ereport(LOG,
-						(errmsg("connection authorized: user=%s database=%s SSL enabled (protocol=%s, cipher=%s, bits=%d, compression=%s)",
+						(port->application_name
+							? errmsg("connection authorized: user=%s database=%s application_name=%s SSL enabled (protocol=%s, cipher=%s, bits=%d, compression=%s)",
+								port->user_name, port->database_name, port->application_name,
+								be_tls_get_version(port),
+								be_tls_get_cipher(port),
+								be_tls_get_cipher_bits(port),
+								be_tls_get_compression(port) ? _("on") : _("off"))
+							: errmsg("connection authorized: user=%s database=%s SSL enabled (protocol=%s, cipher=%s, bits=%d, compression=%s)",
 								port->user_name, port->database_name,
 								be_tls_get_version(port),
 								be_tls_get_cipher(port),
@@ -275,7 +282,10 @@ PerformAuthentication(Port *port)
 			else
 #endif
 				ereport(LOG,
-						(errmsg("connection authorized: user=%s database=%s",
+						(port->application_name
+							? errmsg("connection authorized: user=%s database=%s application_name=%s",
+								port->user_name, port->database_name, port->application_name)
+							: errmsg("connection authorized: user=%s database=%s",
 								port->user_name, port->database_name)));
 		}
 	}
diff --git a/src/include/libpq/libpq-be.h b/src/include/libpq/libpq-be.h
index 7698cd1f88..cad9ca0657 100644
--- a/src/include/libpq/libpq-be.h
+++ b/src/include/libpq/libpq-be.h
@@ -138,6 +138,13 @@ typedef struct Port
 	char	   *cmdline_options;
 	List	   *guc_options;
 
+	/*
+	 * The startup packet application name, only used here for the "connection
+	 * authorized" log message. We shouldn't use this post-startup, instead the
+	 * GUC should be used as application can change it afterward.
+	 */
+	char	   *application_name;
+
 	/*
 	 * Information that needs to be held during the authentication cycle.
 	 */
-- 
2.15.2 (Apple Git-101.1)

