Hello

I'm working on several databases where schemas are used to differentiate the 
tenants.
This is great for performance, but several tools are lacking around this 
usecase by not showing the schema, one of them being log_line_prefix.
It is possible to work around this using the application_name, but a mistake 
on the application side would be fatal, while the search_path would still 
indicate the real tables used in a query.
The attached patch implements this, using %S. I've not written the 
documentation yet, since I'm not sure this would be acceptable as is, or if a 
more "generic" method should be used (I thought of %{name} to fetch an 
arbitrary GUC, but did not implement due to a lack of need for that feature)


>From d28ea4452c6e78bf8e67db49d1c72dbf4bd1ca48 Mon Sep 17 00:00:00 2001
From: Pierre Ducroquet <p.p...@pinaraf.info>
Date: Mon, 25 Jul 2022 09:33:49 +0200
Subject: [PATCH] log_line_prefix: make it possible to add search_path

This adds support for %S to add the current search_path in log_line_prefix, thus
making it easier to track slow queries on databases with for instance one schema
per tenant.
---
 src/backend/utils/error/elog.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 95f32de4e2..41e894bc83 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -68,6 +68,7 @@
 
 #include "access/transam.h"
 #include "access/xact.h"
+#include "catalog/namespace.h"
 #include "libpq/libpq.h"
 #include "libpq/pqformat.h"
 #include "mb/pg_wchar.h"
@@ -83,7 +84,6 @@
 #include "utils/memutils.h"
 #include "utils/ps_status.h"
 
-
 /* In this module, access gettext() via err_gettext() */
 #undef _
 #define _(x) err_gettext(x)
@@ -2791,6 +2791,12 @@ log_status_format(StringInfo buf, const char *format, ErrorData *edata)
 					appendStringInfo(buf, "%lld",
 									 (long long) pgstat_get_my_query_id());
 				break;
+			case 'S':
+				if (padding != 0)
+					appendStringInfo(buf, "%*s", padding, namespace_search_path);
+				else
+					appendStringInfoString(buf, namespace_search_path);
+
 			default:
 				/* format error - ignore it */
 				break;
-- 
2.37.1

Reply via email to