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 1wQFIH-001Nnq-16 for pgsql-hackers@arkaria.postgresql.org; Fri, 22 May 2026 02:07:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wQFIE-00C9nl-0S for pgsql-hackers@arkaria.postgresql.org; Fri, 22 May 2026 02:07:19 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wQFID-00C9nc-2e for pgsql-hackers@lists.postgresql.org; Fri, 22 May 2026 02:07:18 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wQFIC-00000000pBi-2A26 for pgsql-hackers@postgresql.org; Fri, 22 May 2026 02:07:18 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-2ba4efedbeaso49995235ad.1 for ; Thu, 21 May 2026 19:07:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779415634; x=1780020434; 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=Rb2EHuZYiAE7wzirT1bgkJe45C3q6oAccPJWb1BRvAA=; b=jBax+GnGHcSuch89McYmLaWM2ql4F7E8Wt37vktoDx7v045+ah+OXwTtDLzUBjkDl5 EU5U1elw55CuI36j2SLk74kOXyFQ/Zhw65q7LNVcBZTwHtvL3DTUo5fPkH2Jb/5dUXW5 TJauqkupkwlSKbGq8Twn2uAMe+70dpHCRp79Hpj+M2qT856cx6OA72QxNIOOAnjV8l1P 5RaMRx8CtgteGFl8JbydB7jkipwolsTSMt33isLzvk1BvqaariTFENK6LlwnXfCI2lIU jkuYO4/PZ+7/reRxWkQzOm9cdjUs6CUsLe+JbXpJvZyH0ifEFTJj3/2jcchNMbAsZztU am5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779415634; x=1780020434; 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=Rb2EHuZYiAE7wzirT1bgkJe45C3q6oAccPJWb1BRvAA=; b=ZODbtyx+lT2SEFAa1ak9rj8t1vO59xxuaz8CGr60gwplXssOpJde0ohTUEqcgNNqqX /Xsp66PXYK1UnzdSaUEWmJtJ41eEbqTAWL9VG03NbkJipT/sIdyGt5qh0pxv9c46S9gH tqUOBb1/xPaoEu+H5sttgbJWS59CP9rk2+tCYAG0ueY5y4Uc6VA/8Xbx9lp2gzvwo+nZ gEbZZWQg2xGiHr6fukr6ReNk/q0INmAJonV58jBRJCwqSmAHDpLutOzIJYj+tmBzsChi WgpFi0+YxckYy9b53JYwdnPaai27E0INkqHgf0mlM8wz50L0i71JHHUIPUFqQzqBOyUv WUvQ== X-Gm-Message-State: AOJu0YyWTzSyfDt4GKKG+x6h/aEiouPeEhYD5SufgYpf9AfFdfNIzSTB U39FznOSQScqA4coTJq9YBdrPp5GUzl1xLVKGlqZLN0Q67lxdX8ogwWM X-Gm-Gg: Acq92OFW7EocxvNHnNli8M6N754vtKco1FujgblhbNLvEOaBaGJTuAJd+aE+nxBgOe0 1/l7zcaqhMNH6jOag0LgAFXckUQ+g8mcGW/tWqPUkvHP+yOlEHQ+0hBRNadJ1SHXaerBAbjuUdR Rmdgsj43Jjcb9gbIffVddbv4YC4fIp13ruW83uqqb+jIbGVxe/eqGdFogZVU0S9cjEcI683a+qU Cag3sBrXi/QtlgsFR+heyk+pZWzI+obspKcpFChDqB43xxnJ0IS1Elhz6kwl+LV080FIrkNNoA6 ID5in/q7sHJfqsx8CtyLlMSdWK3Ntv3PWydGoezLZjkwe2KWFW36sUx9aQICgvSsJkNwDwb67U7 Rrwtj+wBhNoguLExtRYeetQOMPsr5VEHypm2rgxyrBZIl895Eg9LKC7TVTMvoF0fXyzfI5j+xk2 HDr59/+pXq/qk3GUPOvPd6LodzZr9Mos8w+mu/S0dSTA== X-Received: by 2002:a17:902:d489:b0:2ba:6ca2:bca with SMTP id d9443c01a7336-2beb0680e99mr15316925ad.26.1779415633483; Thu, 21 May 2026 19:07:13 -0700 (PDT) Received: from smtpclient.apple ([45.32.121.103]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2beb13c174dsm6438965ad.68.2026.05.21.19.07.10 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 21 May 2026 19:07:12 -0700 (PDT) From: Chao Li Message-Id: <93526C6D-DE0A-4B7D-B908-366735FC211D@gmail.com> Content-Type: multipart/mixed; boundary="Apple-Mail=_52E463A0-C8F1-40F8-A35D-38AA926DA8D3" 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: Fri, 22 May 2026 10:06:33 +0800 In-Reply-To: <1B9D2BAF-C73A-4A79-852C-17FB2A474AC0@gmail.com> 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> <1B9D2BAF-C73A-4A79-852C-17FB2A474AC0@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=_52E463A0-C8F1-40F8-A35D-38AA926DA8D3 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On May 21, 2026, at 20:29, Chao Li wrote: >=20 >=20 >=20 >> 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 >=20 > Sorry for missing the attachments. Please take a look first. It=E2=80=99= s late here, I can spend more time tomorrow. >=20 > Best regards, > -- > Chao Li (Evan) > HighGo Software Co., Ltd. > https://www.highgo.com/ >=20 >=20 >=20 >=20 > Here comes the patch set: * v3-0001 is the exactly same as v2-0001 * In v3-0002, the change in walreceiverfuncs.c is the same as the = previous diff, and I tuned the test change a little bit. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/ --Apple-Mail=_52E463A0-C8F1-40F8-A35D-38AA926DA8D3 Content-Disposition: attachment; filename=v3-0001-Improve-pg_stat_wal_receiver-for-CONNECTING-statu.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v3-0001-Improve-pg_stat_wal_receiver-for-CONNECTING-statu.patch" Content-Transfer-Encoding: quoted-printable =46rom=20f0091c46ddfba03cfc7f7a1e4154041694edcda6=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20Michael=20Paquier=20=0A= Date:=20Tue,=2019=20May=202026=2022:52:38=20+0900=0ASubject:=20[PATCH=20= v3=201/2]=20Improve=20pg_stat_wal_receiver=20for=20CONNECTING=20status=0A= =0ACommit=20a36164e7465=20added=20a=20CONNECTING=20status=20for=20the=20= WAL=20receiver,=20but=0Apg_stat_wal_receiver=20returned=20no=20= information=20while=20the=20connection=20to=20the=0Aprimary=20was=20= attempted,=20limiting=20the=20usability=20of=20the=20feature=20in=0A= high-latency=20environments=20where=20the=20connection=20attempt=20to=20= the=20primary=0Acould=20take=20time.=0A=0AThis=20commit=20improves=20the=20= report=20of=20the=20status=20by=20splitting=20the=20way=20the=0Ashared=20= memory=20state=20of=20the=20WAL=20receiver=20is=20filled=20before=20and=20= after=20the=0Aconnection=20to=20the=20primary=20is=20attempted:=0A-=20= Before=20the=20attempt,=20reset=20all=20the=20connection=20fields,=20= switch=0Aready_to_display=20to=20true.=0A-=20After=20the=20attempt,=20= fill=20in=20the=20connection=20fields.=0A=0AThis=20change=20means=20two=20= spinlock=20acquisitions=20instead=20of=20one,=20but=20at=20least=0A= monitoring=20tools=20can=20know=20about=20the=20connection=20attempt=20= before=20its=0Acompletion,=20enlarging=20the=20usability=20of=20the=20= feature.=0A=0AReported-by:=20Chao=20Li=20=0A= Author:=20Michael=20Paquier=20=0ADiscussion:=20= https://postgr.es/m/XXX=0A---=0A=20src/backend/replication/walreceiver.c=20= |=2024=20++++++++++++++++--------=0A=201=20file=20changed,=2016=20= insertions(+),=208=20deletions(-)=0A=0Adiff=20--git=20= a/src/backend/replication/walreceiver.c=20= b/src/backend/replication/walreceiver.c=0Aindex=20= 07eac07b9ce..d19317703c1=20100644=0A---=20= a/src/backend/replication/walreceiver.c=0A+++=20= b/src/backend/replication/walreceiver.c=0A@@=20-267,6=20+267,20=20@@=20= WalReceiverMain(const=20void=20*startup_data,=20size_t=20= startup_data_len)=0A=20=09/*=20Unblock=20signals=20(they=20were=20= blocked=20when=20the=20postmaster=20forked=20us)=20*/=0A=20=09= sigprocmask(SIG_SETMASK,=20&UnBlockSig,=20NULL);=0A=20=0A+=09/*=0A+=09=20= *=20Switch=20the=20WAL=20receiver=20state=20as=20ready=20for=20display=20= before=20doing=20a=0A+=09=20*=20connection=20attempt,=20so=20as=20its=20= connecting=20state=20is=20visible=20before=0A+=09=20*=20attempting=20to=20= contact=20the=20primary=20server.=20=20Note=20that=20this=20resets=20the=0A= +=09=20*=20original=20conninfo,=20sender_port=20and=20sender_host,=20for=20= security.=20=20These=0A+=09=20*=20fields=20are=20filled=20once=20the=20= connection=20is=20fully=20established.=0A+=09=20*/=0A+=09= SpinLockAcquire(&walrcv->mutex);=0A+=09memset(walrcv->conninfo,=200,=20= MAXCONNINFO);=0A+=09memset(walrcv->sender_host,=200,=20NI_MAXHOST);=0A+=09= walrcv->sender_port=20=3D=200;=0A+=09walrcv->ready_to_display=20=3D=20= true;=0A+=09SpinLockRelease(&walrcv->mutex);=0A+=0A=20=09/*=20Establish=20= the=20connection=20to=20the=20primary=20for=20XLOG=20streaming=20*/=0A=20= =09appname=20=3D=20cluster_name[0]=20?=20cluster_name=20:=20= "walreceiver";=0A=20=09wrconn=20=3D=20walrcv_connect(conninfo,=20true,=20= false,=20false,=20appname,=20&err);=0A@@=20-277,23=20+291,17=20@@=20= WalReceiverMain(const=20void=20*startup_data,=20size_t=20= startup_data_len)=0A=20=09=09=09=09=09=09appname,=20err)));=0A=20=0A=20=09= /*=0A-=09=20*=20Save=20user-visible=20connection=20string.=20=20This=20= clobbers=20the=20original=0A-=09=20*=20conninfo,=20for=20security.=20= Also=20save=20host=20and=20port=20of=20the=20sender=20server=0A-=09=20*=20= this=20walreceiver=20is=20connected=20to.=0A+=09=20*=20Save=20= user-visible=20connection=20string,=20now=20that=20the=20connection=20= has=20been=0A+=09=20*=20achieved.=0A=20=09=20*/=0A=20=09tmp_conninfo=20=3D= =20walrcv_get_conninfo(wrconn);=0A=20=09walrcv_get_senderinfo(wrconn,=20= &sender_host,=20&sender_port);=0A=20=09SpinLockAcquire(&walrcv->mutex);=0A= -=09memset(walrcv->conninfo,=200,=20MAXCONNINFO);=0A=20=09if=20= (tmp_conninfo)=0A=20=09=09strlcpy(walrcv->conninfo,=20tmp_conninfo,=20= MAXCONNINFO);=0A-=0A-=09memset(walrcv->sender_host,=200,=20NI_MAXHOST);=0A= =20=09if=20(sender_host)=0A=20=09=09strlcpy(walrcv->sender_host,=20= sender_host,=20NI_MAXHOST);=0A-=0A=20=09walrcv->sender_port=20=3D=20= sender_port;=0A-=09walrcv->ready_to_display=20=3D=20true;=0A=20=09= SpinLockRelease(&walrcv->mutex);=0A=20=0A=20=09if=20(tmp_conninfo)=0A--=20= =0A2.50.1=20(Apple=20Git-155)=0A=0A= --Apple-Mail=_52E463A0-C8F1-40F8-A35D-38AA926DA8D3 Content-Disposition: attachment; filename=v3-0002-Avoid-exposing-raw-WAL-receiver-conninfo-during-t.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v3-0002-Avoid-exposing-raw-WAL-receiver-conninfo-during-t.patch" Content-Transfer-Encoding: quoted-printable =46rom=204dfab1d904ac6b86a7452d011e86379fba86623b=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20"Chao=20Li=20(Evan)"=20=0A= Date:=20Fri,=2022=20May=202026=2009:46:16=20+0800=0ASubject:=20[PATCH=20= v3=202/2]=20Avoid=20exposing=20raw=20WAL=20receiver=20conninfo=20during=0A= =20timeline=20jumps=0A=0AWhen=20reusing=20an=20existing=20WAL=20receiver=20= after=20it=20has=20reached=0AWALRCV_WAITING,=20RequestXLogStreaming()=20= copied=20PrimaryConnInfo=20into=0AWalRcv->conninfo=20before=20switching=20= the=20state=20to=20WALRCV_RESTARTING.=20=20At=0Athat=20point=20= ready_to_display=20could=20still=20be=20true,=20so=20= pg_stat_wal_receiver=0Acould=20expose=20the=20raw=20connection=20string,=20= including=20sensitive=20fields.=0A=0AWALRCV_RESTARTING=20does=20not=20= establish=20a=20new=20connection.=20=20The=20waiting=20WAL=0Areceiver=20= reuses=20its=20existing=20connection=20and=20only=20needs=20a=20new=20= startpoint=0Aand=20timeline,=20so=20there=20is=20no=20need=20to=20copy=20= the=20raw=20connection=20string=20into=0Ashared=20memory=20again.=20=20= Only=20copy=20conninfo=20when=20launching=20a=20new=20WAL=20receiver=0A= from=20WALRCV_STOPPED.=0A=0AAdd=20coverage=20to=20the=20timeline-switch=20= test=20to=20verify=20that=20the=20WAL=20receiver=0Aprocess=20remains=20= visible=20in=20pg_stat_wal_receiver=20across=20the=20jump,=20while=20a=0A= raw=20password=20in=20primary_conninfo=20is=20not=20exposed.=0A=0A= Author:=20Chao=20Li=20=0AReviewed-by:=0ADiscussion:=20= https://postgr.es/m/EF91FF76-1E2B-4F3B-9162-290B4DC517FF@gmail.com=0A---=0A= =20src/backend/replication/walreceiverfuncs.c=20|=20=209=20++++-----=0A=20= src/test/recovery/t/004_timeline_switch.pl=20|=2016=20++++++++++++++--=0A= =202=20files=20changed,=2018=20insertions(+),=207=20deletions(-)=0A=0A= diff=20--git=20a/src/backend/replication/walreceiverfuncs.c=20= b/src/backend/replication/walreceiverfuncs.c=0Aindex=20= a0ed853e2f6..279c6c8a7e1=20100644=0A---=20= a/src/backend/replication/walreceiverfuncs.c=0A+++=20= b/src/backend/replication/walreceiverfuncs.c=0A@@=20-281,11=20+281,6=20= @@=20RequestXLogStreaming(TimeLineID=20tli,=20XLogRecPtr=20recptr,=20= const=20char=20*conninfo,=0A=20=09Assert(walrcv->walRcvState=20=3D=3D=20= WALRCV_STOPPED=20||=0A=20=09=09=20=20=20walrcv->walRcvState=20=3D=3D=20= WALRCV_WAITING);=0A=20=0A-=09if=20(conninfo=20!=3D=20NULL)=0A-=09=09= strlcpy(walrcv->conninfo,=20conninfo,=20MAXCONNINFO);=0A-=09else=0A-=09=09= walrcv->conninfo[0]=20=3D=20'\0';=0A-=0A=20=09/*=0A=20=09=20*=20Use=20= configured=20replication=20slot=20if=20present,=20and=20ignore=20the=20= value=20of=0A=20=09=20*=20create_temp_slot=20as=20the=20slot=20name=20= should=20be=20persistent.=20=20Otherwise,=20use=0A@@=20-307,6=20+302,10=20= @@=20RequestXLogStreaming(TimeLineID=20tli,=20XLogRecPtr=20recptr,=20= const=20char=20*conninfo,=0A=20=09{=0A=20=09=09launch=20=3D=20true;=0A=20= =09=09walrcv->walRcvState=20=3D=20WALRCV_STARTING;=0A+=09=09if=20= (conninfo=20!=3D=20NULL)=0A+=09=09=09strlcpy(walrcv->conninfo,=20= conninfo,=20MAXCONNINFO);=0A+=09=09else=0A+=09=09=09walrcv->conninfo[0]=20= =3D=20'\0';=0A=20=09}=0A=20=09else=0A=20=09=09walrcv->walRcvState=20=3D=20= WALRCV_RESTARTING;=0Adiff=20--git=20= a/src/test/recovery/t/004_timeline_switch.pl=20= b/src/test/recovery/t/004_timeline_switch.pl=0Aindex=20= 5afd2f44466..eb7cfa9f8e0=20100644=0A---=20= a/src/test/recovery/t/004_timeline_switch.pl=0A+++=20= b/src/test/recovery/t/004_timeline_switch.pl=0A@@=20-47,11=20+47,15=20@@=20= $node_standby_1->psql(=0A=20=09stdout=20=3D>=20\$psql_out);=0A=20= is($psql_out,=20't',=20"promotion=20of=20standby=20with=20pg_promote");=0A= =20=0A-#=20Switch=20standby=202=20to=20replay=20from=20standby=201=0A+#=20= Switch=20standby=202=20to=20replay=20from=20standby=201.=20=20During=20= the=20timeline=20switch,=0A+#=20the=20WAL=20receiver=20process=20on=20= standby=202=20should=20not=20be=20stopped,=20and=20the=0A+#=20new=20= primary=20connection=20string=20should=20not=20be=20visible=0A+#=20in=20= pg_stat_wal_receiver.=0A+my=20$secret=20=3D=20'dont_show_me';=0A=20my=20= $connstr_1=20=3D=20$node_standby_1->connstr;=0A=20= $node_standby_2->append_conf(=0A=20=09'postgresql.conf',=20qq(=0A= -primary_conninfo=3D'$connstr_1'=0A+primary_conninfo=3D'$connstr_1=20= password=3D$secret'=0A=20));=0A=20=0A=20#=20Rotate=20logfile=20before=20= restarting,=20for=20the=20log=20checks=20done=20below.=0A@@=20-93,6=20= +97,14=20@@=20my=20$wr_pid_after_switch=20=3D=20= $node_standby_2->safe_psql('postgres',=0A=20is($wr_pid_before_switch,=20= $wr_pid_after_switch,=0A=20=09'WAL=20receiver=20PID=20matches=20across=20= timeline=20jumps');=0A=20=0A+my=20$raw_conninfo_count=20=3D=20= $node_standby_2->safe_psql(=0A+=09'postgres',=0A+=09"SELECT=20count(*)=20= FROM=20pg_stat_wal_receiver=20WHERE=20conninfo=20LIKE=20'%$secret%'");=0A= +=0A+is(=0A+=09$raw_conninfo_count,=20'0',=0A+=09'raw=20primary_conninfo=20= password=20is=20not=20visible=20after=20timeline=20jumps');=0A+=0A=20#=20= Ensure=20that=20a=20standby=20is=20able=20to=20follow=20a=20primary=20on=20= a=20newer=20timeline=0A=20#=20when=20WAL=20archiving=20is=20enabled.=0A=20= =0A--=20=0A2.50.1=20(Apple=20Git-155)=0A=0A= --Apple-Mail=_52E463A0-C8F1-40F8-A35D-38AA926DA8D3--