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 1tDkwx-002yUE-GE for pgsql-hackers@arkaria.postgresql.org; Wed, 20 Nov 2024 13:40:56 +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 1tDkww-005kMQ-3H for pgsql-hackers@arkaria.postgresql.org; Wed, 20 Nov 2024 13:40:54 +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 1tDkwv-005kMI-Cf for pgsql-hackers@lists.postgresql.org; Wed, 20 Nov 2024 13:40:53 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tDkwq-002sr8-UR for pgsql-hackers@postgresql.org; Wed, 20 Nov 2024 13:40:51 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1732110045; bh=vut7cacjoVjRwG8VcsJPV6T0AzUjrwNubne8MfMF9pc=; h=From:In-Reply-To:References:Date:To:Message-ID:Subject:User-Agent: From; b=MntXBE8trTs173oTmT8IyAoywJfjh1m8VwMfkZqCT9Z3mhuBn1IFf92bc5oCzoPE+ XEqCygK6FElMs5USNEfNGyEyDYND29MhhlgdXcZlUBEWUWGtA3FsGIL1uVmx2YqhOR Of6EEVUFCv8243MciTRahjFqKmtQf+4Y129Rhv31NHIqL1RtVxHFqfoG5iUO3UoTbV nv5bsCIXrS0iu6sJH0lluWjOPhAeohO/U5rLGUfHRPT5n7lQC/ekEa6vb5Rqd5j01d 5RGTJmN34vU8RYYMisFaGOsU17OYhIDeE3Gu9cLq61puERQ8/bbLfiQe1gtl+RadMS mSnDYm56Rfavg== Received: from sogo.postgrespro.ru (unknown [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 9492060265 for ; Wed, 20 Nov 2024 16:40:45 +0300 (MSK) From: "Vitaly Davydov" In-Reply-To: <2802b6-672cc100-29-48025b80@131204041> Content-Type: multipart/alternative; boundary="----=_=-_OpenGroupware_org_NGMime-851205-1732110045.487621-7------" X-Forward: 93.174.131.141 References: <1d12d2-67235980-35-19a406a0@63439497> <1d2043-67235d00-5-2e3223c0@131762497> <2802b6-672cc100-29-48025b80@131204041> Date: Wed, 20 Nov 2024 16:40:45 +0300 To: "pgsql-hackers" MIME-Version: 1.0 Message-ID: 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.11.2 X-KSMG-AntiPhishing: NotDetected, bases: 2024/11/20 12:48:00 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: 2024/11/20 11:58:00 #26876499 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 ------=_=-_OpenGroupware_org_NGMime-851205-1732110045.487621-7------ Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Length: 5368 Dear Hackers, =C2=A0 To ping the topic, I'd like to clarify what may be wrong with the idea = described here, because I do not see any interest from the community. T= he topic is related to physical replication. The primary idea is to def= ine the horizon of WAL segments (files) removal based on saved on disk = restart LSN values. Now, the WAL segment removal horizon is calculated = based on the current restart LSN values of slots, that can not be saved= on disk at the time of the horizon calculation. The case take place wh= en a slot is advancing during checkpoint as described earlier in the to= pic. =C2=A0 Such behaviour is not a problem when slots are used only for physical r= eplication in a conventional way. But it may be a problem when physical= slot is used for some other goals. For example, I have an extension wh= ich keeps the WAL using physical replication slots. It creates a new ph= ysical slot and advances it as needed. After restart, it can use restar= t lsn of the slot to read WAL from this LSN. In this case, there is no = guarantee that restart lsn will point to an existing WAL segment. =C2=A0 The advantage of the current behaviour is that it requires a little bit= less WAL to keep. The disadvantage is that physical slots do not guara= ntee WAL keeping starting from its' restart lsns in general. =C2=A0 I would be happy to get some advice, whether I am on the right or wrong= way. =C2=A0Thank you in advance. =C2=A0 With best regards, Vitaly On Thursday, November 07, 2024 16:30 MSK, "Vitaly Davydov" wrote: =C2=A0 Dear Hackers, =C2=A0 I'd like to introduce an improved version of my patch (see the attached= file). My original idea was to take into account saved on disk restart= =5Flsn (slot=E2=86=92restart=5Flsn=5Fflushed) for persistent slots when= removing WAL segment files. It helps tackle errors like: ERROR: reques= ted WAL segment 000...0AA has already been removed. =C2=A0 Improvements: * flushed=5Frestart=5Flsn is used only for RS=5FPERSISTENT slots. * Sa= ve physical slot on disk when advancing only once - if restart=5Flsn=5F= flushed is invalid. It is needed because slots with invalid restart LSN= are not used when calculating oldest LSN for WAL truncation. Once rest= art=5Flsn becomes valid, it should be saved to disk immediately to upda= te restart=5Flsn=5Fflushed. Regression tests seems to be ok except: * recovery/t/001=5Fstream=5Frep.pl (checkpoint is needed) * recovery/t= /019=5Freplslot=5Flimit.pl (it seems, slot was invalidated, some adjust= ments are needed) * pg=5Fbasebackup/t/020=5Fpg=5Freceivewal.pl (not sur= e about it) =C2=A0 There are some problems: * More WAL segments may be kept. It may lead to invalidations of slots= in some tests (recovery/t/019=5Freplslot=5Flimit.pl). A couple of test= s should be adjusted. =C2=A0 With best regards, Vitaly Davydov On Thursday, October 31, 2024 13:32 MSK, "Vitaly Davydov" wrote: =C2=A0 Sorry, attached the missed patch. On Thursday, October 31, 2024 13:18 MSK, "Vitaly Davydov" wrote: Dear Hackers, =C2=A0 I'd like to discuss a problem with replication slots's restart LSN. Phy= sical slots are saved to disk at the beginning of checkpoint. At the en= d of checkpoint, old WAL segments are recycled or removed from disk, if= they are not kept by slot's restart=5Flsn values. =C2=A0 If an existing physical slot is advanced in the middle of checkpoint ex= ecution, WAL segments, which are related to saved on disk restart LSN m= ay be removed. It is because the calculation of the replication slot mi= minal LSN is occured at the end of checkpoint, prior to old WAL segment= s removal. If to hard stop (pg=5Fstl -m immediate) the postgres instanc= e right after checkpoint and to restart it, the slot's restart=5Flsn ma= y point to the removed WAL segment. I believe, such behaviour is not go= od. =C2=A0 The doc [0] describes that restart=5Flsn may be set to the some past va= lue after reload. There is a discussion [1] on pghackers where such beh= aviour is discussed. The main reason of not flushing physical slots on = advancing is a performance reason. I'm ok with such behaviour, except o= f that the corresponding WAL segments should not be removed. =C2=A0 I propose to keep WAL segments by saved on disk (flushed) restart=5Flsn= of slots. Add a new field restart=5Flsn=5Fflushed into ReplicationSlot= structure. Copy restart=5Flsn to restart=5Flsn=5Fflushed in SaveSlotTo= Path. It doesn't change the format of storing the slot contents on disk= . I attached a patch. It is not yet complete, but demonstate a way to s= olve the problem. =C2=A0 I reproduced the problem by the following way: * Add some delay in CheckPointBuffers (pg=5Fusleep) to emulate long ch= eckpoint execution. * Execute checkpoint and pg=5Freplication=5Fslot=5F= advance right after starting of the checkpoint from another connection.= * Hard restart the server right after checkpoint completion. * After r= estart slot's restart=5Flsn may point to removed WAL segment. The proposed patch fixes it. =C2=A0 [0] https://www.postgresql.org/docs/current/logicaldecoding-explanation= .html [1] https://www.postgresql.org/message-id/flat/059cc53a-8b14-653a-a24d-= 5f867503b0ee%40postgrespro.ru =C2=A0 ------=_=-_OpenGroupware_org_NGMime-851205-1732110045.487621-7------ Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Length: 6700

Dear Hackers,

 

To ping the topic, I'd like t= o clarify what may be wrong with the idea described here, because I do = not see any interest from the community. The topic is related to physic= al replication. The primary idea is to define the horizon of WAL segmen= ts (files) removal based on saved on disk restart LSN values. Now, the = WAL segment removal horizon is calculated based on the current restart = LSN values of slots, that can not be saved on disk at the time of the h= orizon calculation. The case take place when a slot is advancing during= checkpoint as described earlier in the topic.

 

Such = behaviour is not a problem when slots are used only for physical replic= ation in a conventional way. But it may be a problem when physical slot= is used for some other goals. For example, I have an extension which k= eeps the WAL using physical replication slots. It creates a new physica= l slot and advances it as needed. After restart, it can use restart lsn= of the slot to read WAL from this LSN. In this case, there is no guara= ntee that restart lsn will point to an existing WAL segment.

&nbs= p;

The advantage of the current behaviour is that it requires a l= ittle bit less WAL to keep. The disadvantage is that physical slots do = not guarantee WAL keeping starting from its' restart lsns in general.

 

I would be happy to get some advice, whether I am on = the right or wrong way.  Thank you in advance.

 

= With best regards,

Vitaly


On Thursday, November 07, 202= 4 16:30 MSK, "Vitaly Davydov" <v.davydov@postgrespro.ru> wrote: 

Dear Hackers,

 

I'd like to introduce= an improved version of my patch (see the attached file). My original i= dea was to take into account saved on disk restart=5Flsn (slot=E2=86=92= restart=5Flsn=5Fflushed) for persistent slots when removing WAL segment= files. It helps tackle errors like: ERROR: requested WAL segment 000...0AA= has already been removed.

 

Improvement= s:

  • flushed=5Frestart=5Flsn is used only for RS=5FPERSISTENT = slots.
  • Save physical slot on disk when advancing only once - if= restart=5Flsn=5Fflushed is invalid. It is needed because slots with in= valid restart LSN are not used when calculating oldest LSN for WAL trun= cation. Once restart=5Flsn becomes valid, it should be saved to disk im= mediately to update restart=5Flsn=5Fflushed.

Regression tes= ts seems to be ok except:

  • recovery/t/001=5Fstream=5Frep.pl (= checkpoint is needed)
  • recovery/t/019=5Freplslot=5Flimit.pl (it = seems, slot was invalidated, some adjustments are needed)
  • pg=5F= basebackup/t/020=5Fpg=5Freceivewal.pl (not sure about it)

&= nbsp;

There are some problems:

  • More WAL segments may b= e kept. It may lead to invalidations of slots in some tests (recovery/t= /019=5Freplslot=5Flimit.pl). A couple of tests should be adjusted.
  • =

 

With best regards,

Vitaly Davydov


=
On Thursday, October 31, 2024 13:32 MSK, "Vitaly Davydov" <v.dav= ydov@postgrespro.ru> wrote:

 

Sorry, attached the= missed patch.

On Thursday, October 31, 2024 13:18 MSK, "Vitaly = Davydov" <v.davydov@postgrespro.ru> wrote:

Dear Hackers,

 

I'd like to discuss a problem with replication slots= 's restart LSN. Physical slots are saved to disk at the beginning of ch= eckpoint. At the end of checkpoint, old WAL segments are recycled or re= moved from disk, if they are not kept by slot's restart=5Flsn values.

 

If an existing physical slot is advanced in the middl= e of checkpoint execution, WAL segments, which are related to saved on = disk restart LSN may be removed. It is because the calculation of the r= eplication slot miminal LSN is occured at the end of checkpoint, prior = to old WAL segments removal. If to hard stop (pg=5Fstl -m immediate) th= e postgres instance right after checkpoint and to restart it, the slot'= s restart=5Flsn may point to the removed WAL segment. I believe, such b= ehaviour is not good.

 

The doc [0] describes that res= tart=5Flsn may be set to the some past value after reload. There is a d= iscussion [1] on pghackers where such behaviour is discussed. The main = reason of not flushing physical slots on advancing is a performance rea= son. I'm ok with such behaviour, except of that the corresponding WAL s= egments should not be removed.

 

I propose to keep WAL= segments by saved on disk (flushed) restart=5Flsn of slots. Add a new = field restart=5Flsn=5Fflushed into ReplicationSlot structure. Copy rest= art=5Flsn to restart=5Flsn=5Fflushed in SaveSlotToPath. It doesn't chan= ge the format of storing the slot contents on disk. I attached a patch.= It is not yet complete, but demonstate a way to solve the problem.

=

 

I reproduced the problem by the following way:

    <= li>Add some delay in CheckPointBuffers (pg=5Fusleep) to emulate long ch= eckpoint execution.
  • Execute checkpoint and pg=5Freplication=5Fs= lot=5Fadvance right after starting of the checkpoint from another conne= ction.
  • Hard restart the server right after checkpoint completio= n.
  • After restart slot's restart=5Flsn may point to removed WAL = segment.

The proposed patch fixes it.

 

[0= ] https://www.postgresql.org/docs/current/logicaldecoding-explanation.h= tml
[1] https://www.postgresql.org/message-id/flat/059cc53a-8b14-653= a-a24d-5f867503b0ee%40postgrespro.ru




&nb= sp;

------=_=-_OpenGroupware_org_NGMime-851205-1732110045.487621-7--------