From 3847ab73a79f11038459b68edfced6a9acf5ecb6 Mon Sep 17 00:00:00 2001 From: Konstantin Knizhnik Date: Thu, 24 Jul 2025 17:12:25 +0300 Subject: [PATCH] Cleanup perf_counters on backend exit --- pgxn/neon/communicator.c | 11 ++++++++--- pgxn/neon/neon_perf_counters.c | 7 +++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/pgxn/neon/communicator.c b/pgxn/neon/communicator.c index 03ab065d59..f76c611c8e 100644 --- a/pgxn/neon/communicator.c +++ b/pgxn/neon/communicator.c @@ -2551,12 +2551,17 @@ communicator_read_slru_segment(SlruKind kind, int64 segno, neon_request_lsns *re void communicator_reconfigure_timeout_if_needed(void) { - bool needs_set = (MyPState->ring_receive != MyPState->ring_unused || - (MIN_BACKEND_PREFETCH_LSN != InvalidXLogRecPtr && - MIN_BACKEND_PREFETCH_LSN != GetXLogReplayRecPtr(NULL))) && + bool needs_set = MyPState->ring_receive != MyPState->ring_unused && !AmPrewarmWorker && /* do not pump prefetch state in prewarm worker */ readahead_getpage_pull_timeout_ms > 0; + if (!needs_set && MIN_BACKEND_PREFETCH_LSN != InvalidXLogRecPtr) + { + if (last_replay_lsn == InvalidXLogRecPtr) + MIN_BACKEND_PREFETCH_LSN = InvalidXLogRecPtr; + else + needs_set = true; /* Can not reset MIN_BACKEND_PREFETCH_LSN now, have to do it later */ + } if (needs_set != timeout_set) { /* diff --git a/pgxn/neon/neon_perf_counters.c b/pgxn/neon/neon_perf_counters.c index dd576e4e73..2a7af15560 100644 --- a/pgxn/neon/neon_perf_counters.c +++ b/pgxn/neon/neon_perf_counters.c @@ -13,6 +13,7 @@ #include "funcapi.h" #include "miscadmin.h" +#include "storage/ipc.h" #include "storage/proc.h" #include "storage/shmem.h" #include "utils/builtins.h" @@ -42,6 +43,11 @@ NeonPerfCountersShmemRequest(void) } +static void +NeonPerfCountersReset(int code, Datum arg) +{ + memset(MyNeonCounters, 0, sizeof(*MyNeonCounters)); +} void NeonPerfCountersShmemInit(void) @@ -58,6 +64,7 @@ NeonPerfCountersShmemInit(void) { /* shared memory is initialized to zeros, so nothing to do here */ } + on_shmem_exit(NeonPerfCountersReset, 0); } static inline void