0001-Lift-shutdown-assertion-in-pgstats-for-WAL-senders.patch

text/plain

Filename: 0001-Lift-shutdown-assertion-in-pgstats-for-WAL-senders.patch
Type: text/plain
Part: 0
Message: Re: BUG #18158: Assert in pgstat_report_stat() fails when a backend shutting down with stats pending

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch 0001
Subject: Lift shutdown assertion in pgstats for WAL senders
File+
src/backend/utils/activity/pgstat.c 13 2
From 62bae7f32ea095e0130af762bdf6ef39a7c5d388 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Fri, 5 Jun 2026 10:42:47 +0900
Subject: [PATCH] Lift shutdown assertion in pgstats for WAL senders

Backpatch-through: 15-17
---
 src/backend/utils/activity/pgstat.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/src/backend/utils/activity/pgstat.c b/src/backend/utils/activity/pgstat.c
index 33025cf4aad3..b7cf6fa88ff4 100644
--- a/src/backend/utils/activity/pgstat.c
+++ b/src/backend/utils/activity/pgstat.c
@@ -97,6 +97,7 @@
 #include "lib/dshash.h"
 #include "pgstat.h"
 #include "port/atomics.h"
+#include "replication/walsender.h"
 #include "storage/fd.h"
 #include "storage/ipc.h"
 #include "storage/lwlock.h"
@@ -516,8 +517,12 @@ pgstat_shutdown_hook(int code, Datum arg)
 
 	pgstat_report_stat(true);
 
-	/* there shouldn't be any pending changes left */
-	Assert(dlist_is_empty(&pgStatPending));
+	/*
+	 * There shouldn't be any pending changes left, unless this is a WAL
+	 * sender that would shut down after the checkpointer has flushed the
+	 * stats.
+	 */
+	Assert(dlist_is_empty(&pgStatPending) || am_walsender);
 	dlist_init(&pgStatPending);
 
 	pgstat_detach_shmem();
@@ -608,7 +613,13 @@ pgstat_report_stat(bool force)
 	 * assert that before the checks above, as there is an unconditional
 	 * pgstat_report_stat() call in pgstat_shutdown_hook() - which at least
 	 * the process that ran pgstat_before_server_shutdown() will still call.
+	 *
+	 * WAL senders would be shut down after the checkpointer and may still
+	 * have stats.  Skip them.
 	 */
+	if (pgStatLocal.shmem->is_shutdown && am_walsender)
+		return 0;
+
 	Assert(!pgStatLocal.shmem->is_shutdown);
 
 	if (force)
-- 
2.54.0