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 1tE8bP-005Pk2-DW for pgsql-hackers@arkaria.postgresql.org; Thu, 21 Nov 2024 14:56:16 +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 1tE8bM-00GZ2n-R4 for pgsql-hackers@arkaria.postgresql.org; Thu, 21 Nov 2024 14:56:12 +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 1tE8bM-00GZ2d-8j for pgsql-hackers@lists.postgresql.org; Thu, 21 Nov 2024 14:56:12 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tE8bH-0037XT-E3 for pgsql-hackers@postgresql.org; Thu, 21 Nov 2024 14:56:11 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1732200966; bh=eUPOsfnwCqMCim714fRjUIPKRPLh5kCfjAu2A6Ow3XU=; h=From:In-Reply-To:References:Date:Cc:To:Message-ID:Subject: User-Agent:From; b=Wpg8aE7byJzhdPX6ZaOC8SJrt8DyMyuM4iH5V6t7lhsbzhgo0DlskGQangeMwrcHW FJemFbMrj9qQJcXMVPVqKpllIqZJaR+l+EYDZLlv9WdfPbLEISNAfGrh2mOAA98TZy QetjKSavFKAZf/kMDOwp54kXXF1hTeeeatPh0R1+ChIH6a7D5xalgqSSrGenCwS3AV b76HOslwUKIAeWk2UMH3Qa66l/6XqfFJy070VxL/c/EdkaaiyMfsVVxGOD7R1/Deh8 y3NvQK7PMhyRT3l451ET0c1fE/iaVcn9LvU7R1r2Y7Eoyu9lZ+DLpRJETQxuJKyPsp DoMhpG0fHM/DA== 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 BDF1460A9C; Thu, 21 Nov 2024 17:56:05 +0300 (MSK) From: "Vitaly Davydov" In-Reply-To: <683f3c82-d38e-436d-88fd-27722af62005@vondra.me> Content-Type: multipart/alternative; boundary="----=_=-_OpenGroupware_org_NGMime-1029769-1732200965.635923-11------" X-Forward: 93.174.131.141 References: <1d12d2-67235980-35-19a406a0@63439497> <683f3c82-d38e-436d-88fd-27722af62005@vondra.me> Date: Thu, 21 Nov 2024 17:56:05 +0300 Cc: "pgsql-hackers" To: tomas@vondra.me 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 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/21 09:49:00 #26878913 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-1029769-1732200965.635923-11------ Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Length: 4551 Hi Tomas, =C2=A0 Thank you for the reply and your interest to the investigation. On Wednesday, November 20, 2024 20:24 MSK, Tomas Vondra wrote: =C2=A0 > If an existing physical slot is advanced in the middle of checkpoint > execution, WAL segments, which are related to saved on disk restart L= SN > may be removed. It is because the calculation of the replication slot > miminal LSN is occured at the end of checkpoint, prior to old WAL > segments removal. If to hard stop (pg=5Fstl -m immediate) the postgre= s > instance right after checkpoint and to restart it, the slot's > restart=5Flsn may point to the removed WAL segment. I believe, such > behaviour is not good. >=C2=A0 Not sure I 100% follow, but let me rephrase, just so that we're on the same page. CreateCheckPoint() does this: ... something ... CheckPointGuts(checkPoint.redo, flags); ... something ... RemoveOldXlogFiles(=5FlogSegNo, RedoRecPtr, recptr, checkPoint.ThisTimeLineID); The slots get synced in CheckPointGuts(), so IIUC you're saying if the slot gets advanced shortly after the sync, the RemoveOldXlogFiles() may remove still-needed WAL, because we happen to consider a fresh restart=5Flsn when calculating the logSegNo. Is that right? They key action here is to restart the instance with -m immediate (or k= ill it and start it again) right after checkpoint. After restart, the s= lot's restart=5Flsn will be read from the disk and address to the remov= ed WAL segment, if it's LSN was davanced enought to switch to a new WAL= segment. =C2=A0 I tried to reproduce the issue using a stress test (checkpoint+restart in a loop), but so far without success :-( Can you clarify where exactly you added the pg=5Fusleep(), and how long are the waits you added? I wonder if the sleep is really needed, considering the checkpoints are spread anyway. Also, what you mean by "hard reset"? =C2=A0 I added pg=5Fusleep as show below (in CheckPointBuffers function): =C2=A0 CheckPointBuffers(int flags) { =C2=A0 =C2=A0 BufferSync(flags); + =C2=A0 =C2=A0pg=5Fusleep(10000000); } =C2=A0 Below is the instruction how I run my test (pg=5Fusleep should be added= to the code): =C2=A0 CONSOLE> initdb -D pgdata CONSOLE> pg=5Fctl -D pgdata -l logfile start ... open two psql terminals and connect to the database (lets call them= as PSQL-1, PSQL-2) PSQL-1> =C2=A0select pg=5Fcreate=5Fphysical=5Freplication=5Fslot('myslo= t', true, false); CONSOLE> pgbench -i -s 10 postgres # create some WAL records PSQL-1> =C2=A0checkpoint; -- press ENTER key and go to PSQL-2 console a= nd execute next line in 1 second PSQL-2> =C2=A0select pg=5Freplication=5Fslot=5Fadvance('myslot', pg=5Fc= urrent=5Fwal=5Flsn()); -- advance repslot during checkpoint ... wait for checkpoint to complete CONSOLE> pg=5Fctl -D pgdata -m immediate stop=C2=A0 CONSOLE> pg=5Fctl -D pgdata start PSQL-1> =C2=A0\c PSQL-1> =C2=A0create extension pg=5Fwalinspect; PSQL-1> =C2=A0select pg=5Fget=5Fwal=5Frecord=5Finfo(restart=5Flsn) from= pg=5Freplication=5Fslots where slot=5Fname =3D 'myslot'; ERROR: =C2=A0requested WAL segment pg=5Fwal/000000010000000000000001 ha= s already been removed =C2=A0 I'm trying to create a perl test to reproduce it. Please, give me some = time to create the test script. =C2=A0 I kept thinking about this (sorry it's this incremental), particularly if this applies to logical replication too. And AFAICS it does not, or at least not to this extent. =C2=A0 Yes, it is not applied to logical replication, because logical slot is = synced when advancing. =C2=A0 eah, a simple way to fix this might be to make sure we don't use the required LSN value set after CheckPointReplicationSlots() to remove WAL= . AFAICS the problem is KeepLogSeg() gets the new LSN value by: keep =3D XLogGetReplicationSlotMinimumLSN(); Let's say we get the LSN before calling CheckPointGuts(), and then pass it to KeepLogSeg, so that it doesn't need to get the fresh value. =C2=A0 Yes, it is another solution and it can fix the problem. The question - = which solution to choose. Well, I prefer to add a new in-memory state v= ariable in the slot structure. Such variable may be useful if we want t= o check whether the slot data is synced or not. The calculation of the = keep value before CheckPointGuts(), IMHO, requires to change signatures= of a number of functions. I may prepare a new patch where your solutio= n is implemented. =C2=A0 I'm sorry, if I missed to answer to some other questions. I will answer= later. =C2=A0 With best regards, Vitaly =C2=A0 ------=_=-_OpenGroupware_org_NGMime-1029769-1732200965.635923-11------ Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Length: 11938

