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 1u4dm5-005AFN-A8 for pgsql-performance@arkaria.postgresql.org; Tue, 15 Apr 2025 10:44:18 +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 1u4dm2-007mmJ-RV for pgsql-performance@arkaria.postgresql.org; Tue, 15 Apr 2025 10:44:15 +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 1u4dm2-007mmB-9s for pgsql-performance@lists.postgresql.org; Tue, 15 Apr 2025 10:44:15 +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 1u4dlz-000CMf-2P for pgsql-performance@lists.postgresql.org; Tue, 15 Apr 2025 10:44:14 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1744713851; bh=rOn/WNisGoqAOmrBPV9nnoJmHIoVxBna6iDl0IxDU+4=; h=Message-ID:Date:User-Agent:Subject:To:Cc:References:From: In-Reply-To:From; b=Bxtcime0olObryJPBscUW9Lf3Y0JiDpjkyGQs938CbLW62Ayst1gTB1C2Wb+tIdZq F2vHZkeo4jZJK9c/ZyRJs/lam0Ew2f91uv0Q0B3FuigCiM71YPeThSefmC5ZTknzbz vM6ewKxtDJEd2EKW4QiHUGVQBLML1lzxz0EsrsmyT8K5sGxmjjVG2xShl9FmYvmRgI QBYW33QLNpYLYHQH2htFj/OOuYsJxtdyLMG+E1MACP4skKUs+6xSXc68zhqGQ1OO1g pkkZ9B/kGKO6DmmjYhGhO/hdRer6QEj0iwAcr58QYt16X7Z7Nuu72hc4z/ynOrii9w eGVS+eobS8mnA== 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) server-digest SHA256) (Client did not present a certificate) (Authenticated sender: y.sokolov@postgrespro.ru) by mail.postgrespro.ru (Postfix/465) with ESMTPSA id B824A6029B; Tue, 15 Apr 2025 13:44:10 +0300 (MSK) Message-ID: <4f101390-560c-4bd4-a547-e1c6ad90fe7f@postgrespro.ru> Date: Tue, 15 Apr 2025 13:44:09 +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> 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, bases: 2025/04/15 10:11:00 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: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. But after implementing that I found WALBufMappingLock [2] (which is already removed). And then all stuck in WALWrite lock. > 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. 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. [1] https://commitfest.postgresql.org/patch/5633/ [2] https://commitfest.postgresql.org/patch/5511/ -- regards Yura Sokolov aka funny-falcon