From 821352a11872392547c0ac6790878ef02d0284b4 Mon Sep 17 00:00:00 2001
From: Seongjun Shin <shinsj4653@gmail.com>
Date: Fri, 29 May 2026 14:52:11 +0900
Subject: [PATCH v2 2/2] Report StderrWrite wait event around WriteConsoleW()
 on Windows

On Windows, write_console() emits log messages to the console with
WriteConsoleW() rather than write(2).  Like the write(2) path, this
call can block on a slow console, leaving wait_event IS NULL in
pg_stat_activity.

Wrap WriteConsoleW() with the StderrWrite wait event introduced in
the previous patch, so the Windows console path is instrumented
consistently with the stderr write(2) path.
---
 src/backend/utils/error/elog.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index d4c41fa1263..9ab75b99f5c 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -2635,11 +2635,14 @@ write_console(const char *line, int len)
 			DWORD		written;
 
 			stdHandle = GetStdHandle(STD_ERROR_HANDLE);
+			pgstat_report_wait_start(WAIT_EVENT_STDERR_WRITE);
 			if (WriteConsoleW(stdHandle, utf16, utf16len, &written, NULL))
 			{
+				pgstat_report_wait_end();
 				pfree(utf16);
 				return;
 			}
+			pgstat_report_wait_end();
 
 			/*
 			 * In case WriteConsoleW() failed, fall back to writing the
-- 
2.50.1 (Apple Git-155)

