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 1rdNY0-005IP9-CT for pgsql-hackers@arkaria.postgresql.org; Fri, 23 Feb 2024 04:52:32 +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 1rdNXy-00Fy9J-Sd for pgsql-hackers@arkaria.postgresql.org; Fri, 23 Feb 2024 04:52:31 +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 1rdNXy-00Fy9B-H7 for pgsql-hackers@lists.postgresql.org; Fri, 23 Feb 2024 04:52:31 +0000 Received: from mail-yw1-x112f.google.com ([2607:f8b0:4864:20::112f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rdNXs-000Rn8-6V for pgsql-hackers@lists.postgresql.org; Fri, 23 Feb 2024 04:52:30 +0000 Received: by mail-yw1-x112f.google.com with SMTP id 00721157ae682-6083befe2a7so5283317b3.0 for ; Thu, 22 Feb 2024 20:52:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708663943; x=1709268743; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Xelh1n3MFBlZNv2+lWqbthRa8DXUto7lP0ayHLB3dRQ=; b=cmK4qWyFXPT6sYSZ9uRUrf3/PNPHACNd2xM7W6LAaxuBnlIy+fjfyxKZf5SFd9eHJi 6F0KjYuH4dnM3DetMwImCGloaQLWjhncDByJR6cTuxpoxXonpbliP/sk9H4hbRdNviUl ZsAQbBI7W4Ck2Dy62swN8uK9iNbcVGz6971LtzzZ28ze+RFTeT5r4Rv83VDNfVUQJcbR 62gW7ErZo1pnKhnjcdRaH04YFPXFhj6fSePfbbmYrocYzjiojrBaZYp4xYGt1KUeiNRA GbOx91ZirktoiyEAZInpQVyMkkSsDLpr08Jc/HlAIeVpdi6JxNt5IFQV4U1eBsEzcNN0 nsBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708663943; x=1709268743; h=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=Xelh1n3MFBlZNv2+lWqbthRa8DXUto7lP0ayHLB3dRQ=; b=rSegH7/Gl16BLc5cMmsM/UvG+VA1TVaoPCXOQ7a8isRW9pZEJR/HcrWpOmRnciHmTB MHkgkCFmwWprX5FbQlpHAh5e9MPAYFSCX84fN+ztJPv9GKaT5zupfK4yzurFpstELFtg qeezhTjy4R0KERyCMdZA8EuhOwQ4NEqWuBcPOHHdrdcMhNLUyRWK1E/RXSLYvt7IGJBj KWO02ECav4YrezIYBsR/6d2lwPZopLVe7BUi/439TWO9C1QOz5iGRaje9fse9awsb/yh PYZJldi7m8dJ7WZpQeTchFmUAs7CBen2JesTpIQp2Jb5f4+azg7IyzvCex+sC1YSE34L wyFw== X-Gm-Message-State: AOJu0YyBPMWI+bDMHG2We2J5f2LlxQR7B2ghBA6g/0astL9AcJURZCon 9drFiMKrnynYjSGSp+Shmuo+zBb+DgdgGoi3JK4Du4UXb730A7W1cYRZcwEM3MfM+w975ViLH9P 8KN9UxB2jKtMMHcstvJRRDf1k/Cu2Hi8yhDygiQ== X-Google-Smtp-Source: AGHT+IGRJCObgInDQV6aOxmhK1+pZbd0NM/iJKL47y2hgPdsy458z43W13+ZZGU8aSoJ32cOxhmOi/qT/KjzFV1emw4= X-Received: by 2002:a0d:d615:0:b0:607:910c:9cb3 with SMTP id y21-20020a0dd615000000b00607910c9cb3mr1154214ywd.36.1708663942695; Thu, 22 Feb 2024 20:52:22 -0800 (PST) MIME-Version: 1.0 References: <8fab8-65d74c80-1-2f28e880@39088166> In-Reply-To: <8fab8-65d74c80-1-2f28e880@39088166> From: Ajin Cherian Date: Fri, 23 Feb 2024 15:52:11 +1100 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: multipart/alternative; boundary="000000000000584ee8061205539d" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000584ee8061205539d Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Feb 23, 2024 at 12:29=E2=80=AFAM =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: > Dear All, > > I'd like to present and talk about a problem when 2PC transactions are > applied quite slowly on a replica during logical replication. There is a > master and a replica with established logical replication from the master > to the replica with twophase =3D true. With some load level on the master= , > the replica 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 complete the catchup. Such problem may create significan= t > difficulties in the production. The problem appears at least on > REL_16_STABLE branch. > > To reproduce the problem: > > - Setup logical replication from master to replica with subscription > parameter 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 > master. If to remove the load, the replica will reach the master for much > greater time, than expected. I tried the same for regular transactions, b= ut > such problem doesn't appear even with a decent load. > > > I tried this setup and I do see that the logical subscriber does reach the master in a short time. I'm not sure what I'm missing. I stopped the logical subscriber in between while pgbench was running and then started it again and ran the following: postgres=3D# SELECT sent_lsn, pg_current_wal_lsn() FROM pg_stat_replication= ; sent_lsn | pg_current_wal_lsn -----------+-------------------- 0/6793FA0 | 0/6793FA0 <=3D=3D=3D caught up (1 row) My pgbench command: pgbench postgres -p 6972 -c 2 -j 3 -f /home/ajin/test.sql -T 200 -P 5 my custom sql file: cat test.sql SELECT md5(random()::text) as mygid \gset BEGIN; DELETE FROM test WHERE v =3D pg_backend_pid(); INSERT INTO test(v) SELECT pg_backend_pid(); PREPARE TRANSACTION $$:mygid$$; COMMIT PREPARED $$:mygid$$; regards, Ajin Cherian Fujitsu Australia --000000000000584ee8061205539d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Fri, Feb 23, 2024 at 12:29=E2=80= =AFAM =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 <v.davydo= v@postgrespro.ru> wrote:

Dear All,

I'd like to present and talk about a= problem when 2PC transactions are applied quite slowly on a replica during= logical replication. There is a master and a replica with established logi= cal replication from the master to the replica with twophase =3D true. With= some load level on the master, the replica starts to lag behind the master= , and the lag will be increasing. We have to significantly decrease the loa= d on the master to allow replica to complete the catchup. Such problem may = create significant difficulties in the production. The problem appears at l= east on REL_16_STABLE branch.

To reproduce the problem:

  • Se= tup logical replication from master to replica with subscription parameter = twophase =3D=C2=A0 true.
  • Create some intermediate load on the maste= r (use pgbench with custom sql with prepare+commit)
  • Optionally swit= ch off the replica for some time (keep load on master).
  • Switch on t= he replica and wait until it reaches the master.

The replica wi= ll never reach the master with even some low load on the master. If to remo= ve the load, the replica will reach the master for much greater time, than = expected. I tried the same for regular transactions, but such problem doesn= 't appear even with a decent load.




I tried this setup and I do see that the logical subscriber does re= ach the master in a short time. I'm not sure what I'm missing. I st= opped the logical subscriber in between while pgbench was running and then = started it again and ran the following:
postgres=3D# SELECT s= ent_lsn, pg_current_wal_lsn() FROM pg_stat_replication;
=C2=A0sent_lsn = =C2=A0| pg_current_wal_lsn
-----------+--------------------
=C2=A00/= 6793FA0 | 0/6793FA0 <=3D=3D=3D caught up
(1 row)

My= pgbench command:
pgbench postgres -p 6972 -c 2 -j 3 -f /home/aji= n/test.sql -T 200 -P 5

my custom sql file:
=
cat test.sql
SELECT md5(random()::text) as mygid \gset
BE= GIN;
DELETE FROM test WHERE v =3D pg_backend_pid();
INSERT INTO tes= t(v) SELECT pg_backend_pid();
PREPARE TRANSACTION $$:mygid$$;
COMMI= T PREPARED $$:mygid$$;

regards,
Ajin Che= rian
Fujitsu Australia

--000000000000584ee8061205539d--