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 1rm5JK-005vDx-EG for pgsql-hackers@arkaria.postgresql.org; Mon, 18 Mar 2024 05:13:23 +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 1rm5JH-00FUKL-3t for pgsql-hackers@arkaria.postgresql.org; Mon, 18 Mar 2024 05:13:19 +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 1rm5JF-00FUK5-9X for pgsql-hackers@lists.postgresql.org; Mon, 18 Mar 2024 05:13:19 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rm5JB-0055GI-1G for pgsql-hackers@postgresql.org; Mon, 18 Mar 2024 05:13:16 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 9B18D32000D7; Mon, 18 Mar 2024 01:13:11 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Mon, 18 Mar 2024 01:13:11 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1710738791; x=1710825191; bh=ILxs+0m8z1 F2tZ0w98lHjtTdlaNTHHTZp/N7xw3PIIA=; b=JdLFYajH+LR8pL9Hz2DAOThZ4L GdN0YYgZQO0ut5WS4pJRsnlbEeySKuJJ3Vv0iEhM5BF9RVp8lhQxbtZ1DT1BEN/O HgaVT1LOT7083i81s9siYyEy76kqMst7k+tHMZS0BUm93RhYbukx3PIN+/ci2DCy 2DxAeV3WJa/rbuzG3IU+7y5F3QNlTfUMpbtP+SLhxGMC/HEEem8mf03poJCe1a0a qCqxa6pCyKziTqeS6mdJGyxIiFs53SFMd3oNgWjovH8tKsDyZCvkeDrl1hPT15UQ 5U/MokINm06dubCuaLpLVnQU5dpLNvSK77UG2jguiDKL8CzpeSde8F8xB2UA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1710738791; x=1710825191; bh=ILxs+0m8z1F2tZ0w98lHjtTdlaNT HHTZp/N7xw3PIIA=; b=Gv0gkQwKxsOgF2kUk3hW7+fBgv1RvXN4l1Dh5sqBNVri +2Ng2dqRduzu6kpuK9okRVqt9mqiAGSHxVOnWXXgrc9xp/d0nt3EkJ7b3Bax1ijD xdNu8rkU2e3CgUw9aG39BmK9IvZgaiGA3enYKkbiOVrHU52V4Yxy6Uu1lcppudcr eoJ7uwb7yyAfvW/EiauEXl9JV0YX18hoKbTLNVBAfUfdVZ4eJu3g/U6S7ytUcafr mHysFIcY3CoHp9FjGTxxUm4m2EGHNcS5Ybxcpr9QxJnGSjjpLvdivYaQe4DBMczZ U76QrNX5ZtgaIAl/2Z5IaBzYIF1Qkb1fsKh5QW4efA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrkeeigdekudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenfg hrlhcuvffnffculdefhedmnecujfgurhepfffhvfevuffkfhggtggujgesghdtreertddt vdenucfhrhhomhepofhitghhrggvlhcurfgrqhhuihgvrhcuoehmihgthhgrvghlsehprg hquhhivghrrdighiiiqeenucggtffrrghtthgvrhhnpeegffejvefgveduvdejtddvtdei jeehudeuledvudeftdfgfeejvdekveekiedvvdenucffohhmrghinhepphhoshhtghhrvg hsqhhlrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhf rhhomhepmhhitghhrggvlhesphgrqhhuihgvrhdrgiihii X-ME-Proxy: Feedback-ID: i0fe9450f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 18 Mar 2024 01:13:09 -0400 (EDT) Date: Mon, 18 Mar 2024 14:12:57 +0900 From: Michael Paquier To: Kambam Vinay Cc: pgsql-hackers@postgresql.org Subject: Re: Fix for timestamp lag issue from emit_log_hook when GUC log_line_prefix has '%m' Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="Zk0Pbwwpkf3gGOns" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Zk0Pbwwpkf3gGOns Content-Type: multipart/mixed; boundary="VT+EhD3VPy0xZONq" Content-Disposition: inline --VT+EhD3VPy0xZONq Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Sun, Mar 17, 2024 at 07:35:57PM +0530, Kambam Vinay wrote: > Thanks Michael for the review. Agree with your comment on the patch. > updated the patch with recommended change. That should be fine. I would suggest to document why the reset is done at this location, aka this is to ensure that the same formatted timestamp is used across the board, for all log destinations as well as hook callers that rely on log_line_prefix. While reviewing, I have noticed that a comment was not correct: JSON logs also use the formatted timestamp via get_formatted_log_time(). I may be able to get this one committed just before the feature freeze of v17, as timestamp consistency for hooks that call log_status_format() is narrow. For now, I have added an entry in the CF app to keep track of it: https://commitfest.postgresql.org/48/4901/ -- Michael --VT+EhD3VPy0xZONq Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v2-0001-set-saved_timeval_set-to-false-before-executing-e.patch" Content-Transfer-Encoding: quoted-printable =46rom db20e006f16de889ceb884d63dd25828ec711185 Mon Sep 17 00:00:00 2001 =46rom: Michael Paquier Date: Mon, 18 Mar 2024 14:08:45 +0900 Subject: [PATCH v2] set saved_timeval_set to false before executing emit_log_hook() --- src/backend/utils/error/elog.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c index 52bc01058c..ea96047352 100644 --- a/src/backend/utils/error/elog.c +++ b/src/backend/utils/error/elog.c @@ -154,7 +154,7 @@ static int recursion_depth =3D 0; /* to detect actual r= ecursion */ =20 /* * Saved timeval and buffers for formatted timestamps that might be used by - * both log_line_prefix and csv logs. + * log_line_prefix, csv logs and json logs. */ static struct timeval saved_timeval; static bool saved_timeval_set =3D false; @@ -1678,6 +1678,14 @@ EmitErrorReport(void) CHECK_STACK_DEPTH(); oldcontext =3D MemoryContextSwitchTo(edata->assoc_context); =20 + /* + * Reset the formatted timestamp fields before emitting any logs. This + * includes all the log destinations and the hook, as the latter may use + * Log_line_prefix. + */ + saved_timeval_set =3D false; + formatted_log_time[0] =3D '\0'; + /* * Call hook before sending message to log. The hook function is allowed * to turn off edata->output_to_server, so we must recheck that afterward. @@ -2948,8 +2956,6 @@ log_status_format(StringInfo buf, const char *format,= ErrorData *edata) appendStringInfo(buf, "%ld", log_line_number); break; case 'm': - /* force a log timestamp reset */ - formatted_log_time[0] =3D '\0'; (void) get_formatted_log_time(); =20 if (padding !=3D 0) @@ -3151,9 +3157,6 @@ send_message_to_server_log(ErrorData *edata) =20 initStringInfo(&buf); =20 - saved_timeval_set =3D false; - formatted_log_time[0] =3D '\0'; - log_line_prefix(&buf, edata); appendStringInfo(&buf, "%s: ", _(error_severity(edata->elevel))); =20 --=20 2.43.0 --VT+EhD3VPy0xZONq-- --Zk0Pbwwpkf3gGOns Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmX3zVgACgkQnvQgOdby QH3TIxAApbwWpKBirTyW2/JPsPmr7dr5M9hGHsCuHvpctO8SIa6c3S6IibBXzMB0 5IVqyQsmnDDqjMEmoNNhxkD+4IVWdqenJPMmX2KQD7D/ZsnIzF4wAsJx03yuIuvS gIHjzNnsggYhFBiubIvyGavcQn/CH+EiwP/zEoaR7ELYwWm4+6tiV9AofSv+TLfI YMg/COAdt4iwELyP+T3HGbFzQgyG8ncdTc6gJ6tDFsamlW1rcEOJl56OboTrIdn4 y3Mg6UThjU7JkVQJe7Ng/elmA1Qr2XJoQ1rwoFGpEjeEJqCRAoEF8qtDlgHG3HLq 5w76xEZ4R1vmkEHLynE2x1TyfX/TyhGXuS+vglSU6OU7tYeXRMwfyPmlcFaxhIiY q4iV2hSEniafu4HThbTjAkDAVWbOGr+W1k7nMA9y32iRtzgy9l6afZrBNViAe4yB 5X+zvIrR6rlXO9EaWSUQfBJUZxhd77X2bsfYiOBaibvn4HI/0dbD+V5l0FL+Nnqf 8oHVzJuLFY7GSIuDjURXBQMx7kFGrwfXLDEXxQ7nNZsdT/7ljdRhsj1kwhzVsmDn Dk9hPJrELnpQNEr/DhnugHv5h3Xox/gOrO1+N6+HWouSvz7pMRHQ57vSwK/u5rj6 1jxCfLF0b85SOBK4mB/2M8mpIlhevrVG7pefazYRFYoK6OcZorw= =0KyX -----END PGP SIGNATURE----- --Zk0Pbwwpkf3gGOns--