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 1uJbHd-002f1d-OI for pgsql-hackers@arkaria.postgresql.org; Mon, 26 May 2025 17:06:41 +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 1uJbHb-00380p-Tt for pgsql-hackers@arkaria.postgresql.org; Mon, 26 May 2025 17:06:40 +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.94.2) (envelope-from ) id 1uJbHb-00380e-F5 for pgsql-hackers@lists.postgresql.org; Mon, 26 May 2025 17:06:39 +0000 Received: from mail-ej1-x636.google.com ([2a00:1450:4864:20::636]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uJbHZ-00046D-16 for pgsql-hackers@postgresql.org; Mon, 26 May 2025 17:06:38 +0000 Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-ad564b7aea9so693892966b.1 for ; Mon, 26 May 2025 10:06:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1748279195; x=1748883995; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=eKelAU9MrrDsHb1C4t/GEQ1wGTVk1HTKuV6Q4x0vxs4=; b=hngM90DyKAFqvioAdZ/7FKb4JqwJhgK1wYuswMXbYQZ9cAculG7kOWSMgrrmeZx3lG LdeDvMtLylWrirxcMCSNNfPHpb+L7w2Jurs74rHkCID7xY3dwc83ukjKZqerp9Fy7uDU kzg3gLzkgqmUPuYsdoRRFfYaRm4eDIyKY2jd5pYqSXG/xNITQT6NFWainaHEYNJv356Q gRVl1tjDPgz6SRR9MDMOsJQxhUqtzVP1Ak/V2W8fFCb29m1Ti7tCCU1gVCmyY33SELza WYfo7KNVMeuEa4KrabMCFaqkLjCGfAGDC8wyb9820HObnxkX7I6ov8lXHLNrVEpfQvgi ws7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748279195; x=1748883995; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eKelAU9MrrDsHb1C4t/GEQ1wGTVk1HTKuV6Q4x0vxs4=; b=UZ1vQE1YJvesr6I0GrijdWiGM279ixxTqh79FPpYHkA/4rx8i2wx6rify9AeYuQlaW QYVNixAc4yH3Ix4txU9dlflrTmlqkxopLeJ2YWcl6vG8cuSCMjZeAc2YfklXjgmLSDJx EvvPDhDQWL8LoLEjc5REkvi+dCKmJU3LZxYMwDsFcPyEViEkeelO2qrweDSROue/inUC evZBuUAk/UsvhB3AuLs7QSLH8SyhP+coYzgp9ZCgwdWhuRkaWiMZgDD5Sa0mQ/VLHIXa RI8qE4dcIa9FQ4Y9ebJS+ZUbWpdSJ+nlZHzRCXzyx0iINNID2/rjEtqKiaKs4c/JcKaM W9ZA== X-Forwarded-Encrypted: i=1; AJvYcCW8yFLx+fk9tbeJakeLFRl0tIvN4KNvVucgTJTt+J0W2IEKthdJqffAwY0ueq22TVVGmNM+zgcB3IwPV70C@postgresql.org X-Gm-Message-State: AOJu0Yysrj2G8JnhaeiCk1+1Z2obRFFZxkWyWMt8UMJyhpiruJNNLd7C OaYA0FO5LFnfokKB/W+oWHM9kxGBBiaYx0dOZ8OlGQhhLztyBpZxXOsZCR45+u7KXHQae6gfu97 4GbjXX6aXpVZmSaoMnYXVePwwqQSqJ7E= X-Gm-Gg: ASbGncsObWnNJF77DXckxGjJ3z+j+o5wJPQ8fhroDlDJoeCi75pFhBn8O3WWv0kf9oe 0jvNsp+8fDl9f+VyIGiqcMvVPRWec/+tB9FDzut8zO1XJbEiUcA/cq1Evx8HBhOA3VcoXdN2L28 pL20lzeOy+eoC1bZMdDlyp+ya/MCYDAQ== X-Google-Smtp-Source: AGHT+IFfEHwRfY4e1XwomNESahCNzi4wVlRQyNoq1V0bST6oHdLBZvkAMdpiMCYB8METq30LdbEZg2baS3guUBf+fVc= X-Received: by 2002:a17:907:3f9c:b0:ad5:d6b3:5cd5 with SMTP id a640c23a62f3a-ad8596d9843mr960532666b.5.1748279194464; Mon, 26 May 2025 10:06:34 -0700 (PDT) MIME-Version: 1.0 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> In-Reply-To: From: Alexander Korotkov Date: Mon, 26 May 2025 20:06:23 +0300 X-Gm-Features: AX0GCFszEPRa_wDXABgR9k0Jrng1yvzHCcwan6umDyiN7U-5sFF7954Y70cgO2w Message-ID: Subject: Re: Slot's restart_lsn may point to removed WAL segment after hard restart unexpectedly To: Amit Kapila Cc: Vitaly Davydov , pgsql-hackers , Masahiko Sawada , tomas@vondra.me Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, May 26, 2025 at 2:43=E2=80=AFPM Amit Kapila wrote: > > On Mon, May 26, 2025 at 3:52=E2=80=AFPM Vitaly Davydov wrote: > > > > Dear Alexander, Amit, All > > > > > Amit wrote: > > > > Is my understanding correct that we need 0001 because > > > > PhysicalConfirmReceivedLocation() doesn't save the slot to disk aft= er > > > > changing the slot's restart_lsn? > > > > > > Yes. Also, even if it would save slot to the disk, there is still > > > race condition that concurrent checkpoint could use updated value fro= m > > > the shared memory to clean old WAL segments, and then crash happens > > > before we managed to write the slot to the disk. > > > > > > How can that happen, if we first write the updated value to disk and > > > then update the shared memory as we do in > > > LogicalConfirmReceivedLocation? > > > > I guess, that the problem with logical slots still exist. Please, see t= he tap > > test: src/test/recovery/t/046_logical_slot.pl from the v6 version of th= e patch. > > A race condition may happen when logical slot's restart_lsn was changed= but not > > yet written to the disk. Imagine, there is another physical slot which = is > > advanced at this moment. It recomputes oldest min LSN and takes into ac= count > > changed but not saved to disk restart_lsn of the logical slot. We come = to the > > situation when the WAL segment for the logical slot's restart_lsn may b= e > > truncated after immediate restart. > > > > Okay, so I was missing the point that the physical slots can consider > the updated value of the logical slot's restart_lsn. The point I was > advocating for logical slots sanctity was when no physical slots are > involved. When updating replicationSlotMinLSN value in shared memory, > the logical slot machinery took care that the value we use should be > flushed to disk. One can argue that we should improve physical slots > machinery so that it also takes care to write the slot to disk before > updating the replicationSlotMinLSN, which is used to remove WAL. I > understand that the downside is physical slots will be written to disk > with a greater frequency, which will not be good from the performance > point of view, but can we think of doing it for the period when a > checkpoint is in progress? That could cause replication slowdown while checkpointing is in-progress. This is certainly better than slowing down the replication permanently, but still doesn't look good. > OTOH, if we don't want to adjust physical > slot machinery, it seems saving the logical slots to disk immediately > when its restart_lsn is updated is a waste of effort after your patch, > no? If so, why are we okay with that? I don't think so. I think the reason why logical slots are synced to disk immediately after update is that logical changes are not idempotent (you can't safely apply the same change twice) unlike physical block-level changes. This is why logical slots need to be synced to prevent double replication of same changes, which could lead, for example, to double insertion. > I understand that your proposed patch fixes the reported problem but I > am slightly afraid that the proposed solution is not a good idea w.r.t > logical slots so I am trying to see if there are any other alternative > ideas to fix this issue. I don't understand exact concerns about this fix. For sure, we can try to implement a fix hacking LogicalConfirmReceivedLocation() and PhysicalConfirmReceivedLocation(). But that would be way more cumbersome, especially if we have to keep ABI compatibility. Also, it doesn't seem to me that either LogicalConfirmReceivedLocation() or PhysicalConfirmReceivedLocation() currently try to address this issue: LogicalConfirmReceivedLocation() implements immediate sync for different reasons. ------ Regards, Alexander Korotkov Supabase