Hi,

On 2014-05-16 19:54:56 +0200, Andres Freund wrote:
> Hi,
> 
> elog.c's log_line_prefix() processes %d with:
>             case 'd':
>                 if (MyProcPort)
>                 {
>                     const char *dbname = MyProcPort->database_name;
> 
>                     if (dbname == NULL || *dbname == '\0')
>                         dbname = _("[unknown]");
>                     if (padding != 0)
>                         appendStringInfo(buf, "%*s", padding, dbname);
>                     else
>                         appendStringInfoString(buf, dbname);
>                 }
>                 else if (padding != 0)
>                     appendStringInfoSpaces(buf,
>                                            padding > 0 ? padding : -padding);
> write_csvlog() uses similar logic.
> 
> Unfortunately MyProcPort only exists in user initiated backends.
> 
> It's imo pretty annoying that neither bgworkers nor autovacuum workers
> show the proper database in the log. Why don't we just populate a global
> variable in InitPostgres() once we're sure which database the backend is
> connected to? We could fill fake MyProcPorts, but that doesn't seem like
> a good idea to me.

The attached simple patch implements the former idea.

We could probably replace a couple of get_database_name(MyDatabaseId)
calls by it, but that doesn't look that important.

Greetings,

Andres Freund

-- 
 Andres Freund                     http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services
>From e0b5bae6323f22494ba8aad79a50a8fd69a0d21c Mon Sep 17 00:00:00 2001
From: Andres Freund <and...@anarazel.de>
Date: Fri, 16 May 2014 22:34:47 +0200
Subject: [PATCH] Support showing the database in log messages originating in a
 background process.

---
 src/backend/utils/error/elog.c    | 11 +++++++++--
 src/backend/utils/init/globals.c  |  1 +
 src/backend/utils/init/postinit.c |  3 +++
 src/include/miscadmin.h           |  1 +
 4 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 0d92dcd..db6cc43 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -2320,9 +2320,14 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
 										   padding > 0 ? padding : -padding);
 				break;
 			case 'd':
-				if (MyProcPort)
+				if (MyProcPort || MyDatabaseName)
 				{
-					const char *dbname = MyProcPort->database_name;
+					const char *dbname;
+
+					if (MyProcPort)
+						dbname = MyProcPort->database_name;
+					else
+						dbname = MyDatabaseName;
 
 					if (dbname == NULL || *dbname == '\0')
 						dbname = _("[unknown]");
@@ -2577,6 +2582,8 @@ write_csvlog(ErrorData *edata)
 	/* database name */
 	if (MyProcPort)
 		appendCSVLiteral(&buf, MyProcPort->database_name);
+	else if (MyDatabaseName)
+		appendCSVLiteral(&buf, MyDatabaseName);
 	appendStringInfoChar(&buf, ',');
 
 	/* Process id  */
diff --git a/src/backend/utils/init/globals.c b/src/backend/utils/init/globals.c
index be74835..46d4cf4 100644
--- a/src/backend/utils/init/globals.c
+++ b/src/backend/utils/init/globals.c
@@ -62,6 +62,7 @@ char		postgres_exec_path[MAXPGPATH];		/* full path to backend */
 BackendId	MyBackendId = InvalidBackendId;
 
 Oid			MyDatabaseId = InvalidOid;
+char	   *MyDatabaseName = NULL;
 
 Oid			MyDatabaseTableSpace = InvalidOid;
 
diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c
index ed936d7..0f5aaa8 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -51,6 +51,7 @@
 #include "utils/acl.h"
 #include "utils/fmgroids.h"
 #include "utils/guc.h"
+#include "utils/memutils.h"
 #include "utils/pg_locale.h"
 #include "utils/portal.h"
 #include "utils/ps_status.h"
@@ -795,6 +796,7 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
 		MyDatabaseTableSpace = dbform->dattablespace;
 		/* take database name from the caller, just for paranoia */
 		strlcpy(dbname, in_dbname, sizeof(dbname));
+		MyDatabaseName = MemoryContextStrdup(TopMemoryContext, in_dbname);
 	}
 	else
 	{
@@ -815,6 +817,7 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
 		/* pass the database name back to the caller */
 		if (out_dbname)
 			strcpy(out_dbname, dbname);
+		MyDatabaseName = MemoryContextStrdup(TopMemoryContext, dbname);
 	}
 
 	/* Now we can mark our PGPROC entry with the database ID */
diff --git a/src/include/miscadmin.h b/src/include/miscadmin.h
index c2b786e..91710d3 100644
--- a/src/include/miscadmin.h
+++ b/src/include/miscadmin.h
@@ -165,6 +165,7 @@ extern char postgres_exec_path[];
  * extern BackendId    MyBackendId;
  */
 extern PGDLLIMPORT Oid MyDatabaseId;
+extern PGDLLIMPORT char *MyDatabaseName;
 
 extern PGDLLIMPORT Oid MyDatabaseTableSpace;
 
-- 
2.0.0.rc2.4.g1dc51c6.dirty

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to