Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nnzwK-00043F-C3 for pgsql-hackers@arkaria.postgresql.org; Mon, 09 May 2022 09:44:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nnzwJ-0008Jh-74 for pgsql-hackers@arkaria.postgresql.org; Mon, 09 May 2022 09:44:27 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nnzwI-0008J0-Sb for pgsql-hackers@lists.postgresql.org; Mon, 09 May 2022 09:44:26 +0000 Received: from mail-oi1-x229.google.com ([2607:f8b0:4864:20::229]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nnzwC-0007EG-B1 for pgsql-hackers@lists.postgresql.org; Mon, 09 May 2022 09:44:25 +0000 Received: by mail-oi1-x229.google.com with SMTP id v66so14570635oib.3 for ; Mon, 09 May 2022 02:44:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=efAzsSYLbht4vr4gk1Y/CGX33JdEn+vlzJLEIyxakHg=; b=Q5/Eb6e06JU9lg0sI52zVmVtuCn1nDhv0RvbMnWNTKQ9PYZHpN9hCfCNxLo7JBjDj8 YauyNn5fBhhJJhkio/Q/0Sdi20IBNiZ7pL61ZqAjeBE2oeQwE/vb0vSHEE9r0oLWkDG5 cCE8qVvIlsqYMI1HTTuu4Sdkus1vIBqBzkKnNiVVtvmKLEunfUFUeCyXwz/4OzQwO4OP k1qhHv02mGakWqkNNz/DX9OfGnJ9hak6YXNqnQQ+PrAQPZtAZpeADGBowdkiVgYpIfGA ASw+JIa69eJhDhY/iXxo2REFUJGFiQTm25B5CA10+q1HEa+mtCBL7qsLg7tO+zbPqTxT oTrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=efAzsSYLbht4vr4gk1Y/CGX33JdEn+vlzJLEIyxakHg=; b=GChTto04gojLc8Dl5SfaHMHXNv/EhPoenABTF/7M1b6s9DGQOLQVCAPS7cVp/416hx bHBt4bej4/c++ji4c+XoVN9izxGmMot+hb+BtvWLA4xoUeyZvrv62Hhpee5013A9Nu31 XbKoYoAd5zhJtC0R8XNy5L/oRl4w2uDWfwzGDY2fBsdtaQE6wQQ389rLo4dSNsgemGMD Yt3r5ZJXWVYmdSNjzhRlf6z9+9CzDiadAyXMWTNsnKBM/P2ACE7FdhCJT2QL7Rs9b/jq Bg181h6qktrRA2jgHw4JDZqMxgG/u7ZfhW4KYfT5u6qEUjYnGucJKCzbwP284E3xhTj7 T0Xg== X-Gm-Message-State: AOAM531UJc79A5E4E1olkUoIK8/BqbZbSOUjWOyLbGrUckIlO9cwkKZ4 1g7tnDGivtmzpXdXVfREgFwgZwdINKh6cvvM0VM= X-Google-Smtp-Source: ABdhPJySb7AX5QJ5s4ut3SOLQWZx86EC0SpszLIlb37rtJmYhmS1a8RShJaUqtAOT8Rfb/BMv3j7jBFt5Pk6RVfZJ30= X-Received: by 2002:a05:6808:645:b0:325:abf1:e7d0 with SMTP id z5-20020a056808064500b00325abf1e7d0mr7145517oih.123.1652089459598; Mon, 09 May 2022 02:44:19 -0700 (PDT) MIME-Version: 1.0 References: <9290b55b6ae2b04e002ca9dadadd1cca09461482.camel@cybertec.at> In-Reply-To: From: Dilip Kumar Date: Mon, 9 May 2022 15:14:03 +0530 Message-ID: Subject: Re: An attempt to avoid locally-committed-but-not-replicated-to-standby-transactions in synchronous replication To: Bharath Rupireddy Cc: Laurenz Albe , PostgreSQL Hackers , SATYANARAYANA NARLAPURAM Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, May 9, 2022 at 2:50 PM Bharath Rupireddy wrote: > > On Tue, Apr 26, 2022 at 11:57 AM Laurenz Albe wrote: > > > > On Mon, 2022-04-25 at 19:51 +0530, Bharath Rupireddy wrote: > > > With synchronous replication typically all the transactions (txns) > > > first locally get committed, then streamed to the sync standbys and > > > the backend that generated the transaction will wait for ack from sync > > > standbys. While waiting for ack, it may happen that the query or the > > > txn gets canceled (QueryCancelPending is true) or the waiting backend > > > is asked to exit (ProcDiePending is true). In either of these cases, > > > the wait for ack gets canceled and leaves the txn in an inconsistent > > > state [...] > > > > > > Here's a proposal (mentioned previously by Satya [1]) to avoid the > > > above problems: > > > 1) Wait a configurable amount of time before canceling the sync > > > replication by the backends i.e. delay processing of > > > QueryCancelPending and ProcDiePending in Introduced a new timeout GUC > > > synchronous_replication_naptime_before_cancel, when set, it will let > > > the backends wait for the ack before canceling the synchronous > > > replication so that the transaction can be available in sync standbys > > > as well. > > > 2) Wait for sync standbys to catch up upon restart after the crash or > > > in the next txn after the old locally committed txn was canceled. > > > > While this may mitigate the problem, I don't think it will deal with > > all the cases which could cause a transaction to end up committed locally, > > but not on the synchronous standby. I think that only using the full > > power of two-phase commit can make this bulletproof. > > Not sure if it's recommended to use 2PC in postgres HA with sync > replication where the documentation says that "PREPARE TRANSACTION" > and other 2PC commands are "intended for use by external transaction > management systems" and with explicit transactions. Whereas, the txns > within a postgres HA with sync replication always don't have to be > explicit txns. Am I missing something here? > > > Is it worth adding additional complexity that is not a complete solution? > > The proposed approach helps to avoid some common possible problems > that arise with simple scenarios (like cancelling a long running query > while in SyncRepWaitForLSN) within sync replication. IMHO, making it wait for some amount of time, based on GUC is not a complete solution. It is just a hack to avoid the problem in some cases. -- Regards, Dilip Kumar EnterpriseDB: http://www.enterprisedb.com