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 1qpx9m-007frt-Mc for pgsql-hackers@arkaria.postgresql.org; Mon, 09 Oct 2023 20:47:14 +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 1qpx9j-001FHB-SC for pgsql-hackers@arkaria.postgresql.org; Mon, 09 Oct 2023 20:47:12 +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 1qpx9j-001FGV-GY for pgsql-hackers@lists.postgresql.org; Mon, 09 Oct 2023 20:47:12 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qpx9d-001Aqw-9z for pgsql-hackers@postgresql.org; Mon, 09 Oct 2023 20:47:11 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 2F7685C02B1; Mon, 9 Oct 2023 16:47:04 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Mon, 09 Oct 2023 16:47:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to; s=fm2; t=1696884424; x=1696970824; bh=6v kcSrxgQgJK6VhvnSwOXXYxfILFZvRTBLDkhs9fQbo=; b=Dx5vIPIgfvYD6rnC+p +XVuYV+6UBflFdyG+ikMt8jEa+mcfycaEzj+7rtS6fQGM+znFkUFrL3/4/hVr6ID WngX4WsU91qBWZNQkHrZ7x2ry+29NqzZP3EuXIThmHpX7dhUZFeXs/hOhFyHvMFo jvgxIyQUC3XuerrEtOMxmHfI43SIQfcF4vdfrbxi3rl0+dnfyQPaOzf3UGBpUrsy vmunYoiCfllMK9EYxNeh1Y6BWN8obQUhO5FIA95mufSOExjn4XJWEoNTd/rZsJdS og8Cwv5Vnei52FVdfsUGtONYm/PAbZclLxM6fzSVeDL3zTz6bAqEruzDKxhLyPLP 1Bmg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; t=1696884424; x=1696970824; bh=6vkcSrxgQgJK6 VhvnSwOXXYxfILFZvRTBLDkhs9fQbo=; b=fJq0GCN2kZE3PC9Q767WIuUk3N0v5 j1FWo+b3mczCUpT2BcNnfdjaOHbu+3K2oaa5MCuVgCNZ5HC68+ETHhoizvq1KhbO iBzzHGTVjh3Jq2+SwzK8a+Z4zdzNJCGYZ/4v88SWcE+HsXuJ6voh7hkRUgb+4ujE lifHnw75866tt9vCGRlYLeKXsd0Dd/SUCZzl1XZaZ1cm0H4Fp7VTTAqmLFOCLKOm p9kbbYKeA8663CZAzD9iXvE8CV3yp98KW6xgmBaWBbaQb8gA1PHggEDk7fulEdCP DP2l+5DNGiU00uLDaiFVrxbtlSn132JzNxT8qHsxUbTgtus9LE+8JgOFQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrheefgdduheefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomheptehnughr vghsucfhrhgvuhhnugcuoegrnhgurhgvshesrghnrghrrgiivghlrdguvgeqnecuggftrf grthhtvghrnhepgfevieelhffhvdefgeelveevfffgkeeitdevhedtgfeigeevhefgudeu udegjedunecuffhomhgrihhnpehuohhpshdrihhnfhhonecuvehluhhsthgvrhfuihiivg eptdenucfrrghrrghmpehmrghilhhfrhhomheprghnughrvghssegrnhgrrhgriigvlhdr uggv X-ME-Proxy: Feedback-ID: id4a34324:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 9 Oct 2023 16:47:03 -0400 (EDT) Date: Mon, 9 Oct 2023 13:47:02 -0700 From: Andres Freund To: Robert Haas Cc: Dilip Kumar , Heikki Linnakangas , pgsql-hackers Subject: Re: New WAL record to detect the checkpoint redo location Message-ID: <20231009204702.cy3ilp6wgh7a5qms@awork3.anarazel.de> References: <20230714151626.rhgae7taigk2xrq7@awork3.anarazel.de> <20231005183400.n5myso7vu6crd656@alap3.anarazel.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, As noted in my email from a few minutes ago, I agree that optimizing this shouldn't be a requirement for merging the patch. On 2023-10-09 15:58:36 -0400, Robert Haas wrote: > 1. The reason why we're doing this multiplication and division is to > make sure that the code in ReserveXLogInsertLocation which executes > while holding insertpos_lck remains as simple and brief as possible. > We could eliminate the conversion between usable byte positions and > LSNs if we replaced Insert->{Curr,Prev}BytePos with LSNs and had > ReserveXLogInsertLocation work out by how much to advance the LSN, but > it would have to be worked out while holding insertpos_lck (or some > replacement lwlock, perhaps) and that cure seems worse than the > disease. Given that, I think we're stuck with converting between > usable bye positions and LSNs, and that intrinsically needs some > multiplication and division. Right, that's absolutely crucial for scalability. > 2. It seems possible to remove one branch in each of > XLogBytePosToRecPtr and XLogBytePosToEndRecPtr. Rather than testing > whether bytesleft < XLOG_BLCKSZ - SizeOfXLogLongPHD, we could simply > increment bytesleft by SizeOfXLogLongPHD - SizeOfXLogShortPHD. Then > the rest of the calculations can be performed as if every page in the > segment had a header of length SizeOfXLogShortPHD, with no need to > special-case the first page. However, that doesn't get rid of any > multiplication or division, just a branch. This reminded me about something I've been bugged by for a while: The whole idea of short xlog page headers seems like a completely premature optimization. The page header is a very small amount of the overall data (long: 40/8192 ~= 0.00488, short: 24/8192 ~= 0.00292), compared to the space we waste in many other places, including on a per-record level, it doesn't seem worth the complexity. > 3. Aside from that, there seems to be no simple way to reduce the > complexity of an individual calculation, but ReserveXLogInsertLocation > does perform 3 rather similar computations, and I believe that we know > that it will always be the case that *PrevPtr < *StartPos < *EndPos. > Maybe we could have a fast-path for the case where they are all in the > same segment. We could take prevbytepos modulo UsableBytesInSegment; > call the result prevsegoff. If UsableBytesInSegment - prevsegoff > > endbytepos - prevbytepos, then all three pointers are in the same > segment, and maybe we could take advantage of that to avoid performing > the segment calculations more than once, but still needing to repeat > the page calculations. Or, instead or in addition, I think we could by > a similar technique check whether all three pointers are on the same > page; if so, then *StartPos and *EndPos can be computed from *PrevPtr > by just adding the difference between the corresponding byte > positions. I think we might be able to speed some of this up by pre-compute values so we can implement things like bytesleft / UsableBytesInPage with shifts. IIRC we already insist on power-of-two segment sizes, so instead of needing to divide by a runtime value, we should be able to shift by a runtime value (and the modulo should be a mask). > I'm not really sure whether that would come out cheaper. It's just the > only idea that I have. It did also occur to me to wonder whether the > apparent delays performing multiplication and division here were > really the result of the arithmetic itself being slow or whether they > were synchronization-related, SpinLockRelease(&Insert->insertpos_lck) > being a memory barrier just before. But I assume you thought about > that and concluded that wasn't the issue here. I did verify that they continue to be a bottleneck even after (incorrectly obviously), removing the spinlock. It's also not too surprising, the latency of 64bit divs is just high, particularly on intel from a few years ago (my cascade lake workstation) and IIRC there's just a single execution port for it too, so multiple instructions can't be fully parallelized. https://uops.info/table.html documents a worst case latency of 89 cycles on cascade lake, with the division broken up into 36 uops (reducing what's available to track other in-flight instructions). It's much better on alter lake (9 cycles and 7 uops on the perf cores, 44 cycles and 4 uops on efficiency cores) and on zen 3+ (19 cycles, 2 uops). Greetings, Andres Freund