Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rOQFu-0042g5-K4 for pgsql-admin@arkaria.postgresql.org; Fri, 12 Jan 2024 22:44:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rOQFt-005yuP-SC for pgsql-admin@arkaria.postgresql.org; Fri, 12 Jan 2024 22:44:01 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rOQFt-005yuG-Gz for pgsql-admin@lists.postgresql.org; Fri, 12 Jan 2024 22:44:01 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rOQFq-001GXU-Ow for pgsql-admin@lists.postgresql.org; Fri, 12 Jan 2024 22:44:00 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=momjian.us; s=2024010820; h=In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-ID:Content-Description; bh=ggGBSf999MDXJCy6RxHF62T88HJe1OYu7sYEl/l9xkw=; b=JRsTMoLVX+DEULdqq5LuizDzlx PngTZ6USmLJb9W7QDL/0pw12zwIRUjxaUX7sNTwOd49k9g7kj4iZZFgqOvzW5hRBfuquEufn9SAsO qMqs91LIT26cUWT26dmUV8QzMAqM6CA40mIXVX85JXfBDMoZ7/Rx6goPoDzfvuzKMtc4oc91K3dFW Oxp7onU0PVdKEqx54zfFRXfPf+fZ6hO9gKl8GkCyXOHQIZKaV2D121/iHNAW0rBovv/OcKRdadYBv vIQi9Eoe7scEbQIcLj+MqZW7VMR8sHBu5r2EzF7Z5DwR2NxHyO6/fj6WWXn9NJutLtF9m+Leps40e fuQVMEOA==; Received: from bruce by momjian.us with local (Exim 4.96) (envelope-from ) id 1rOQFp-008WaY-0a; Fri, 12 Jan 2024 17:43:57 -0500 Date: Fri, 12 Jan 2024 17:43:57 -0500 From: Bruce Momjian To: "David G. Johnston" Cc: P C , "pgsql-admin@lists.postgresql.org" Subject: Re: Synchronous Replication: Where is data visible first? Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, Jan 10, 2024 at 11:43:50PM -0700, David G. Johnston wrote: > On Wednesday, January 10, 2024, P C wrote: > > Hello, > > While discussing with the development team, an interesting question came > up: in a synchronous streaming replication scenario, with > synchronous_commit = remote_apply, will the change be first visible on > Standby (replica)? Primary will wait till the change is applied and > committed on standby, and hence logically this looks to be correct. But I > couldn't find this mentioned explicitly anywhere and hence seeking comments > from the community. > > > IIUC, no, the standbys can never reflect a newer state than what would be seen > on the primary.  The state of a given transaction, including most importantly > the locking surrounding it, exists first on the primary and then is replicated > to the secondary.  Either the locking will prevent a dirty read or the dirty > read will see the unconfirmed but committed data on the primary.  The locking > will be removed on the primary strictly before the standbys. Uh, my research contradicts that, see: https://momjian.us/main/blogs/pgblog/2020.html#June_3_2020 1 Write commit record to the write-ahead log 2 Flush the write-ahead log to durable storage 3 Update the pg_xact (slide 57) 4 Transfer to replicas -> 5 Mark the commit as visible to other sessions (ProcArrayEndTransaction() updates PGPROC) 6 Communicate commit to the client While the transaction will be visible on the primary to the creating transaction session, it might not be visible to other sessions before it appears on the replica. -- Bruce Momjian https://momjian.us EDB https://enterprisedb.com Only you can decide what is important to you.