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 1wxSDs-002jCd-0N for pgsql-hackers@arkaria.postgresql.org; Fri, 21 Aug 2026 16:36:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wxSDp-00E9jT-2N for pgsql-hackers@arkaria.postgresql.org; Fri, 21 Aug 2026 16:36:01 +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 1wxSDo-00E9jE-2x for pgsql-hackers@lists.postgresql.org; Fri, 21 Aug 2026 16:36:01 +0000 Received: from fout-b8-smtp.messagingengine.com ([202.12.124.151]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wxSDl-00000000XEr-2lKy for pgsql-hackers@lists.postgresql.org; Fri, 21 Aug 2026 16:36:00 +0000 Received: from phl-compute-03.internal (phl-compute-03.internal [10.202.2.43]) by mailfout.stl.internal (Postfix) with ESMTP id 51A841D000BD; Fri, 21 Aug 2026 12:35:54 -0400 (EDT) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-03.internal (MEProxy); Fri, 21 Aug 2026 12:35:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kurilemu.de; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to; s=fm2; t=1787330154; x= 1787416554; bh=1dV2JTJ05MJqx7iEa2eLVzEf4cqbHkibN8hKad8rIkQ=; b=l azU34S8lZvzko+D9C4eXgewmFG/vtoZ43wGP7ck51cVZW6j1YXGn+Gu/0pLElqul /N1RVXbZMjn2Kj6zfj2ji6Yw+KB6hb+4WDHLGlI8Fjj+wMjrLtMWNwB7Iwna/g8T mOgFUWbtseTlIq0yrUVhI7Z+k062S220v81vijN21xGQPNVDuqov6QqZjNPybveM f0XUUX87ncCbT6Us1V+0QrumBuPQBIAXpCHxtdnN25HIkiVoZGkZ4zaNNVmkxjEX 64xoK/Z7WMVTqW3FdYIJAvVBlqmGJ89wuVL+Nc11SiAXGBMo+D24HeN6GIZp2FeC A7eDcJOJwEiLhYICDEzCw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1787330154; x=1787416554; bh=1 dV2JTJ05MJqx7iEa2eLVzEf4cqbHkibN8hKad8rIkQ=; b=VFyF/54Ks1lMm3o2K /gF3HXKz7AaA3Ks8tLjkxMoChj5Z7Y0SGem2+P85OBnC964MatC+6VHUHzXoveF2 6WC+CUfeBRliG+t4eMM8NOZFDC2qTCXdG+0nhaRzALnJQjWEg/aJxD4Dhe2KgvSk vG4XPS28btHszq9Y4j589Qf2yxwjD/BiatSvhBpMS+29gEK3EolmnGdlHOECgzOw h9ks7LNp5BEVjFFQg/rp23RBNVg/Y/KCsNp5fKfA8pN3THxeH8buPC+i7UM5rzon yHHit9WPXXRoLmPHD89uS3+zQ/tdaXwBC26fBxaSk4ZXREPUlwy7X10n8l8nOt6b wLa1g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTGhs/KSJs/eU+LKa0Sdr0pRyxPjVafdTzFINOVK0XDK+Z8/fwCaKqbLLueQ9F3iT+ xq5TJFlvU5tH5OMqN0kS4szSCDYN5NcKfG7NRAHa/HidP0C7f3prab6H3es0sNn2MuQlyX O3b0EdRUN7L59Y/Cp7kSK3MJHpmVhR+LmTvxIrUo2ed+X3tu2vJCbKrZcxLe3DccwVmEhV sSoP5+2j2Uvw9QzFt7mCkiMkYTWKbqxtyzwt+BZn6im7dw9QUaMd53QH0UCEcmJLX3dCLn P+YB3xL5jvNR60uh5YfonS+ow9v13aQI7fOK6kfuWMctoI++45dEyRojKVwRqhB2iIDeC1 qQiIui6LHbH4ogdKe2xe4+o7k21l69IaBSJTNnDqGO/BAV116ftZ5xMQ0AxwsVtx80+el6 omsuWCdIagZ1E16joR+b8IlJR1yg0hhUOmRFGqSiyTADhlzFZOLXaZHKlYKaix4X5vlJ6Y aBRQh2A0Jon1HhkwvlaLMEOXqMTJUhSjOSoa0WjPszxBBpE2IekXKnnLJop6MwJSnw6PGx LLp1KXyxAdd3ykmCgyLvIrC8vzXCOx3tnaMI+fJCEHfshEsRQcFwi4qvqFRvoJPhozpsai Q1l8aYYSqXeL1Mqc4Mv1YmoM7V3Ui8L+POiY78WdAHm+yX6tygS4QZ5BC9yA X-ME-Proxy: Feedback-ID: ie3de48e3:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 21 Aug 2026 12:35:53 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kurilemu.de; s=schmee; t=1787330149; bh=lufZ3mTkSRE+F8aOKP9FNj/sWpn8etXZc4xg4paJvtI=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=OcbH5NwyECo5/0ZRXw3P+XKo/U11loDpSIpyqWTqj0nixCSlGnJJSJLOHkxg/xglz DdhAlnQfq/xfEd3mo44Lj1u0SjNFLIThwaRnVn+CuJ+pqeVkLqGJ9XaPDEUZwb63F+ PCtwVlCtpglQGxQtfHAZHLEQrOfGcWAmv/beVKzJXfBM+BFoPVo8ZlCrf6XPabwz/6 D8ickP10lnJcjsZT9dzIcNvWiaITatUFTpUDKRM687FuoXXdE1upSEbnKXT7/+bBlZ W4IGf05BoMP4qcxDX+740Jq0LUdryHw/1NR9yOpexE1g4mh8ZP3caK96QqQKe6f2vk BZe+Jyc2mqwhw== Received: by ida.kurilemu.internal (Postfix, from userid 1000) id 2131FB001BA; Fri, 21 Aug 2026 18:35:49 +0200 (CEST) Date: Fri, 21 Aug 2026 18:35:49 +0200 From: =?utf-8?Q?=C3=81lvaro?= Herrera To: Nick Ivanov Cc: pgsql-hackers@lists.postgresql.org Subject: Re: Possible race condition in pg_basebackup Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello Nick On 2026-Aug-21, Nick Ivanov wrote: > We are encountering a possible race condition when executing several > `pg_basebackup --wal-method=stream --slot=... --create-slot` concurrently > while initialising streaming replicas. Our automation starts 3 > pg_basebackup processes on 3 future replica servers within 1 second of each > other. One of them almost always fails with "requested WAL segment ... has > already been removed". I think this is related to this thread here: https://www.postgresql.org/message-id/flat/5e045179-236f-4f8f-84f1-0f2566ba784c.mengjuan.cmj%40alibaba-inc.com and to this commit Author: Amit Kapila Branch: master Release: REL_19_BR [006dd4b2e] 2025-12-08 05:21:22 +0000 Branch: REL_18_STABLE Release: REL_18_2 [d3ceb2084] 2025-12-08 05:33:14 +0000 Prevent invalidation of newly created replication slots. A race condition could cause a newly created replication slot to become invalidated between WAL reservation and a checkpoint. Previously, if the required WAL was removed, we retried the reservation process. However, the slot could still be invalidated before the retry if the WAL was not yet removed but the checkpoint advanced the redo pointer beyond the slot's intended restart LSN and computed the minimum LSN that needs to be preserved for the slots. The fix is to acquire an exclusive lock on ReplicationSlotAllocationLock during WAL reservation to serialize WAL reservation and checkpoint's minimum restart_lsn computation. This ensures that, if WAL reservation occurs first, the checkpoint waits until restart_lsn is updated before removing WAL. If the checkpoint runs first, subsequent WAL reservations pick a position at or after the latest checkpoint's redo pointer. We can't use the same fix for branch 17 and prior because commit 2090edc6f3 changed to compute to the minimum restart_LSN among slot's at the beginning of checkpoint (or restart point). The fix for 17 and prior branches is under discussion and will be committed separately. Reported-by: suyu.cmj Author: Hou Zhijie Reviewed-by: Vitaly Davydov Reviewed-by: Masahiko Sawada Reviewed-by: Amit Kapila Backpatch-through: 18 Discussion: https://postgr.es/m/5e045179-236f-4f8f-84f1-0f2566ba784c.mengjuan.cmj@alibaba-inc.com and to this other commit Author: Amit Kapila Branch: REL_17_STABLE Release: REL_17_8 [3510ebeb0] 2026-01-08 07:17:56 +0000 Branch: REL_16_STABLE Release: REL_16_12 [24cce33c3] 2026-01-08 07:07:23 +0000 Branch: REL_15_STABLE Release: REL_15_16 [aae05622a] 2026-01-08 06:54:52 +0000 Branch: REL_14_STABLE Release: REL_14_21 [7406df605] 2026-01-08 06:44:28 +0000 Prevent invalidation of newly created replication slots. A race condition could cause a newly created replication slot to become invalidated between WAL reservation and a checkpoint. Previously, if the required WAL was removed, we retried the reservation process. However, the slot could still be invalidated before the retry if the WAL was not yet removed but the checkpoint advanced the redo pointer beyond the slot's intended restart LSN and computed the minimum LSN that needs to be preserved for the slots. The fix is to acquire an exclusive lock on ReplicationSlotAllocationLock during WAL reservation, and a shared lock during the minimum LSN calculation at checkpoints to serialize the process. This ensures that, if WAL reservation occurs first, the checkpoint waits until restart_lsn is updated before calculating the minimum LSN. If the checkpoint runs first, subsequent WAL reservations pick a position at or after the latest checkpoint's redo pointer. We used a similar fix in HEAD (via commit 006dd4b2e5) and 18. The difference is that in 17 and prior branches we need to additionally handle the race condition with slot's minimum LSN computation during checkpoints. Reported-by: suyu.cmj Author: Hou Zhijie Author: vignesh C Reviewed-by: Hayato Kuroda Reviewed-by: Masahiko Sawada Reviewed-by: Amit Kapila Backpatch-through: 14 Discussion: https://postgr.es/m/5e045179-236f-4f8f-84f1-0f2566ba784c.mengjuan.cmj@alibaba-inc.com What version are you using? If you're using a version that contains these fixes, then we may have some slightly different bug ... -- Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/ "No hay ausente sin culpa ni presente sin disculpa" (Prov. francés)