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 1uNCPG-00E4fb-8A for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Jun 2025 15:21:27 +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 1uNCPE-00Abyr-9p for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Jun 2025 15:21:24 +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.94.2) (envelope-from ) id 1uNCPD-00Abyj-Rl for pgsql-hackers@lists.postgresql.org; Thu, 05 Jun 2025 15:21:24 +0000 Received: from mail.postgrespro.ru ([93.174.132.70]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uNCPC-000NdV-16 for pgsql-hackers@postgresql.org; Thu, 05 Jun 2025 15:21:23 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1749136880; bh=d+DVB4SCWyos5w10aoHESU5IKo6aDs1H8wteisx4jFI=; h=From:In-Reply-To:References:Date:Cc:To:Message-ID:Subject: User-Agent:From; b=eiz/W12ZLqaf/DMJkIbhU9ekM8dak8MIu/1fGpexs2MNUQr76IVAvSuOT/vGt3HbG JiA6p29OOMc5BdhvjGSHxbZudC3y5dk8TF1G2KfI7GJgJ5/YktBNkYfEBkWVXogylq c82PFCNluIUX3zK3PVzSOTtAEwpfXvRG6V8AevGi34J6zQXdS3PMkIu82XXzlCO7wY 8tFYdNiS7pmvhNI/d/JwnQjIAKMX2trYsl0ahpNXV5c/MkKj/mWY4tjCeqM4UrLYy8 T1iP+WV/FZFoySlYrZOw4SFPqQsb8LOkQkhHSQDTShnIv6Vib+mGA4sB86qtlgZqgi zBedIiwgN5vvg== Received: from sogo.postgrespro.ru (sogo.l.postgrespro.ru [192.168.2.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (Client did not present a certificate) by mail.postgrespro.ru (Postfix/587) with ESMTPS id 4AC6360C2E; Thu, 5 Jun 2025 18:21:20 +0300 (MSK) From: "Vitaly Davydov" In-Reply-To: Content-Type: text/plain; charset="utf-8" X-Forward: 93.174.131.141 References: <1d12d2-67235980-35-19a406a0@63439497> <683f3c82-d38e-436d-88fd-27722af62005@vondra.me> <8679fea7-94ce-4a52-8e48-1a8cd0857fcb@vondra.me> <135e8-675c4600-21-304d7f40@228322535> <1538a2-67c5c700-7-77ec5a80@179382871> <27589a-680a4b80-6d-6e07880@117064404> <3ea6a1-68150580-1-7b53be0@80017081> <309f08-682ca380-25-139bf240@62751029> <2c1d-68344100-b7-3411b8c0@256938178> Date: Thu, 05 Jun 2025 18:21:20 +0300 Cc: "pgsql-hackers" , "Masahiko Sawada" , tomas@vondra.me To: "Alexander Korotkov" , "Amit Kapila" MIME-Version: 1.0 Message-ID: <1d97ec-6841b600-3-b7b4780@40747246> Subject: =?utf-8?q?Re=3A?= Slot's =?utf-8?q?restart=5Flsn?= may point to removed WAL segment after hard restart unexpectedly User-Agent: SOGoMail 5.12.0 Content-Transfer-Encoding: quoted-printable X-KSMG-AntiPhishing: NotDetected X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.1.0.7854, bases: 2025/06/05 14:53:00 #27542404 X-KSMG-AntiVirus-Status: NotDetected, skipped X-KSMG-LinksScanning: not scanned, disabled by settings X-KSMG-Message-Action: skipped X-KSMG-Rule-ID: 1 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Dear Alexander, Amit Alexander Korotkov wrote: > Also, I've changed ReplicationSlotsComputeRequiredLSN() call to > CheckPointReplicationSlots() to update required LSN after > SaveSlotToPath() updated last=5Fsaved=5Frestart=5Flsn. This helps to= pass > checks in 001=5Fstream=5Frep.pl without additional hacks. Thank you for the improvement and patch preparation. I confirm the test= is passed without additional hacks now. I sill do not understand why this solution is favored. It is, in my opi= nion, a non backward-compatible solution. In any case, I'm ok to go with this= patch. If needed, I may prepare a backward-compatible solution where last=5Fsaved=5Frestart=5Flsn values will be in an another place of the = shmem, rather than in ReplicationSlot struct. I still would like to add my 5 cents to the discussion. The purpose of the xmin value is to prevent tuples from vacuuming. Slot= s' restart=5Flsn values are used to calculate the oldest lsn to keep WAL s= egments from removal in checkpoint. These processes are pretty independent. The logical slots are advanced in 2 steps. At the first step, the logic= al decoding stuff periodically sets consistent candidate values for catalo= g=5Fxmin and restart=5Flsn. At the second step, when LogicalConfirmReceivedLocation = is called, the candidate values are assigned on catalog=5Fxmin and restart=5Flsn v= alues based on the confirmed lsn value. The slot is saved with these consistent val= ues. It is important, that the candidate values are consistent, decoding gua= rantees it. In case of a crash, we should guarantee that the loaded from the di= sk catalog=5Fxmin and restart=5Flsn values are consistent and valid for lo= gical slots. LogicalConfirmReceivedLocation function keeps this consistency by updat= ing them from consistent candidate values in a single operation. We have to guarantee that we use saved to disk values to calculate xmin= horizon and slots' oldest lsn. For this purpose, effective=5Fcatalog=5Fxmin is = used. We update effective=5Fcatalog=5Fxmin in LogicalConfirmReceivedLocation jus= t after saving slot to disk. Another place where we update effective=5Fca= talog=5Fxmin is when walsender receives hot standby feedback message. Once, we have two independent processes (vacuuming, checkpoint), we can= calculate xmin horizon and oldest WAL lsn values independently (at different time= s) from the saved to disk values. Note, these values are updated in a non atomi= c way. The xmin value is set when the node receives hot standby feedback and i= t is used to keep tuples from vacuuming as well as catalog=5Fxmin for decoding st= uff. Not sure, xmin is applicable for logical replication. The confirmed flush lsn is used as a startpoint when a peer node doesn'= t provide the start lsn and to check that the start lsn is not older than the lat= est confirmed flush lsn. The saving of the slot on disk at each call of LogicalConfirmReceivedLocation doesn't help to avoid conflicts complete= ly, but it helps to decrease the probability of conflicts. So, i'm still not su= re, we need to save logical slots on each advance to avoid conflicts, because = it doesn't help in general. The conflicts should be resolved by other mean= s. Once, we truncate old wal segments in checkpoint only. I believe, it is= ok if we calculate the oldest lsn only at the beginning of the checkpoint, as it= was in the alternative solution. I think, we can update xmin horizon in checkp= oint only but the horizon advancing will be more lazy in this case. Taking into account these thoughts, I can't see any problems with the a= lternative patch where oldest wal lsn is calculated only in checkpoint. With best regards, Vitaly