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 1ihwKG-0001iT-5j for pgsql-general@arkaria.postgresql.org; Thu, 19 Dec 2019 13:58:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ihwKE-0005lT-VM for pgsql-general@arkaria.postgresql.org; Thu, 19 Dec 2019 13:58:46 +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 1ihwKE-0005lL-Hb for pgsql-general@lists.postgresql.org; Thu, 19 Dec 2019 13:58:46 +0000 Received: from mail-lf1-x144.google.com ([2a00:1450:4864:20::144]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ihwKB-0003PL-33 for pgsql-general@lists.postgresql.org; Thu, 19 Dec 2019 13:58:45 +0000 Received: by mail-lf1-x144.google.com with SMTP id v201so4396666lfa.11 for ; Thu, 19 Dec 2019 05:58:42 -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=xJRPSJnw84V8kBibegKNyMDWUVQOdWbvudFvxQFDwJ0=; b=ap04WZbCzfJm5PlUNKY0i9uFRczVQBCrGn2gfZxGCcAA3MUe3OmxK5KAE2wC3GF7Vl SLiveIINNsszsbdvt5Ig9gl7cJRdfQPHwmI/0nJ43/Ed5OcByWp0AXqItxDitXwXXa3T E6c/KKz5UFXHQiUJ/rE9QQf84q5HbTkYeZe1P+NxlNXt8Sx5pWLc1vrwoLUI3XUvaOFg zu3oHcxKscwx8LeGm8rK6aPwwzg4a6ADXl7lF/oq1HwGxc8NL/0S69NDKQwLFYCnmWMD RFrQPQcLcZ3edtmmKd2/V+MNWV0pucwq9THOhm4H8HRNYVZkXJBaDTKA8HyHusiECSRJ FjmQ== 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=xJRPSJnw84V8kBibegKNyMDWUVQOdWbvudFvxQFDwJ0=; b=SikTne1YvxDH9vjWBDyiHufvHnrstUfqN3B5Lk7cuTiSCb7oM4USUcm0sqjIkC4pyL tPuk5yExgTLGzasfByygWqIcBzZryl7jLoh1qdJOzvh3dP3bnsetkdA06yXXdFykhiKP /3hMKXBFyIb8SZ8FvGjXNeUJhJWfWJq3GmgRj67z19k9TneOncB1oVlG5/BGz8U1bgGO 3XALMkzslL9XHoxuZhhMErHrPzpgge9LaqsQl5q3FOYyQwfV5iZIKBkQh0tmCIlGfwWy ljdr96P8gytz+siilJ6IynodAqixFRDfXQGqwq6OHFGRKIUajwbBJF8lyZvuDmDAHClT rL2g== X-Gm-Message-State: APjAAAUZdd891rVjfScrbbx+x07Q7HnbgYos3m8S7ujTKZ2xxGyP4DxX 7bL+4X2PkyvUqI/eybjd8Qh5FaNe X-Google-Smtp-Source: APXvYqyYYRrvuyYJNndk1PKmpWymwei3NGvvWNLIPDiaGd0WgzC4WkH+3HxfaK6RhhGCuM1QWFo4rg== X-Received: by 2002:ac2:4909:: with SMTP id n9mr5582301lfi.21.1576763921305; Thu, 19 Dec 2019 05:58:41 -0800 (PST) Received: from [10.25.234.129] ([213.33.200.166]) by smtp.gmail.com with ESMTPSA id e21sm3354798lfc.63.2019.12.19.05.58.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 19 Dec 2019 05:58:39 -0800 (PST) Subject: Re: Commit to primary with unavailable sync standby To: Andrey Borodin , pgsql-general@lists.postgresql.org References: From: Maksim Milyutin Message-ID: <2957464c-f5a2-5e8b-41af-a8809ebb9cd3@gmail.com> Date: Thu, 19 Dec 2019 16:58:37 +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: 7bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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 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-under-such-circumstances . Best regards, Maksim Milyutin