From a8a8867912c46a68c9ac14903b3dba2fab8f7097 Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Wed, 12 Jan 2011 11:34:53 -0500
Subject: [PATCH] Add support for logging the current role.

Stephen Frost, with some editorialization by me.
---
 doc/src/sgml/config.sgml       |  7 ++++++-
 src/backend/utils/error/elog.c | 10 ++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 8e2a2c5d736..d014bb57380 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -3504,7 +3504,12 @@ local0.*    /var/log/postgresql
             </row>
             <row>
              <entry><literal>%u</literal></entry>
-             <entry>User name</entry>
+             <entry>Login user name</entry>
+             <entry>yes</entry>
+            </row>
+            <row>
+             <entry><literal>%U</literal></entry>
+             <entry>Current user name</entry>
              <entry>yes</entry>
             </row>
             <row>
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index a1a449f2ddc..49d9236767d 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -1826,6 +1826,16 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
 					appendStringInfoString(buf, username);
 				}
 				break;
+			case 'U':
+				if (MyProcPort)
+				{
+					const char *username = GetUserNameFromId(GetUserId());
+
+					if (username == NULL || *username == '\0')
+						username = _("[unknown]");
+					appendStringInfoString(buf, username);
+				}
+				break;
 			case 'd':
 				if (MyProcPort)
 				{
-- 
GitLab