Add backend type to csvlog and optionally log_line_prefix
authorPeter Eisentraut
Sun, 15 Mar 2020 10:20:21 +0000 (11:20 +0100)
committerPeter Eisentraut
Sun, 15 Mar 2020 10:20:21 +0000 (11:20 +0100)
The backend type, which corresponds to what
pg_stat_activity.backend_type shows, is added as a column to the
csvlog and can optionally be added to log_line_prefix using the new %b
placeholder.

Reviewed-by: Julien Rouhaud
Reviewed-by: Kuntal Ghosh
Reviewed-by: Alvaro Herrera
Reviewed-by: Justin Pryzby
Discussion: https://www.postgresql.org/message-id/flat/c65e5196-4f04-4ead-9353-6088c19615a3@2ndquadrant.com

doc/src/sgml/config.sgml
src/backend/utils/error/elog.c
src/backend/utils/misc/postgresql.conf.sample
src/test/regress/pg_regress.c

index 371d7838fb6473e87755633729e7843dfc5a8ed5..3cac340f3237f2358c6ca503945a56ad8ecc792d 100644 (file)
@@ -6438,9 +6438,13 @@ local0.*    /var/log/postgresql
          right with spaces to give it a minimum width, whereas a positive
          value will pad on the left. Padding can be useful to aid human
          readability in log files.
+       
+
+       
          This parameter can only be set in the postgresql.conf
          file or on the server command line. The default is
          '%m [%p] ' which logs a time stamp and the process ID.
+       
 
          
           
@@ -6477,6 +6481,11 @@ local0.*    /var/log/postgresql
              Remote host name or IP address
              yes
             
+            
+             %b
+             Backend type
+             no
+            
             
              %p
              Process ID
@@ -6548,6 +6557,14 @@ local0.*    /var/log/postgresql
           
          
 
+         
+          The backend type corresponds to the column
+          backend_type in the view 
+          linkend="pg-stat-activity-view"/>, but additional types can appear
+          in the log that don't show in that view.
+         
+
+         
          The %c escape prints a quasi-unique session identifier,
          consisting of two 4-byte hexadecimal numbers (without leading zeros)
          separated by a dot.  The numbers are the process start time and the
@@ -6772,7 +6789,7 @@ log_line_prefix = '%m [%p] %q%u@%d/%a '
         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:
 
 
@@ -6801,6 +6818,7 @@ CREATE TABLE postgres_log
   query_pos integer,
   location text,
   application_name text,
+  backend_type text,
   PRIMARY KEY (session_id, session_line_num)
 );
 
index f8ae94729cce335276d5b7c5eb91ec854c6080f2..62eef7b71f463fb60b31c15e79be264fea8a5fba 100644 (file)
@@ -72,6 +72,7 @@
 #include "libpq/pqformat.h"
 #include "mb/pg_wchar.h"
 #include "miscadmin.h"
+#include "postmaster/bgworker.h"
 #include "postmaster/postmaster.h"
 #include "postmaster/syslogger.h"
 #include "storage/ipc.h"
@@ -2492,6 +2493,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 = GetBackendTypeDesc(MyBackendType);
+
+                   if (padding != 0)
+                       appendStringInfo(buf, "%*s", padding, backend_type_str);
+                   else
+                       appendStringInfoString(buf, backend_type_str);
+                   break;
+               }
            case 'u':
                if (MyProcPort)
                {
@@ -2920,6 +2938,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, GetBackendTypeDesc(MyBackendType));
+
    appendStringInfoChar(&buf, '\n');
 
    /* If in the syslogger process, try to write messages direct to file */
index e58e4788a8efb181b9e8a55e3d4db9f6e9c7749d..aa44f0c9bf25595d4b8c3325a573944644016b42 100644 (file)
                    #   %d = database name
                    #   %r = remote host and port
                    #   %h = remote host
+                   #   %b = backend type
                    #   %p = process ID
                    #   %t = timestamp without milliseconds
                    #   %m = timestamp with milliseconds
index a53e4a6243b52cf796e6babc9ba279549d5456de..f6a5e1b9c7661e0e033524d89d6c2f2c5bc27674 100644 (file)
@@ -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);