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.96) (envelope-from ) id 1wQ2Xg-001EvJ-2r for pgsql-hackers@arkaria.postgresql.org; Thu, 21 May 2026 12:30:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wQ2Xd-00A6mB-22 for pgsql-hackers@arkaria.postgresql.org; Thu, 21 May 2026 12:30:22 +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.96) (envelope-from ) id 1wQ2Xd-00A6m2-0m for pgsql-hackers@lists.postgresql.org; Thu, 21 May 2026 12:30:22 +0000 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wQ2Xc-0000000069x-199f for pgsql-hackers@postgresql.org; Thu, 21 May 2026 12:30:21 +0000 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-2baca4df358so35718275ad.2 for ; Thu, 21 May 2026 05:30:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779366619; x=1779971419; darn=postgresql.org; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=NztgnHM+tAk9aoZmmej/3Gt7WDpsM9z56a1Rn8HUj8s=; b=KNoJahHS3rUMpBgMUd6xfVOH5fVnqRIqyaBxO09RboyTm64Fc8Kvs3bZLiF6AEsmDo +QimXFvEqY+U8G/vp+IVogrBgE6J+2aaFfc8EP2gzYe0PniGiWNZta5GA3Ieg7NOx6u8 lQyZT8uLbTa2upM9+uSJ2tBZwyobXkRqLEoBCL21az7/UBtpM/VkRmt42EYt/sufuOSs 8d7PDo4IP8kWU1LumteV8gb5JybmF+4yay0c7dlEUHZY1++4mgJAiYEem1f8Utv+lwtB UEwe+E3D1a8str/Ft4rD9nugkf3zjFnwVgojkbjdJ1nBKBuNJABXt89ORKzv+0MFRetf zjrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779366619; x=1779971419; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=NztgnHM+tAk9aoZmmej/3Gt7WDpsM9z56a1Rn8HUj8s=; b=aSUnwQ5JPo0G1pch8bF+pwk3LT7e64h/XnSGI/sIGQkUdQfWPCgmhHOmUfNR6HrFcy oEf4LsxdebieqFuen1DIAEFfeLlxFq4Ik3q727cy0ffPdV2dgKgXUA/JZ5hoBF8aPoUp dfQCqVXx/Ql9sGgb6Y3eTgXLNKhpVYFrrao1W2ARkVStZwJb3Kt/15FBlsPLjiIxgjAS fnZovPESNdKzCkBC2SeaA39CySaB8SabAwkNB/42+x4rGy6OYAA6VrC5kKlu6Z669fsN wYiov0+dYiWOf0VtdX186k0Rwu+TtQ6K3ssU8Q9z42HEE9GNDlG/6e5Ub1i9q6uH2gWj bNVQ== X-Gm-Message-State: AOJu0YzklPx7oTzvvfJMd5IWk/qQVv9ErHG+9fK/Ef7FLSJp+3onKunO 0mdopmoVUhYvCUTaZZqkotHJQwzz8fIa2KSWHku4NG0AKlO5v1lWZkIt X-Gm-Gg: Acq92OEjslACLEv8/wZdpE2tfxsb1iSRaXr5oiI9F5EDT1bA3rmi5AWbRQvZW1PS1IQ IBQmMkUhK0Ep5RRUh1GcOnNa55bywpdImGpx5o6XwQhl/Pet+viDSjB8SeIAAZRPE6cISV9U/+m mvrEr79ZKFJJqCkb+Q0N/qptjY9mKkhbDd/Qev4hKJlqc6XSRcQo9N74GlxdCGTZIyTJVIHL8Gv yf25mHVp9Ddebfv0h94LU6hZvCW2Y0wqoOJCVMOJiWaly76U4maxC8qdvJ1bjpkvDm5f85Yh2n7 JAO1rtBW5ILeecAuXj3SYk4z2TOJB7qG7kmxyqlKh6DAOXp9L9L+3q4EN+R3X4kq5e9cEpiMTWD TCDti9W35WFnsmZ/J9KF7Ri/NIdPnc00/f/s5wPsYLyiJzc0w6KQWWCvlneGawUGGgWyrgB78bB gs0/UjJKZWEI/qR0rR42iVIz1NILgHqOc= X-Received: by 2002:a17:903:3d10:b0:2b2:57f3:8d07 with SMTP id d9443c01a7336-2bea3068ca0mr28640515ad.7.1779366616100; Thu, 21 May 2026 05:30:16 -0700 (PDT) Received: from smtpclient.apple ([45.32.121.103]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2bea929f859sm11695635ad.33.2026.05.21.05.30.13 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 21 May 2026 05:30:15 -0700 (PDT) From: Chao Li Message-Id: <1B9D2BAF-C73A-4A79-852C-17FB2A474AC0@gmail.com> Content-Type: multipart/mixed; boundary="Apple-Mail=_85C8D83B-DFB6-4AD6-8A60-3DA5D5FDD9BD" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.600.51.1.1\)) Subject: Re: Fix pg_stat_wal_receiver to show CONNECTING status Date: Thu, 21 May 2026 20:29:35 +0800 In-Reply-To: Cc: PostgreSQL-development , Michael Paquier , Xuneng Zhou To: Michael Paquier References: <1F153E64-B791-42FA-A60A-64813B20B81E@gmail.com> <75CDE990-29D5-4D5C-BFE1-3840F19C0163@gmail.com> <1B695040-F544-447C-A6A8-C8BFF7F799D1@gmail.com> X-Mailer: Apple Mail (2.3864.600.51.1.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_85C8D83B-DFB6-4AD6-8A60-3DA5D5FDD9BD Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On May 21, 2026, at 20:08, Michael Paquier = wrote: >=20 > On Thu, May 21, 2026 at 03:20:13PM +0800, Chao Li wrote: >> I spent more time here, and found that it is still possible to leak >> conninfo in the WAL receiver reuse path: >>=20 >> * WalRcvWaitForStartPosition() sets the state to WALRCV_WAITING. >> * Then RequestXLogStreaming() copies raw conninfo into >> * walrcv->conninfo and sets the state to WALRCV_RESTARTING. >> * WalRcvWaitForStartPosition() then moves the state to >> * WALRCV_CONNECTING, but this path does not clear walrcv->conninfo >> * again. >>=20 >> The attached nocfbot_test.diff demonstrates the leak. >=20 > File is missing, but I get it. This is a legit bug from what I can > see, that also affects all the stable branches, not only HEAD. >=20 >> Initially I thought we could also set ready_to_display to false when >> setting the state to WALRCV_WAITING in WalRcvWaitForStartPosition(), >> and set it back to true when switching back to >> WALRCV_CONNECTING. However, that would make the WALRCV_WAITING and >> WALRCV_RESTARTING states invisible in pg_stat_wal_receiver. >=20 > Nah, we should not do that. We want to track the waiting and > restarting states in the view. >=20 >> I ended up with a solution that copies the primary connection info >> to walrcv->conninfo only when RequestXLogStreaming() is switching to >> WALRCV_STARTING. In the WALRCV_WAITING reuse path, the WAL receiver >> keeps using the existing wrconn, so it does not need raw conninfo to >> be copied into shared memory again. See the attached >> nocfbot_walreceiverfuncs.c.diff. >=20 > Ah, yeah. This solution to this problem makes sense. We should not > clobber conninfo either in this case, or we'd lose the > user-displayable string returned by walrcv_get_conninfo() (conninfo > cannot be NULL based on the in-core callers of RequestXLogStreaming() > AFAIK, but who knows for things out there). As mentioned above, this > is a different issue than the visibility of the connection information > while we are connecting, and it should be backpatched. Would you like > to send a patch? > -- > Michael Sorry for missing the attachments. Please take a look first. It=E2=80=99s = late here, I can spend more time tomorrow. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/ --Apple-Mail=_85C8D83B-DFB6-4AD6-8A60-3DA5D5FDD9BD Content-Disposition: attachment; filename=nocfbot_test.diff Content-Type: application/octet-stream; x-unix-mode=0644; name="nocfbot_test.diff" Content-Transfer-Encoding: 7bit diff --git a/src/test/recovery/t/004_timeline_switch.pl b/src/test/recovery/t/004_timeline_switch.pl index 5afd2f44466..85df9ad422c 100644 --- a/src/test/recovery/t/004_timeline_switch.pl +++ b/src/test/recovery/t/004_timeline_switch.pl @@ -48,10 +48,11 @@ $node_standby_1->psql( is($psql_out, 't', "promotion of standby with pg_promote"); # Switch standby 2 to replay from standby 1 +my $secret = 'dont_show_me'; my $connstr_1 = $node_standby_1->connstr; $node_standby_2->append_conf( 'postgresql.conf', qq( -primary_conninfo='$connstr_1' +primary_conninfo='$connstr_1 password=$secret' )); # Rotate logfile before restarting, for the log checks done below. @@ -62,9 +63,9 @@ $node_standby_2->restart; # verify that after reconnection, the walreceiver stays alive during # the timeline switch. $node_standby_2->poll_query_until('postgres', - "SELECT EXISTS(SELECT 1 FROM pg_stat_wal_receiver)"); + "SELECT EXISTS(SELECT 1 FROM pg_stat_activity WHERE backend_type = 'walreceiver')"); my $wr_pid_before_switch = $node_standby_2->safe_psql('postgres', - "SELECT pid FROM pg_stat_wal_receiver"); + "SELECT pid FROM pg_stat_activity WHERE backend_type = 'walreceiver'"); # Insert some data in standby 1 and check its presence in standby 2 # to ensure that the timeline switch has been done. @@ -88,11 +89,19 @@ ok( !$node_standby_2->log_contains( # Verify that the walreceiver process stayed alive across the timeline # switch, check its PID. my $wr_pid_after_switch = $node_standby_2->safe_psql('postgres', - "SELECT pid FROM pg_stat_wal_receiver"); + "SELECT pid FROM pg_stat_activity WHERE backend_type = 'walreceiver'"); is($wr_pid_before_switch, $wr_pid_after_switch, 'WAL receiver PID matches across timeline jumps'); +my $raw_conninfo_count = $node_standby_2->safe_psql( + 'postgres', + "SELECT count(*) FROM pg_stat_wal_receiver WHERE conninfo LIKE '%$secret%'"); + +is( + $raw_conninfo_count, '0', + 'raw primary_conninfo password is not visible after timeline jumps'); + # Ensure that a standby is able to follow a primary on a newer timeline # when WAL archiving is enabled. --Apple-Mail=_85C8D83B-DFB6-4AD6-8A60-3DA5D5FDD9BD Content-Disposition: attachment; filename=nocfbot_walreceiverfuncs.c.diff Content-Type: application/octet-stream; x-unix-mode=0644; name="nocfbot_walreceiverfuncs.c.diff" Content-Transfer-Encoding: 7bit diff --git a/src/backend/replication/walreceiverfuncs.c b/src/backend/replication/walreceiverfuncs.c index a0ed853e2f6..279c6c8a7e1 100644 --- a/src/backend/replication/walreceiverfuncs.c +++ b/src/backend/replication/walreceiverfuncs.c @@ -281,11 +281,6 @@ RequestXLogStreaming(TimeLineID tli, XLogRecPtr recptr, const char *conninfo, Assert(walrcv->walRcvState == WALRCV_STOPPED || walrcv->walRcvState == WALRCV_WAITING); - if (conninfo != NULL) - strlcpy(walrcv->conninfo, conninfo, MAXCONNINFO); - else - walrcv->conninfo[0] = '\0'; - /* * Use configured replication slot if present, and ignore the value of * create_temp_slot as the slot name should be persistent. Otherwise, use @@ -307,6 +302,10 @@ RequestXLogStreaming(TimeLineID tli, XLogRecPtr recptr, const char *conninfo, { launch = true; walrcv->walRcvState = WALRCV_STARTING; + if (conninfo != NULL) + strlcpy(walrcv->conninfo, conninfo, MAXCONNINFO); + else + walrcv->conninfo[0] = '\0'; } else walrcv->walRcvState = WALRCV_RESTARTING; --Apple-Mail=_85C8D83B-DFB6-4AD6-8A60-3DA5D5FDD9BD--