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 1uJqMk-007d6g-9U for pgsql-hackers@arkaria.postgresql.org; Tue, 27 May 2025 09:12:58 +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 1uJqMi-0071r2-Ps for pgsql-hackers@arkaria.postgresql.org; Tue, 27 May 2025 09:12:57 +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 1uJqMi-0071qu-Eq for pgsql-hackers@lists.postgresql.org; Tue, 27 May 2025 09:12:56 +0000 Received: from mail-ej1-x62a.google.com ([2a00:1450:4864:20::62a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uJqMf-000LEG-2w for pgsql-hackers@postgresql.org; Tue, 27 May 2025 09:12:56 +0000 Received: by mail-ej1-x62a.google.com with SMTP id a640c23a62f3a-acae7e7587dso400445666b.2 for ; Tue, 27 May 2025 02:12:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1748337173; x=1748941973; 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=vmoQEJI16s3532OXve2iQkislK80QPgc4FSThhWofMg=; b=d3/FZhzcia5QYv8nh3jvX2ek0c4VExUc8f3ced9bzVta7wDVgCL2ILlE4P3x5ADek7 z60512V0SVYbi0iJRTBd4YfGvCjSBWAeGFc8XLZvy2c0DBpnymcDLs9YBOMwAuDRyJlm zIiX0k+H/OIievBe0YFiPZLOA+/6xmT+RF6SZyLNRrfrYXOmC7UcUpDs4S9zOjFqpBeS hg/ywvGGoD95TzZPVOpFdUmJTCZJUl9pW6dwJJ/86CsgiwioXSNUqKXEqCY/webB04yS 0RkR8NDigIYEmZWuSmk8n4Fvh4qwYIZjmnD2sPQvKlB29LUPoiMgN8Utd6r6kD4K4CWw 14WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748337173; x=1748941973; 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=vmoQEJI16s3532OXve2iQkislK80QPgc4FSThhWofMg=; b=qnrv9jiAEi1uKB+4dDYYU3EcDvqt5iCAD4KjKs9ZLgeO+g5AdGK3Od5J6hg06yD1Pi BC9Li56jPmNpNUH9RzqKee56u33oLGnPQk0KHfJwau6mGNXoqTf6atYJ6Ceixyv8AygH bDi2uXYuumlxEf5NQ6gEP9Z39ZiznRK6Wd5JR7SoKucBmHpOuhjZIr5VprrmY+WX1dlX Fy0cMc8SgivjxMQLUyWTtM/engR2yHmgtD9OnFJcb7Rp1fc6+YTktrAwiKPFbvrcX5eb 28SNlYP+CGyu2yp5saNcJZNoXqXUr/Xl4fGWyoZrflzdvCxyr4wphezS/gZbXRvhAo5D 5oyg== X-Forwarded-Encrypted: i=1; AJvYcCVdy5wt9OZvpDruhs/c0apjhrkfatr5N7PryykWr0xc5tYttJOd98oKukt+dG1sdVRpwdmtfGHx8R0P00gr@postgresql.org X-Gm-Message-State: AOJu0YyUzW+zTd8SMjx0As4hprps5OMmbWwoIi8dSV3Rj63SldUqp5LH JTtS8YpYsgzjzLHYA2KbrA6revr1vTXFFjK3p+gixssPp1nPn1hMHkE7YWfSm99vWVUYHVHh3pY LawpY7ibHEcPt2laGMYILyEM9FDCaGOk= X-Gm-Gg: ASbGnct53j2L/NnAF6T6iPs2qHA8HpBa0owo9AiNtqkcetQW+fohH633fkAyKsjLcHv nld7/ZCpTMKvMlvzUr7ax6DsZ1Blj0OJ52Oe2wDRIVkzQVN+qW94BkzB1mB1ZPMruuSKBL7q0tj J/BuNzORWcsIUSmGpOmFruToGOtRa52mIb X-Google-Smtp-Source: AGHT+IEZH+phJUMAIwdM76atGC+i5bSU02tZzlstCZoLWwlRtVuuIezvE1SksVDVh8OddL7pu5LVb24WW8L2HuNefsE= X-Received: by 2002:a17:907:25c7:b0:acb:4e0c:23ed with SMTP id a640c23a62f3a-ad85b05ed5cmr1230050866b.14.1748337173000; Tue, 27 May 2025 02:12:53 -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: Tue, 27 May 2025 12:12:41 +0300 X-Gm-Features: AX0GCFthRL5VkJG2HWh_kiavPXFR9dlhE9tVKiFLxmuixv4RoR3k6dhMofgj9hA 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 Tue, May 27, 2025 at 7:08=E2=80=AFAM Amit Kapila wrote: > On Mon, May 26, 2025 at 10:36=E2=80=AFPM Alexander Korotkov > wrote: > > > > 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: > > > > > 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. > > > > Hmm, if this has to be true, then even in the else branch of > LogicalConfirmReceivedLocation [1], we should have saved the slot. > AFAIU, whether the logical changes are sent to the client is decided > based on two things: (a) the replication origins, which tracks > replication progress and are maintained by clients (which for built-in > replication are subscriber nodes), see [2]; and (b) confirmed_flush > LSN maintained in the slot by the server. Now, for each ack by the > client after applying/processing changes, we update the > confirmed_flush LSN of the slot but don't immediately flush it. This > shouldn't let us send the changes again because even if the system > crashes and restarts, the client will send the server the location to > start sending the changes from based on its origin tracking. There is > more to it, like there are cases when confirm_flush LSN in the slot > could be ahead the origin's LSN, and we handle all such cases, but I > don't think those are directly related here, so I am skipping those > details for now. > > Note that LogicalConfirmReceivedLocation won't save the slot to disk > if it updates only confirmed_flush LSN, which is used to decide > whether to send the changes. You're right, I didn't study these aspects careful enough. > > LogicalConfirmReceivedLocation() implements immediate sync for > > different reasons. > > > > I may be missing something, but let's discuss some more before we conclud= e this. So, yes probably LogicalConfirmReceivedLocation() tries to care about keeping all WAL segments after the synchronized value of restart_lsn. But it just doesn't care about concurrent ReplicationSlotsComputeRequiredLSN(). In order to fix that logic, we need effective_restart_lsn field by analogy to effective_catalog_xmin (similar approach was discussed in this thread before). But that would require ABI compatibility breakage. So, I'd like to propose following: backpatch 0001 and 0002, but implement effective_restart_lsn field for pg19. What do you think? ------ Regards, Alexander Korotkov Supabase