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 1t6SGK-00D66i-FB for pgsql-hackers@arkaria.postgresql.org; Thu, 31 Oct 2024 10:18:45 +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 1t6SGI-00HObn-LU for pgsql-hackers@arkaria.postgresql.org; Thu, 31 Oct 2024 10:18:43 +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 1t6SGI-00HObd-54 for pgsql-hackers@lists.postgresql.org; Thu, 31 Oct 2024 10:18:42 +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 1t6SGE-003llt-IW for pgsql-hackers@postgresql.org; Thu, 31 Oct 2024 10:18:40 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1730369915; bh=uSDYDdEyalFiCdy5Q+gYakSe2RRaAYqxmNNLiCzx9GY=; h=From:To:User-Agent:Date:Subject:Message-ID:From; b=WakxqxkNQukh4N5we4quYfSWIeV19cWBLtpDeIlRKG9CJkkQYzyXpnjhQLxAWINZ8 unwduAQPkje0gSeEHGgf7XHVzPnoTcNsFk17oMaL6jU4J2gHBtZMgK9vx94J9zR4DD tJ7MLcfVjB2qSuqn7bx//SUhjV73WIkA4YBjCmsRK7dx+DFg4TKwKCJ/Zg1jFstZ/s ExyOqe9ytKIx7k64/xZcVkJV9se/t+7Lq+noxf9+LbgfsBIUnGg2OENROLvFzBLkdS fdtCndn0QhP7kQw3z/CFxDkxUH7cuiywWW1B3lX29QcaHLv8PhZU7cQ2yW3qG4tJfm bwgP85+vmnFRg== 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 CC24260139 for ; Thu, 31 Oct 2024 13:18:34 +0300 (MSK) From: "Vitaly Davydov" To: pgsql-hackers@postgresql.org User-Agent: SOGoMail 5.11.2 MIME-Version: 1.0 Date: Thu, 31 Oct 2024 13:18:34 +0300 Subject: Slot's =?utf-8?q?restart=5Flsn?= may point to removed WAL segment after hard restart unexpectedly Message-ID: <1d12d2-67235980-35-19a406a0@63439497> X-Forward: 46.31.29.164 Content-Type: multipart/alternative; boundary="----=_=-_OpenGroupware_org_NGMime-1905362-1730369914.708235-13------" X-KSMG-AntiPhishing: NotDetected, bases: 2024/10/31 09:07: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/10/31 05:12:00 #26797869 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-1905362-1730369914.708235-13------ Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Length: 2214 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 ------=_=-_OpenGroupware_org_NGMime-1905362-1730369914.708235-13------ Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Length: 2364

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 checkpoint. At the end of checkpoint, old WAL segm= ents are recycled or removed from disk, if they are not kept by slot's = restart=5Flsn values.

 

If an existing physical slot i= s advanced in the middle of checkpoint execution, WAL segments, which a= re related to saved on disk restart LSN may be removed. It is because t= he calculation of the replication slot miminal LSN is occured at the en= d of checkpoint, prior to old WAL segments removal. If to hard stop (pg= =5Fstl -m immediate) the postgres instance right after checkpoint and t= o restart it, the slot's restart=5Flsn may point to the removed WAL seg= ment. I believe, such behaviour is not good.

 

The doc= [0] describes that restart=5Flsn may be set to the some past value aft= er reload. There is a discussion [1] on pghackers where such behaviour = is discussed. The main reason of not flushing physical slots on advanci= ng is a performance reason. I'm ok with such behaviour, except of that = the corresponding WAL segments should not be removed.

 

<= p>I propose to keep WAL segments by saved on disk (flushed) restart=5Fl= sn of slots. Add a new field restart=5Flsn=5Fflushed into ReplicationSl= ot structure. Copy restart=5Flsn to restart=5Flsn=5Fflushed in SaveSlot= ToPath. It doesn't change the format of storing the slot contents on di= sk. 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:

  • Add some delay in CheckPointBuffers (pg=5Fusl= eep) to emulate long checkpoint execution.
  • Execute checkpoint a= nd pg=5Freplication=5Fslot=5Fadvance right after starting of the checkp= oint from another connection.
  • Hard restart the server right aft= er checkpoint completion.
  • After restart slot's restart=5Flsn ma= y point to removed WAL segment.

The proposed patch fixes it= .

 

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

------=_=-_OpenGroupware_org_NGMime-1905362-1730369914.708235-13--------