From 5f38ecb859a6399f78d009029307436b1019df76 Mon Sep 17 00:00:00 2001 From: Nisha Moond Date: Tue, 28 Jan 2025 10:27:58 +0530 Subject: [PATCH v1 1/2] Ensure same inactive_since time for all inactive slots Update the logic to make sure all slots get the same 'inactive_since' time in following cases: 1) When updating the synced slots on standby in update_synced_slots_inactive_since() and 2) When restoring slots from disk in RestoreSlotFromDisk(). --- src/backend/replication/logical/slotsync.c | 9 ++++----- src/backend/replication/slot.c | 6 +++++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/backend/replication/logical/slotsync.c b/src/backend/replication/logical/slotsync.c index f6945af1d4..db52795b73 100644 --- a/src/backend/replication/logical/slotsync.c +++ b/src/backend/replication/logical/slotsync.c @@ -1508,7 +1508,7 @@ ReplSlotSyncWorkerMain(char *startup_data, size_t startup_data_len) static void update_synced_slots_inactive_since(void) { - TimestampTz now = 0; + TimestampTz now; /* * We need to update inactive_since only when we are promoting standby to @@ -1523,6 +1523,9 @@ update_synced_slots_inactive_since(void) /* The slot sync worker or SQL function mustn't be running by now */ Assert((SlotSyncCtx->pid == InvalidPid) && !SlotSyncCtx->syncing); + /* Use same inactive_since time for all slots */ + now = GetCurrentTimestamp(); + LWLockAcquire(ReplicationSlotControlLock, LW_SHARED); for (int i = 0; i < max_replication_slots; i++) @@ -1537,10 +1540,6 @@ update_synced_slots_inactive_since(void) /* The slot must not be acquired by any process */ Assert(s->active_pid == 0); - /* Use the same inactive_since time for all the slots. */ - if (now == 0) - now = GetCurrentTimestamp(); - SpinLockAcquire(&s->mutex); s->inactive_since = now; SpinLockRelease(&s->mutex); diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c index b30e0473e1..c567c1fef3 100644 --- a/src/backend/replication/slot.c +++ b/src/backend/replication/slot.c @@ -2208,6 +2208,7 @@ RestoreSlotFromDisk(const char *name) bool restored = false; int readBytes; pg_crc32c checksum; + TimestampTz now; /* no need to lock here, no concurrent access allowed yet */ @@ -2368,6 +2369,9 @@ RestoreSlotFromDisk(const char *name) NameStr(cp.slotdata.name)), errhint("Change \"wal_level\" to be \"replica\" or higher."))); + /* Use same inactive_since time for all slots */ + now = GetCurrentTimestamp(); + /* nothing can be active yet, don't lock anything */ for (i = 0; i < max_replication_slots; i++) { @@ -2400,7 +2404,7 @@ RestoreSlotFromDisk(const char *name) * slot from the disk into memory. Whoever acquires the slot i.e. * makes the slot active will reset it. */ - slot->inactive_since = GetCurrentTimestamp(); + slot->inactive_since = now; restored = true; break; -- 2.34.1