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 1rsf8M-008RvX-5T for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Apr 2024 08:41:15 +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 1rsf8K-008U4g-DY for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Apr 2024 08:41:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rsf8J-008U2k-0S for pgsql-hackers@lists.postgresql.org; Fri, 05 Apr 2024 08:41:12 +0000 Received: from fout4-smtp.messagingengine.com ([103.168.172.147]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rsf8D-000iOy-Mj for pgsql-hackers@lists.postgresql.org; Fri, 05 Apr 2024 08:41:09 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfout.nyi.internal (Postfix) with ESMTP id 249031380105; Fri, 5 Apr 2024 04:41:04 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Fri, 05 Apr 2024 04:41:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1712306464; x= 1712392864; bh=JuM1In2XEg44M0TkTQcwQqGCimuELXNrm5kQodjtgaA=; b=Z ijalI/4vBPJCJi7cUJOU0XA4GZVh5jwvOsXo1BanTSWQaBMD8dsfnccBgGpCWq2g MBROtR79Om+NykDaNeWKaZ/Q+RzPFnrqRraCgIM27/aI/u971J9bHYEcbVrbweuv IehJT1ajn6nEU9q/N2V9WDQh05QJLYVA79QVigxBZxGu0ovmwNBtSmyp+PUI2NLC i2dX1IxO5udd9CRsKnz3ziDQ6DEEgXF6j+pXH55CaIEy/+AomxOxjHwVWk1bLuoN XvdT+FNzoJWhvVgV38VsaXVtOLvt1iAqKXZiTTxfiJoZz4IWGlYm342xcuwst9f8 DAEMN2Tg2s7T5EnGTZeNg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudegtddgtdeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkgggtugfgjgesthekredttddtjeenucfhrhhomheptehlvhgr rhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh eqnecuggftrfgrthhtvghrnhephfdtvddvheeugfegjedukedvfeevueejieehkeeftdfh ffevudeggffhkedvkeefnecuffhomhgrihhnpehmvghrrhhirghmqdifvggsshhtvghrrd gtohhmpdgvnhhtvghrphhrihhsvggusgdrtghomhenucevlhhushhtvghrufhiiigvpedt necurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqih hprdhorhhg X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 5 Apr 2024 04:41:03 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1712306461; bh=hkdTPeaiqyv/8q4a0oeuSCv432OF4yCD3yujLCbzgJs=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=DcesJuTrTdRShQycaW6kDRL23qZBF9nERJzH29Vuv7p/aRfVtS9CvA/FygPmw4E/P mtFBFd4Zd4p6DOTaIZyoFIPzNdfuQoP4X8LFLapIH7ZWytDtYRgR5bi+IikigbcQgj N+REO/ZIdId+/Uyapip7ZoIWHGpMU1YunFANsJOhjx2X2JBz4akmV+bv2ilk+eemWG Nr4Fcm3BWwcg6I9igQ0aZpR8d+zfW3b1DJ4neDq6t6bdSE4AwX/ATiQDsElj9uPeEf 19KYRmlUPIxnHuGCkrnIbUOhsEdqhKi11kZPAWDp232rmZU7dsAJ8nAIRqovOUxfZ7 j7lJ2VEamYwDw== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id 4084F46F; Fri, 5 Apr 2024 10:41:01 +0200 (CEST) Date: Fri, 5 Apr 2024 10:41:01 +0200 From: Alvaro Herrera To: Bharath Rupireddy Cc: Jeff Davis , Tom Lane , Andres Freund , pgsql-hackers@lists.postgresql.org, Jaime Casanova Subject: Re: LogwrtResult contended spinlock Message-ID: <202404050841.qajhfn4wkksi@alvherre.pgsql> 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 2024-Apr-05, Bharath Rupireddy wrote: > 1. > /* > * Update local copy of shared XLogCtl->log{Write,Flush}Result > + * > + * It's critical that Flush always trails Write, so the order of the reads is > + * important, as is the barrier. > */ > #define RefreshXLogWriteResult(_target) \ > do { \ > - _target.Write = XLogCtl->logWriteResult; \ > - _target.Flush = XLogCtl->logFlushResult; \ > + _target.Flush = pg_atomic_read_u64(&XLogCtl->logFlushResult); \ > + pg_read_barrier(); \ > + _target.Write = pg_atomic_read_u64(&XLogCtl->logWriteResult); \ > } while (0) > > Is it "Flush always trails Write" or "Flush always leades Write"? I > guess the latter, no? Well, Flush cannot lead Write. We cannot Flush what hasn't been Written! To me, "Flush trails Write" means that flush is behind. That seems supported by Merriam-Webster[1], which says in defining it as a transitive verb 3 a : to follow upon the scent or trace of : TRACK b : to follow in the footsteps of : PURSUE c : to follow along behind d : to lag behind (someone, such as a competitor) Maybe it's not super clear as a term. We could turn it around and say "Write always leads Flush". [1] https://www.merriam-webster.com/dictionary/trail The reason we have the barrier, and the reason we write and read them in this order, is that we must never read a Flush value that is newer than the Write value we read. That is: the values are written in pairs (w1,f1) first, (w2,f2) next, and so on. It's okay if we obtain (w2,f1) (new write, old flush), but it's not okay if we obtain (w1,f2) (old write, new flush). If we did, we might end up with a Flush value that's ahead of Write, violating the invariant. > 2. > + > + pg_atomic_write_u64(&XLogCtl->logWriteResult, LogwrtResult.Write); > + pg_write_barrier(); > + pg_atomic_write_u64(&XLogCtl->logFlushResult, LogwrtResult.Flush); > } > > Maybe add the reason as to why we had to write logWriteResult first > and then logFlushResult, similar to the comment atop > RefreshXLogWriteResult? Hmm, I had one there and removed it. I'll put something back. > > For 0002, did you consider having pg_atomic_monotonic_advance_u64() > > return the currval? > > +1 for returning currval here. Oh yeah, I had that when the monotonic stuff was used by 0001 but lost it when I moved to 0002. I'll push 0001 now and send an updated 0002 with the return value added. -- Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/ "La rebeldía es la virtud original del hombre" (Arthur Schopenhauer)