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 1ihuhX-0006WE-F3 for pgsql-general@arkaria.postgresql.org; Thu, 19 Dec 2019 12:14:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ihuhW-00052t-6C for pgsql-general@arkaria.postgresql.org; Thu, 19 Dec 2019 12:14:42 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ihuhV-00052l-Q9 for pgsql-general@lists.postgresql.org; Thu, 19 Dec 2019 12:14:41 +0000 Received: from mail-wm1-x343.google.com ([2a00:1450:4864:20::343]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ihuhS-0003cM-SO for pgsql-general@lists.postgresql.org; Thu, 19 Dec 2019 12:14:41 +0000 Received: by mail-wm1-x343.google.com with SMTP id f4so6896953wmj.1 for ; Thu, 19 Dec 2019 04:14:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ocado.com; s=google; h=reply-to:subject:to:references:from:organization:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=6zP1ghxeb+wbseqY7FefQQDi9tklSM5ceFGOFmOEUkI=; b=bgZWHa1v2d4mFCOXBSCeFxHuezpmndUqS8w/2PDB/pMTQurNRJ2hS4PA5Oxn8B+Kwi 8OhVZSQFLl9CCO9p3Pq6KSIXmA+JTxt+fetkdRmWyKrB5YTxbRgrqQOvvl1jwP1dwA/X Ubj3AO4ONPEtiFfSIn8mB4Wp7jejXWrAgJBTQSzJ4I76dKAsChyztR9F9n9yKJ+sE+Bp vHTqMrF4YlYRAYJpmX227xt3RwzjaIxTc984JTiohV3YwuklUZPOq/lZwHT4VQ7fiSYA QKLOqVk4iEWfACZMtpbKx3SfjxUSiDmdsciUFHstWq5v3h8u8suGcMrUTqC+fzPu0m7U TLPw== 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:references:from:organization :message-id:date:user-agent:mime-version:in-reply-to :content-language; bh=6zP1ghxeb+wbseqY7FefQQDi9tklSM5ceFGOFmOEUkI=; b=Tvc58Mt3I9/MCWv5qThvsHMeCaDcxI4g3nES0wkvIH3fnNWJdpGFkJem0YrIVLcXGJ e3LDcjCIFI792dbOmtkQQc26toKrrcLZHSzfelKqm1bUVFgW2EOXjCoJeaRQhvadqo/D HdQ/43YsQKwNrgBhMd1FIA9XkatbbgELhlFqpNu9JoLYLooOlJ2irfEQaoZHdEZZCYTB xzFtMDY637nh3/cdPfMCluE9egB4YbyMFQn5DoBfh/FAQxsbBGihCQA5Nt3y8bl4rb9K nIy1arFM8TmS/H3K1B5mSmdsliuBA6qbi58S1HQ55HG5m+N67jxzll/D00fxYzplxkFY 3kGg== X-Gm-Message-State: APjAAAWOihVR27M0b3LBuyjkwvItWN3qPcjArya+S8tkUcFhTy+RGe3h DC0lSZM0Eun1Km3QXD+o9a7SSM6nCg/j3StKsyLw+2X7yfZD3VNLy0E3HJErXgvyTILyzUpgwul vfB5wqyQFPHDMnih5zBtC+hqydFJneOkJLGchVDXRLMxAZprT8v18X6QOM3tYz71KeprtcISLyv othTG4MCsLnhOF X-Google-Smtp-Source: APXvYqxZNCQj5ywl91PXzO6gkwdiFXfpbay5NAgaGwpsgEc6fqSzrCNzXJGshggNdlB0JyeysWgZ4Q== X-Received: by 2002:a1c:3189:: with SMTP id x131mr9313891wmx.59.1576757677388; Thu, 19 Dec 2019 04:14:37 -0800 (PST) Received: from [10.62.12.22] ([91.206.0.63]) by smtp.gmail.com with ESMTPSA id k19sm5864014wmi.42.2019.12.19.04.14.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 19 Dec 2019 04:14:36 -0800 (PST) Reply-To: f.venchiarutti@ocado.com Subject: Re: Commit to primary with unavailable sync standby To: Andrey Borodin , pgsql-general@lists.postgresql.org References: From: Fabio Ugo Venchiarutti Organization: Ocado Technology Message-ID: Date: Thu, 19 Dec 2019 12:14:36 +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 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 19/12/2019 11:04, Andrey Borodin wrote: > Hi! > > I cannot figure out proper way to implement safe HA upsert. I will be very grateful if someone would help me. > > Imagine we have primary server after failover. It is network-partitioned. We are doing INSERT ON CONFLICT DO NOTHING; that eventually timed out. > > az1-grx88oegoy6mrv2i/db1 M > WITH new_doc AS ( > INSERT INTO t( > pk, > v, > dt > ) > VALUES > ( > 5, > 'text', > now() > ) > ON CONFLICT (pk) DO NOTHING > RETURNING pk, > v, > dt) > SELECT new_doc.pk from new_doc; > ^CCancel request sent > WARNING: 01000: canceling wait for synchronous replication due to user request > DETAIL: The transaction has already committed locally, but might not have been replicated to the standby. > LOCATION: SyncRepWaitForLSN, syncrep.c:264 > Time: 2173.770 ms (00:02.174) > > Here our driver decided that something goes wrong and we retry query. > > az1-grx88oegoy6mrv2i/db1 M > WITH new_doc AS ( > INSERT INTO t( > pk, > v, > dt > ) > VALUES > ( > 5, > 'text', > now() > ) > ON CONFLICT (pk) DO NOTHING > RETURNING pk, > v, > dt) > SELECT new_doc.pk from new_doc; > pk > ---- > (0 rows) > > Time: 4.785 ms > > Now we have split-brain, because we acknowledged that row to client. > How can I fix this? > > There must be some obvious trick, but I cannot see it... Or maybe cancel of sync replication should be disallowed and termination should be treated as system failure? > > Best regards, Andrey Borodin. > You're hitting the CAP theorem ( https://en.wikipedia.org/wiki/CAP_theorem ) You cannot do it with fewer than 3 nodes, as the moment you set your standby to synchronous to achieve consistency, both your nodes become single points of failure. With 3 or more nodes you can perform what is called a quorum write against ( floor( / 2) + 1 ) nodes . With 3+ nodes, the "easy" strategy is to set a number of standby 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 WAL location without running into race conditions (as the operation is non-atomic, stateful and sticky). I personally prefer to designate a fixed synchronous set at setup time and automatically set a static synchronous_standby_names on the master whenever a failover occurs. That allows for a simpler failover mechanism as you know they got the latest WAL location. If you want an off-the shelf solution, nowadays Patroni seems to be all the rage. -- Regards Fabio Ugo Venchiarutti OSPCFC Network Engineering Dpt. Ocado Technology -- Notice: This email is confidential and may contain copyright material of members of the Ocado Group. Opinions and views expressed in this message may not necessarily reflect the opinions and views of the members of the Ocado Group. If you are not the intended recipient, please notify us immediately and delete all copies of this message. Please note that it is your responsibility to scan this message for viruses. References to the "Ocado Group" are to Ocado Group plc (registered in England and Wales with number 7098618) and its subsidiary undertakings (as that expression is defined in the Companies Act 2006) from time to time. The registered office of Ocado Group plc is Buildings One & Two, Trident Place, Mosquito Way, Hatfield, Hertfordshire, AL10 9UL.