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 1qlnmo-004QFM-VK for pgsql-hackers@arkaria.postgresql.org; Thu, 28 Sep 2023 09:58:23 +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 1qlnmn-00AwXQ-Iu for pgsql-hackers@arkaria.postgresql.org; Thu, 28 Sep 2023 09:58:21 +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 1qlnmn-00AwX9-5E for pgsql-hackers@lists.postgresql.org; Thu, 28 Sep 2023 09:58:21 +0000 Received: from meesny.iki.fi ([2001:67c:2b0:1c1::201]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qlnmj-00848v-RW for pgsql-hackers@postgresql.org; Thu, 28 Sep 2023 09:58:20 +0000 Received: from [192.168.1.115] (dsl-hkibng22-54f8db-125.dhcp.inet.fi [84.248.219.125]) (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) (No client certificate requested) (Authenticated sender: hlinnaka) by meesny.iki.fi (Postfix) with ESMTPSA id 4Rx8786zyFzyYH; Thu, 28 Sep 2023 12:58:12 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=meesny; t=1695895093; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UjiZvrOBu1a1DuyiTq1SxvEEK1zH7vCQ2QiZUSqQwOw=; b=F7C2rJyHR1kqKnCO4MlcdH/wqXORRw3pEVpCuOUz5JMAoaYynd8P47Z4yK6J+zEjS0iuZT uxKq0ajNkrovcWhdqW/Xx+2jDzWnag4r7SzttXyNJlcI+hWDHE7mL2uIQmkCwrYohWoLwp VtztYsocRh6hQqhjImzdgBBz2qcR+2c= ARC-Seal: i=1; s=meesny; d=iki.fi; t=1695895093; a=rsa-sha256; cv=none; b=QyKO/HajqtxZwuSyCLFBlmqZy3EziPa7Xdnb0sLj5g4f2LSJZWnJ/uHkzqnFLxqiYnRZnI shXJgNIPG3GrD7yFlaY2d3W3ZedP3bFDbRmnkFEG9tkpwyT85TjGBBel2Lyai0IBmi+s/F dtSwMJKIgXzmxjV1WFpsJHeRomjh65c= ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=meesny; t=1695895093; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UjiZvrOBu1a1DuyiTq1SxvEEK1zH7vCQ2QiZUSqQwOw=; b=aVV2TkAFjv8xZ60dm1yxh9mC4ZH1KBu9jDc6csbkwriZTuaZnGb/UzXZz/4yvMuwkwaFiG AQZwcIF8IMmz9rcTwTVZ7k4H3nRq/eF6Vehy5JCdzgayY/p37DbCuMTPRnHuF2+MR/ToIB H5JxI6b6f2XPZ1fP3uBlZ+B7PBVU8zs= Message-ID: <53c844e1-a21a-e481-cca5-2184c8aa47b9@iki.fi> Date: Thu, 28 Sep 2023 12:58:12 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: Latches vs lwlock contention Content-Language: en-US To: Thomas Munro , pgsql-hackers Cc: Yura Sokolov , Andres Freund References: From: Heikki Linnakangas In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 28/10/2022 06:56, Thomas Munro wrote: > One example is heavyweight lock wakeups. If you run BEGIN; LOCK TABLE > t; ... and then N other sessions wait in SELECT * FROM t;, and then > you run ... COMMIT;, you'll see the first session wake all the others > while it still holds the partition lock itself. They'll all wake up > and begin to re-acquire the same partition lock in exclusive mode, > immediately go back to sleep on*that* wait list, and then wake each > other up one at a time in a chain. We could avoid the first > double-bounce by not setting the latches until after we've released > the partition lock. We could avoid the rest of them by not > re-acquiring the partition lock at all, which ... if I'm reading right > ... shouldn't actually be necessary in modern PostgreSQL? Or if there > is another reason to re-acquire then maybe the comment should be > updated. ISTM that the change to not re-aqcuire the lock in ProcSleep is independent from the other changes. Let's split that off to a separate patch. I agree it should be safe. Acquiring a lock just to hold off interrupts is overkill anwyway, HOLD_INTERRUPTS() would be enough. LockErrorCleanup() uses HOLD_INTERRUPTS() already. There are no CHECK_FOR_INTERRUPTS() in GrantAwaitedLock(), so cancel/die interrupts can't happen here. But could we add HOLD_INTERRUPTS(), just pro forma, to document the assumption? It's a little awkward: you really should hold interrupts until the caller has done "awaitedLock = NULL;". So it's not quite enough to add a pair of HOLD_ and RESUME_INTERRUPTS() at the end of ProcSleep(). You'd need to do the HOLD_INTERRUPTS() in ProcSleep() and require the caller to do RESUME_INTERRUPTS(). In a sense, ProcSleep downgrades the lock on the partition to just holding off interrupts. Overall +1 on this change to not re-acquire the partition lock. -- Heikki Linnakangas Neon (https://neon.tech)