From ec2b614282f5f661708413b72a94586d95ea0366 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Thu, 20 Feb 2020 18:16:39 +0100 Subject: [PATCH v3 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 @@ What to Log Application name yes + + %b + Backend process type + yes + %u User name @@ -6785,7 +6790,7 @@ Using CSV-Format Log Output character count of the error position therein, location of the error in the PostgreSQL source code (if log_error_verbosity is set to verbose), - and application name. + application name, and backend type. Here is a sample table definition for storing CSV-format log output: @@ -6814,6 +6819,7 @@ Using CSV-Format Log Output query_pos integer, location text, application_name text, + backend_type text, PRIMARY KEY (session_id, session_line_num) ); 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 e58e4788a8..c0e9531f9c 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 a53e4a6243..f6a5e1b9c7 100644 --- a/src/test/regress/pg_regress.c +++ b/src/test/regress/pg_regress.c @@ -2334,7 +2334,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