From 631007052f818942067e71d6be3aa06f2746e0da Mon Sep 17 00:00:00 2001
From: Seongjun Shin <shinsj4653@gmail.com>
Date: Fri, 13 Nov 2026 00:04:20 +0900
Subject: [PATCH v10 3/3] Add wait events for Windows-specific logging output
 paths

On Windows, write_console() uses WriteConsoleW() when writing to a
console, and write_eventlog() uses ReportEventW()/ReportEventA() to
write to the event log.  Both can block like the write(2) paths
handled in the previous commit.

WriteConsoleW() reuses StderrWrite.  A new event is added for the
event log:

  IO / EventlogWrite - ReportEventW()/ReportEventA().

This is kept separate from the previous commit as it only affects
Windows.

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: Henson Choi <assam258@gmail.com>
Reviewed-by: Nikolay Samokhvalov <nik@postgres.ai>
Tested-by: Nikolay Samokhvalov <nik@postgres.ai>
Discussion: https://postgr.es/m/CACdN0M78U+GvpqA7oey-GA7fFSYM636aDp6H9FVvCztv9zXxSA@mail.gmail.com
---
 src/backend/utils/activity/wait_event_names.txt |  1 +
 src/backend/utils/error/elog.c                  | 11 ++++++++++-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 4c64bceb47b..adb9a6cbc55 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -229,6 +229,7 @@ DATA_FILE_TRUNCATE	"Waiting for a relation data file to be truncated."
 DATA_FILE_WRITE	"Waiting for a write to a relation data file."
 DSM_ALLOCATE	"Waiting for a dynamic shared memory segment to be allocated."
 DSM_FILL_ZERO_WRITE	"Waiting to fill a dynamic shared memory backing file with zeroes."
+EVENTLOG_WRITE	"Waiting for a write to the Windows event log."
 LOCK_FILE_ADDTODATADIR_READ	"Waiting for a read while adding a line to the data directory lock file."
 LOCK_FILE_ADDTODATADIR_SYNC	"Waiting for data to reach durable storage while adding a line to the data directory lock file."
 LOCK_FILE_ADDTODATADIR_WRITE	"Waiting for a write while adding a line to the data directory lock file."
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 6c860403336..f9c16f845b0 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -2965,6 +2965,7 @@ write_eventlog(int level, const char *line, int len)
 {
 	int			eventlevel = EVENTLOG_ERROR_TYPE;
 	static HANDLE evtHandle = INVALID_HANDLE_VALUE;
+	uint32		old_wait_event;
 
 	if (evtHandle == INVALID_HANDLE_VALUE)
 	{
@@ -3027,6 +3028,7 @@ write_eventlog(int level, const char *line, int len)
 		{
 			const WCHAR *utf16_const = utf16;
 
+			old_wait_event = pgstat_report_wait_swap(WAIT_EVENT_EVENTLOG_WRITE);
 			ReportEventW(evtHandle,
 						 eventlevel,
 						 0,
@@ -3036,12 +3038,14 @@ write_eventlog(int level, const char *line, int len)
 						 0,
 						 &utf16_const,
 						 NULL);
+			pgstat_report_wait_swap(old_wait_event);
 			/* XXX Try ReportEventA() when ReportEventW() fails? */
 
 			pfree(utf16);
 			return;
 		}
 	}
+	old_wait_event = pgstat_report_wait_swap(WAIT_EVENT_EVENTLOG_WRITE);
 	ReportEventA(evtHandle,
 				 eventlevel,
 				 0,
@@ -3051,6 +3055,7 @@ write_eventlog(int level, const char *line, int len)
 				 0,
 				 &line,
 				 NULL);
+	pgstat_report_wait_swap(old_wait_event);
 }
 #endif							/* WIN32 */
 
@@ -3091,9 +3096,13 @@ write_console(const char *line, int len)
 		{
 			HANDLE		stdHandle;
 			DWORD		written;
+			BOOL		ok;
 
 			stdHandle = GetStdHandle(STD_ERROR_HANDLE);
-			if (WriteConsoleW(stdHandle, utf16, utf16len, &written, NULL))
+			old_wait_event = pgstat_report_wait_swap(WAIT_EVENT_STDERR_WRITE);
+			ok = WriteConsoleW(stdHandle, utf16, utf16len, &written, NULL);
+			pgstat_report_wait_swap(old_wait_event);
+			if (ok)
 			{
 				pfree(utf16);
 				return;
-- 
2.50.1 (Apple Git-155)

