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 1ijQhU-0005MO-5r for pgsql-general@arkaria.postgresql.org; Mon, 23 Dec 2019 16:36:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ijQhS-00045O-UD for pgsql-general@arkaria.postgresql.org; Mon, 23 Dec 2019 16:36:54 +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 1ijQhS-000430-JZ for pgsql-general@lists.postgresql.org; Mon, 23 Dec 2019 16:36:54 +0000 Received: from mail-lj1-x244.google.com ([2a00:1450:4864:20::244]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ijQhP-00015n-R7 for pgsql-general@lists.postgresql.org; Mon, 23 Dec 2019 16:36:54 +0000 Received: by mail-lj1-x244.google.com with SMTP id r19so18305475ljg.3 for ; Mon, 23 Dec 2019 08:36:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=f9BS3TIpLAHqrPbox152pQaAR9YsnRJTjxC1ZVqJltQ=; b=cfhBzDy28eDk9zyC/0pnlYWu1Tke4/jXOoUBB0uXh8KYsMT6LVGaNaasNYl+kfQGVG ShDdAa7XZPpwMksr7F3a/WnzV77faisGq3W39U4cdo4krZTbY+CCcBfTlahm+WOZjdnN MkeLUAzOzb4sj3Lejq3a1sRLfTLSyZbuu+rz7zKQOFJKWCgVVzh257uFU7MAg54FNSEX YP2O1Afx2kz5LGsYB9n7W0iUGw/zJywzHwBWvrbhaeG/5ikhKikWKZX/ylUJ7t8Oc2DO 0qppVLk8C8IX+FZbxb2ZUKdtia+LtUYZtkW1cn3P1mD7rEmalEcX5EFm2RmRkWii4rrp lzQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=f9BS3TIpLAHqrPbox152pQaAR9YsnRJTjxC1ZVqJltQ=; b=TYND35a1F4qe+7vjWL333M2VtepRPBfTVmx8tgSqv9Ks+KNEKIPohzXYiBnT6rtXwy Dilxqf3GGz65cacm2wJC0L4BanmvXxZhqnGAfSgQ9MAj4uLpToCd8Zm3knILJyUTIimJ Qsr0q0SSuQMWkDS4XaevuVBLdFUK3GFS7ni/5Ua0lcGjF5mM1NCbUSW4lekKdkDs2LhG qB3CA4yrq4HEngy7kXxi8nKivcgOG4dthFn4J50NB6XwCyvrcZRN3jD8qIRTsaknUvSZ EKrMyPprInCWN3KKUVTqYlQdVWcqp1qhqsLCemCWYoSzRn95f8/qNtont8UnNsIeQR/U 4ANA== X-Gm-Message-State: APjAAAVQEiRMR9ipFHj32lxSxP6dfE8e4gRnlF/qGrHtyzA+hqT1z3Qq CXGx/V78wx8llo2SWuuMQy2LyM30 X-Google-Smtp-Source: APXvYqwa+MkW5LZJJ6Qd2bDaU6gozdvp+fz9i2IOWYN4vjBwMHzZpqxIq6eAfunKkEjYYaTVoR82Qw== X-Received: by 2002:a2e:b017:: with SMTP id y23mr18958553ljk.229.1577119010832; Mon, 23 Dec 2019 08:36:50 -0800 (PST) Received: from [10.25.234.129] ([213.33.200.166]) by smtp.gmail.com with ESMTPSA id a14sm8528623lfh.50.2019.12.23.08.36.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 23 Dec 2019 08:36:50 -0800 (PST) Subject: Re: Commit to primary with unavailable sync standby To: f.venchiarutti@ocado.com, pgsql-general@lists.postgresql.org References: <2957464c-f5a2-5e8b-41af-a8809ebb9cd3@gmail.com> From: Maksim Milyutin Message-ID: <13d29c87-9864-8298-45b1-0684a69746a4@gmail.com> Date: Mon, 23 Dec 2019 19:36:49 +0300 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-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 19.12.2019 18:08, Fabio Ugo Venchiarutti wrote: > > > On 19/12/2019 13:58, Maksim Milyutin wrote: >> On 19.12.2019 14:04, Andrey Borodin wrote: >> >>> Hi! >> >> >> Hi! >> >> FYI, this topic was up recently in -hackers >> https://www.postgresql.org/message-id/CAEET0ZHG5oFF7iEcbY6TZadh1mosLmfz1HLm311P9VOt7Z+jeg@mail.gmail.com >> >> >> >>> 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? >>> >> >> I think the most appropriate way to handle such issues is to catch by >> client driver such warnings (with message about local commit) and >> mark the status of posted transaction as undetermined. If connection >> with sync replica will come back then this transaction eventually >> commits but after triggering of autofailover and *not replicating >> this commit to replica* this commit aborts. Therefore client have to >> wait some time (that exceeds the duration of autofailover) and check >> (logically based on committed data) the status of commit. >> >> The problem here is the locally committed data becomes visible to >> future transactions (before autofailover) that violates the property >> of consistent reading from master. IMO the more correct behavior for >> PostgreSQL here is to ignore any cancel / termination queries when >> backend is in status of waiting response from sync replicas. >> >> However, there is another way to get locally applied commits via >> restart of master after initial recovery. This case is described in >> doc >> https://www.postgresql.org/docs/current/warm-standby.html#SYNCHRONOUS-REPLICATION-HA >> . But here HA orchestrator agent can close access from external users >> (via pg_hba.conf manipulations) until PostgreSQL instance synchronizes > > > And this is where the unsafety lies: that assumes that the isolated > master is in enough of a sane state to apply a self-ban (and that can > do it in near-zero time). > > > Although the retry logic in Andrey's case is probably not ideal (and > you offered a more correct approach to synchronous commit), there are > many "grey area" failure modes that in his scenario would either > prevent a given node from sealing up fast enuogh if at all (eg: PID > congestion causing fork()/system() to fail while backends are already > up and happily flushing WAL). > > > This is particularly relevant to situations when only a subset of > critical transactions set synchronous_commit to remote_*: it'd still > be undesirable to sink "tier 2" data in a stale primary for any > significant length of time). Could you more concrete describe your thesis? In my proposal the self-ban to master is applied after restarting one so that changes from locally committed transactions was not visible for new incoming transactions. > In the case of postgres (or any RDBMS, really), all I can think of is > either an inline proxy performing some validation as part of the > forwarding (which is what we did internally but that has not been > green lit for FOSS :( ) External validation unfortunately is not option here. AIMB the local commits become visible to future transactions coming to master and even if some proxy reports to client that transaction is not committed completely, new incoming transactions reading locally applied changes and making its changes based on these ones implicitly confirms the status of these changes as committed. > or some logic in the backend that rejects asynchronous commits too if > some condition is not met (eg: synchronous standby nodes > not present - a builtin version of the pg_stat_replication look-aside > CTE I suggested earlier). CTE with sub-query using pg_stat_replication is not option too. The view pg_stat_replication is in fact shows the stale info about statuses of replicas and is formed from statuses of wal_sender processes. That is when replica loses contact with master then at most wal_sender_timeout master will see this replica in pg_stat_replication without any changes of row attributes, so local commits also are capable to overslip inside this timeout. -- Best regards, Maksim Milyutin