From 44c8dc28017800892adbdc7f248f6bed5aa90bef Mon Sep 17 00:00:00 2001 From: Amit Kapila Date: Mon, 20 Apr 2026 10:23:22 +0530 Subject: [PATCH] Flush statistics during idle periods in parallel apply worker. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Parallel apply workers previously failed to report statistics while waiting for new work in the main loop. This resulted in the stats from the most recent transaction remaining unbuffered, leading to arbitrary reporting delays—particularly when streamed transactions were infrequent. This commit ensures that statistics are explicitly flushed when the worker is idle, providing timely visibility into accumulated worker activity. Author: Zhijie Hou Reviewed-by: Chao Li Reviewed-by: Amit Kapila Backpatch-through: 16, where it was introduced Discussion: https://postgr.es/m/TYRPR01MB1419579F217CC4332B615589594202@TYRPR01MB14195.jpnprd01.prod.outlook.com --- src/backend/replication/logical/applyparallelworker.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/backend/replication/logical/applyparallelworker.c b/src/backend/replication/logical/applyparallelworker.c index 2d140e262c1..b70adf342a5 100644 --- a/src/backend/replication/logical/applyparallelworker.c +++ b/src/backend/replication/logical/applyparallelworker.c @@ -811,6 +811,15 @@ LogicalParallelApplyLoop(shm_mq_handle *mqh) if (rc & WL_LATCH_SET) ResetLatch(MyLatch); + + /* + * Force stats reporting to avoid long delays. There can be long + * idle gaps before the leader assigns the next transaction, and + * the only opportunity to report stats during such gaps is + * here. + */ + if ((rc & WL_TIMEOUT) && !IsTransactionState()) + pgstat_report_stat(true); } } else