Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vDEbD-005ghS-1i for pgsql-bugs@arkaria.postgresql.org; Mon, 27 Oct 2025 04:12:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1vDEbB-006EU3-SQ for pgsql-bugs@arkaria.postgresql.org; Mon, 27 Oct 2025 04:12:48 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vDEbB-006ETv-BF for pgsql-bugs@lists.postgresql.org; Mon, 27 Oct 2025 04:12:48 +0000 Received: from mail-pg1-x52b.google.com ([2607:f8b0:4864:20::52b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vDEb7-003wup-2w for pgsql-bugs@lists.postgresql.org; Mon, 27 Oct 2025 04:12:46 +0000 Received: by mail-pg1-x52b.google.com with SMTP id 41be03b00d2f7-b550a522a49so3447662a12.2 for ; Sun, 26 Oct 2025 21:12:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1761538364; x=1762143164; darn=lists.postgresql.org; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=MR9f+YyLHJM3dw4Xkr8JNRgrWHGCQkKBWX2dc+0318k=; b=S63GslywmYP9fwgBas6LQtwNSjEM5xss7bnPHLb2x/q9/yrzrXcy8dSVdQ5oSxW9GX hqCo/FowLAON+hT4WaKGednh9jpvZHwsc//+dpSexh7StDBbysxNtwLO8Mk8Clk5Z8R7 eOTlmyeUsma74j1eVRcNor8QXhY+EgTQsblgk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761538364; x=1762143164; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=MR9f+YyLHJM3dw4Xkr8JNRgrWHGCQkKBWX2dc+0318k=; b=nhqsdEQcGrPAlAdQN1VPtakhnUH3hOsijaMf+kdcmSzRg2x9id3xj34/9fBYj1LV78 h7ErpXn3a/nXjGq5adqHOtRzR4iip04Vf06tCdE7cKKqJLVM3nM4uA0ZH/Zx5ovoRJQM 4AQ0sfNLZZhl05v8K+vryAi7vr1XnoilGWmFP62YXe3j4GH4Kt9Vwp3A9Lhx0Jh5szcC O/NRXHL1KVOaRrO65bBfi/LVQCG/dtjHLeeBsAuCEX9oczhhCC92BiLjx4PPUOwxhkjw yhIgCD6cWuOHRP+dIy1I6u/9TO6y2p0mVgt/LmSliAAAVIBhRAN9zaS0DUZPBE0rwS2B G9gw== X-Gm-Message-State: AOJu0YwNxtmbEfiP/OXcVdCjQAUSl8jLD6cSg1Gwkjwdew/GR4M6CR9Z k0gKEqr3vLij15c4PmqzzNH+svkxlergs5Xdzlz57ZRGPWzyEqfEExwbzoCmKVlOFg== X-Gm-Gg: ASbGncscPxN8fHB/wfmOcKGXEX20b/8H02ChqVEY51bDxCxbPX9h5jjBRWWyNQu+XzO DHSZOcWWlRCVILeBPs6CPBM0R7y8Z21KgMS7NGoDJrSqf4bfUoVjMFMoqP7zmd0xhokQOXtiEqb +eFEFE4jz9A0jcMzJpvDFuiniDkhZ0o2w582OgqL/7gQHHgiJLXD8aBkKgKUWET46VjfLnhxrtf Xa5HhoBIaINfiO3wKzl8oJzocX2clU715dwzNepp82TXYkPjWjzEiFJPSrjkB2QDep5rFJxUoMR 3soL1F/2EJbkWUquhqU13J2kaOLerSk7q/3IezwAGCJWYpLBIVL2AXtUO6NoRXRT5yw5tFF2ka8 Uk4FTnLhEwBch4YVzz0BVAKqw6cGoFzbIPD2YKyy6131o2FTVeVx1EmCDUFeTcwVnrW05f0WcRL scpQw5CVk0V8ql7kijAXGP/2Rx374o X-Google-Smtp-Source: AGHT+IFOKSN5/nxOjG7yJ1rQuiZBiDgwmtPtMM7W49WXdP53CS/VReuNZRE10jFLDB73uVJZ3IgtNw== X-Received: by 2002:a17:903:248:b0:290:cd73:33f1 with SMTP id d9443c01a7336-290cd7334edmr472388405ad.59.1761538363774; Sun, 26 Oct 2025 21:12:43 -0700 (PDT) Received: from google.com (c-73-15-160-255.hsd1.ca.comcast.net. [73.15.160.255]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-33fed3eca8dsm2884352a91.0.2025.10.26.21.12.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 26 Oct 2025 21:12:43 -0700 (PDT) Date: Sun, 26 Oct 2025 21:12:41 -0700 From: Noah Misch To: ryanzxg@gmail.com, Xuneng Zhou Cc: pgsql-bugs@lists.postgresql.org, Euler Taveira Subject: Re: BUG #19093: Behavioral change in walreceiver termination between PostgreSQL 14.17 and 14.18 Message-ID: <20251027041241.ab.nmisch@google.com> References: <19093-c4fff49a608f82a0@postgresql.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/2.2.12 (2023-09-09) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Oct 24, 2025 at 02:20:39PM +0800, Xuneng Zhou wrote: > Thanks for reporting this issue! Yes, thank you for the detailed report. > On Fri, Oct 24, 2025 at 2:43 AM PG Bug reporting form wrote: > > 1. Is terminating the walreceiver process in this scenario (end of WAL on a > > diverged timeline) the expected behavior in 14.18 or later release? > > I think this is not the expected behavior. It is likely a bug > introduced in commit 3635a0a. Right. > > 2. Is it expected that pg_stat_wal_receiver.status = 'streaming' may not > > accurately reflect streaming health in this case? Yes. Even before the regression from commit 3635a0a, status='streaming' was not a reliable indicator of health. walreceiver sets that status during early startup, before it has attempted a connection. Perhaps a better health check for your application would be one of these: - If you have multiple replicas with quorum, track per-replica lag. If a replica isn't advancing, it has a problem regardless of walreceiver status. - If you have just one replica, issue a commit from the primary and see if it hangs. (Long-term, in master only, perhaps we should introduce another status like 'connecting'. Perhaps enact the connecting->streaming status transition just before tendering the first byte of streamed WAL to the startup process. Alternatively, enact that transition when the startup process accepts the first streamed byte. Then your application's health check would get what it wants.) > IMO, the expected behavior is for the walreceiver to remain in > WALRCV_WAITING state with status 'waiting', clearly indicating that > replication has stalled due to timeline issues. Right. While status='streaming' doesn't mean healthy in general, seeing a lot of 'streaming' is still not expected for this particular scenario. > --- a/src/backend/access/transam/xlogrecovery.c > +++ b/src/backend/access/transam/xlogrecovery.c > @@ -3687,8 +3687,18 @@ WaitForWALToBecomeAvailable(XLogRecPtr RecPtr, bool randAccess, > * Before we leave XLOG_FROM_STREAM state, make sure that > * walreceiver is not active, so that it won't overwrite > * WAL that we restore from archive. > + * > + * If walreceiver is actively streaming (or attempting to > + * connect), we must shut it down. However, if it's already > + * in WAITING state (e.g., due to timeline divergence), we > + * only need to reset the install flag to allow archive > + * restoration, while keeping the process alive for > + * monitoring visibility. > */ > - XLogShutdownWalRcv(); > + if (WalRcvStreaming()) > + XLogShutdownWalRcv(); > + else > + ResetInstallXLogFileSegmentActive(); I think this will do the right thing for all reachable cases. In WALRCV_WAITING, the walreceiver won't install segments until the startup process moves the walreceiver to WALRCV_RESTARTING. There's no reason to take the further step of terminating the walreceiver. The discussion before commit 3635a0a wasn't convinced status='waiting' was reachable at this location, but this thread shows it is reachable. WalRcvStreaming() return values map to WalRcvState values as follows: true: STREAMING STARTING RESTARTING false: STOPPED WAITING STOPPING This change would be wrong if WALRCV_STOPPING were a reachable state here. That state is the startup process asking walreceiver to stop. walreceiver may then still be installing segments, so this location would want to call XLogShutdownWalRcv() to wait for WALRCV_STOPPED. That said, WALRCV_STOPPING is a transient state while the startup process is in ShutdownWalRcv(). Hence, I expect STOPPING never appears here, and there's no bug. An assertion may be in order. Can you add a TAP test for this case? Since it was wrong in v15+ for >3y and wrong in v14 for 5mon before this report, clearly we had a blind spot. postgr.es/m/YyACvP++zgDphlcm@paquier.xyz discusses a "standby.signal+primary_conninfo" case. How will this patch interact with that case? Thanks, nm