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 1ihxQE-0004gV-81 for pgsql-general@arkaria.postgresql.org; Thu, 19 Dec 2019 15:09:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ihxQD-0002Hz-0O for pgsql-general@arkaria.postgresql.org; Thu, 19 Dec 2019 15:09:01 +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 1ihxQC-0002Hr-K3 for pgsql-general@lists.postgresql.org; Thu, 19 Dec 2019 15:09:00 +0000 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ihxQ9-000523-Lw for pgsql-general@lists.postgresql.org; Thu, 19 Dec 2019 15:09:00 +0000 Received: by mail-wm1-x344.google.com with SMTP id d139so7267473wmd.0 for ; Thu, 19 Dec 2019 07:08:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ocado.com; s=google; h=from:subject:reply-to:to:references:organization:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=A1qjrf9hB/+pDc/P1ay1x7QZA26PX/43I244ZHerZbo=; b=09XpAY3DJi3g4iEqJoS4OrWbF8/b+C/1GJP1HYiWU4XWmGzULesk+9MUqbGUHj68nT /pkoI7CYG2rF4B87LS2xIS0Xahm9jlWNAmBhX46NzapFh0UMiCXMktYPtkjyV5jyKsHA HzF8Ppn9xfqoc0b45XpDxEuRhnsWBmMt/I6nfnPNiNTBkJSqThF/H6PpJfFSrJSS2f7z HYOz0pzfe8n2LKHCdY8p792jTWpZ4DSQiM3PvgL+9vVjMTg9ZcZoVfc2p3jw8Fi3+oCk WQIVXuo4CY8zWV9lhkDMMfvbWB46WR2ca4AIDFTNCvv7uh/J6w9sOEQe3RtTarpgG1p/ hh5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:reply-to:to:references:organization :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=A1qjrf9hB/+pDc/P1ay1x7QZA26PX/43I244ZHerZbo=; b=aYQt9EkxNjUPop9HHnwiNQJLneIsrk80G7DqsZBf+SyFjJ0zaK1dkBs/jU6eo1MxA2 2jjxUF2TJzmKBYBD9NrETx3GgkSIQkvexTj/mn9p2ULNiRF8AVoihEPP+1bS/iUpLtDz zYavRZvkXHT23TBkHTYsm2PpjL+MUUsx6t+gXtyEWYSp7Fn3z+pnv6TB3cINTAzIdNMK Z5glCR2D2BvG00yfraTZAZBxHHFlSz1yEZO76TDaer7MHgN7VypB8ab/dP40g46Pa69S 5iNO8AwVvsJq9dLMtNkbhp1xjmirqND8NCVwctlNEpo5ZJv/EBqMfwEgsrKSM7JZvP8O aMkg== X-Gm-Message-State: APjAAAUyMdCEhr36mzrI4Az9EVGVhBNJ6DVi8iASLvr/BIOdADbXaJ1h E7E1h83n89+lPy+GuFPNgVdwXZpHx03QoRbqIxIEg+o2Z5ikzStBin9Uxo1J5XXMenYIWjqirZr Kx2/aecCAvhO3VUu8whLWWKIQonSNU+8uQtLdvBaG1sDrg/cGYdmT0EgLq8d54r8Dvlajy1emSO YrtQ7/Ao2/3mJ5 X-Google-Smtp-Source: APXvYqy8Kumpxkj2d187fOdejr0haw7j0xcAQ3HO9AuH3lYz4flqObCwXjY6GamedoUacFS2suC7CA== X-Received: by 2002:a7b:c08d:: with SMTP id r13mr11240816wmh.104.1576768135727; Thu, 19 Dec 2019 07:08:55 -0800 (PST) Received: from [10.62.12.22] ([91.206.0.63]) by smtp.gmail.com with ESMTPSA id o129sm6685799wmb.1.2019.12.19.07.08.54 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 19 Dec 2019 07:08:55 -0800 (PST) From: Fabio Ugo Venchiarutti Subject: Re: Commit to primary with unavailable sync standby Reply-To: f.venchiarutti@ocado.com To: pgsql-general@lists.postgresql.org References: <2957464c-f5a2-5e8b-41af-a8809ebb9cd3@gmail.com> Organization: Ocado Technology Message-ID: Date: Thu, 19 Dec 2019 15:08:54 +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: <2957464c-f5a2-5e8b-41af-a8809ebb9cd3@gmail.com> 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 13:58, Maksim Milyutin wrote: > On 19.12.2019 14:04, Andrey Borodin wrote: >=20 >> Hi! >=20 >=20 > Hi! >=20 > FYI, this topic was up recently in -hackers=20 > https://www.postgresql.org/message-id/CAEET0ZHG5oFF7iEcbY6TZadh1mosLmfz1H= Lm311P9VOt7Z+jeg@mail.gmail.com=20 >=20 >=20 >=20 >> I cannot figure out proper way to implement safe HA upsert. I will be=20 >> very grateful if someone would help me. >> >> Imagine we have primary server after failover. It is=20 >> network-partitioned. We are doing INSERT ON CONFLICT DO NOTHING; that=20 >> eventually timed out. >> >> az1-grx88oegoy6mrv2i/db1 M > WITH new_doc AS ( >> =C2=A0=C2=A0=C2=A0=C2=A0 INSERT INTO t( >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 pk, >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 v, >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 dt >> =C2=A0=C2=A0=C2=A0=C2=A0 ) >> =C2=A0=C2=A0=C2=A0=C2=A0 VALUES >> =C2=A0=C2=A0=C2=A0=C2=A0 ( >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 5, >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 'text', >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 now() >> =C2=A0=C2=A0=C2=A0=C2=A0 ) >> =C2=A0=C2=A0=C2=A0=C2=A0 ON CONFLICT (pk) DO NOTHING >> =C2=A0=C2=A0=C2=A0=C2=A0 RETURNING pk, >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 v, >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 dt) >> =C2=A0=C2=A0=C2=A0 SELECT new_doc.pk from new_doc; >> ^CCancel request sent >> WARNING:=C2=A0 01000: canceling wait for synchronous replication due to= =20 >> user request >> DETAIL:=C2=A0 The transaction has already committed locally, but might n= ot=20 >> have been replicated to the standby. >> LOCATION:=C2=A0 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 ( >> =C2=A0=C2=A0=C2=A0=C2=A0 INSERT INTO t( >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 pk, >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 v, >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 dt >> =C2=A0=C2=A0=C2=A0=C2=A0 ) >> =C2=A0=C2=A0=C2=A0=C2=A0 VALUES >> =C2=A0=C2=A0=C2=A0=C2=A0 ( >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 5, >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 'text', >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 now() >> =C2=A0=C2=A0=C2=A0=C2=A0 ) >> =C2=A0=C2=A0=C2=A0=C2=A0 ON CONFLICT (pk) DO NOTHING >> =C2=A0=C2=A0=C2=A0=C2=A0 RETURNING pk, >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 v, >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 dt) >> =C2=A0=C2=A0=C2=A0 SELECT new_doc.pk from new_doc; >> =C2=A0 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=20 >> cancel of sync replication should be disallowed and termination should= =20 >> be treated as system failure? >> >=20 > I think the most appropriate way to handle such issues is to catch by=20 > client driver such warnings (with message about local commit) and mark=20 > the status of posted transaction as undetermined. If connection with=20 > sync replica will come back then this transaction eventually commits but= =20 > after triggering of autofailover and *not replicating this commit to=20 > replica* this commit aborts. Therefore client have to wait some time=20 > (that exceeds the duration of autofailover) and check (logically based=20 > on committed data) the status of commit. >=20 > The problem here is the locally committed data becomes visible to future= =20 > transactions (before autofailover) that violates the property of=20 > consistent reading from master. IMO the more correct behavior for=20 > PostgreSQL here is to ignore any cancel / termination queries when=20 > backend is in status of waiting response from sync replicas. >=20 > However, there is another way to get locally applied commits via restart= =20 > of master after initial recovery. This case is described in doc=20 > https://www.postgresql.org/docs/current/warm-standby.html#SYNCHRONOUS-REP= LICATION-HA=20 > . But here HA orchestrator agent can close access from external users=20 > (via pg_hba.conf manipulations) until PostgreSQL instance synchronizes And this is where the unsafety lies: that assumes that the isolated=20 master is in enough of a sane state to apply a self-ban (and that can do=20 it in near-zero time). Although the retry logic in Andrey's case is probably not ideal (and you=20 offered a more correct approach to synchronous commit), there are many=20 "grey area" failure modes that in his scenario would either prevent a=20 given node from sealing up fast enuogh if at all (eg: PID congestion=20 causing fork()/system() to fail while backends are already up and=20 happily flushing WAL). This is particularly relevant to situations when only a subset of=20 critical transactions set synchronous_commit to remote_*: it'd still be=20 undesirable to sink "tier 2" data in a stale primary for any significant=20 length of time). Distributed systems like Etcd and Cassandra have a notion of=20 "coordination node" in the context of a request (not having to deal with=20 an "authoritative" transaction makes it easier). In the case of postgres (or any RDBMS, really), all I can think of is=20 either an inline proxy performing some validation as part of the=20 forwarding (which is what we did internally but that has not been green=20 lit for FOSS :( ) or some logic in the backend that rejects asynchronous=20 commits too if some condition is not met (eg: synchronous=20 standby nodes not present - a builtin version of the pg_stat_replication=20 look-aside CTE I suggested earlier). > its changes with all sync replicas as it's implemented in Stolon=20 > https://github.com/sorintlab/stolon/blob/master/doc/syncrepl.md#handling-= postgresql-sync-repl-limits-under-such-circumstances=20 > . >=20 >=20 > Best regards, > Maksim Milyutin >=20 >=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.