Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1jEgBp-0003Tx-Ct for pgsql-hackers@arkaria.postgresql.org; Wed, 18 Mar 2020 21:25:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jEgBo-0005tz-4p for pgsql-hackers@arkaria.postgresql.org; Wed, 18 Mar 2020 21:25:24 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1jEgBn-0005px-JZ for pgsql-hackers@lists.postgresql.org; Wed, 18 Mar 2020 21:25:23 +0000 Received: from mail-qk1-x742.google.com ([2607:f8b0:4864:20::742]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jEgBf-0003RV-Kd for pgsql-hackers@postgresql.org; Wed, 18 Mar 2020 21:25:22 +0000 Received: by mail-qk1-x742.google.com with SMTP id d11so2688716qko.3 for ; Wed, 18 Mar 2020 14:25:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=BuS+HlT6ByirVRhghACz181RX79BWEQOydSDdwc1a/U=; b=IpA623y0Lludmp88ItN2FnNr3WmyN0baWlP1rkvXchXWxO/twT6qimXQbkTUmRxqx3 W7GXEB90uhrTM4o+9MTFCy+ifh2ojh+prtMX/zH8Ddl6iFOOyEmQvGdbcFrIPJVaTZYS lQybMs8BF/5jto3d9w1jVhESv6TSJIxHmgQ+kibywXtRctWHFMKTgygJNRl7TArDyojP EhL88DFOg51d0CEUe6d3REzZN6Q5foJf2+DLkHn9jy9lchitkbXjekRseNYZEOQ3S/v2 ryaai7XuGSF1FlZFBgiRvWdB936rsamTFIQ88mhuetSupxoFaY+GmRLzczFWigHGEvwZ Rqmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=BuS+HlT6ByirVRhghACz181RX79BWEQOydSDdwc1a/U=; b=odHWx3Hl9vXxTuiy+97aU2iNBTAsGfnglojNMK1z0kOlo6jhurrnKI8ciw3cDwuH4P ufH3UHL7bG5ZRS0gIfYRv7gg8iiS1RpuQ1nioOHFShal6PSe+sykv1BUMzsOXMWaL+bI 0KX8GcaKxrQHEQ/76sk7irDeEWHcq6bi9oAH9gPra9ExHnN9gLMGzp+WaS1BXCFlmrlW EfavTY0xrkVoqCeBBOdhQ6lEOGh9+zdwjJFFp4ET6si8PYj+Bm5qSFVp08vD4gtU91jP vFiX2E2jNcuANo5yptjyxHm1BjP1BA86Cq79buwJnOhYAaR6q4yXUg4uatMM6O+3ic7G aXpw== X-Gm-Message-State: ANhLgQ2DEh6tWOAO+tq67AesdvNi1OuevlRLDfa/DkMY0AyBsY9SwMYH JkSrpZBKAbNinzXKaBgqYZb1Hw== X-Google-Smtp-Source: ADFU+vuc8DxQLhJ6unTrjq7K7frPztATtqH42yfj/5gi9DvtjhYkq449GIox4r5NV36HBfns6N9AuQ== X-Received: by 2002:a37:bf42:: with SMTP id p63mr6347249qkf.392.1584566714508; Wed, 18 Mar 2020 14:25:14 -0700 (PDT) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id w18sm116918qkw.130.2020.03.18.14.25.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 18 Mar 2020 14:25:13 -0700 (PDT) Received: by pryzbyj (Postfix, from userid 1000) id C8D61800C55; Wed, 18 Mar 2020 16:25:11 -0500 (CDT) Date: Wed, 18 Mar 2020 16:25:11 -0500 From: Justin Pryzby To: Julien Rouhaud Cc: pgsql-hackers@postgresql.org Subject: Re: expose parallel leader in CSV and log_line_prefix Message-ID: <20200318212511.GN26184@telsasoft.com> References: <20200315111831.GA21492@telsasoft.com> <20200315114933.udjdx5wcgwkjnw45@nol> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="MdJ3p2aQbVp3YFXz" Content-Disposition: inline In-Reply-To: <20200315114933.udjdx5wcgwkjnw45@nol> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --MdJ3p2aQbVp3YFXz Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Sun, Mar 15, 2020 at 12:49:33PM +0100, Julien Rouhaud wrote: > On Sun, Mar 15, 2020 at 06:18:31AM -0500, Justin Pryzby wrote: > > See also: > > https://commitfest.postgresql.org/27/2390/ > > https://www.postgresql.org/message-id/flat/CAOBaU_Yy5bt0vTPZ2_LUM6cUcGeqmYNoJ8-Rgto+c2+w3defYA@mail.gmail.com > > b025f32e0b Add leader_pid to pg_stat_activity > > FTR this is a followup of https://www.postgresql.org/message-id/20200315095728.GA26184%40telsasoft.com Yes - but I wasn't going to draw attention to the first patch, in which I did something needlessly complicated and indirect. :) > + case 'k': > + if (MyBackendType != B_BG_WORKER) > + ; /* Do nothing */ > > > Isn't the test inverted? Also a bgworker could run parallel queries through > SPI I think, should we really ignore bgworkers? I don't think it's reversed, but I think I see your point: the patch is supposed to be showing the leader's own PID for the leader itself. So I think that can just be removed. > + else if (!MyProc->lockGroupLeader) > + ; /* Do nothing */ > > There should be a test that MyProc isn't NULL. Yes, done. > + else if (padding != 0) > + appendStringInfo(buf, "%*d", padding, MyProc->lockGroupLeader->pid); > + else > + appendStringInfo(buf, "%d", MyProc->lockGroupLeader->pid); > + break; > > I think that if padding was asked we should append spaces rather than doing > nothing. Done It logs like: template1=# SET log_temp_files=0; explain analyze SELECT a,COUNT(1) FROM t a JOIN t b USING(a) GROUP BY 1; 2020-03-15 21:20:47.288 CDT [5537 5537]LOG: statement: SET log_temp_files=0; SET 2020-03-15 21:20:47.289 CDT [5537 5537]LOG: statement: explain analyze SELECT a,COUNT(1) FROM t a JOIN t b USING(a) GROUP BY 1; 2020-03-15 21:20:51.253 CDT [5627 5537]LOG: temporary file: path "base/pgsql_tmp/pgsql_tmp5627.0", size 6094848 2020-03-15 21:20:51.253 CDT [5627 5537]STATEMENT: explain analyze SELECT a,COUNT(1) FROM t a JOIN t b USING(a) GROUP BY 1; 2020-03-15 21:20:51.254 CDT [5626 5537]LOG: temporary file: path "base/pgsql_tmp/pgsql_tmp5626.0", size 6103040 2020-03-15 21:20:51.254 CDT [5626 5537]STATEMENT: explain analyze SELECT a,COUNT(1) FROM t a JOIN t b USING(a) GROUP BY 1; 2020-03-15 21:20:51.263 CDT [5537 5537]LOG: temporary file: path "base/pgsql_tmp/pgsql_tmp5537.1.sharedfileset/o15of16.p0.0", size 557056 Now, with the leader showing its own PID. This also fixes unsafe access to lockGroupLeader->pid, same issue as in the original v1 patch for b025f32e0b. -- Justin --MdJ3p2aQbVp3YFXz Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v3-0001-Include-the-leader-PID-in-logfile.patch" From 53d766e88537ca018d46efc671d5b586862fb30b Mon Sep 17 00:00:00 2001 From: Justin Pryzby Date: Fri, 13 Mar 2020 22:03:06 -0500 Subject: [PATCH v3] Include the leader PID in logfile See also: b025f32e0b, which adds the leader PID to pg_stat_activity --- doc/src/sgml/config.sgml | 11 +++++++- src/backend/utils/error/elog.c | 27 +++++++++++++++++++ src/backend/utils/misc/postgresql.conf.sample | 1 + 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index 672bf6f1ee..b9a1bd9a31 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -6511,6 +6511,14 @@ local0.* /var/log/postgresql Process ID no + + %k + Process ID of the parallel group leader if this + process is or was involved in parallel query, null + otherwise. For a parallel group leader, this field is + set to its own process ID. + no + %t Time stamp without milliseconds @@ -6809,7 +6817,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), - application name, and backend type. + application name, backend type, and leader PID. Here is a sample table definition for storing CSV-format log output: @@ -6839,6 +6847,7 @@ CREATE TABLE postgres_log location text, application_name text, backend_type text, + leader_pid integer, PRIMARY KEY (session_id, session_line_num) ); diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c index 62eef7b71f..5328664f4d 100644 --- a/src/backend/utils/error/elog.c +++ b/src/backend/utils/error/elog.c @@ -77,6 +77,7 @@ #include "postmaster/syslogger.h" #include "storage/ipc.h" #include "storage/proc.h" +#include "storage/procarray.h" #include "tcop/tcopprot.h" #include "utils/guc.h" #include "utils/memutils.h" @@ -2560,6 +2561,22 @@ log_line_prefix(StringInfo buf, ErrorData *edata) else appendStringInfo(buf, "%d", MyProcPid); break; + + case 'k': + if (MyProc) + { + PGPROC *leader = MyProc->lockGroupLeader; + if (leader == NULL) + /* padding only */ + appendStringInfoSpaces(buf, + padding > 0 ? padding : -padding); + else if (padding != 0) + appendStringInfo(buf, "%*d", padding, leader->pid); + else + appendStringInfo(buf, "%d", leader->pid); + } + break; + case 'l': if (padding != 0) appendStringInfo(buf, "%*ld", padding, log_line_number); @@ -2948,6 +2965,16 @@ write_csvlog(ErrorData *edata) else appendCSVLiteral(&buf, GetBackendTypeDesc(MyBackendType)); + appendStringInfoChar(&buf, ','); + + /* leader PID */ + if (MyProc) + { + PGPROC *leader = MyProc->lockGroupLeader; + if (leader) + appendStringInfo(&buf, "%d", leader->pid); + } + 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 aa44f0c9bf..6874792a15 100644 --- a/src/backend/utils/misc/postgresql.conf.sample +++ b/src/backend/utils/misc/postgresql.conf.sample @@ -530,6 +530,7 @@ # %h = remote host # %b = backend type # %p = process ID + # %k = leader PID # %t = timestamp without milliseconds # %m = timestamp with milliseconds # %n = timestamp with milliseconds (as a Unix epoch) -- 2.17.0 --MdJ3p2aQbVp3YFXz--