Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tnC3T-0032FV-Pa for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Feb 2025 07:42:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tnC3R-000Au4-D1 for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Feb 2025 07:42:05 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tnC3Q-000Ate-V1 for pgsql-hackers@lists.postgresql.org; Wed, 26 Feb 2025 07:42:05 +0000 Received: from mail-wr1-x435.google.com ([2a00:1450:4864:20::435]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tnC3O-0009Kx-0B for pgsql-hackers@postgresql.org; Wed, 26 Feb 2025 07:42:04 +0000 Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-38f29a1a93bso5192413f8f.1 for ; Tue, 25 Feb 2025 23:42:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740555719; x=1741160519; darn=postgresql.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=KMlKWHVLDylfR21hcNyLFcv/89U2ve71kOvTjpEZLY8=; b=jH3oucWj3lrDOSZFAYvxCdkAORIoAKLt8U6X89FP830PH2hcpEPiNK1h+TwS8iPcGf 6uAnDgF0hPUP19nLQ/FTy+5IY/dE1P8aRNxWrhZbZdYQ4VvEbs7AEHn+BlRYYzZDIKBk L4CetTf5CjxjK/5BF2N0DRS1x/kT0d8hudcxHWV/+SIZYDUz4zkV94KXyFKF+PwAZXLn Da3/oFejXpBAbg8ZzhuyBGI9LTHO5m+XHnQu+mhGW7mLQNOERY2NLeRZQHMeWW90GNxl KR3FawJTi1xn3WIWlYJ1TbYlRI4u+OMI2p25hC3p9fKpU0wLCd6d8oGCp0J8Wdg/spjI otlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740555719; x=1741160519; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=KMlKWHVLDylfR21hcNyLFcv/89U2ve71kOvTjpEZLY8=; b=T7qTk661/Cq1g+NVXoDiaIR6GsZ4vtXEPQgtvlHQ4Wj2bZGQdnPoQKn513FCaOenth To1w+LvqgozDV60IE9elpyyVVKGSAgAqqc8WBnV/k5jFCrxUT18FDwt+ElbD0D4Y1HC9 GntPNLv+sKyI0XLijS642foQo/zlrpGCdRrXbmyjhtHKmChnAVolfeoEo1FHHVKGDO0s f1mPe3Ff6kfDdivFDZX7lX3M3kzLc8BvvWSDO8e/Aw23BFYAEcqCM+Dk1UGtZaVfJV0A zYjrE6TfF6LyAGTSVB2BkrqP8391E+luxRPgJPHhS4ZyVNcIPBv8JJXCL41K4+w28Kcl +eww== X-Forwarded-Encrypted: i=1; AJvYcCVjx7Uk8EwtHWEL4MctDMcFJF2/TKPuyKR6vvbeh2tyzabcnMxpbq5nL5XQP4E0UImYg6Ekxy+WmbJ/V9Rj@postgresql.org X-Gm-Message-State: AOJu0YzGkZzqOsg/pxDv0emlI5X4prTj73eJzm5lTcRMTRv8Ok8Admge 4gAgvU2w+O9oGML8w0+aS91mhs4IpKTc8jBw779Yy64KS+lc7nCx X-Gm-Gg: ASbGnctLgHVxwTN/nQhRFM9VvPF1kY2WV908Dk4mBWmg3eIa+iEsT3zolrLcqNNxQbY 33kLj0n4LNirUYkjwBGpjI492Qn4D1/d6U22xiAiscCu4mAjgHV86M774Uno/sxpL/9+t/f4vNF yJgVj2nWwE99yyIxutDMVDqNRK9s9KFazoNuIXDQTN+hCAaCgGCyDM6piRm2Nl8zcPfSJk93Aeh eT/kohgxuH7K5ng+G0S7h5yDXH4mLBi/Q8tJtrylfnDqPIubbe1tHpVdgy2BT6qmddQZ0GeCRz1 ypiJui1BHIpT6BMlVkOH+Q9MUYJpBKqOkMowPiXI9yEHxOiDZEG4JOoOIk+3CEF4pRA42fmZXMB PzkUW2yEpbo7DxHKYUm9c/oRUgirULznh/qVATZDyGKEpyRV3e9lKBheV8A== X-Google-Smtp-Source: AGHT+IGPN28BqkdVluZ6bzQn2Z7UYH2a/jauoGd6c7V7qWobVs1yN9IEJU9Dtt0lI1NlbeWDFXOpAw== X-Received: by 2002:adf:f249:0:b0:38f:2b54:58dd with SMTP id ffacd0b85a97d-390d4f3cf5cmr1293806f8f.21.1740555719135; Tue, 25 Feb 2025 23:41:59 -0800 (PST) Received: from ip-10-97-1-34.eu-west-3.compute.internal (ec2-15-237-181-182.eu-west-3.compute.amazonaws.com. [15.237.181.182]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-390cd70be4csm4651022f8f.0.2025.02.25.23.41.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Feb 2025 23:41:58 -0800 (PST) Date: Wed, 26 Feb 2025 07:41:57 +0000 From: Bertrand Drouvot To: Fujii Masao Cc: Melanie Plageman , Guillaume Lelarge , Pg Hackers , Daniel Gustafsson , andrey.chudnovskiy@microsoft.com, Jelte Fennema-Nio , Jacob Champion Subject: Re: Log connection establishment timings Message-ID: References: <65fe2d68-a214-40f0-80a8-56ca7323ad5c@oss.nttdata.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <65fe2d68-a214-40f0-80a8-56ca7323ad5c@oss.nttdata.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On Wed, Feb 26, 2025 at 01:46:19PM +0900, Fujii Masao wrote: > > > On 2025/02/26 6:36, Melanie Plageman wrote: > > On Tue, Feb 25, 2025 at 3:23 PM Melanie Plageman > > wrote: > > > > > > Thanks for doing this! I have implemented your suggestion in attached v3. Thanks for the new patch version! > + /* Capture time Postmaster initiates fork for logging */ > + if (child_type == B_BACKEND) > + INSTR_TIME_SET_CURRENT(((BackendStartupData *) startup_data)->fork_time); > > When log_connections is enabled, walsender connections are also logged. > However, with the patch, it seems the connection time for walsenders isn't captured. > Is this intentional? Good point. I'm tempted to say that it should also be, specially because a connection done as "psql replication=database" is of "walsender" backend type and would not be reported. > With the current patch, when log_connections is enabled, the connection time is always > captured, and which might introduce performance overhead. No? Some users who enable > log_connections may not want this extra detail and want to avoid such overhead. > So, would it make sense to extend log_connections with a new option like "timing" and > log the connection time only when "timing" is specified? +1, I also think it's a good idea to let users decide if they want the timing measurement overhead (and it's common practice with track_io_timing, track_wal_io_timing, the newly track_cost_delay_timing for example) > + ereport(LOG, > + errmsg("backend ready for query. pid=%d. socket=%d. connection establishment times (ms): total: %ld, fork: %ld, authentication: %ld", > + MyProcPid, > + (int) MyClientSocket->sock, > > Why expose the socket's file descriptor? I'm not sure how users would use that information. > > > Including the PID seems unnecessary since it's already available via log_line_prefix with %p? Yeah, we would get things like: [1111539] LOG: connection received: host=[local] [1111539] LOG: connection authenticated: user="postgres" method=trust (/home/postgres/postgresql/pg_installed/pg18/data/pg_hba.conf:117) [1111539] LOG: connection authorized: user=postgres database=postgres application_name=psql [1111539] LOG: backend ready for query. pid=1111539. socket=9. connection establishment times (ms): total: 2, fork: 0, authentication: 0 I also wonder if "backend ready for query" is worth it. Maybe something like: 2025-02-26 06:44:23.265 UTC [1111539] LOG: connection establishment times (ms): total: 2, fork: 0, authentication: 0 would be good enough? A few random comments: === 1 +typedef struct ConnectionTiming +{ + instr_time fork_duration; + instr_time auth_duration; +} ConnectionTiming; As it's all about instr_time, I wonder if we could use an enum + array instead. That's probably just a matter of taste but that sounds more flexible to extend (should we want to add more timing in the future). === 2 +ConnectionTiming conn_timing = {0}; There is no padding in ConnectionTiming and anyway we just access its fields so that's ok to initialize that way. === 3 Add a few words in the log_connections GUC doc? (anyway we will have to if Fujii-san idea above about the timing is implemented) === 4 + /* Calculate total fork duration in child backend for logging */ + if (child_type == B_BACKEND) + { + INSTR_TIME_SET_CURRENT(conn_timing.fork_duration); + INSTR_TIME_SUBTRACT(conn_timing.fork_duration, + ((BackendStartupData *) startup_data)->fork_time); + } + /* Close the postmaster's sockets */ ClosePostmasterPorts(child_type == B_LOGGER); @@ -618,6 +630,14 @@ SubPostmasterMain(int argc, char *argv[]) /* Read in the variables file */ read_backend_variables(argv[2], &startup_data, &startup_data_len); + /* Calculate total fork duration in child backend for logging */ + if (child_type == B_BACKEND) + { + INSTR_TIME_SET_CURRENT(conn_timing.fork_duration); + INSTR_TIME_SUBTRACT(conn_timing.fork_duration, + ((BackendStartupData *) startup_data)->fork_time); + } worth to add a helper function to avoid code duplication? Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com