Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ihvKB-0007jn-17 for pgsql-general@arkaria.postgresql.org; Thu, 19 Dec 2019 12:54:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ihvK9-0006HE-FT for pgsql-general@arkaria.postgresql.org; Thu, 19 Dec 2019 12:54:37 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ihvK8-0006H7-Uy for pgsql-general@lists.postgresql.org; Thu, 19 Dec 2019 12:54:37 +0000 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ihvK1-0002wk-ME for pgsql-general@lists.postgresql.org; Thu, 19 Dec 2019 12:54:35 +0000 Received: by mail-wm1-x341.google.com with SMTP id t14so5488464wmi.5 for ; Thu, 19 Dec 2019 04:54:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ocado.com; s=google; h=reply-to:subject:to:cc:references:from:organization:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=YHRe2ZKxNWWvNgGas6EmTnp2C9M9bNaSg3e/uJYD+L0=; b=AilujF6CJS2ARKYyV+t5BZ9/5cfD0+z2yN15Lx+26ksOgcN9fkv9P8eKh7c5BFVF/h 51ECbgL93WGtyYpUucECx8bjX/+YgekfI7K10/RJx9GvRGLlFUSg3SLxxFbGSN0MMFcL UcTyd8j2ldfqs7KMecMmhvX0iz3okiNSiN0B5RVeWYJ5htZo2JF85katEk/fltEx7yQg TT+eRgyyay03UqQ6/t6/t4eXdk1bd1d9BVcgfIbqzY3kPGQwq6WcjVJGU+94mx7lCLE2 ORjD46F5YfIwQtACOYy/f04sylr+sxO5lY0S8UHMadOYF3TUp3nO6rX38/HSLYK2nx9N KBhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:subject:to:cc:references:from :organization:message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=YHRe2ZKxNWWvNgGas6EmTnp2C9M9bNaSg3e/uJYD+L0=; b=Shc+vo/pIEnWscIMsS218/zQxkZShXH3z+zFfOA7TgJQ/t2Hb6OqeomU3CwEnOuyex QWfex+yP+CksVa5hm2CJeH6YDLn7FVZIAMuJWJFiyBPq9IftLk26XWgthpvamf+C9ubG IIMtSusfhAFyVm7NeFuggOaO19dfYqrHpUU42RQ71pSGazfGHdf3Fvx1kDqivYmGtRH2 lRyc4vCaA9Na6W67M6C41/gtvom1A65Q8E6WVowsdAL773jLoJv0PC4nQVzE3Dg3KP3C /28h+Ihk/EBNRW1UsQXtLf50dSHu4CCSXEOqBZV6NrWf50nZMNjC81g6s17edynmI6Iy MAEA== X-Gm-Message-State: APjAAAUyINERehD42hyqOE7PR6hVUf7BQSAU2l2fuEmMdedZYlmnLPXN 0oo/vIZGfJ8HfYNtKMjtNBSEjOmxADGgTfjnATQ6FDzAjeajjBnuHh8gqPy1tHguiFXHOYuak1l 9hJ5VHX0VHQhzgkiYiN9J+5eISk+F0iw7/wKjg+zOZWCGaMmNj99tub3F5eb0QIOuLMJuN+IYfz 0pLTUKeWFCaQRs X-Google-Smtp-Source: APXvYqy/HE1xCQSd6qpXttiWTBjlvuPdzBWQ5SNPfl7khntJNgAeLOn5plpbrRZ7QL5xIc6uxHY6DQ== X-Received: by 2002:a1c:3c89:: with SMTP id j131mr9903973wma.34.1576760067928; Thu, 19 Dec 2019 04:54:27 -0800 (PST) Received: from [10.62.12.22] ([91.206.0.63]) by smtp.gmail.com with ESMTPSA id x11sm6674137wre.68.2019.12.19.04.54.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 19 Dec 2019 04:54:27 -0800 (PST) Reply-To: f.venchiarutti@ocado.com Subject: Re: Commit to primary with unavailable sync standby To: Andrey Borodin Cc: pgsql-general@lists.postgresql.org References: From: Fabio Ugo Venchiarutti Organization: Ocado Technology Message-ID: <6ed36f0e-25f4-4b22-5067-5616ab6beda7@ocado.com> Date: Thu, 19 Dec 2019 12:54:26 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Language: en-US Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 19/12/2019 12:25, Andrey Borodin wrote: > Hi Fabio! >=20 > Thanks for looking into this. >=20 >> 19 =D0=B4=D0=B5=D0=BA. 2019 =D0=B3., =D0=B2 17:14, Fabio Ugo Venchiarutt= i =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB(=D0= =B0): >> >> >> You're hitting the CAP theorem ( https://en.wikipedia.org/wiki/CAP_theor= em ) >> >> >> You cannot do it with fewer than 3 nodes, as the moment you set your sta= ndby to synchronous to achieve consistency, both your nodes become single p= oints of failure. > We have 3 nodes, and the problem is reproducible with all standbys being = synchronous. >=20 >> With 3 or more nodes you can perform what is called a quorum write again= st ( floor( / 2) + 1 ) nodes . > The problem seems to be reproducible in quorum commit too. >=20 >> With 3+ nodes, the "easy" strategy is to set a number of st= andby nodes in synchronous_standby_names ( https://www.postgresql.org/docs/= current/runtime-config-replication.html#GUC-SYNCHRONOUS-STANDBY-NAMES ) >> >> >> This however makes it tricky to pick the correct standby for promotions = during auto-failovers, as you need to freeze all the standbys listed in the= above setting in order to correctly determine which one has the highest WA= L location without running into race conditions (as the operation is non-at= omic, stateful and sticky). > After promotion of any standby we still can commit to old primary with th= e combination of cancel and retry. AFAICT this pseudo-idempotency issue can only be solved if every query=20 is validated against the quorum. A quick-and-dirty solution would be to wrap the whole thing in a CTE=20 which also returns a count from pg_stat_replication (a stray/partitioned=20 master would have less than (quorum - 1 standbys). (May be possible to do it directly in the RETURNING clause, I don't have=20 a backend handy test that). You can either look into the result at the client or force an error via=20 some bad cast/zero division in the query. All the above is however still subject to (admittedly tight) race=20 conditions. This problem is precisely why I don't use any of the off-the shelf=20 solutions: last time I checked none of that had a connection=20 proxy/router to direct clients to the real master and not a node that=20 thinks it is. >=20 >> I personally prefer to designate a fixed synchronous set at setup time a= nd automatically set a static synchronous_standby_names on the master whene= ver a failover occurs. That allows for a simpler failover mechanism as you = know they got the latest WAL location. > No, synchronous standby does not necessarily own latest WAL. It has WAL p= oint no earlier than all commits acknowledged to client. You're right. I should have said "latest WAL holding an acknowledged=20 transaction" >=20 > Thanks! >=20 > Best regards, Andrey Borodin. >=20 --=20 Regards Fabio Ugo Venchiarutti OSPCFC Network Engineering Dpt. Ocado Technology --=20 Notice:=20 This email is confidential and may contain copyright material of=20 members of the Ocado Group. Opinions and views expressed in this message=20 may not necessarily reflect the opinions and views of the members of the=20 Ocado Group. If you are not the intended recipient, please notify us=20 immediately and delete all copies of this message. Please note that it is= =20 your responsibility to scan this message for viruses. References to the=20 "Ocado Group" are to Ocado Group plc (registered in England and Wales with= =20 number 7098618) and its subsidiary undertakings (as that expression is=20 defined in the Companies Act 2006) from time to time. The registered office= =20 of Ocado Group plc is Buildings One & Two, Trident Place, Mosquito Way,=20 Hatfield, Hertfordshire, AL10 9UL.