From: Maksim Milyutin <milyutinma@gmail.com>
To: Andrey Borodin <x4mmm@yandex-team.ru>
To: pgsql-general@lists.postgresql.org
Subject: Re: Commit to primary with unavailable sync standby
Date: Thu, 19 Dec 2019 16:58:37 +0300
Message-ID: <2957464c-f5a2-5e8b-41af-a8809ebb9cd3@gmail.com> (raw)
In-Reply-To: <B70260F9-D0EC-438D-9A59-31CB996B320A@yandex-team.ru>
References: <B70260F9-D0EC-438D-9A59-31CB996B320A@yandex-team.ru>
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...> 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
its changes with all sync replicas as it's implemented in Stolon
https://github.com/sorintlab/stolon/blob/master/doc/syncrepl.md#handling-postgresql-sync-repl-limits...
.
Best regards,
Maksim Milyutin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: milyutinma@gmail.com, x4mmm@yandex-team.ru
Subject: Re: Commit to primary with unavailable sync standby
In-Reply-To: <2957464c-f5a2-5e8b-41af-a8809ebb9cd3@gmail.com>
References: <B70260F9-D0EC-438D-9A59-31CB996B320A@yandex-team.ru> <2957464c-f5a2-5e8b-41af-a8809ebb9cd3@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox