On 2026-May-26, Michael Paquier wrote:

> This issue is different, it is a Postgres logic bug, so adding an
> exception like the one you are suggesting is just a shortcut hiding
> the real issue: the log file is not ready yet, but the syslogger is
> invoked at a point when it thinks the log file exists.

I think we can solve this easily by flipping a new Boolean value at the
same point were MyBackendType was previously set.  The attached POC
fixes the scenario you described; can you confirm?  It needs some
additional comments, of course.

(There is one more place in elog.c where we check that MyBackendType is
_not_ B_LOGGER, but I think that one is correct as-is; and I'm wondering
if that would behave correctly before 0c8e082fba8d.)

-- 
Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
“Cuando no hay humildad las personas se degradan” (A. Christie)
>From 3fee63b168279acb6556525b879559c03427ac14 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=81lvaro=20Herrera?= <[email protected]>
Date: Tue, 26 May 2026 12:45:01 +0200
Subject: [PATCH] POC: syslogger: add Boolean state indicating readiness

This replaces a comparison of MyBackendType, which was made obsolete by
commit 0c8e082fba8d.
---
 src/backend/postmaster/syslogger.c | 5 +++++
 src/backend/utils/error/elog.c     | 6 +++---
 src/include/postmaster/syslogger.h | 1 +
 3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c
index acfe0a01715..d736f6a4fab 100644
--- a/src/backend/postmaster/syslogger.c
+++ b/src/backend/postmaster/syslogger.c
@@ -76,6 +76,9 @@ char	   *Log_filename = NULL;
 bool		Log_truncate_on_rotation = false;
 int			Log_file_mode = S_IRUSR | S_IWUSR;
 
+/* for elog.c */
+bool		syslogger_setup_done = false;
+
 /*
  * Private state
  */
@@ -195,6 +198,8 @@ SysLoggerMain(const void *startup_data, size_t startup_data_len)
 	Assert(startup_data_len == 0);
 #endif
 
+	syslogger_setup_done = true;
+
 	/*
 	 * Now that we're done reading the startup data, release postmaster's
 	 * working memory context.
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 50c53b571a0..a6936a0c664 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -3831,7 +3831,7 @@ send_message_to_server_log(ErrorData *edata)
 		 * pipe).  If this is not possible, fallback to an entry written to
 		 * stderr.
 		 */
-		if (redirection_done || MyBackendType == B_LOGGER)
+		if (redirection_done || syslogger_setup_done)
 			write_csvlog(edata);
 		else
 			fallback_to_stderr = true;
@@ -3845,7 +3845,7 @@ send_message_to_server_log(ErrorData *edata)
 		 * pipe).  If this is not possible, fallback to an entry written to
 		 * stderr.
 		 */
-		if (redirection_done || MyBackendType == B_LOGGER)
+		if (redirection_done || syslogger_setup_done)
 		{
 			write_jsonlog(edata);
 		}
@@ -3885,7 +3885,7 @@ send_message_to_server_log(ErrorData *edata)
 	}
 
 	/* If in the syslogger process, try to write messages direct to file */
-	if (MyBackendType == B_LOGGER)
+	if (syslogger_setup_done)
 		write_syslogger_file(buf.data, buf.len, LOG_DESTINATION_STDERR);
 
 	/* No more need of the message formatted for stderr */
diff --git a/src/include/postmaster/syslogger.h b/src/include/postmaster/syslogger.h
index 30c4b2daf3f..44409fc2542 100644
--- a/src/include/postmaster/syslogger.h
+++ b/src/include/postmaster/syslogger.h
@@ -85,6 +85,7 @@ extern PGDLLIMPORT int syslogPipe[2];
 extern PGDLLIMPORT HANDLE syslogPipe[2];
 #endif
 
+extern bool syslogger_setup_done;
 
 extern int	SysLogger_Start(int child_slot);
 
-- 
2.47.3

Reply via email to