From 7031d6d59468de68eac38f4b9be603e87af74608 Mon Sep 17 00:00:00 2001 From: Andrey Borodin Date: Tue, 21 Jul 2026 15:59:14 +0500 Subject: [PATCH 2/2] Don't count foreign IO wait time as read time in pg_stat_io When waiting for IO started by another backend, WaitReadBuffers() recorded the wait time as IOOP_READ time with a zero count. The read itself is counted by the initiating backend, and the waiting backend counts the buffer as a hit, so this left read time without any reads in the waiting backend's statistics, tripping the pgstat_bktype_io_stats_valid() assertion on the next stats flush. Skip the timing for foreign IO waits. Author: Andrey Rachitskiy Reported-by: Justin Pryzby Discussion: https://postgr.es/m/ak5lccE4qiQpOBHn@pryzbyj2023 --- src/backend/storage/buffer/bufmgr.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index d6c0cc1f6d4..8f76e831d47 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1831,9 +1831,14 @@ WaitReadBuffers(ReadBuffersOperation *operation) /* * The IO operation itself was already counted earlier, in * AsyncReadBuffers(), this just accounts for the wait time. + * For foreign IO the read is counted by the initiating + * backend, and this backend counts the buffer as a hit, so + * recording read time here would leave read time without + * any reads in this backend's statistics. */ - pgstat_count_io_op_time(io_object, io_context, IOOP_READ, - io_start, 0, 0); + if (!operation->foreign_io) + pgstat_count_io_op_time(io_object, io_context, IOOP_READ, + io_start, 0, 0); } else { -- 2.50.1 (Apple Git-155)