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 1omolM-00013A-Hg for pgsql-general@arkaria.postgresql.org; Mon, 24 Oct 2022 04:08:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1omolJ-0003Et-O5 for pgsql-general@arkaria.postgresql.org; Mon, 24 Oct 2022 04:08:29 +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 1omolJ-0003Ek-8S for pgsql-general@lists.postgresql.org; Mon, 24 Oct 2022 04:08:29 +0000 Received: from mail-ed1-x52a.google.com ([2a00:1450:4864:20::52a]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1omolG-0006H4-RF for pgsql-general@postgresql.org; Mon, 24 Oct 2022 04:08:28 +0000 Received: by mail-ed1-x52a.google.com with SMTP id a5so13640180edb.11 for ; Sun, 23 Oct 2022 21:08:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=OZMDG1Rgc5KYE3U5XR7ArLO2ykp142n0OOzmRKIJcok=; b=IrXl0Ia4XAMNwDfPZq2sWx9KtnSdtW20ziI8mT3rl4YTfxC94i3LRs76fMY2bJD+8j ZsSezgSB5y5cXjwnKTK5h3nHx3Bx2Tmi8ykAbsMX69O2jPvCaycBY3GDl9yoq/Qx7uJK 0IAeeDIRbGFde2eAw+9Nd3HyqDwyh8xBbiadPPBlW0QZiVUK/aQQMvPmY3EneJ/ia0SW +q9Z0Ec9l9NP2h4h95kb34wuFkK3q7CjWZx/kk1nhQNwCiV/hJ6M6LFvb5BhcOfZWYnS z1kb4p96viMN7igbY8kxVLem7BtbYvG4gNYpiHfn9cJmGRT25gCdCCCIZRqHXxhITfU2 81DA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=OZMDG1Rgc5KYE3U5XR7ArLO2ykp142n0OOzmRKIJcok=; b=TC/MMVENyv1yPg6wlW32iFcNsrad1j66rhvp+oz5s12KXvV420pO6TljmfONYASksJ +dmXEuxBUhuXpPUNYJo213ZOqt9WC8beH90pTX+jBmvCxWx3JlOPxLYQymjkdg2AKmAm dCPUgd7XGY9PPfoNjLh+LXDk2REqIWj7Uc70LPxm+xgN1+S0uC174mmFfJJ1tiVuO0H8 hnf1n6WFfIoXegj1JjAY+p+u0zjAtGo6e/sH25YKPIOwSDQKI7QZiCiB1N6SB7wKx0BP xXMkVVTEjd43XcHGTnPf3HVyH+BJio+nCR3HV20ji+qLt2ZaHisRUNjHa7bqm3TNEwbH zJ9g== X-Gm-Message-State: ACrzQf0o/L4YupCdNYDwQO/BraUUiicdlgwuksnmbKmOJ/7ZwtvVrsl0 3r+smUgWmEmCFKUV504aMCpuaPmG79uwyQ== X-Google-Smtp-Source: AMsMyM7jwjEWX5fTDy2glAjfhLCq2RuEx1YJhzAvu7MxQWHPsEnJjmuBgOW20mNvLRniQmAY53gIcQ== X-Received: by 2002:a05:6402:90b:b0:45c:16a0:ec1e with SMTP id g11-20020a056402090b00b0045c16a0ec1emr29800575edz.427.1666584504860; Sun, 23 Oct 2022 21:08:24 -0700 (PDT) Received: from localhost.localdomain (178-190-173-175.adsl.highway.telekom.at. [178.190.173.175]) by smtp.gmail.com with ESMTPSA id z61-20020a509e43000000b00461816beef9sm3317327ede.14.2022.10.23.21.08.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Oct 2022 21:08:24 -0700 (PDT) Message-ID: Subject: Re: synchronous streaming replication From: Laurenz Albe To: Joseph Kennedy , pgsql-general@postgresql.org Date: Mon, 24 Oct 2022 06:08:22 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.42.4 (3.42.4-2.fc35) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sat, 2022-10-22 at 17:28 +0200, Joseph Kennedy wrote: > Hi,I have a question. Its stays at the intersection of software engineering and PostgreSQL. > > I have configured streaming synchronous replication and whit setting "synchronous_commit=remote_apply" > to make sure that the slave will always respond the same as the MASTER (this is a developers' > requirement that the MASTER always responds the same as SLAVE). I set "hot_standby_feedback=on" > and "max_standby_streaming_delay=-1", > max_standby_streaming_delay set to -1 to make MASTER wait indefinitely before SELECT conflicts on the SLAVE will end. >   > Here's where the problem arises, because not long after the replication has been started some serious > delays occur in the form of "replay_lag" - which rather indicates the appearance of conflicts; > the replication stops working properly. >   > From the server logs it appears that UPDATE (select for update) has occurred on the MASTER, and SELECT > queries are in progress on SLAVE causing replication conflicts, with setting "max_standby_streaming_delay=-1" > they never ends and there are huge lags. >   > From the findings with the developers it emerged that they do not want me to set max_standby_streaming_delay > to a value after which the queries conflicted with replication will be canceled. >  * So I'm wondering if, in this configuration, it can work properly at all without setting, for example, > "max_standby_streaming_delay=30" ? >  * On the other hand I wonder if the application should not be developed in such a way to support replication > of PostgreSQL configured as a streaming synchronous replication cluster with "synchronous_commit=remote_apply" ? >  * Or perhaps "synchronous streaming replication" is a bad choice, maybe logical replication would be better ? >  * What are the best practices? >  * Perhaps you just need to force/teach applications to work with synchronous replication in such a way that > when the SELECT causes conflicts with replication such queries are canceled and the application should resend/repeat query ? >  * I also think that after setting, for example, "max_standby_streaming_delay=30" queries (addressed) to the > database should be very well optimized, so that too long queries are not canceled too frequently? >  * Do you know any books focused on applications adapted to work in postgresql synchronous streaming > replication environment i.e. High Availability? This can never work properly. If you have synchronous replication with "synchronous_commit = remote_apply", COMMIT on the primary will wait until the information has been replayed on the standby. If you set "max_standby_streaming_delay = -1", replication can be delayed indefinitely long in the event of a replication conflict, so COMMIT can take arbitrarily long. You can reduce replication conflicts (by setting "hot_standby_feedback = on" and by altering all tables to set "vacuum_truncate = off"), but you will never get rid of them completely. You will either have to accept stale ready on the standby (by setting "synchronous_commit" to something lower) or you have to accept canceled queries on the standby (by lowering "max_standby_streaming_delay"). Yours, Laurenz Albe