Hi Tomas,

 

Thank you for the reply and your = interest to the investigation.

On Wednesday, November 20, 2024 2= 0:24 MSK, Tomas Vondra <tomas@vondra.me> wrote:
 

> If an existing physical slot is advanced in the middle o= f checkpoint
> execution, WAL segments, which are related to save= d on disk restart LSN
> may be removed. It is because the calcula= tion of the replication slot
> miminal LSN is occured at the end = of checkpoint, prior to old WAL
> segments removal. If to hard st= op (pg=5Fstl -m immediate) the postgres
> instance right after ch= eckpoint and to restart it, the slot's
> restart=5Flsn may point = to the removed WAL segment. I believe, such
> behaviour is not go= od.
> 

Not sure I 100% follow, but let me rephrase, j= ust so that we're on the
same page. CreateCheckPoint() does this:
... something ...

CheckPointGuts(checkPoint.redo, flags);
... something ...

RemoveOldXlogFiles(=5FlogSegNo, RedoRecP= tr, recptr,
checkPoint.ThisTimeLineID);

The slots get synced = in CheckPointGuts(), so IIUC you're saying if the
slot gets advanced= shortly after the sync, the RemoveOldXlogFiles() may
remove still-n= eeded WAL, because we happen to consider a fresh
restart=5Flsn when = calculating the logSegNo. Is that right?

