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 1uZqA5-00911S-Lv for pgsql-hackers@arkaria.postgresql.org; Thu, 10 Jul 2025 12:14:01 +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 1uZqA3-00BypA-7J for pgsql-hackers@arkaria.postgresql.org; Thu, 10 Jul 2025 12:13:59 +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 1uZqA2-00Byms-5j for pgsql-hackers@lists.postgresql.org; Thu, 10 Jul 2025 12:13:59 +0000 Received: from fout-b2-smtp.messagingengine.com ([202.12.124.145]) by makus.postgresql.org with smtp (Exim 4.96) (envelope-from ) id 1uZqA0-006ZJw-2k for pgsql-hackers@lists.postgresql.org; Thu, 10 Jul 2025 12:13:57 +0000 Received: from phl-compute-06.internal (phl-compute-06.phl.internal [10.202.2.46]) by mailfout.stl.internal (Postfix) with ESMTP id D23331D0024E; Thu, 10 Jul 2025 08:13:55 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-06.internal (MEProxy); Thu, 10 Jul 2025 08:13:55 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=burd.me; h=cc:cc :content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm2; t=1752149635; x=1752236035; bh=s2/TtPHIHQ6awUkyyq8s68pbE5LmLBsX+gO1VH74w0c=; b= eOzD6OszTxo7OePhg+gg8BCjeWn8ef13Tx6hS2cx+N3AAWNh1JuVCjvclsuXHrT3 EX5mPR7wCdw3OFmhWDcnBkxtKahczmet6iAmVSRUb6+rHixsJOHlM/NgGzTwZhss ReOCFEPLvYjCfTiipkSk7bKMt/+zmN91SesDqBXEDzTmXmtY0HG3UzyjmpHfBiHi TqkM3Wa3GCnxixPTQ9F8lev2uKQJqH466CqZrWOXDc3Dkx7dXzvNvtZQ0OiED/A6 kDrNTYxRcjMbuNlh/dElNHstl+8nBDGm/1nZM9z0bgn1EU2yjjAaTKxd6una7VVn v4AbVVU/YlouDhmo9a6uZQ== 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 :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1752149635; x= 1752236035; bh=s2/TtPHIHQ6awUkyyq8s68pbE5LmLBsX+gO1VH74w0c=; b=i niX//AsPHAM+mQbBG4p8WAoNZtGl04NgfNegKkFTOnn9geOlUhhk1ZzDygWEOQSd ds/nUBgpzRe6bR+wSWRA3oKeMO10ATImV8RZVGSCb24BPKdFrXxvip6Yc/fUDLKj VDm3dSHIxKlmqryXBLFP+9FIq3nETNEu/d+eyPz0qIOOEV5oYl/f5UaqKYCbda/f u699CVBD6cOHQscDoAA+VKqhyBFgWxR5BX8osjFOfdhrXGig/lKq/MPkN3arBIef 1zNOrcAnIfcr6VfyJCM0I+0hSES0VGkIJv9VgxKo446vLzHiZ0jwIT4i4EnWFsXj Y93Y4Cbo+B7Wa1HqApFcA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdefgdegtdegudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpegtggfuhfgjffevgffkfhfvofesthhqmhdthhdtjeenucfhrhhomhepfdeuuhhruggm pdcuifhrvghgfdcuoehgrhgvghessghurhgurdhmvgeqnecuggftrfgrthhtvghrnheple dthfeltdeuiefhhefggeduudefueelteejudduffdvheevhfdvhfffudegkeegnecuffho mhgrihhnpehushgvnhhigidrohhrghenucevlhhushhtvghrufhiiigvpedtnecurfgrrh grmhepmhgrihhlfhhrohhmpehgrhgvghessghurhgurdhmvgdpnhgspghrtghpthhtohep hedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtoheprghnughrvghssegrnhgrrhgrii gvlhdruggvpdhrtghpthhtohepthhomhgrshesvhhonhgurhgrrdhmvgdprhgtphhtthho pegrshhhuhhtohhshhdrsggrphgrthdrohhsshesghhmrghilhdrtghomhdprhgtphhtth hopehpghhsqhhlqdhhrggtkhgvrhhssehlihhsthhsrdhpohhsthhgrhgvshhqlhdrohhr ghdprhgtphhtthhopeelvghrthhhrghlihhonheisehgmhgrihhlrdgtohhm X-ME-Proxy: Feedback-ID: i675e48f3:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 10 Jul 2025 08:13:54 -0400 (EDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.600.51.1.1\)) Subject: Re: Adding basic NUMA awareness From: "Burd, Greg" In-Reply-To: Date: Thu, 10 Jul 2025 08:13:43 -0400 Cc: Tomas Vondra , Ashutosh Bapat , PostgreSQL Hackers , Dmitry Dolgov <9erthalion6@gmail.com> Content-Transfer-Encoding: quoted-printable Message-Id: <628EE169-6901-466E-9191-B33DBAB05B26@burd.me> References: To: Andres Freund X-Mailer: Apple Mail (2.3826.600.51.1.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Jul 9, 2025, at 1:23=E2=80=AFPM, Andres Freund = wrote: >=20 > Hi, >=20 > On 2025-07-09 12:55:51 -0400, Greg Burd wrote: >> On Jul 9 2025, at 12:35 pm, Andres Freund wrote: >>=20 >>> FWIW, I've started to wonder if we shouldn't just get rid of the = freelist >>> entirely. While clocksweep is perhaps minutely slower in a single >>> thread than >>> the freelist, clock sweep scales *considerably* better [1]. As it's = rather >>> rare to be bottlenecked on clock sweep speed for a single thread >>> (rather then >>> IO or memory copy overhead), I think it's worth favoring clock = sweep. >>=20 >> Hey Andres, thanks for spending time on this. I've worked before on >> freelist implementations (last one in LMDB) and I think you're onto >> something. I think it's an innovative idea and that the speed >> difference will either be lost in the noise or potentially entirely >> mitigated by avoiding duplicate work. >=20 > Agreed. FWIW, just using clock sweep actually makes things like DROP = TABLE > perform better because it doesn't need to maintain the freelist = anymore... >=20 >=20 >>> Also needing to switch between getting buffers from the freelist and >>> the sweep >>> makes the code more expensive. I think just having the buffer in = the sweep, >>> with a refcount / usagecount of zero would suffice. >>=20 >> If you're not already coding this, I'll jump in. :) >=20 > My experimental patch is literally a four character addition ;), = namely adding > "0 &&" to the relevant code in StrategyGetBuffer(). >=20 > Obviously a real patch would need to do some more work than that. = Feel free > to take on that project, I am not planning on tackling that in near = term. >=20 I started on this last night, making good progress. Thanks for the = inspiration. I'll create a new thread to track the work and = cross-reference when I have something reasonable to show (hopefully = later today). > There's other things around this that could use some attention. It's = not hard > to see clock sweep be a bottleneck in concurrent workloads - partially = due to > the shared maintenance of the clock hand. A NUMAed clock sweep would = address > that. Working on it. Other than NUMA-fying clocksweep there is a function = have_free_buffer() that might be a tad tricky to re-implement = efficiently and/or make NUMA aware. Or maybe I can remove that too? It = is used in autoprewarm.c and possibly other extensions, but no where = else in core. > However, we also maintain StrategyControl->numBufferAllocs, which is a > significant contention point and would not necessarily be removed by a > NUMAificiation of the clock sweep. Yep, I noted this counter and its potential for contention too. = Fortunately, it seems like it is only used so that "bgwriter can = estimate the rate of buffer consumption" which to me opens the door to a = less accurate partitioned counter, perhaps something lock-free (no = mutex/CAS) that is bucketed then combined when read. A quick look at bufmgr.c indicates that recent_allocs (which is = StrategyControl->numBufferAllocs) is used to track a "moving average" = and other voodoo there I've yet to fully grok. Any thoughts on this = approximate count approach? Also, what are your thoughts on updating the algorithm to CLOCK-Pro [1] = while I'm there? I guess I'd have to try it out, measure it a lot and = see if there are any material benefits. Maybe I'll keep that for a = future patch, or at least layer it... back to work! > Greetings, >=20 > Andres Freund best. -greg [1] = https://www.usenix.org/legacy/publications/library/proceedings/usenix05/te= ch/general/full_papers/jiang/jiang_html/html.html