Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nl9K3-0002Gr-HV for pgsql-hackers@arkaria.postgresql.org; Sun, 01 May 2022 13:09:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nl9K0-0003lS-Qf for pgsql-hackers@arkaria.postgresql.org; Sun, 01 May 2022 13:09:08 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nl9Jy-0003bG-MF for pgsql-hackers@lists.postgresql.org; Sun, 01 May 2022 13:09:08 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nl9Jt-0004Ow-6H for pgsql-hackers@postgresql.org; Sun, 01 May 2022 13:09:03 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id CB71F5C008D; Sun, 1 May 2022 09:08:59 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Sun, 01 May 2022 09:08:59 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= cc:cc:content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to; s=fm1; t=1651410539; x=1651496939; bh=CNSN9puW7s hztYw1+N+ifNa0I4q0qDOTiebZ0sK/qLY=; b=KSgsZHn4+jEc3urBJRaHj1Ha8R hlUKlQCjsV7n/5xJHVz68jqFhL30R0pwiq+kNZdPNq12zgzakBeHPVBXFZFSciZ0 stiUSqFEY5FUPnczfuvHhBpRBtG57bkkwbCcUqK92MFekqqGoXjGmie7MzapSyP7 Dk5bQ1bv9L1sF9gTmkauHwYrAJK96mVLNVkjbB1r8D2OoxeE3xaL+VKvIaJvT3w6 VN6b0AnobSFyIa21jcJN7iX9cuajoHde0KwSWhcmxR7aIL7lukR1HCblPDh8/Cpx H4GqkUXIzpYSyo0o6fCuop8GDHmGlCFEWW+sG6nwCl2+RNEaz0CAA24GFe2g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1651410539; x= 1651496939; bh=CNSN9puW7shztYw1+N+ifNa0I4q0qDOTiebZ0sK/qLY=; b=I sv28I1L+khONTd79SUBZxEv2Sd1gtIUzVvhdMnXZK7svubN8gdGdsMGdQNpqdcRU 7InDd1nAuP0mp+k0WHVDxOYZ/P7RNjlNwEs+p8Ui8T9qD/+tWXs/RsjZv0OYIXTv U2bUM7AZfU8V5G4KpWIb0k+jfYX7fGvxKfSozdOdkkgn45PaL3+4gEti8pi5NnF1 IDxtaDoYiSlVSV4cY3fAiLi0TaBz1nKZthh2wgWTCKltcaeaE4cbJOgQwIF4TVZR ExMwGY28O/CSR3MqL9FNKRmnsc9Sa2+IrgMsubxJALpqu3wam+Jm6M+daIFG3wwf TVv3ECxRNDOqP3CoKRXvw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrvdefgdeiudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenfg hrlhcuvffnffculddujedmnecujfgurhepfffhvfevuffkfhggtggujgesghdtreertddt vdenucfhrhhomhepofhitghhrggvlhcurfgrqhhuihgvrhcuoehmihgthhgrvghlsehprg hquhhivghrrdighiiiqeenucggtffrrghtthgvrhhnpefhfffgheejkeeguddvfedtudeg ffehtedtffetfedvieegveekjeeljedvffdtgeenucffohhmrghinhepphhoshhtghhrrd gvshdpphhoshhtghhrvghsqhhlrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfr rghrrghmpehmrghilhhfrhhomhepmhhitghhrggvlhesphgrqhhuihgvrhdrgiihii X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 1 May 2022 09:08:57 -0400 (EDT) Date: Sun, 1 May 2022 22:08:53 +0900 From: Michael Paquier To: Nathan Bossart Cc: Kyotaro Horiguchi , tgl@sss.pgh.pa.us, robertmhaas@gmail.com, pgsql-hackers@postgresql.org Subject: Re: avoid multiple hard links to same WAL file after a crash Message-ID: References: <289699.1649694527@sss.pgh.pa.us> <20220411165257.GB1915258@nathanxps13> <20220412.154631.417529439388886590.horikyota.ntt@gmail.com> <20220412162742.GB2062175@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="jZAaLdPE9Xn6/lWI" Content-Disposition: inline In-Reply-To: <20220412162742.GB2062175@nathanxps13> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --jZAaLdPE9Xn6/lWI Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Apr 12, 2022 at 09:27:42AM -0700, Nathan Bossart wrote: > On Tue, Apr 12, 2022 at 03:46:31PM +0900, Kyotaro Horiguchi wrote: >> At Mon, 11 Apr 2022 09:52:57 -0700, Nathan Bossart wrote in=20 >>> I traced this back a while ago. I believe the link() was first added in >>> November 2000 as part of f0e37a8. This even predates WAL recycling, wh= ich >>> was added in July 2001 as part of 7d4d5c0. >>=20 >> f0e37a8 lacks discussion.. It introduced the CHECKPOINT command from >> somwhere out of the ML.. This patch changed XLogFileInit to >> supportusing existent files so that XLogWrite can use the new segment >> provided by checkpoint and still allow XLogWrite to create a new >> segment by itself. Yes, I think that you are right here. I also suspect that the checkpoint command was facing a concurrency issue while working on the feature and that Vadim saw that this part of the implementation would be safer in the long run if we use link() followed by unlink(). > Yeah, I've been unable to find any discussion besides a brief reference to > adding checkpointing [0]. >=20 > [0] https://postgr.es/m/8F4C99C66D04D4118F580090272A7A23018D85%40sectorba= se1.sectorbase.com While looking at the history of this area, I have also noticed this argument, telling also that this is a safety measure if this code were to run in parallel, but that's without counting on the control file lock hold while doing this operation anyway: https://www.postgresql.org/message-id/24974.982597735@sss.pgh.pa.us As mentioned already upthread, f0e37a8 is the origin of the link()/unlink() business in the WAL segment initialization logic, and also note 1f159e5 that has added a rename() as extra code path for systems where link() was not working. At the end, switching directly from durable_rename_excl() to durable_rename() should be fine for the WAL segment initialization, but we could do things a bit more carefully by adding a check on the file existence before calling durable_rename() and issue a elog(LOG) if a file is found, giving a mean for the WAL recycling to give up peacefully as it does now. Per my analysis, the TLI history file created at the end of recovery ought to issue an elog(ERROR). Now, I am surprised by the third code path of durable_rename_excl(), as of the WAL receiver doing writeTimeLineHistoryFile(), to not cause any issues, as link() should exit with EEXIST when the startup process grabs the same history file concurrently. It seems to me that in this last case using durable_rename() could be an improvement and prevent extra WAL receiver restarts as a TLI history fetched from the primary via streaming or from some archives should be the same, but we could be more careful, like the WAL init logic, by skipping the durable_rename() and issuing an elog(LOG). That would not be perfect, still a bit better than the current state of HEAD. As we are getting closer to the beta release, it looks safer to let this change aside a bit longer and wait for v16 to be opened for business on HEAD. -- Michael --jZAaLdPE9Xn6/lWI Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmJuhmUACgkQnvQgOdby QH2+GA//TVMjfB5l0olgKtDgeuT9ZEzyvEMWHYGfIsVz0AVaWxCL5QTrgOrGgDJX 1rt0EkGyehsgPLI5MnRlq9FvAL4njYAMUZKcMe4VHVLUhtL+jTNDfFPQ+uEn6zmF 40yzP6lRZtfCQpXGllt3DbsTvmaBTmdTvICq/loW8+I/Kbcx8bXADNgYepRVWaQx PQw+xRdgtnja0Nnmp/8Hqk8T7WCfrfwM5mr8g/fAhOJmCFqM3ytsLbP50kT0yhTe 88durhiuyijBHbUv7nLNgJbhDnyE30Un++XFyoX/PH1lBftuulYMRf6PZubED1vJ w1HHuBdJhzLvqPO06MaUY8X9dPsDX7L+9+lT0EWNaKVl2BGlvionp6p7eGOhcdi8 ERMp9dzd5xDh0+EAteP5THXPFjyKpEKQ8H2keFWr9kMcUfwQXi+Np24Wg2BCijjn fPZ+ZCs0/k5ENFy0Vfg/iceShTro0A7VeIQj5COkpfJOns4wxNN1qbN61Wt+xM7T XcEZiDOlnCjiVZ1EL4yNJP921Dw0I9UeIVA8GkL0GrCbozPG+3X3t8xCGwXcSwad gf9FFvjOTYcIZagCYXq7XLLAx+Ue04WsYMfX2fiAr6cl622hvX8usdIJEhiVpJY0 Pa9MAsGjOdqFr9qD5OCkcRB5uyLKccp13zZ28yNoT7DkGx04zlM= =6pVD -----END PGP SIGNATURE----- --jZAaLdPE9Xn6/lWI--