From 41a2844d843fda83d7e3e69506c08ef1943817c7 Mon Sep 17 00:00:00 2001 From: Jan Nidzwetzki Date: Thu, 11 Jun 2026 11:38:05 +0200 Subject: [PATCH 1/2] Refactor: extract EnableHotStandbyConnections() helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move the block in CheckRecoveryConsistency() that marks the standby active and signals the postmaster to begin accepting connections into a new function, EnableHotStandbyConnections(). This is a preparatory, behavior-preserving change. The previous single condition (standbyState == STANDBY_SNAPSHOT_READY && !LocalHotStandbyActive && reachedConsistency && IsUnderPostmaster) is split between the caller, which still gates on STANDBY_SNAPSHOT_READY, and the new function, which checks the remaining conditions as early returns. No functional change. A following commit adds a second caller of this helper. Co-authored-by: Fabrízio de Royes Mello --- src/backend/access/transam/xlogrecovery.c | 46 ++++++++++++++++------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c index 73b78a83fa7..254603d7ef6 100644 --- a/src/backend/access/transam/xlogrecovery.c +++ b/src/backend/access/transam/xlogrecovery.c @@ -364,6 +364,7 @@ static char *getRecoveryStopReason(void); static void recoveryPausesHere(bool endOfRecovery); static bool recoveryApplyDelay(XLogReaderState *record); static void ConfirmRecoveryPaused(void); +static void EnableHotStandbyConnections(void); static XLogRecord *ReadRecord(XLogPrefetcher *xlogprefetcher, int emode, bool fetching_ckpt, @@ -2142,6 +2143,36 @@ CheckTablespaceDirectory(void) } } +/* + * Enable hot standby connections: mark the standby active and signal the + * postmaster that it may start accepting read-only connections. + * + * Must only be called in hot standby mode (standbyState is at least + * STANDBY_SNAPSHOT_PENDING). Does nothing if connections were already + * enabled, if we have not yet reached consistency (no valid snapshot for + * queries), or if we are not running under the postmaster. + */ +static void +EnableHotStandbyConnections(void) +{ + Assert(InHotStandby); + + if (LocalHotStandbyActive) + return; + if (!reachedConsistency) + return; + if (!IsUnderPostmaster) + return; + + SpinLockAcquire(&XLogRecoveryCtl->info_lck); + XLogRecoveryCtl->SharedHotStandbyActive = true; + SpinLockRelease(&XLogRecoveryCtl->info_lck); + + LocalHotStandbyActive = true; + + SendPostmasterSignal(PMSIGNAL_BEGIN_HOT_STANDBY); +} + /* * Checks if recovery has reached a consistent state. When consistency is * reached and we have a valid starting standby snapshot, tell postmaster @@ -2230,19 +2261,8 @@ CheckRecoveryConsistency(void) * run? If so, we can tell postmaster that the database is consistent now, * enabling connections. */ - if (standbyState == STANDBY_SNAPSHOT_READY && - !LocalHotStandbyActive && - reachedConsistency && - IsUnderPostmaster) - { - SpinLockAcquire(&XLogRecoveryCtl->info_lck); - XLogRecoveryCtl->SharedHotStandbyActive = true; - SpinLockRelease(&XLogRecoveryCtl->info_lck); - - LocalHotStandbyActive = true; - - SendPostmasterSignal(PMSIGNAL_BEGIN_HOT_STANDBY); - } + if (standbyState == STANDBY_SNAPSHOT_READY) + EnableHotStandbyConnections(); } /* -- 2.47.3