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 1ruVyc-005rrY-VV for pgsql-hackers@arkaria.postgresql.org; Wed, 10 Apr 2024 11:18:51 +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 1ruVyc-001DP5-0N for pgsql-hackers@arkaria.postgresql.org; Wed, 10 Apr 2024 11:18:50 +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 1ruVyb-001DOx-GI for pgsql-hackers@lists.postgresql.org; Wed, 10 Apr 2024 11:18:49 +0000 Received: from mail-oo1-xc33.google.com ([2607:f8b0:4864:20::c33]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1ruVyU-002AeD-PL for pgsql-hackers@lists.postgresql.org; Wed, 10 Apr 2024 11:18:48 +0000 Received: by mail-oo1-xc33.google.com with SMTP id 006d021491bc7-5a4f608432bso4775337eaf.2 for ; Wed, 10 Apr 2024 04:18:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712747922; x=1713352722; 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=XLhn7XKFzjBluxYScITYvExYK127EoW7htSsrGZXL3o=; b=TTUqUx1+i0Vo6Tda8kU3DmLUlzF9d/pEB8HJe2po6YB1YpQXKm6ldPmYz5F6luFH4Q rKuQ2gJ3gjs6RLC8s0/G1wQxua6asKjuIHkGC/XhD3shzH5Y/hFdKpzzq/eGepKlytAm Pc6/euQZXqsosv0F2DqjHD798PnLj6ThZD6IYTOpsk2qpwARWB3vP+sDj1C2CEXKUE7X Vf71oIsEVlz7ew/ytCmasCjd6szd6Go+APCI7oUkR6p+inNA/m08xI60nn7Mzor8CrdK 439uHmYr7Vnf33lmUy0IR3bsfVlTuRaltJQ44+pUKm29VPtDgOng7oFum40xTIgj5DsA Qp8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712747922; x=1713352722; 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=XLhn7XKFzjBluxYScITYvExYK127EoW7htSsrGZXL3o=; b=Fp3ANFrY/zV3bzZMNd5qyXvVsbkHdm0PBTJEoOfHapTD0eFbg5ziUGmkxvEArup22k 50l/m4eWO793H/9fTcWtGAU1vBG7IzQ5ZbyanT4kqq/N10rPkhULqMLN3s1DwfYvzPj2 qv84fCJ7iQ85S70COPvjuqK59JaiOPBgKMvQeepaFBN4H6r6crcwfJS2az8IoqjL4ALf ap6p9b9Vlo93kSI+/sVzF9YOi/h5INdcJatdJyJeAPsBOz4dzg5vezZlafMMbEYY9Q90 fEwSmzc889GSPL1NasYlC1W9fIUK7cpAJVSIhNAjWdqYohnc1eRTfRiuXsnL2Ka8RPuS 3R/w== X-Forwarded-Encrypted: i=1; AJvYcCUCn9UKmbSp3jTECzUiaxtmy8NRstYX1klC+9XpL/MyVerm5wlg/l43vlx2/c6D1zq5m5iSTFp5reK9oa/lZCccCjt15G6vdupTAm6NScq4+rMk X-Gm-Message-State: AOJu0YyXUOZPb9jw4B7obNd/Cdw7UyHCJ79HeURMR1dT/ePlUhWYS1Av I/K9cEq5dCqPtzw+ZxmjqOGL3DmBsuq88r32z8wYU/SY64eE92XQ6wf4naoYfC2QvnwfFaCX+hf wpODcXx93mX9dho1Lkez3bZ/L+ao= X-Google-Smtp-Source: AGHT+IEwhraf3bcW+ktxaX8B/1BEvZL/X6olbtqCXNTmrSCbllWTVUDlqUyseRPax8sKKxKz0qRcecNTI99dwGbA58I= X-Received: by 2002:a05:6870:9193:b0:22e:bace:ed96 with SMTP id b19-20020a056870919300b0022ebaceed96mr2265658oaf.15.1712747921871; Wed, 10 Apr 2024 04:18:41 -0700 (PDT) MIME-Version: 1.0 References: <8fab8-65d74c80-1-2f28e880@39088166> <99df2-65d8d200-1-63c26880@172687752> In-Reply-To: From: Amit Kapila Date: Wed, 10 Apr 2024 16:48:26 +0530 Message-ID: Subject: Re: Slow catchup of 2PC (twophase) transactions on replica in LR To: Ajin Cherian Cc: =?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 Fri, Apr 5, 2024 at 4:59=E2=80=AFPM Ajin Cherian wro= te: > > On Thu, Apr 4, 2024 at 4:38=E2=80=AFPM Amit Kapila wrote: >> >> >> I think this would probably be better than the current situation but >> can we think of a solution to allow toggling the value of two_phase >> even when prepared transactions are present? Can you please summarize >> the reason for the problems in doing that and the solutions, if any? >> > > > Updated the patch, as it wasn't addressing updating of two-phase in the r= emote slot. > 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 use c= ase? > Currently the main issue that needs to be handled is the handling of pen= ding prepared transactions while the two_phase is altered. I see 3 issues w= ith the current approach. > > 1. Uncommitted prepared transactions when toggling two_phase from true to= false > =E2=80=83=E2=80=83When two_phase was true, prepared transactions were dec= oded at PREPARE time and send to the subscriber, which is then prepared on = the subscriber with a new gid. Once the two_phase is toggled to false, then= the COMMIT PREPARED on the publisher is converted to commit and the entire= transaction is decoded and sent to the subscriber. This will leave the p= reviously prepared transaction pending. > > 2. Uncommitted prepared transactions when toggling two_phase form false t= o true > =E2=80=83=E2=80=83When two_phase was false, prepared transactions were ig= nored and not decoded at PREPARE time on the publisher. Once the two_phase = is toggled to true, the apply worker and the walsender are restarted and a = replication is restarted from a new "start_decoding_at" LSN. Now, this new = "start_decoding_at" could be past the LSN of the PREPARE record and if so, = the PREPARE record is skipped and not send to the subscriber. Look at comme= nts in DecodeTXNNeedSkip() for detail. Later when the user issues COMMIT P= REPARED, this is decoded and sent to the subscriber. but there is no prepar= ed transaction on the subscriber, and this fails because the corresponding= gid of the transaction couldn't be found. > > 3. While altering the two_phase of the subscription, it is required to al= so alter the two_phase field of the slot on the primary. The subscription c= annot remotely alter the two_phase option of the slot when the subscription= is enabled, as the slot is owned by the walsender on the publisher side. > Thanks for summarizing the reasons for not allowing altering the two_pc property for a subscription. > Possible solutions for the 3 problems: > > 1. While toggling two_phase from true to false, we could probably get a l= ist of prepared transactions for this subscriber id and rollback/abort the = prepared transactions. This will allow the transactions to be re-applied li= ke a normal transaction when the commit comes. Alternatively, if this isn't= appropriate doing it in the ALTER SUBSCRIPTION context, we could store the= xids of all prepared transactions of this subscription in a list and when = the corresponding xid is being committed by the apply worker, prior to comm= it, we make sure the previously prepared transaction is rolled back. But th= is 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? > 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? > 3. We could mandate that the altering of two_phase state only be done aft= er disabling the subscription, just like how it is handled for failover opt= ion. > makes sense. --=20 With Regards, Amit Kapila.