Add %P to log_line_prefix for parallel group leader
authorMichael Paquier <michael@paquier.xyz>
Mon, 3 Aug 2020 04:38:48 +0000 (13:38 +0900)
committerMichael Paquier <michael@paquier.xyz>
Mon, 3 Aug 2020 04:38:48 +0000 (13:38 +0900)
This is useful for monitoring purposes with log parsing.  Similarly to
pg_stat_activity, the leader's PID is shown only for active parallel
workers, minimizing the log footprint for the leaders as the equivalent
shared memory field is set as long as a backend is alive.

Author: Justin Pryzby
Reviewed-by: Álvaro Herrera, Michael Paquier, Julien Rouhaud, Tom Lane
Discussion: https://postgr.es/m/20200315111831.GA21492@telsasoft.com

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

index 994155ca00e226fe23f7f48add8690bd2e93014e..7a7177c55083f5bdd24120774c49fc537082c12b 100644 (file)
@@ -6694,6 +6694,12 @@ local0.*    /var/log/postgresql
              <entry>Process ID</entry>
              <entry>no</entry>
             </row>
+            <row>
+             <entry><literal>%P</literal></entry>
+             <entry>Process ID of the parallel group leader, if this process
+              is a parallel query worker</entry>
+             <entry>no</entry>
+            </row>
             <row>
              <entry><literal>%t</literal></entry>
              <entry>Time stamp without milliseconds</entry>
@@ -7026,7 +7032,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 <varname>log_error_verbosity</varname> is set to <literal>verbose</literal>),
-        application name, and backend type.
+        application name, backend type, and process ID of parallel group leader.
         Here is a sample table definition for storing CSV-format log output:
 
 <programlisting>
@@ -7056,6 +7062,7 @@ CREATE TABLE postgres_log
   location text,
   application_name text,
   backend_type text,
+  leader_pid integer,
   PRIMARY KEY (session_id, session_line_num)
 );
 </programlisting>
index e4b717c79a9c199cebc1e8ead949d284d56b93fc..d0b368530e7e3764ad42029c3d69f6b5b02d5ea3 100644 (file)
@@ -2448,6 +2448,29 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
                else
                    appendStringInfo(buf, "%d", MyProcPid);
                break;
+
+           case 'P':
+               if (MyProc)
+               {
+                   PGPROC     *leader = MyProc->lockGroupLeader;
+
+                   /*
+                    * Show the leader only for active parallel workers. This
+                    * leaves out the leader of a parallel group.
+                    */
+                   if (leader == NULL || leader->pid == MyProcPid)
+                       appendStringInfoSpaces(buf,
+                                              padding > 0 ? padding : -padding);
+                   else if (padding != 0)
+                       appendStringInfo(buf, "%*d", padding, leader->pid);
+                   else
+                       appendStringInfo(buf, "%d", leader->pid);
+               }
+               else if (padding != 0)
+                   appendStringInfoSpaces(buf,
+                                          padding > 0 ? padding : -padding);
+               break;
+
            case 'l':
                if (padding != 0)
                    appendStringInfo(buf, "%*ld", padding, log_line_number);
@@ -2836,6 +2859,21 @@ write_csvlog(ErrorData *edata)
    else
        appendCSVLiteral(&buf, GetBackendTypeDesc(MyBackendType));
 
+   appendStringInfoChar(&buf, ',');
+
+   /* leader PID */
+   if (MyProc)
+   {
+       PGPROC     *leader = MyProc->lockGroupLeader;
+
+       /*
+        * Show the leader only for active parallel workers.  This leaves out
+        * the leader of a parallel group.
+        */
+       if (leader && leader->pid != MyProcPid)
+           appendStringInfo(&buf, "%d", leader->pid);
+   }
+
    appendStringInfoChar(&buf, '\n');
 
    /* If in the syslogger process, try to write messages direct to file */
index b0715ae1881803dda187ae804d07b6a6f6addbde..9cb571f7cc73992c4531e5c8c9a39905f13b78db 100644 (file)
                    #   %h = remote host
                    #   %b = backend type
                    #   %p = process ID
+                   #   %P = process ID of parallel group leader
                    #   %t = timestamp without milliseconds
                    #   %m = timestamp with milliseconds
                    #   %n = timestamp with milliseconds (as a Unix epoch)