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 1u4dKo-0052ga-Gd for pgsql-performance@arkaria.postgresql.org; Tue, 15 Apr 2025 10:16:07 +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 1u4dKm-007MjC-Pe for pgsql-performance@arkaria.postgresql.org; Tue, 15 Apr 2025 10:16:05 +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 1u4dGP-0075Hg-Ur for pgsql-performance@lists.postgresql.org; Tue, 15 Apr 2025 10:11:34 +0000 Received: from fout-b3-smtp.messagingengine.com ([202.12.124.146]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u4dGM-000C5E-2A for pgsql-performance@lists.postgresql.org; Tue, 15 Apr 2025 10:11:34 +0000 Received: from phl-compute-04.internal (phl-compute-04.phl.internal [10.202.2.44]) by mailfout.stl.internal (Postfix) with ESMTP id 3AC011140287; Tue, 15 Apr 2025 06:11:29 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-04.internal (MEProxy); Tue, 15 Apr 2025 06:11:29 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; 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=1744711889; x=1744798289; bh=jPmOIiZn3HYsyxaFXHVHW9Gv8heoMk+Pv1PLscJSqBs=; b= Lw1Tk0hbjTjbPyBwkLMsx5axP8KzYXdvgrtJRni4/miQOPWdYLAQUYgomuQOIMWW KyPPEkCpTf5W7woMHgoIKyoHWu/AhWyoCdhx9+CYEblqm4DvhViAxJQG4Z7MIEZ8 lToYbeWKYdfngRLSZM7jcDElZkAXYPQFSXe741kq3X1HbTyRUe3EkVMYmEfeBCJ5 uZALNUAGSXDvO3pmbt7FrNQheSLwvb4t0JS0ZJICO9TG79BSLkI0PJhUgxfjydND hLtX74Ccj0JdmGXwcrqnDdnB8tORklXeVL5idQe7RnScAb4POO5SsLx+9Z4AGSqi QcS//6YFHACRA9qbhgyyeQ== 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=1744711889; x= 1744798289; bh=jPmOIiZn3HYsyxaFXHVHW9Gv8heoMk+Pv1PLscJSqBs=; b=T 7WEqQnv88vdzoAsjJq9uRTAZYTmxVC78KM5QfZqaYb9JkbE6IkXJKto3EypZ0o08 103iHMO8NLjVLvysdoBWkXSN1aMapluTV6/UeR21g5TwKsiJbRfJTIiHVMA8eI0k yYGEs9mZbLEs3Lnjz9x6zb9lQ/zvdtLconyoDJ3EWD4vijXgiS9BUT6ypiTvEUBK DA8b7y0jXwv6EjNV/NIyraQUxo8daPndT2CtYVVe1dIzBJBTojXmhh8MOZO9byRs 5vobP6fl3j3wCmPMQB0E4T1oiWViHevBOEZqtFajysAS/I3DP/J5NggDC6XBsaHm zB1tyvoiwba8HyAyyAu5g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvvdefvdduucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpih gvnhhtshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggugfgjsehtkefstddt tdejnecuhfhrohhmpeetnhgurhgvshcuhfhrvghunhguuceorghnughrvghssegrnhgrrh griigvlhdruggvqeenucggtffrrghtthgvrhhnpedtleelvdfgjedvffeiueekfeeuleff hfegfffhgfffkeevueehieehhfeigffhvdenucevlhhushhtvghrufhiiigvpedtnecurf grrhgrmhepmhgrihhlfhhrohhmpegrnhgurhgvshesrghnrghrrgiivghlrdguvgdpnhgs pghrtghpthhtohepfedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepjhgrmhgvsh hprghnghekkeeisehgmhgrihhlrdgtohhmpdhrtghpthhtohepphhgshhqlhdqphgvrhhf ohhrmhgrnhgtvgeslhhishhtshdrphhoshhtghhrvghsqhhlrdhorhhgpdhrtghpthhtoh ephidrshhokhholhhovhesphhoshhtghhrvghsphhrohdrrhhu X-ME-Proxy: Feedback-ID: id4a34324:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 15 Apr 2025 06:11:27 -0400 (EDT) Date: Tue, 15 Apr 2025 06:00:48 -0400 From: Andres Freund To: Yura Sokolov Cc: James Pang , pgsql-performance@lists.postgresql.org Subject: Re: many sessions wait on LWlock WALWrite suddenly Message-ID: References: <20ee7c44-bb93-4590-a984-b7fe4e4a9b14@postgrespro.ru> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20ee7c44-bb93-4590-a984-b7fe4e4a9b14@postgrespro.ru> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On 2025-04-15 12:16:40 +0300, Yura Sokolov wrote: > 11.04.2025 17:36, James Pang пишет: > >    pgv14.8 , during peak time, we suddenly see hundreds of active sessions > > waiting on LWlock  WALWrite at the same time, but we did not find any issue > > on storage . > > any suggestions ? > > No real suggestions... > > There is single WALWrite lock. That's true - but it's worth specifically calling out that the reason you'd see a lot of WALWrite lock wait events isn't typically due to real lock contention. Very often we'll flush WAL for many sessions at once, in those cases the WALWrite lock wait events just indicate that all those sessions are actually waiting for the WAL IO to complete. It'd be good if we could report a different wait event for the case of just waiting for WAL IO to complete, but right now that's not entirely trivial to do reliably. But we could perhaps do at least the minimal thing and report a different wait event if we reach XLogFlush() with an LSN that's already in the process of being written out? > In the results, backends waits each other, or, in other words, they waits > latest of them!!! All backends waits until WAL record written by latest of > them will be written and fsynced to disk. They don't necessarily wait for the *latest* write, they just write for the latest write from the time they started writing. FWIW, in the v1 AIO prototype I had split up the locking for this so that we'd not unnnecessarily need to wait previous writes in many cases - unfortunately for *many* types of storage that turns out to be a significant loss (most extremely on non-enterprise Samsung SSDs). The "maximal" group commit behaviour minimizes the number of durable writes that need to be done, and that is a significant benefit on many forms of storage. On other storage it's a significant benefit to have multiple concurrent flushes, but it's a hard hard tuning problem - I spent many months trying to get it right, and I never fully got there. > (Andres, iiuc it looks to be main bottleneck on the way of increasing > NUM_XLOGINSERT_LOCKS. Right?) I don't think that the "single" WALWriteLock is a blocker to increasing NUM_XLOGINSERT_LOCKS to a meaningful degree. However, I think there's somewhat of an *inverse* relationship. To efficiently flush WAL in smaller increments, we need a cheap way of identifying the number of backends that need to wait up to a certain LSN. For that I think we may need a refinement of the WALInsertLock infrastructure. I think the main blockers for increasing NUM_XLOGINSERT_LOCKS are: 1) Increasing NUM_XLOGINSERT_LOCKS allows more contention on insertpos_lck and spinlocks scale really badly under heavy contention 2) There are common codepaths where we need to iterate over all NUM_XLOGINSERT_LOCKS slots, that turns out to become rather expensive, the relevant cachelines are very commonly not going to be in the local CPU cache. I think we can redesign the mechanism so that there's an LSN ordered ringbuffer of in-progress insertions, with the reservation being a single 64bit atomic increment, without the need for a low limit like NUM_XLOGINSERT_LOCKS (the ring size needs to be limited, but I didn't see a disadvantage with using something like MaxConnections * 2). Greetings, Andres Freund