diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index cee09c7..023e3f7 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -3562,7 +3562,14 @@ local0.* /var/log/postgresql
%u
- User name
+ The user name used for authentication
+ yes
+
+
+ %U
+ The user name set via
+ or , if any,
+ or the authenticated username, otherwise
yes
@@ -3790,6 +3797,7 @@ FROM pg_stat_activity;
with these columns:
timestamp with milliseconds,
user name,
+ current role name,
database name,
process ID,
client host:port number,
@@ -3820,6 +3828,7 @@ CREATE TABLE postgres_log
(
log_time timestamp(3) with time zone,
user_name text,
+ curr_role text,
database_name text,
process_id integer,
connection_from text,
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 5679d5b..509755f 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -65,6 +65,7 @@
#include "access/transam.h"
#include "access/xact.h"
+#include "commands/variable.h"
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
#include "mb/pg_wchar.h"
@@ -1832,6 +1833,20 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
appendStringInfoString(buf, username);
}
break;
+ case 'U':
+ {
+ const char *curr_role;
+
+ /*
+ * You can't actually have a role named 'none', so this is
+ * safer than it looks.
+ */
+ curr_role = show_role();
+ if (strcmp(curr_role, "none") == 0)
+ curr_role = show_session_authorization();
+ appendStringInfoString(buf, curr_role);
+ }
+ break;
case 'd':
if (MyProcPort)
{
@@ -1967,6 +1982,16 @@ write_csvlog(ErrorData *edata)
/* has counter been reset in current process? */
static int log_my_pid = 0;
+ const char *curr_role;
+
+ /*
+ * You can't actually have a role named 'none', so this is safer than
+ * it looks.
+ */
+ curr_role = show_role();
+ if (strcmp(curr_role, "none") == 0)
+ curr_role = show_session_authorization();
+
/*
* 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
@@ -1995,11 +2020,15 @@ write_csvlog(ErrorData *edata)
appendStringInfoString(&buf, formatted_log_time);
appendStringInfoChar(&buf, ',');
- /* username */
+ /* authenticated-with username */
if (MyProcPort)
appendCSVLiteral(&buf, MyProcPort->user_name);
appendStringInfoChar(&buf, ',');
+ /* current role name, matches %U in log_line_prefix */
+ appendCSVLiteral(&buf, curr_role);
+ appendStringInfoChar(&buf, ',');
+
/* database name */
if (MyProcPort)
appendCSVLiteral(&buf, MyProcPort->database_name);
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
index 6726733..0db585b 100644
--- a/src/backend/utils/misc/postgresql.conf.sample
+++ b/src/backend/utils/misc/postgresql.conf.sample
@@ -361,7 +361,8 @@
#log_hostname = off
#log_line_prefix = '' # special values:
# %a = application name
- # %u = user name
+ # %u = authenticated user name
+ # %U = current role name
# %d = database name
# %r = remote host and port
# %h = remote host