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 1rwIvg-000sYU-Tf for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Apr 2024 09:47:13 +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 1rwIve-00CBhq-MA for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Apr 2024 09:47:10 +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 1rwIve-00CBhi-BP for pgsql-hackers@lists.postgresql.org; Mon, 15 Apr 2024 09:47:10 +0000 Received: from mail-oo1-xc2e.google.com ([2607:f8b0:4864:20::c2e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rwIvb-00135h-RM for pgsql-hackers@lists.postgresql.org; Mon, 15 Apr 2024 09:47:09 +0000 Received: by mail-oo1-xc2e.google.com with SMTP id 006d021491bc7-5ac8e8ce2a7so780876eaf.1 for ; Mon, 15 Apr 2024 02:47:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713174426; x=1713779226; 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=lvXKOStRVNvROOCvJp1O3//ProjLZIe0MfpmNPYsr9w=; b=M8UzSgECBW+emrdM2Z/pazR4iOP1DNyPQ30l4O+HSSRFyrUbRqan7Obddb1cmMEkGI H1y7CfhiwgB8Duu/o4L+nB9HOBTqPMV/cRY8fS0usnxR2ZplkZ+USrTX1IgI+HIfp0y/ mOtuhXLOdyOlM0dAGs7Vh0EeBS2kfvAJC6TEvpUF8SRbf1syDtnPT/NEaeg6CV4mOYmR aL5R+kkcTN3OMZHE37oKtHrzl3TvNDhLmceeWv23F9LpR+8AV9mRjDtc5tRBEtvyC3Ev XRA3SBhTq6M67ePXN9siRqWzhQ9tJ1MmluSdl1oAhFF4it6pPSHnRD/61n1foXTrKcBr OlSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713174426; x=1713779226; 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=lvXKOStRVNvROOCvJp1O3//ProjLZIe0MfpmNPYsr9w=; b=skQDaYsdj0n8XZylqvKsHjRnIdtkwfpufo+oo1sie5TzVVN6+B00xaPku/AQw/jw2f xlGIakGOI3qHSZZ/qgbKp2FfFbFWMwss7BiSOXLCSSfzAUqQ2l++u0GwAqeloFMIATei YW6nEnYpGrE5F1Ir7eLeo35uwFdyJsA47hLdSbV/79jy7DMTMyAujR3Om71s86ItcipL YunhIegFOxQIcQ6stSO3esT6NmeB/1OhxzpvFWjqUb7nIIvfK/xMh9h6Uu2Lmqjaf1CS MkGmDakeAAYM6EDmICKCveK1GizR6BVvS4XiiG/M0z0Zb5k1Us8UIkfLRsEe/sheAFUh j68Q== X-Forwarded-Encrypted: i=1; AJvYcCWEvkbareta9kI1kT0DOjDuE3+cCOk6/S4O2A+8K9tAem5EWREuYlGYXQaqqgYJ6Tz6MwsaVdUwPGoSGeiTdgDeKu6vBJ9aXut5Q1dJRa935csh X-Gm-Message-State: AOJu0Yw7o1m2cye2SUEMO6NRU660uu/ua59P/XZTT86ZzPx+MbXavUYR lYrI0oh+pjEFe1o3vXFsX+pQ09LJuSe+RhFreI4K0acWAAdhXxQ/iBZ56UyE2pcIpZPjAqE8Cir Qi7fSyRsVooJ/hqTyu9maOfawyIA= X-Google-Smtp-Source: AGHT+IGDxlBiESE+cA17b+Eoyxm7llcmnM/8Neg5rCmFryNNNgLaem31PtjWeq+h9XHCQwdLX45zaJpmulVHdUSE+cg= X-Received: by 2002:a05:6871:28e:b0:22e:c24d:95ae with SMTP id i14-20020a056871028e00b0022ec24d95aemr4736356oae.10.1713174425630; Mon, 15 Apr 2024 02:47:05 -0700 (PDT) MIME-Version: 1.0 References: <8fab8-65d74c80-1-2f28e880@39088166> <99df2-65d8d200-1-63c26880@172687752> In-Reply-To: From: Amit Kapila Date: Mon, 15 Apr 2024 15:16:53 +0530 Message-ID: Subject: Re: Slow catchup of 2PC (twophase) transactions on replica in LR To: "Hayato Kuroda (Fujitsu)" Cc: Ajin Cherian , =?UTF-8?B?0JTQsNCy0YvQtNC+0LIg0JLQuNGC0LDQu9C40Lk=?= , Heikki Linnakangas , "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 Mon, Apr 15, 2024 at 1:28=E2=80=AFPM Hayato Kuroda (Fujitsu) wrote: > > > Vitaly, does the minimal solution provided by the proposed patch > > (Allow to alter two_phase option of a subscriber provided no > > uncommitted > > prepared transactions are pending on that subscription.) address your u= se case? > > I think we do not have to handle cases which there are prepared transacti= ons on > publisher/subscriber, as the first step. It leads additional complexity a= nd we > do not have smarter solutions, especially for problem 2. > IIUC it meets the Vitaly's condition, right? > > > > 1. While toggling two_phase from true to false, we could probably get= a list of > > prepared transactions for this subscriber id and rollback/abort the pre= pared > > transactions. This will allow the transactions to be re-applied like a = normal > > transaction when the commit comes. Alternatively, if this isn't appropr= iate doing it > > in the ALTER SUBSCRIPTION context, we could store the xids of all prepa= red > > transactions of this subscription in a list and when the corresponding = xid is being > > committed by the apply worker, prior to commit, we make sure the previo= usly > > prepared transaction is rolled back. But this would add the overhead of= checking > > this list every time a transaction is committed by the apply worker. > > > > > > > In the second solution, if you check at the time of commit whether > > there exists a prior prepared transaction then won't we end up > > applying the changes twice? I think we can first try to achieve it at > > the time of Alter Subscription because the other solution can add > > overhead at each commit? > > Yeah, at least the second solution might be problematic. I prototyped > the first one and worked well. However, to make the feature more consiste= nt, > it is prohibit to exist prepared transactions on subscriber for now. > We can ease based on the requirement. > > > > 2. No solution yet. > > > > > > > One naive idea is that on the publisher we can remember whether the > > prepare has been sent and if so then only send commit_prepared, > > otherwise send the entire transaction. On the subscriber-side, we > > somehow, need to ensure before applying the first change whether the > > corresponding transaction is already prepared and if so then skip the > > changes and just perform the commit prepared. One drawback of this > > approach is that after restart, the prepare flag wouldn't be saved in > > the memory and we end up sending the entire transaction again. One way > > to avoid this overhead is that the publisher before sending the entire > > transaction checks with subscriber whether it has a prepared > > transaction corresponding to the current commit. I understand that > > this is not a good idea even if it works but I don't have any better > > ideas. What do you think? > > I considered but not sure it is good to add such mechanism. Your idea req= uires > additional wait-loop, which might lead bugs and unexpected behavior. And = it may > degrade the performance based on the network environment. > As for the another solution (worker sends a list of prepared transactions= ), it > is also not so good because list of prepared transactions may be huge. > > Based on above, I think we can reject the case for now. > > FYI - We also considered the idea which walsender waits until all prepare= d transactions > are resolved before decoding and sending changes, but it did not work wel= l > - the restarted walsender sent only COMMIT PREPARED record for transactio= ns which > have been prepared before disabling the subscription. This happened becau= se > 1) if the two_phase option of slots is false, the confirmed_flush can be = ahead of > PREPARE record, and > 2) after the altering and restarting, start_decoding_at becomes same as > confirmed_flush and records behind this won't be decoded. > I don't understand the exact problem you are facing. IIUC, if the commit is after start_decoding_at point and prepare was before it, we expect to send the entire transaction followed by a commit record. The restart_lsn should be before the start of such a transaction and we should have recorded the changes in the reorder buffer. --=20 With Regards, Amit Kapila.