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 1u4e0M-005EER-1n for pgsql-performance@arkaria.postgresql.org; Tue, 15 Apr 2025 10:59: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 1u4e0J-00831J-Cn for pgsql-performance@arkaria.postgresql.org; Tue, 15 Apr 2025 10:59:00 +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 1u4e0I-0082zh-TN for pgsql-performance@lists.postgresql.org; Tue, 15 Apr 2025 10:58:59 +0000 Received: from mail.postgrespro.ru ([93.174.132.70]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u4e0G-000CSy-1t for pgsql-performance@lists.postgresql.org; Tue, 15 Apr 2025 10:58:59 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1744714736; bh=EsYgfWAKLCcAibVKscAcope6FsAKaYaVLP+LTXuDhHY=; h=Message-ID:Date:User-Agent:Subject:To:Cc:References:From: In-Reply-To:From; b=sZmCP75WtRBdnQ+EFxxy/EkOQMaJbKpezwpu4X/LnWhzfSKXSDPEWXx6FElRFcHdk hnHzl8AazMkpkjN3djX2+UeVWgMPNWw3rHQGG8bm4H+Szrx8ZQdMC8N6sXaDTKBsqt +ZoriD372yfOiF7p+K3j6+Eet9NSFiPgVjQAshD9suNz0Fwhi8ZULHxJ5uJCxnjb0w 0VtSiHL8u8Kiwo/lcBhaLdDJnBj8Jx8vC04eqpms2QBuCPOdhUmaxD0w3q6jNLkx8P owJ521bvHFGUCJuPXojVDPzBh7cELkiS/RPoygL+FYBYmRuk8XWkrVjNYcEbKj8jNn SUpiz5QPKm+Cg== Received: from [172.30.50.150] (unknown [93.174.131.138]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (Client did not present a certificate) (Authenticated sender: y.sokolov@postgrespro.ru) by mail.postgrespro.ru (Postfix/465) with ESMTPSA id 3548660363; Tue, 15 Apr 2025 13:58:56 +0300 (MSK) Message-ID: Date: Tue, 15 Apr 2025 13:58:55 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: many sessions wait on LWlock WALWrite suddenly To: Andres Freund Cc: James Pang , pgsql-performance@lists.postgresql.org References: <20ee7c44-bb93-4590-a984-b7fe4e4a9b14@postgrespro.ru> <4f101390-560c-4bd4-a547-e1c6ad90fe7f@postgrespro.ru> Content-Language: en-US From: Yura Sokolov In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-KSMG-AntiPhishing: NotDetected X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.1.0.7854, bases: 2025/04/15 09:26:00 #27887931 X-KSMG-AntiVirus-Status: NotDetected, skipped X-KSMG-LinksScanning: not scanned, disabled by settings X-KSMG-Message-Action: skipped X-KSMG-Rule-ID: 1 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 15.04.2025 13:53, Andres Freund пишет: > Hi, > > On 2025-04-15 13:44:09 +0300, Yura Sokolov wrote: >> 15.04.2025 13:00, Andres Freund пишет: >>> 1) Increasing NUM_XLOGINSERT_LOCKS allows more contention on insertpos_lck and >>> spinlocks scale really badly under heavy contention >>> >>> 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). >> >> There is such attempt at [1]. And Zhiguo tells it really shows promising >> results. >> >> No, I did it not with "ring-buffer", but rather with hash-table. But it is >> still lock-free. > > I don't find that approach particularly promising - I do think we want this to > be an ordered datastructure, not something as fundamentally unordered as a > hashtable. I've tried to construct such thing. But "Switch WAL" record thing didn't allow me to finish the design. Because "Switch WAL" have no fixed size, and it is allowed to not be inserted. It breaks ordering. Probably, I just didn't think hard enough to work around. And certainly I though about it only for log reservation, not for waiting on insertion to complete, nor for waiting writing to complete. >> And then all stuck in WALWrite lock. > > That will often, but not always, mean that you're just hitting the IO > throughput of the storage device. Right now it's too hard to tell the > difference, hence the suggestion to make the wait events more informative. > > >>> 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. >> >> I believe, LWLockWaitForVar should be redone: >> - currently it waits for variable to change (ie to be disctinct from >> provided value). >> - but I believe, it should wait for variable to be greater than provided value. > > I think we should simply get rid of the mechanism alltogether :) > > >> This way: >> - WALInsertLock waiter will not awake for every change of insertingAt >> - process, which writes and fsync WAL, will be able to awake waiters on >> every fsync, instead of end of whole write. >> >> It will reduce overhead of waiting WALInsertLock a lot, and will greately >> reduce time spend on waiting WALWrite lock. > >> Btw, insertingAt have to be filled at the start of copying wal record to >> wal buffers. Yes, we believe copying of small wal record is fast, but when >> a lot of wal inserters does their job, we needlessly sleep on their >> WALInsertLock although they are already in the future. > > Yes, that's a problem - but it also adds some overhead. I think we'll be > better off going with the ringbuffer approach where insertions are naturally > ordered and we can wait for precisely the insertions that we need to. -- regards Yura Sokolov aka funny-falcon