Rewrite min-request-lsn reset mechanism on backend exit

This commit is contained in:
Konstantin Knizhnik
2025-07-25 19:17:06 +03:00
parent d56a72afec
commit 252876515c
3 changed files with 20 additions and 36 deletions

View File

@@ -547,7 +547,7 @@ communicator_prefetch_pump_state(void)
PrefetchRequest* slot = GetPrfSlot(ring_index);
min_backend_prefetch_lsn = Min(slot->request_lsns.request_lsn, min_backend_prefetch_lsn);
}
MIN_BACKEND_PREFETCH_LSN = min_backend_prefetch_lsn;
MIN_BACKEND_REQUEST_LSN = min_backend_prefetch_lsn;
}
communicator_reconfigure_timeout_if_needed();
}
@@ -1919,6 +1919,13 @@ nm_to_string(NeonMessage *msg)
return s.data;
}
static void
reset_min_request_lsn(int code, Datum arg)
{
if (MyProc != NULL)
MIN_BACKEND_REQUEST_LSN = InvalidXLogRecPtr;
}
/*
* communicator_init() -- Initialize per-backend private state
*/
@@ -1930,6 +1937,8 @@ communicator_init(void)
if (MyPState != NULL)
return;
on_shmem_exit(reset_min_request_lsn, 0);
/*
* Sanity check that theperf counters array is sized correctly. We got
* this wrong once, and the formula for max number of backends and aux
@@ -2555,12 +2564,12 @@ communicator_reconfigure_timeout_if_needed(void)
!AmPrewarmWorker && /* do not pump prefetch state in prewarm worker */
readahead_getpage_pull_timeout_ms > 0;
if (!needs_set && MIN_BACKEND_PREFETCH_LSN != InvalidXLogRecPtr)
if (!needs_set && MIN_BACKEND_REQUEST_LSN != InvalidXLogRecPtr)
{
if (last_replay_lsn == InvalidXLogRecPtr)
MIN_BACKEND_PREFETCH_LSN = InvalidXLogRecPtr;
MIN_BACKEND_REQUEST_LSN = InvalidXLogRecPtr;
else
needs_set = true; /* Can not reset MIN_BACKEND_PREFETCH_LSN now, have to do it later */
needs_set = true; /* Can not reset MIN_BACKEND_REQUEST_LSN now, have to do it later */
}
if (needs_set != timeout_set)
{
@@ -2571,7 +2580,7 @@ communicator_reconfigure_timeout_if_needed(void)
*/
if (AmBackgroundWriterProcess() || AmCheckpointerProcess())
{
MIN_BACKEND_PREFETCH_LSN = InvalidXLogRecPtr;
MIN_BACKEND_REQUEST_LSN = InvalidXLogRecPtr;
if (timeout_set)
{
disable_timeout(PS_TIMEOUT_ID, false);
@@ -2664,9 +2673,9 @@ neon_communicator_min_inflight_request_lsn(PG_FUNCTION_ARGS)
size_t n_procs = ProcGlobal->allProcCount;
for (size_t i = 0; i < n_procs; i++)
{
if (neon_per_backend_counters_shared[i].min_prefetch_lsn != InvalidXLogRecPtr)
if (neon_per_backend_counters_shared[i].min_request_lsn != InvalidXLogRecPtr)
{
min_lsn = Min(min_lsn, neon_per_backend_counters_shared[i].min_prefetch_lsn);
min_lsn = Min(min_lsn, neon_per_backend_counters_shared[i].min_request_lsn);
}
}
PG_RETURN_INT64(min_lsn);

View File

@@ -43,31 +43,6 @@ NeonPerfCountersShmemRequest(void)
}
static int my_proc_num = -1;
static void
my_perf_counters_reset(int code, Datum arg)
{
Assert(my_proc_num >= 0);
memset(&neon_per_backend_counters_shared[my_proc_num], 0, sizeof(neon_per_backend_counters));
}
neon_per_backend_counters*
get_my_perf_counters(void)
{
if (my_proc_num < 0)
{
#if PG_MAJORVERSION_NUM < 17
my_proc_num = MyProc->pgprocno;
#else
my_proc_num = MyProcNumber;
#endif
on_shmem_exit(my_perf_counters_reset, 0);
}
return &neon_per_backend_counters_shared[my_proc_num];
}
void
NeonPerfCountersShmemInit(void)
{

View File

@@ -156,9 +156,9 @@ typedef struct
QTHistogramData query_time_hist;
/*
* Minimal LSN of infligth prefetch requests
* Minimal LSN of in-fligth request requests
*/
XLogRecPtr min_prefetch_lsn;
XLogRecPtr min_request_lsn;
} neon_per_backend_counters;
/* Pointer to the shared memory array of neon_per_backend_counters structs */
@@ -175,10 +175,10 @@ extern neon_per_backend_counters *neon_per_backend_counters_shared;
#define MyNeonCounters (&neon_per_backend_counters_shared[MyProcNumber])
/*
* Backend-local minimal in-flight prefetch LSN.
* Backend-local minimal in-flight request LSN.
* We store it in neon_per_backend_counters_shared and not in separate array to minimize false cache sharing
*/
#define MIN_BACKEND_PREFETCH_LSN MyNeonCounters->min_prefetch_lsn
#define MIN_BACKEND_REQUEST_LSN MyNeonCounters->min_request_lsn
extern void inc_getpage_wait(uint64 latency);
extern void inc_page_cache_read_wait(uint64 latency);