From 415cdd93c31c8eaecc8b80a0866582e2ff425691 Mon Sep 17 00:00:00 2001
From: Seongjun Shin <shinsj4653@gmail.com>
Date: Sat, 14 Nov 2026 18:45:09 +0900
Subject: [PATCH v11] Add wait events for server logging destination writes

Writing server log output can block: write(2) to the syslogger pipe
once the pipe is full, write(2) to stderr when the output device is
slow, and syslog(3) or openlog(3) when the system logger is slow.  In
all these cases pg_stat_activity showed wait_event as NULL.

This adds three wait events for these calls:

  IO / SysloggerWrite - write(2) to the syslogger pipe.
  IO / StderrWrite    - write(2) to stderr.
  IO / SyslogWrite    - syslog(3) and openlog(3).

Author: Seongjun Shin <shinsj4653@gmail.com>
Reviewed-by: Kirk Wolak <wolakk@gmail.com>
Reviewed-by: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Andrey Borodin <x4mmm@yandex-team.ru>
Reviewed-by: Henson Choi <assam258@gmail.com>
Reviewed-by: Nikolay Samokhvalov <nik@postgres.ai>
Reviewed-by: Jihyun Bahn <rring0727@gmail.com>
Reviewed-by: Jakub Wartak <jakub.wartak@enterprisedb.com>
Tested-by: Nikolay Samokhvalov <nik@postgres.ai>
Tested-by: Jihyun Bahn <rring0727@gmail.com>
Tested-by: Jakub Wartak <jakub.wartak@enterprisedb.com>
Discussion: https://postgr.es/m/CACdN0M78U+GvpqA7oey-GA7fFSYM636aDp6H9FVvCztv9zXxSA@mail.gmail.com
---
 src/backend/utils/activity/wait_event_names.txt |  3 +++
 src/backend/utils/error/elog.c                  | 13 +++++++++++++
 2 files changed, 16 insertions(+)

diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 112873dc46b..f6ade4300c7 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -260,6 +260,9 @@ SLRU_WRITE	"Waiting for a write of an SLRU page."
 SNAPBUILD_READ	"Waiting for a read of a serialized historical catalog snapshot."
 SNAPBUILD_SYNC	"Waiting for a serialized historical catalog snapshot to reach durable storage."
 SNAPBUILD_WRITE	"Waiting for a write of a serialized historical catalog snapshot."
+STDERR_WRITE	"Waiting for a write to the server's standard error stream."
+SYSLOGGER_WRITE	"Waiting for a write to the syslogger pipe."
+SYSLOG_WRITE	"Waiting for a write to the system logger (syslog), including connection setup."
 TIMELINE_HISTORY_FILE_SYNC	"Waiting for a timeline history file received via streaming replication to reach durable storage."
 TIMELINE_HISTORY_FILE_WRITE	"Waiting for a write of a timeline history file received via streaming replication."
 TIMELINE_HISTORY_READ	"Waiting for a read of a timeline history file."
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index e215ed053ff..d8e440a83f8 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -89,6 +89,7 @@
 #include "utils/pg_locale.h"
 #include "utils/ps_status.h"
 #include "utils/varlena.h"
+#include "utils/wait_event.h"
 
 
 /* In this module, access gettext() via err_gettext() */
@@ -2837,9 +2838,11 @@ write_syslog(int level, const char *line)
 	/* Open syslog connection if not done yet */
 	if (!openlog_done)
 	{
+		pgstat_report_wait_start(WAIT_EVENT_SYSLOG_WRITE);
 		openlog(syslog_ident ? syslog_ident : "postgres",
 				LOG_PID | LOG_NDELAY | LOG_NOWAIT,
 				syslog_facility);
+		pgstat_report_wait_end();
 		openlog_done = true;
 	}
 
@@ -2912,10 +2915,12 @@ write_syslog(int level, const char *line)
 
 			chunk_nr++;
 
+			pgstat_report_wait_start(WAIT_EVENT_SYSLOG_WRITE);
 			if (syslog_sequence_numbers)
 				syslog(level, "[%lu-%d] %s", seq, chunk_nr, buf);
 			else
 				syslog(level, "[%d] %s", chunk_nr, buf);
+			pgstat_report_wait_end();
 
 			line += buflen;
 			len -= buflen;
@@ -2924,10 +2929,12 @@ write_syslog(int level, const char *line)
 	else
 	{
 		/* message short enough */
+		pgstat_report_wait_start(WAIT_EVENT_SYSLOG_WRITE);
 		if (syslog_sequence_numbers)
 			syslog(level, "[%lu] %s", seq, line);
 		else
 			syslog(level, "%s", line);
+		pgstat_report_wait_end();
 	}
 }
 #endif							/* HAVE_SYSLOG */
@@ -3112,7 +3119,9 @@ write_console(const char *line, int len)
 	 * We ignore any error from write() here.  We have no useful way to report
 	 * it ... certainly whining on stderr isn't likely to be productive.
 	 */
+	pgstat_report_wait_start(WAIT_EVENT_STDERR_WRITE);
 	rc = write(fileno(stderr), line, len);
+	pgstat_report_wait_end();
 	(void) rc;
 }
 
@@ -3959,7 +3968,9 @@ write_pipe_chunks(char *data, int len, int dest)
 		/* no need to set PIPE_PROTO_IS_LAST yet */
 		p.proto.len = PIPE_MAX_PAYLOAD;
 		memcpy(p.proto.data, data, PIPE_MAX_PAYLOAD);
+		pgstat_report_wait_start(WAIT_EVENT_SYSLOGGER_WRITE);
 		rc = write(fd, &p, PIPE_HEADER_SIZE + PIPE_MAX_PAYLOAD);
+		pgstat_report_wait_end();
 		(void) rc;
 		data += PIPE_MAX_PAYLOAD;
 		len -= PIPE_MAX_PAYLOAD;
@@ -3969,7 +3980,9 @@ write_pipe_chunks(char *data, int len, int dest)
 	p.proto.flags |= PIPE_PROTO_IS_LAST;
 	p.proto.len = len;
 	memcpy(p.proto.data, data, len);
+	pgstat_report_wait_start(WAIT_EVENT_SYSLOGGER_WRITE);
 	rc = write(fd, &p, PIPE_HEADER_SIZE + len);
+	pgstat_report_wait_end();
 	(void) rc;
 }
 
-- 
2.50.1 (Apple Git-155)

