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 1rdMAR-0059XQ-7g for pgsql-hackers@arkaria.postgresql.org; Fri, 23 Feb 2024 03:24:07 +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 1rdMAO-00FVIr-Cb for pgsql-hackers@arkaria.postgresql.org; Fri, 23 Feb 2024 03:24:04 +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 1rdMAO-00FVIi-3D for pgsql-hackers@lists.postgresql.org; Fri, 23 Feb 2024 03:24:04 +0000 Received: from mail-oi1-x234.google.com ([2607:f8b0:4864:20::234]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rdMAM-000RCh-4S for pgsql-hackers@lists.postgresql.org; Fri, 23 Feb 2024 03:24:03 +0000 Received: by mail-oi1-x234.google.com with SMTP id 5614622812f47-3bba50cd318so271249b6e.0 for ; Thu, 22 Feb 2024 19:24:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708658640; x=1709263440; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=2z0KLt97qFr1OU7sdwESHClXweIGovnG1hpgSMA5yuI=; b=W3Ix0gwOyyf7tlLmy+xfKevA4vV/6kN2kwll6bWVET3DTO4AbLe/ybUE7H5ceWezH8 X8chsv0j4DkMTUwxRp8J1oRNHVNuVqkC2mR/IbMAw89rElHbWVmhZxDQk73X4o1P/ci0 7366BaIpMbHLgA6kO5wH+q4HHt82ThyTmru0ZUNjUkufoUzEmdfUv82XpFP60k+E830a qxhCTqu+xn9EnwFaN5CFLO7IQNeNn/f+adASc6QQcHNzCBCciOo/9EbLr/6HiLyV0FOV eNiyOrsqX/lLoN7ujjI//7Cnz0MMR50WwihSwxTYMKcTcxM2quqxKiYY93UQVT5kb/p3 8HGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708658640; x=1709263440; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2z0KLt97qFr1OU7sdwESHClXweIGovnG1hpgSMA5yuI=; b=aWUy9qOvRiRwCfACApz2ozGBGI/m70RMoetrG3Up6OIhSbAK7/o0Q+iW4ZX+NLLfIU 6N4dck7uvLIuyCBXHzZORQqkiu1WSEwIJLbaGxXz7YezI9cqvozQl7vZHHO7Hv6JK5DX edmMUmpCDs26nns+8fXiWUntMznhvPes+LHMlBbCdf8qvQjegHz/dvrEN/AehbNrqH7v OA+esMcmppUkuD+t3v8K0GEaBJOgPf48DEyR8miVnGBRkSE6H46ngjf09eDZFzFf8vjc Rsf4g0l7Hx9o2QKKxxdnU0jzgHbiNsOfipa2JYqVKUwSECM85wNLivkmiSEm3rrLQQmx nvxA== X-Gm-Message-State: AOJu0YwEYfOi3MdKMFhfNhxPRuligDiK+93pqoC5jNQ8V4z3Kx7ivLoo zrX5ESMoiPHJhbPJ9SwO2zyQTo2K+F5xsyMRnc0Yp3OF6CUEyBil9rzuw+XYeIl1NmTiqUs+uSk DFSebJtoc2rK09cq5HISuTf7VAlo= X-Google-Smtp-Source: AGHT+IFaisqOYfi0VjPQBMWgShkLEc4y+QfN7hpwSJQGb2/RD+K6L3gDpzcZWVN3DMuwYBGzeqF5MNGwwlOvfTDAbvE= X-Received: by 2002:a05:6870:148d:b0:21e:bbe0:ab1 with SMTP id k13-20020a056870148d00b0021ebbe00ab1mr979766oab.32.1708658640246; Thu, 22 Feb 2024 19:24:00 -0800 (PST) MIME-Version: 1.0 References: <8fab8-65d74c80-1-2f28e880@39088166> In-Reply-To: <8fab8-65d74c80-1-2f28e880@39088166> From: Amit Kapila Date: Fri, 23 Feb 2024 08:53:49 +0530 Message-ID: Subject: Re: Slow catchup of 2PC (twophase) transactions on replica in LR To: =?UTF-8?B?0JTQsNCy0YvQtNC+0LIg0JLQuNGC0LDQu9C40Lk=?= Cc: pgsql-hackers@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, Feb 22, 2024 at 6:59=E2=80=AFPM =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 wrote: > > I'd like to present and talk about a problem when 2PC transactions are ap= plied quite slowly on a replica during logical replication. There is a mast= er and a replica with established logical replication from the master to th= e replica with twophase =3D true. With some load level on the master, the r= eplica starts to lag behind the master, and the lag will be increasing. We = have to significantly decrease the load on the master to allow replica to c= omplete the catchup. Such problem may create significant difficulties in th= e production. The problem appears at least on REL_16_STABLE branch. > > To reproduce the problem: > > Setup logical replication from master to replica with subscription parame= ter twophase =3D true. > Create some intermediate load on the master (use pgbench with custom sql = with prepare+commit) > Optionally switch off the replica for some time (keep load on master). > Switch on the replica and wait until it reaches the master. > > The replica will never reach the master with even some low load on the ma= ster. If to remove the load, the replica will reach the master for much gre= ater time, than expected. I tried the same for regular transactions, but su= ch problem doesn't appear even with a decent load. > > I think, the main proplem of 2PC catchup bad performance - the lack of as= ynchronous commit support for 2PC. For regular transactions asynchronous co= mmit is used on the replica by default (subscrition sycnronous_commit =3D o= ff). It allows the replication worker process on the replica to avoid fsync= (XLogFLush) and to utilize 100% CPU (the background wal writer or checkpoi= nter will do fsync). I agree, 2PC are mostly used in multimaster configurat= ions with two or more nodes which are performed synchronously, but when the= node in catchup (node is not online in a multimaster cluster), asynchronou= s commit have to be used to speedup the catchup. > I don't see we do anything specific for 2PC transactions to make them behave differently than regular transactions with respect to synchronous_commit setting. What makes you think so? Can you pin point the code you are referring to? > There is another thing that affects on the disbalance of the master and r= eplica performance. When the master executes requestes from multiple client= s, there is a fsync optimization takes place in XLogFlush. It allows to dec= rease the number of fsync in case when a number of parallel backends write = to the WAL simultaneously. The replica applies received transactions in one= thread sequentially, such optimization is not applied. > Right, I think for this we need to implement parallel apply. > I see some possible solutions: > > Implement asyncronous commit for 2PC transactions. > Do some hacking with enableFsync when it is possible. > Can you be a bit more specific about what exactly you have in mind to achieve the above solutions? --=20 With Regards, Amit Kapila.