They key ac= tion here is to restart the instance with -m immediate (or kill it and = start it again) right after checkpoint. After restart, the slot's resta= rt=5Flsn will be read from the disk and address to the removed WAL segm= ent, if it's LSN was davanced enought to switch to a new WAL segment.

 


I tried to reproduce the issue using = a stress test (checkpoint+restart
in a loop), but so far without suc= cess :-(

Can you clarify where exactly you added the pg=5Fusleep= (), and how long
are the waits you added? I wonder if the sleep is r= eally needed,
considering the checkpoints are spread anyway. Also, w= hat you mean by
"hard reset"?
 

I added p= g=5Fusleep as show below (in CheckPointBuffers function):

 <= /p>

CheckPointBuffers(int flags)
{
    BufferSync(fla= gs);
+    pg=5Fusleep(10000000);
}
 

Belo= w is the instruction how I run my test (pg=5Fusleep should be added to = the code):

 

CONSOLE> initdb -D pgdata
CONSOLE&g= t; pg=5Fctl -D pgdata -l logfile start
... open two psql terminals a= nd connect to the database (lets call them as PSQL-1, PSQL-2)
PSQL-1= >  select pg=5Fcreate=5Fphysical=5Freplication=5Fslot('myslot',= true, false);
CONSOLE> pgbench -i -s 10 postgres # create some W= AL records
PSQL-1>  checkpoint; -- press ENTER key and go to= PSQL-2 console and execute next line in 1 second
PSQL-2>  s= elect pg=5Freplication=5Fslot=5Fadvance('myslot', pg=5Fcurrent=5Fwal=5F= lsn()); -- advance repslot during checkpoint
... wait for checkpoint= to complete
CONSOLE> pg=5Fctl -D pgdata -m immediate stop <= br>CONSOLE> pg=5Fctl -D pgdata start
PSQL-1>  \c
PSQL-= 1>  create extension pg=5Fwalinspect;
PSQL-1>  selec= t pg=5Fget=5Fwal=5Frecord=5Finfo(restart=5Flsn) from pg=5Freplication=5F= slots where slot=5Fname =3D 'myslot';
ERROR:  requested WAL seg= ment pg=5Fwal/000000010000000000000001 has already been removed

&= nbsp;

I'm trying to create a perl test to reproduce it. Please, g= ive me some time to create the test script.

 

= I kept thinking about this (sorry it's this incremental), particularly<= /span>
if this applies to logical replication too. And AFAICS it = does not, or
at least not to this extent.

 

Yes, it is not applied to logical replica= tion, because logical slot is synced when advancing.

 

eah, a simple way to fix this might be to make sure we don't u= se the
required LSN value set after CheckPointReplicationS= lots() to remove WAL.
AFAICS the problem is KeepLogSeg() g= ets the new LSN value by:

keep =3D XLogGetReplicationS= lotMinimumLSN();

Let's say we get the LSN before calli= ng CheckPointGuts(), and then pass
it to KeepLogSeg, so th= at it doesn't need to get the fresh value.

 

Yes, it is another solution and it can fix the problem. T= he question - which solution to choose. Well, I prefer to add a new in-= memory state variable in the slot structure. Such variable may be usefu= l if we want to check whether the slot data is synced or not. The calcu= lation of the keep value before CheckPointGuts(), IMHO, requires to cha= nge signatures of a number of functions. I may prepare a new patch wher= e your solution is implemented.

 

I'm sorry,= if I missed to answer to some other questions. I will answer later.

 

With best regards,

Vitaly


 

------=_=-_OpenGroupware_org_NGMime-1029769-1732200965.635923-11--------