Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nNiVb-00074G-VJ for pgsql-hackers@arkaria.postgresql.org; Fri, 25 Feb 2022 21:52:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nNiVZ-00085H-R1 for pgsql-hackers@arkaria.postgresql.org; Fri, 25 Feb 2022 21:52:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nNiVZ-000858-0l for pgsql-hackers@lists.postgresql.org; Fri, 25 Feb 2022 21:52:13 +0000 Received: from smtp-fw-9102.amazon.com ([207.171.184.29]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nNiVV-0000FN-Sq for pgsql-hackers@lists.postgresql.org; Fri, 25 Feb 2022 21:52:12 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1645825930; x=1677361930; h=message-id:date:mime-version:to:cc:references:from: in-reply-to:content-transfer-encoding:subject; bh=QmqxA+7k7ctCaVHpWbHb+DpWKQddyIMS18BP+qygmag=; b=aUm0wyZHydUKYOXW6IKXoXuD8KuIyw6vgHRXZ2bb3QJ0czHHS+fJae5Z W7WtiW8JZxdvy2F9CErdCsN5jMzPTJV+wCLyfI+ivGMVUL3AAgBxEPvx6 hxlVEkoCOGIjtzRGYxhYZt+K4bjNTnLvCkX7pcJLNtnszjVP/GfGiXfJu 8=; X-IronPort-AV: E=Sophos;i="5.90,137,1643673600"; d="scan'208";a="197844840" Subject: Re: Allow async standbys wait for sync replication (was: Disallow quorum uncommitted (with synchronous standbys) txns in logical replication subscribers) Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO email-inbound-relay-pdx-2b-28a78e3f.us-west-2.amazon.com) ([10.25.36.214]) by smtp-border-fw-9102.sea19.amazon.com with ESMTP; 25 Feb 2022 21:52:06 +0000 Received: from EX13MTAUWB001.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan2.pdx.amazon.com [10.236.137.194]) by email-inbound-relay-pdx-2b-28a78e3f.us-west-2.amazon.com (Postfix) with ESMTPS id 78A2AA2B18; Fri, 25 Feb 2022 21:52:05 +0000 (UTC) Received: from EX13D11UWC004.ant.amazon.com (10.43.162.101) by EX13MTAUWB001.ant.amazon.com (10.43.161.207) with Microsoft SMTP Server (TLS) id 15.0.1497.28; Fri, 25 Feb 2022 21:52:04 +0000 Received: from [10.135.235.123] (10.43.160.203) by EX13D11UWC004.ant.amazon.com (10.43.162.101) with Microsoft SMTP Server (TLS) id 15.0.1497.28; Fri, 25 Feb 2022 21:52:04 +0000 Message-ID: Date: Fri, 25 Feb 2022 13:52:03 -0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.5.1 Content-Language: en-US To: Nathan Bossart , Bharath Rupireddy CC: SATYANARAYANA NARLAPURAM , PostgreSQL Hackers References: <20220225193819.GB662561@nathanxps13> From: "Hsu, John" In-Reply-To: <20220225193819.GB662561@nathanxps13> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.43.160.203] X-ClientProxiedBy: EX13D45UWA001.ant.amazon.com (10.43.160.91) To EX13D11UWC004.ant.amazon.com (10.43.162.101) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello, On 2/25/22 11:38 AM, Nathan Bossart wrote: > CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe. > > > > On Fri, Feb 25, 2022 at 08:31:37PM +0530, Bharath Rupireddy wrote: >> Thanks Satya and others for the inputs. Here's the v1 patch that >> basically allows async wal senders to wait until the sync standbys >> report their flush lsn back to the primary. Please let me know your >> thoughts. > I haven't had a chance to look too closely yet, but IIUC this adds a new > function that waits for synchronous replication. This new function > essentially spins until the synchronous LSN has advanced. > > I don't think it's a good idea to block sending any WAL like this. AFAICT > it is possible that there will be a lot of synchronously replicated WAL > that we can send, and it might just be the last several bytes that cannot > yet be replicated to the asynchronous standbys. І believe this patch will > cause the server to avoid sending _any_ WAL until the synchronous LSN > advances. > > Perhaps we should instead just choose the SendRqstPtr based on the current > synchronous LSN. Presumably there are other things we'd need to consider, > but in general, I think we ought to send as much WAL as possible for a > given call to XLogSendPhysical(). I think you're right that we'll avoid sending any WAL until sync_lsn advances. We could setup a contrived situation where the async-walsender never advances because it terminates before the flush_lsn of the synchronous_node catches up. And when the async-walsender restarts, it'll start with the latest flushed on the primary and we could go into a perpetual loop. I took a look at the patch and tested basic streaming with async replicas ahead of the synchronous standby and with logical clients as well and it works as expected. > > ereport(LOG, >            (errmsg("async standby WAL sender with request LSN %X/%X is waiting as sync standbys are ahead with flush LSN %X/%X", >                    LSN_FORMAT_ARGS(flushLSN), LSN_FORMAT_ARGS(sendRqstPtr)), >             errhidestmt(true))); I think this log formatting is incorrect. s/sync standbys are ahead/sync standbys are behind/ and I think you need to swap flushLsn and sendRqstPtr When a walsender is waiting for the lsn on the synchronous replica to advance and a database stop is issued to the writer, the pg_ctl stop isn't able to proceed and the database seems to never shutdown. > Assert(priority >= 0); What's the point of the assert here? Also the comments/code refer to AsyncStandbys, however it's also used for logical clients, which may or may not be standbys. Don't feel too strongly about the naming here but something to note. > if (!ShouldWaitForSyncRepl()) >        return; > ... > for (;;) > { >    // rest of work > } If we had a walsender already waiting for an ack, and the conditions of ShouldWaitForSyncRepl() change, such as disabling async_standbys_wait_for_sync_replication or synchronous replication it'll still wait since we never re-check the condition. postgres=# select wait_event from pg_stat_activity where wait_event like 'AsyncWal%';               wait_event --------------------------------------  AsyncWalSenderWaitForSyncReplication  AsyncWalSenderWaitForSyncReplication  AsyncWalSenderWaitForSyncReplication (3 rows) postgres=# show synchronous_standby_names;  synchronous_standby_names --------------------------- (1 row) postgres=# show async_standbys_wait_for_sync_replication;  async_standbys_wait_for_sync_replication ------------------------------------------  off (1 row) >    LWLockAcquire(SyncRepLock, LW_SHARED); >    flushLSN = walsndctl->lsn[SYNC_REP_WAIT_FLUSH]; >    LWLockRelease(SyncRepLock); Should we configure this similar to the user's setting of synchronous_commit instead of just flush? (SYNC_REP_WAIT_WRITE, SYNC_REP_WAIT_APPLY) Thanks, John H