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 1rdZ5Y-006Fvc-VU for pgsql-hackers@arkaria.postgresql.org; Fri, 23 Feb 2024 17:11:58 +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 1rdZ5W-002Lb7-I8 for pgsql-hackers@arkaria.postgresql.org; Fri, 23 Feb 2024 17:11:55 +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 1rdZ5V-002Laz-VL for pgsql-hackers@lists.postgresql.org; Fri, 23 Feb 2024 17:11:54 +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 1rdZ5R-000dKs-6R for pgsql-hackers@lists.postgresql.org; Fri, 23 Feb 2024 17:11:52 +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 C765EE20EC3; Fri, 23 Feb 2024 20:11:46 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1708708306; bh=WQhmiiIio56tB6RHDWWFsHo/dd4Jgh6kMcJS0M+dTWE=; h=From:In-Reply-To:References:Date:Cc:To:Message-ID:Subject: User-Agent:From; b=QlfIsS1h1gTKfDQ6XYCdazresS31og4dPPbURWuIZfpIhCy8UulcMq3ZGt406lGGk 0OdU9ScVusuMZM7J5sOiYM5hly0IP3kd4BaS7goZua33K0/lBf4akWeP0LVzL5noN4 3wTDCNRG/HcbdhhdvmpIHFZ1oeWjP2n9pRrVYgjd2pnOs1yg672xAEkuSSi8mYpDxg YU7XoG3N/jh4qpqxg1utw3YH3c3rmD4L6peTTJ9MHYgszovqmr4+xA361lTrjX4DW3 HATuckichkrk5JqasJOFAIwSm7DMgaxWQCwVuMpHu4FOP6G9IYqsmwFzFwP5OotG52 LKvQ4K2CBs0QA== 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-630258-1708708306.652385-0------" X-Forward: 46.31.31.81 References: <8fab8-65d74c80-1-2f28e880@39088166> Date: Fri, 23 Feb 2024 20:11:46 +0300 Cc: pgsql-hackers@lists.postgresql.org To: "Amit Kapila" MIME-Version: 1.0 Message-ID: <99df2-65d8d200-1-63c26880@172687752> 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-630258-1708708306.652385-0------ Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Length: 2818 Hi Amit, Amit Kapila wrote: I don't see we do anything specific for 2PC transactions to make them b= ehave differently than regular transactions with respect to synchronous= =5Fcommit setting. What makes you think so? Can you pin point the code = you are referring to?Yes, sure. The function RecordTransactionCommitPre= pared is called on prepared transaction commit (twophase.c). It calls X= LogFlush unconditionally. The function RecordTransactionCommit (for reg= ular transactions, xact.c) calls XLogFlush if synchronous=5Fcommit > OF= F, otherwise it calls=C2=A0XLogSetAsyncXactLSN. There is some comment in RecordTransactionCommitPrepared (by Bruce Momj= ian) that shows that async commit is not supported yet: /* * We don't currently try to sleep before flush here ... nor is there an= y * support for async commit of a prepared xact (the very idea is probabl= y * a contradiction) */ /* Flush XLOG to disk */ XLogFlush(recptr); Right, I think for this we need to implement parallel apply.Yes, parall= el apply is a good point. But, I believe, it will not work if asynchron= ous commit is not supported. You have only one receiver process which s= hould dispatch incoming messages to parallel workers. I guess, you will= never reach such rate of parallel execution on replica as on the maste= r with multiple backends. =C2=A0 Can you be a bit more specific about what exactly you have in mind to a= chieve the above solutions?My proposal is to implement async commit for= 2PC transactions as it is for regular transactions. It should signific= antly speedup the catchup process. Then, think how to apply in parallel= , which is much diffcult to do. The current problem is to get 2PC state= from the WAL on commit prepared. At this moment, the WAL is not flushe= d yet, commit function waits until WAL with 2PC state is to be flushed.= I just tried to do it in my sandbox and found such a problem. Inabilit= y to get 2PC state from unflushed WAL stops me right now. I think about= possible solutions. The idea with enableFsync is not a suitable solution, in general, I thi= nk. I just pointed it as an alternate idea. You just do enableFsync =3D= false before prepare or commit prepared and do enableFsync =3D true af= ter these functions. In this case, 2PC records will not be fsync-ed, bu= t FlushPtr will be increased. Thus, 2PC state can be read from WAL on c= ommit prepared without waiting. To make it work correctly, I guess, we = have to do some additional work to keep more wal on the master and filt= er some duplicate transactions on the replica, if replica restarts duri= ng catchup. =E2=80=8B=E2=80=8B=E2=80=8B=E2=80=8B=E2=80=8B=E2=80=8B With best regards, =E2=80=8B=E2=80=8B=E2=80=8B=E2=80=8B=E2=80=8B=E2=80=8BVitaly Davydov =C2=A0 ------=_=-_OpenGroupware_org_NGMime-630258-1708708306.652385-0------ Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Length: 3075 Hi Amit,

