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 1uJqS6-007esk-7v for pgsql-hackers@arkaria.postgresql.org; Tue, 27 May 2025 09:18:30 +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 1uJqS3-0074kl-RI for pgsql-hackers@arkaria.postgresql.org; Tue, 27 May 2025 09:18:28 +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 1uJqS3-0074kc-GB for pgsql-hackers@lists.postgresql.org; Tue, 27 May 2025 09:18:27 +0000 Received: from mail-ed1-x52b.google.com ([2a00:1450:4864:20::52b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uJqS0-000LHR-3C for pgsql-hackers@postgresql.org; Tue, 27 May 2025 09:18:27 +0000 Received: by mail-ed1-x52b.google.com with SMTP id 4fb4d7f45d1cf-601f278369bso6492734a12.1 for ; Tue, 27 May 2025 02:18:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1748337504; x=1748942304; 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=MTQAiYcrkIxQOZu+REv9Zx7t4x+FTgMS6GKrR6lQgOI=; b=ilTL4Nd1a+Vk1P085PJc5dMUVlTq5FpNwPTjzLpRX6H8DYYblcEGcIRT1pLgvL2DuD LjRNBGPdA/2W2XK/LqsK0toJH0XEy4/XzxZTEOa9QWi216+FQ8cp6E4LOxZuw2VxGrPX i9iDsEuSpbte287oOAaEJNv4bZGkUhXqnWzENguAYO5Mo7UoGbnKeunv7OvV/PEBZ8Uj bxpHNgu1FAj8Lk82vSotpq7pP+8IR9b6IPtXTlBdr6fLN7TlGWzHcm1smItnBFdcB6JE tI/YAwWhITTYp9vp0MKm2HqWF2BNM3KeBcSpRcM6816KfP4oeXqLl+/HHZ0MZ1fA7TJq 5dmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748337504; x=1748942304; 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=MTQAiYcrkIxQOZu+REv9Zx7t4x+FTgMS6GKrR6lQgOI=; b=XkwMI4ycxaxcpBwGdl/wOfxN6SXvy2MqAj1n9Imkalwp+u524zk/vK1oZOTYNvbeGj 0n5ljaAi6+louCkyKnVc9/9AYvXuNSBmApRxO+DzqejFTGlPUr3AHqB0g60P41ApkOcF 8WQ5tQ64Up7l0xrdP/zFTcmE+zxuCbbRoLBWG4poCfaxECCx8pKR9Kj7eRZsuYum9lCk 3VlzzVujo2CNiIkc78IVGcFv2bqwE277MbOLzc603S8c9l3s17V9t/DNZSQswdeu+RZz XjdWXefZ0LoIKAXwwKgzmTN0virEIBVjiEzpV1Egh7ZINsaJzH/rTqo2BEyKP4fflCJ2 8UYg== X-Forwarded-Encrypted: i=1; AJvYcCVFU7Fpl2DTXA1a1c98nJax0MWI6r89f8nk6DEYInbNwaJyuCIjfhhQFFRX1FQ0lf6w680UeOhVMN4/m63o@postgresql.org X-Gm-Message-State: AOJu0YwmP1/rri89oQ0THNVj+B6g6+RwEvB5Mv1fhbwGWytFMZqIyVFW 1+uOVNCWD0u88oHWA7FAJ8cw5VEBh9Brqgn1s+NRJukq5oMlMo2mNSIbX3GxatynH/68oq+dPlo tm9i4OStZFFsKwIFYoRB24aFaaBYWjnw= X-Gm-Gg: ASbGncuk2xczuvDt8F0U3ToUikda0218qix82VhbXrhtzq55y/N856Pday2k4e8pTIE 1beoW8ZfSFe0RDrMOE/wvrADsGq2iKgiXWqL9T1psitAhlDpDbaozwLrOxOiSAqGTWadRfpnBd9 99v3th3Rwjv1DcbGYgd5+YFEcxUi056DqF X-Google-Smtp-Source: AGHT+IE3GkH/Orl92v6lvlcrOCsFXB6GyTnTbqTpdtE4z5O7gOZi4Rd+4e1iVC984Xot7tGjy1yOmdQa/pUFCxk9/I0= X-Received: by 2002:a17:907:6d0c:b0:ad5:1fe2:d392 with SMTP id a640c23a62f3a-ad85b1301ddmr1011757966b.29.1748337503909; Tue, 27 May 2025 02:18:23 -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:18:12 +0300 X-Gm-Features: AX0GCFu3QbxIYc95bEAfrj_JoyWvYAjuht85l6rqGQfEBHLsVbPoBKpct7N06Fg 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 12:12=E2=80=AFPM Alexander Korotkov wrote: > > 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 immediate= ly > > > > when its restart_lsn is updated is a waste of effort after your pat= ch, > > > > 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 concl= ude 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? Possibly we could implement effective_restart_lsn even for pg18. As I know, keeping ABI compatibility is not required for beta. ------ Regards, Alexander Korotkov Supabase