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 1uSPYW-0077is-OL for pgsql-hackers@arkaria.postgresql.org; Fri, 20 Jun 2025 00:24:32 +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 1uSPYU-00FdNI-Q9 for pgsql-hackers@arkaria.postgresql.org; Fri, 20 Jun 2025 00:24:31 +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 1uSPYU-00FdN9-F4 for pgsql-hackers@lists.postgresql.org; Fri, 20 Jun 2025 00:24:31 +0000 Received: from mail-ej1-x630.google.com ([2a00:1450:4864:20::630]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uSPYS-0032HE-2A for pgsql-hackers@postgresql.org; Fri, 20 Jun 2025 00:24:30 +0000 Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-ade48b24c97so206421066b.2 for ; Thu, 19 Jun 2025 17:24:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750379067; x=1750983867; 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=fyGJQLMYXf8g3bgRQMyCS46dGargJJSbP/8fkcYh0QM=; b=iC7I+6Y4Ebu2jM3G80crJdZN+/Kt/ByPuPoSdgY34Czrk/fMqd61kGaJbgAqJLypA8 cRHvvZ3XBkZoJm9XcRBL7WOhsBYLVfnyDISEMkk8KwCIA4/966e8b3hpxQO1Kba1p+c8 4zIxiydMEhSvEJeKt3mecpwYY9v4vdYTHxZZl8FWmxFpVQDvP4td5vUhJCtD6daf7vgp +sr1GuiYt5qxZKd4Cc1Pu4LFMKl/faYkIpwmHAUzHouSy9mWo1KoumYa+D7PZnk5os7C xVQYar48/UQHQDagGfxmathq8u1ERh7WRt3osngoyj9rzclR9axV88wLHd8OjoksXccr 8exQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750379067; x=1750983867; 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=fyGJQLMYXf8g3bgRQMyCS46dGargJJSbP/8fkcYh0QM=; b=qrW47INv++a+aDzhYK2Np5xyy4B8zzcvRFJJO1P4UIrUrOZdmkx4x7ndMV1euVd2BV 195mbNdjvwY40gIM6dc2A9oen6szVFcN2MsCQx9VAmOFVLHXouMn+pnMndhhrsh6Sa/R kU0Ow23ORplvqF3KR3T0RSwYYf9sYxAan5F711D89tyTDYVzfY+CQZYycTqfL8hz7V5y V8V5fMCzTqF5Hl0hpfMAwsCe5Hbl8ZeWxDsLgVMt22SrNTqfsBkTSFpTkpLyLAq+cU6P TbR8nUF5VsxdMdrqbZZtyunZ24hRF5j8r9hyz9QvI7ne9jS9eUPm4YoNE5goG+sGDqU2 KLDg== X-Forwarded-Encrypted: i=1; AJvYcCW/ZFEroytNRka1G4DKOTmdFa5C395gG/VV6YiSup5WgdYVkpzdH2A8cxp69uBfjvBafOX3ivn8U9QKBNQq@postgresql.org X-Gm-Message-State: AOJu0Yzi4ZdDzVOTa6FBxEgAN/8zpIYHj5iLH4vtOxmcyYN1M9BWYQ9L FeV0ocy+qcbuGSgECfH5FIcc0O0JCC4BUTKCcIFNXCsmji/G/vgSP7rVj1qHv+EU+NRcFk9feaL CTjuS+R36yo2gbwAgpAOoM5zmc4WwQxM= X-Gm-Gg: ASbGncsasrTUGAePIJt9fEgiGyuozzHkmyLwiAz4v/Lfw00xXXDHtdzWEz6rpiD4oKi 6BcMD51ViPsUOg/T/vKKWVNjsGPNHWIUJE4z/d50zVtk5qDjJH3WFW2O+5fO+P7M6HWpVeqEhNI MS/WTWtODLlv1u24oHI/yHG1yJNd63+dtU44DPZx9ua+3y7NEWm+7CxQ== X-Google-Smtp-Source: AGHT+IFLm5QVxfzxq4fZRMCxTwcbE3ytbxi2WChgMlkQJzbWulBp5Ae4AN0Fw17cC5tYgJvQXDofiDHLk+mfwvreCM8= X-Received: by 2002:a17:907:2da9:b0:ade:8df:5b4d with SMTP id a640c23a62f3a-ae0579c04edmr94563366b.15.1750379067138; Thu, 19 Jun 2025 17:24:27 -0700 (PDT) MIME-Version: 1.0 References: <1d12d2-67235980-35-19a406a0@63439497> <1d97ec-6841b600-3-b7b4780@40747246> <28c8bf-68470780-3-51b29480@89454035> <0a617d34-abc3-4090-92a5-ea15d45e36f6@gmail.com> <228607.1750003556@sss.pgh.pa.us> <3f242e-68518380-11-78125f00@134027639> <214ba-6852df80-9-7e0df800@134698799> <21b44-6852e000-3-a556820@87831171> In-Reply-To: From: Alexander Korotkov Date: Fri, 20 Jun 2025 03:24:16 +0300 X-Gm-Features: AX0GCFvk5jsOd4_YJSM5HGByYc4J8Ew50ahoqd6nMAdtw4TNY3WelsKoxvTgBlY Message-ID: Subject: Re: Slot's restart_lsn may point to removed WAL segment after hard restart unexpectedly To: "Hayato Kuroda (Fujitsu)" Cc: Amit Kapila , Vitaly Davydov , vignesh C , Tom Lane , Alexander Lakhin , 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 Dear Kuroda-san, On Thu, Jun 19, 2025 at 2:05=E2=80=AFPM Hayato Kuroda (Fujitsu) wrote: > > > Regarding assertion failure, I've found that assert in > > > PhysicalConfirmReceivedLocation() conflicts with restart_lsn > > > previously set by ReplicationSlotReserveWal(). As I can see, > > > ReplicationSlotReserveWal() just picks fresh XLogCtl->RedoRecPtr lsn. > > > So, it doesn't seems there is a guarantee that restart_lsn never goes > > > backward. The commit in ReplicationSlotReserveWal() even states ther= e > > > is a "chance that we have to retry". > > > > > > > I don't see how this theory can lead to a restart_lsn of a slot going > > backwards. The retry mentioned there is just a retry to reserve the > > slot's position again if the required WAL is already removed. Such a > > retry can only get the position later than the previous restart_lsn. > > We analyzed the assertion failure happened at pg_basebackup/020_pg_receiv= ewal, > and confirmed that restart_lsn can go backward. This meant that Assert() = added > by the ca307d5 can cause crash. > > Background > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > When pg_receivewal starts the replication and it uses the replication slo= t, it > sets as the beginning of the segment where restart_lsn exists, as the sta= rtpoint. > E.g., if the restart_lsn of the slot is 0/B000D0, pg_receivewal requests = WALs > from 0/B00000. > More detail of this behavior, see f61e1dd2 and d9bae531. > > What happened here > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > Based on above theory, walsender sent from the beginning of segment (0/B0= 0000). > When walreceiver receives, it tried to send reply. At that time the flush= ed > location of WAL would be 0/B00000. walsender sets the received lsn as res= tart_lsn > in PhysicalConfirmReceivedLocation(). Here the restart_lsn went backward = (0/B000D0->0/B00000). > > The assertion failure could happen if CHECKPOINT happened at that time. > Attribute last_saved_restart_lsn of the slot was 0/B000D0, but the data.r= estart_lsn > was 0/B00000. It could not satisfy the assertion added in InvalidatePossi= blyObsoleteSlot(). Thank you for your detailed explanation! > Note > =3D=3D=3D=3D > 1. > In this case, starting from the beginning of the segment is not a problem= , because > the checkpoint process only removes WAL files from segments that precede = the > restart_lsn's wal segment. The current segment (0/B00000) will not be rem= oved, > so there is no risk of data loss or inconsistency. > > 2. > A similar pattern applies to pg_basebackup. Both use logic that adjusts t= he > requested streaming position to the start of the segment, and it replies = the > received LSN as flushed. > > 3. > I considered the theory above, but I could not reproduce 040_standby_fail= over_slots_sync > because it is a timing issue. Have someone else reproduced? > > We are still investigating failure caused at 040_standby_failover_slots_s= ync. I didn't manage to reproduce this. But as I see from the logs [1] on mamba that START_REPLICATION command was issued just before assert trap. Could it be something similar to what I described in [2]. Namely: 1. ReplicationSlotReserveWal() sets restart_lsn for the slot. 2. Concurrent checkpoint flushes that restart_lsn to the disk. 3. PhysicalConfirmReceivedLocation() sets restart_lsn of the slot to the beginning of the segment. [1] https://buildfarm.postgresql.org/cgi-bin/show_stage_log.pl?nm=3Dmamba&d= t=3D2025-06-17%2005%3A10%3A36&stg=3Drecovery-check [2] https://www.postgresql.org/message-id/CAPpHfdv3UEUBjsLhB_CwJT0xX9LmN6U%= 2B__myYopq4KcgvCSbTg%40mail.gmail.com ------ Regards, Alexander Korotkov Supabase