v2-0003-Add-backend-type-to-csvlog-and-optionally-log_lin.patch
text/plain
Filename: v2-0003-Add-backend-type-to-csvlog-and-optionally-log_lin.patch
Type: text/plain
Part: 2
Message:
Re: backend type in log_line_prefix?
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: format-patch
Series: patch v2-0003
Subject: Add backend type to csvlog and optionally log_line_prefix
| File | + | − |
|---|---|---|
| doc/src/sgml/config.sgml | 7 | 1 |
| src/backend/utils/error/elog.c | 29 | 0 |
| src/backend/utils/misc/postgresql.conf.sample | 1 | 0 |
| src/test/regress/pg_regress.c | 1 | 1 |
From 75ac8ed0c47801712eb2aa300d9cb29767d2e121 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Thu, 20 Feb 2020 18:16:39 +0100
Subject: [PATCH v2 3/4] Add backend type to csvlog and optionally
log_line_prefix
---
doc/src/sgml/config.sgml | 8 ++++-
src/backend/utils/error/elog.c | 29 +++++++++++++++++++
src/backend/utils/misc/postgresql.conf.sample | 1 +
src/test/regress/pg_regress.c | 2 +-
4 files changed, 38 insertions(+), 2 deletions(-)
diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index c1128f89ec..206778b1c3 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -6470,6 +6470,11 @@ <title>What to Log</title>
<entry>Application name</entry>
<entry>yes</entry>
</row>
+ <row>
+ <entry><literal>%b</literal></entry>
+ <entry>Backend process type</entry>
+ <entry>yes</entry>
+ </row>
<row>
<entry><literal>%u</literal></entry>
<entry>User name</entry>
@@ -6785,7 +6790,7 @@ <title>Using CSV-Format Log Output</title>
character count of the error position therein,
location of the error in the PostgreSQL source code
(if <varname>log_error_verbosity</varname> is set to <literal>verbose</literal>),
- and application name.
+ application name, and backend type.
Here is a sample table definition for storing CSV-format log output:
<programlisting>
@@ -6814,6 +6819,7 @@ <title>Using CSV-Format Log Output</title>
query_pos integer,
location text,
application_name text,
+ backend_type text,
PRIMARY KEY (session_id, session_line_num)
);
</programlisting>
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index f5b0211f66..bb65eb41ca 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -72,6 +72,8 @@
#include "libpq/pqformat.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
+#include "pgstat.h"
+#include "postmaster/bgworker.h"
#include "postmaster/postmaster.h"
#include "postmaster/syslogger.h"
#include "storage/ipc.h"
@@ -2492,6 +2494,23 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
padding > 0 ? padding : -padding);
break;
+ case 'b':
+ {
+ const char *backend_type_str;
+
+ if (MyProcPid == PostmasterPid)
+ backend_type_str = "postmaster";
+ else if (MyBackendType == B_BG_WORKER)
+ backend_type_str = MyBgworkerEntry->bgw_type;
+ else
+ backend_type_str = pgstat_get_backend_desc(MyBackendType);
+
+ if (padding != 0)
+ appendStringInfo(buf, "%*s", padding, backend_type_str);
+ else
+ appendStringInfoString(buf, backend_type_str);
+ break;
+ }
case 'u':
if (MyProcPort)
{
@@ -2920,6 +2939,16 @@ write_csvlog(ErrorData *edata)
if (application_name)
appendCSVLiteral(&buf, application_name);
+ appendStringInfoChar(&buf, ',');
+
+ /* backend type */
+ if (MyProcPid == PostmasterPid)
+ appendCSVLiteral(&buf, "postmaster");
+ else if (MyBackendType == B_BG_WORKER)
+ appendCSVLiteral(&buf, MyBgworkerEntry->bgw_type);
+ else
+ appendCSVLiteral(&buf, pgstat_get_backend_desc(MyBackendType));
+
appendStringInfoChar(&buf, '\n');
/* If in the syslogger process, try to write messages direct to file */
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
index e1048c0047..323f120edd 100644
--- a/src/backend/utils/misc/postgresql.conf.sample
+++ b/src/backend/utils/misc/postgresql.conf.sample
@@ -524,6 +524,7 @@
#log_hostname = off
#log_line_prefix = '%m [%p] ' # special values:
# %a = application name
+ # %b = backend type
# %u = user name
# %d = database name
# %r = remote host and port
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index 9a4e52bc7b..37fcdaabe2 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -2333,7 +2333,7 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
fputs("\n# Configuration added by pg_regress\n\n", pg_conf);
fputs("log_autovacuum_min_duration = 0\n", pg_conf);
fputs("log_checkpoints = on\n", pg_conf);
- fputs("log_line_prefix = '%m [%p] %q%a '\n", pg_conf);
+ fputs("log_line_prefix = '%m %b[%p] %q%a '\n", pg_conf);
fputs("log_lock_waits = on\n", pg_conf);
fputs("log_temp_files = 128kB\n", pg_conf);
fputs("max_prepared_transactions = 2\n", pg_conf);
--
2.25.0