From eef6b63f67f5e47806e40e591632a2c1737b3703 Mon Sep 17 00:00:00 2001 From: Dmitry Fomin Date: Sat, 28 Nov 2026 15:15:14 +0100 Subject: [PATCH 2/3] v18 0002: time wait events via the existing report API Turn on wait timing by folding it into the two existing reporting inlines, rather than renaming call sites to a parallel _timed API. pgstat_report_wait_start() and pgstat_report_wait_end() gain a single branch each: once this process has enabled timing (wait_event_report_state.buf != NULL), they call the cold, out-of-line slow path added in 0001. The test is hinted unlikely() so the disabled case is the fall-through: with GCC 8.5 at -O2 each inline compiles to one compare of the buffer pointer against zero and one conditional branch to an out-of-line .cold block that holds the call. In pgstat_report_wait_end() the reported event is cleared before the branch, so *my_wait_event_info is already zero while the cold function runs and the event pointer does not have to stay live across the call. Because the timing lives in the ordinary API, every wait is timed the same way, in core and in extensions, with no second entry point to keep in sync and no call site to convert. The compiled hot path in LWLockAcquire, FileReadV, WaitEventSetWait and friends is identical to what a renamed _timed inline would produce, so there is no extra machine code to review beyond this one branch. --- src/include/utils/wait_event.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/include/utils/wait_event.h b/src/include/utils/wait_event.h index b579e99e910..fb4de29ad63 100644 --- a/src/include/utils/wait_event.h +++ b/src/include/utils/wait_event.h @@ -207,6 +207,10 @@ pgstat_report_wait_start(uint32 wait_event_info) * four-bytes, updates are atomic. */ *(volatile uint32 *) my_wait_event_info = wait_event_info; + + /* if timing is enabled here, record the start; see the slow fns above */ + if (unlikely(wait_event_report_state.buf != NULL)) + pgstat_wait_timing_begin_slow(wait_event_info); } /* ---------- @@ -220,6 +224,14 @@ pgstat_report_wait_end(void) { /* see pgstat_report_wait_start() */ *(volatile uint32 *) my_wait_event_info = 0; + + /* + * Clear the reported event first (above), then record the finished wait. + * Storing first keeps *my_wait_event_info zero while the cold function + * runs and avoids keeping the event pointer live across the call. + */ + if (unlikely(wait_event_report_state.buf != NULL)) + pgstat_wait_timing_end_slow(); } -- 2.49.0