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 1rhVnf-00Br8q-Be for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Mar 2024 14:29:48 +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 1rhVnd-0007M5-Ju for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Mar 2024 14:29:46 +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 1rhVnd-0007Lx-27 for pgsql-hackers@lists.postgresql.org; Tue, 05 Mar 2024 14:29:45 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by makus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rhVnU-002z18-7d for pgsql-hackers@lists.postgresql.org; Tue, 05 Mar 2024 14:29:43 +0000 Received: from sogo.postgrespro.ru (unknown [192.168.2.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix/587) with ESMTPS id A8939E20E8C; Tue, 5 Mar 2024 17:29:32 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1709648972; bh=NmrocQp1gHm2J6Kwv3VilrJjRvuHmu/WiWQI/FHJDhM=; h=From:In-Reply-To:References:Date:Cc:To:Message-ID:Subject: User-Agent:From; b=h3dnPULCJJT/UH6peeuVJ826eoGuLCWjNbBWMlXmni5JXfL64QFhxN7Fbfgq4gHqc ggao2l8+nw2V/7opn9nfnl+mr8DwLL6cPXPGEfgLF8t14Wrvrhe8v6/WweohxEiDko MpQLpl6bj4x0WgutgYREQSdGPV6gbDrsgu+X8DSj8L4CLzJ4t7kcQ5CDKykSFgOoht JYbOM5yhKWqj66CVhHViGVNsfW48nqnv0VBbERPuziGqoItl7tAyIFmPe0krc4yEwm dcT5jeJ0fxOJK5DVHwUDDbUb0HKPwtYO8g27VdZTN3oPcjSoDtAaijTfLc2Xf7IqjJ LB2ZaEttWA+Tw== From: =?utf-8?q?=D0=94=D0=B0=D0=B2=D1=8B=D0=B4=D0=BE=D0=B2_=D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9?= In-Reply-To: Content-Type: multipart/alternative; boundary="----=_=-_OpenGroupware_org_NGMime-1023771-1709648972.525257-3------" X-Forward: 93.174.131.141 References: <8fab8-65d74c80-1-2f28e880@39088166> <99df2-65d8d200-1-63c26880@172687752> Date: Tue, 05 Mar 2024 17:29:32 +0300 Cc: pgsql-hackers@lists.postgresql.org To: "Heikki Linnakangas" MIME-Version: 1.0 Message-ID: Subject: =?utf-8?q?Re=3A?= Slow catchup of 2PC (twophase) transactions on replica in LR User-Agent: SOGoMail 5.9.1 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------=_=-_OpenGroupware_org_NGMime-1023771-1709648972.525257-3------ Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Length: 2341 Hi Heikki, Thank you for the reply. On Tuesday, March 05, 2024 12:05 MSK, Heikki Linnakangas wrote: =C2=A0In a nutshell, this changes PREPARE TRANSACTION so that if synchronous=5Fcommit is 'off', the PREPARE TRANSACTION is not fsync'd t= o disk. So if you crash after the PREPARE TRANSACTION has returned, the transaction might be lost. I think that's completely unacceptable.=E2=80= =8B=E2=80=8B=E2=80=8B=E2=80=8B=E2=80=8B You are right, the prepared transaction might be lost after crash. The = same may happen with regular transactions that are not fsync-ed on repl= ica in logical replication by default. The subscription parameter synch= ronous=5Fcommit is OFF by default. I'm not sure, is there some auto rec= overy for regular transactions? I think, the main difference between th= ese two cases - how to manually recover when some PREPARE TRANSACTION o= r COMMIT PREPARED are lost. For regular transactions, some updates or d= eletes in tables on replica may be enough to fix the problem. For twoph= ase transactions, it may be harder to fix it by hands, but it is possib= le, I believe. If you create a custom solution that is based on twophas= e transactions (like multimaster) such auto recovery may happen automat= ically. Another solution is to ignore errors on commit prepared if the = corresponding prepared tx is missing. I don't know other risks that may= happen with async commit of twophase transactions. =C2=A0If you're ok to lose the prepared state of twophase transactions = on crash, why don't you create the subscription with 'two=5Fphase=3Doff' t= o begin with?In usual work, the subscription has two=5Fphase =3D on. I ha= ve to change this option at catchup stage only, but this parameter can = not be altered. There was a patch proposal in past to implement alterin= g of two=5Fphase option, but it was rejected. I think, the recreation o= f the subscription with two=5Fphase =3D off will not work. I believe, async commit for twophase transactions on catchup will signi= ficantly improve the catchup performance. It is worth to think about su= ch feature. P.S. We might introduce a GUC option to allow async commit for twophase= transactions. By default, sync commit will be applied for twophase tra= nsactions, as it is now. With best regards, Vitaly Davydov ------=_=-_OpenGroupware_org_NGMime-1023771-1709648972.525257-3------ Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Length: 2668 Hi Heikki,

Thank you for the reply.

On Tues= day, March 05, 2024 12:05 MSK, Heikki Linnakangas <hlinnaka@iki.fi&g= t; wrote:
 
In a nutshell, this changes PREPARE TRANSA= CTION so that if
synchronous=5Fcommit is 'off', the PREPARE TRANSA= CTION is not fsync'd to
disk. So if you crash after the PREPARE TR= ANSACTION has returned, the
transaction might be lost. I think tha= t's completely unacceptable.=E2=80=8B=E2=80=8B=E2=80=8B=E2=80=8B=E2=80=8B=

You are right, the prepared transaction might be los= t after crash. The same may happen with regular transactions that are n= ot fsync-ed on replica in logical replication by default. The subscript= ion parameter synchronous=5Fcommit is OFF by default. I'm not sure, is = there some auto recovery for regular transactions? I think, the main di= fference between these two cases - how to manually recover when some PR= EPARE TRANSACTION or COMMIT PREPARED are lost. For regular transactions= , some updates or deletes in tables on replica may be enough to fix the= problem. For twophase transactions, it may be harder to fix it by hand= s, but it is possible, I believe. If you create a custom solution that = is based on twophase transactions (like multimaster) such auto recovery= may happen automatically. Another solution is to ignore errors on comm= it prepared if the corresponding prepared tx is missing. I don't know o= ther risks that may happen with async commit of twophase transactions.<= br /> 
If you're ok to lose the prepared state of twophase = transactions on
crash, why don't you create the subscription with = 'two=5Fphase=3Doff' to
begin with?
In usual work, the = subscription has two=5Fphase =3D on. I have to change this option at ca= tchup stage only, but this parameter can not be altered. There was a pa= tch proposal in past to implement altering of two=5Fphase option, but i= t was rejected. I think, the recreation of the subscription with two=5F= phase =3D off will not work.

I believe, async commit for two= phase transactions on catchup will significantly improve the catchup pe= rformance. It is worth to think about such feature.

P.S. We = might introduce a GUC option to allow async commit for twophase transac= tions. By default, sync commit will be applied for twophase transaction= s, as it is now.

With best regards,
Vitaly Davydov ------=_=-_OpenGroupware_org_NGMime-1023771-1709648972.525257-3--------