Amit Kapila <amit.kapila16@gmail.com>= ; wrote:

I don't see we do anything spec= ific for 2PC transactions to make them behave differently than regular = transactions with respect to synchronous=5Fcommit setting. What makes y= ou think so? Can you pin point the code you are referring to?

Yes, sure. The function RecordTransactionCommitPrepared is calle= d on prepared transaction commit (twophase.c). It calls XLogFlush uncon= ditionally. The function RecordTransactionCommit (for regular transacti= ons, xact.c) calls XLogFlush if synchronous=5Fcommit > OFF, otherwis= e it calls XLogSetAsyncXactLSN.

There is some comment i= n RecordTransactionCommitPrepared (by Bruce Momjian) that shows that as= ync commit is not supported yet:
/*
* We don't currently try = to sleep before flush here ... nor is there any
* support for asyn= c commit of a prepared xact (the very idea is probably
* a contrad= iction)
*/
/* Flush XLOG to disk */
XLogFlush(recptr);

Right, I think for this we need to implement parallel appl= y.

Yes, parallel apply is a good point. But, I believe,= it will not work if asynchronous commit is not supported. You have onl= y one receiver process which should dispatch incoming messages to paral= lel workers. I guess, you will never reach such rate of parallel execut= ion on replica as on the master with multiple backends.
 

Can you be a bit more specific about what exactly you have i= n mind to achieve the above solutions?

My proposal is t= o implement async commit for 2PC transactions as it is for regular tran= sactions. It should significantly speedup the catchup process. Then, th= ink how to apply in parallel, which is much diffcult to do. The current= problem is to get 2PC state from the WAL on commit prepared. At this m= oment, the WAL is not flushed yet, commit function waits until WAL with= 2PC state is to be flushed. I just tried to do it in my sandbox and fo= und such a problem. Inability to get 2PC state from unflushed WAL stops= me right now. I think about possible solutions.

The idea wi= th enableFsync is not a suitable solution, in general, I think. I just = pointed it as an alternate idea. You just do enableFsync =3D false befo= re prepare or commit prepared and do enableFsync =3D true after these f= unctions. In this case, 2PC records will not be fsync-ed, but FlushPtr = will be increased. Thus, 2PC state can be read from WAL on commit prepa= red without waiting. To make it work correctly, I guess, we have to do = some additional work to keep more wal on the master and filter some dup= licate transactions on the replica, if replica restarts during catchup.=
=E2=80=8B=E2=80=8B=E2=80=8B=E2=80=8B=E2=80=8B=E2=80=8B
With = best regards,
=E2=80=8B=E2=80=8B=E2=80=8B=E2=80=8B=E2=80=8B=E2=80=8B= Vitaly Davydov

  ------=_=-_OpenGroupware_org_NGMime-630258-1708708306.652385-0--------