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.96) (envelope-from ) id 1w8Gb0-000Mb5-23 for pgsql-hackers@arkaria.postgresql.org; Thu, 02 Apr 2026 11:52:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w8Gaz-005a64-1L for pgsql-hackers@arkaria.postgresql.org; Thu, 02 Apr 2026 11:52: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.96) (envelope-from ) id 1w8Gay-005a5w-3D for pgsql-hackers@lists.postgresql.org; Thu, 02 Apr 2026 11:52:21 +0000 Received: from lahtoruutu.iki.fi ([2a0b:5c81:1c1::37]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1w8Gaw-00000000Bq5-47yh for pgsql-hackers@postgresql.org; Thu, 02 Apr 2026 11:52:21 +0000 Received: from [10.0.2.15] (unknown [130.41.208.1]) (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 lahtoruutu.iki.fi (Postfix) with ESMTPSA id 4fmgFT2cf8z49Pv1; Thu, 02 Apr 2026 14:52:13 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1775130733; 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=afIy7rA90VJATaR9XfXIJL1VZm+HnWAByl3j/Bmk2rc=; b=DL/1ftJuuIwi3ioW2f6P4fHlTwJTkKnw5Dh5AaL+GY71xpsCx7+h3t40FyUmHEB0f1pwCp ph9Y2uZDyH3VSTdHApbgeVobl7lVPbnXVl0LWqjc1M3C6OMEgdKhep66IPC3V0TBx1cM42 PIV74X1TB5q4/jdUPviK06sxqbjQJZgPOxfuhFl1xPrhFPSHaq9Y7D1AOIPZhBfAQp8zwj 9yXOM/IbNVGSrfQBs1Gx/HLIaOOwnLCNNFD+43+kOuPOkrCxObE8/mOjKvgfINDjJ3hsnS TdWBbYdXMWEqA6K1HEUWBi1MDvof4EGRiWr5NRYg59mbWjglMGLI1EoT1tdQPg== ARC-Seal: i=1; a=rsa-sha256; d=iki.fi; s=lahtoruutu; cv=none; t=1775130733; b=JbhsWaxHnrgJgiai1EuSqYbc99NbtSH5McRhxd/5AW9Jp6v02Ism4dw9AVgJJw0EuoQqhC 1PF8XGbXLYZz6+X1zaRIxLoNELfcyxIzWKA02nMHc74jtrgMLeH3mIN29G16LFXaULjWKu Qlg89aNy3D9KE8F900DbZ55SX0ceQGL6o8O3LPqE+WpMlH9z00DNRTAQ4UW0jUVkRtWnp8 9SbR5MK6B2t/+yxpeCYzBSbIQLiOuMGHmrCtCEdACeExTCEcEg6OC1QReGdJJLALsHQw2a q4hpexkt3kmthMXXhb85L01oLVOorlLWE9wlo6a2FOX3a4MSssxQDs/YDbkHKg== 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=lahtoruutu; t=1775130733; 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=afIy7rA90VJATaR9XfXIJL1VZm+HnWAByl3j/Bmk2rc=; b=LnmIfxIE/kevChVQJqNr1XBek9NByovpzh4W+Hd4qbbGSeeyghmyXPVo+LkRDnh13iKgRL gc0auTUiEDtTvAjPKGw5TJc9vqelksNiMMyclULtH8EtTV+j5WtO2k/8lBb8bbKDCL9kqa d2gmWE8AxgPvnA3+9LlQKyFWLA83sAIX4zupGswDHiUB/ThX16eHNEd0/BL9aXq0lGPgiw VIKgcsJiw2mUc3Gff16oIH1JoPv0105BDbaiz1xV0gW3HpHs9H3j7oPlqCFFb0wFm1prYk I/a4KVte9A3lw0DeSFGmgizfqoENFUF1PUJThg+tIInoFnJdiUkzlXrc6ucNcg== Message-ID: Date: Thu, 2 Apr 2026 14:52:07 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Shared hash table allocations To: Matthias van de Meent Cc: Tomas Vondra , "pgsql-hackers@postgresql.org" , Robert Haas , Rahila Syed References: <01ab1d41-3eda-4705-8bbd-af898f5007f1@iki.fi> <2981bb36-6bbe-4bdc-9a94-29b1114c79bd@vondra.me> <3026ec05-f664-4ebe-8bf6-0a1218b234ec@iki.fi> <19945803-6bcc-40fe-a14a-7dc5c462ed80@iki.fi> Content-Language: en-US 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 02/04/2026 13:24, Matthias van de Meent wrote: > On Tue, 31 Mar 2026 at 23:25, Heikki Linnakangas wrote: >> >> 0003: In patch 0003 I removed that flexibility by marking them both with >> HASH_FIXED_SIZE, and making init_size equal to max_size. That also stops >> the hash tables from using any of the other remaining wiggle room, >> making them truly fixed-size. > > I think this patch finally gave me a good reason why PROCLOCK would've > needed to be allocated with double the sizes of LOCK: > > LOCK is (was) initialized with only 50% of its max capacity. If > PROCLOCK was initialized with the same parameters and all spare shmem > is then allocated to other processes, then backends wouldn't be able > to safely use max_locks_per_transaction. To guarantee no OOMs when all > backends use max_locks_per_transaction, PROCLOCK's size must be > doubled to make sure PROCLOCK has sufficient space. (The same isn't > usually an issue for LOCK, because it's very likely backends will > operate on the same tables, and thus will be able to share most of the > LOCK structs.) Hmm, I don't know if that makes sense. It can happen that you have a lot of backends acquiring the same, smaller set of locks, growing PROCLOCK so that it uses up all the available wiggle room, and LOCK can never grow from its initial size, 1/2 * max_locks_per_transactions * MaxBackends. If the workload then changes so that every backend tries to acquire exactly max_locks_per_transactions locks, but this time each lock is on a different object, you will run out of shared memory at 1/2 the size of what you expected. The opposite can't happen, because PROCLOCK is always at least as large as LOCK. It doesn't matter what you set PROCLOCK's initial size to, it will grow together with LOCK, and you will not run out of shared memory before PROCLOCK has grown up to max_locks_per_transactions * MaxBackends anyway. > Now that LOCK is fully allocated, I think the size doubling can be > removed, or possibly parameterized for those that need it. I don't think that follows. The 2x factor is pretty arbitrary, but it's still a fair assumption that many backends will be acquiring locks on the same objects so you need more space in PROCLOCK than in LOCK. I don't know how true that assumption is. It feels right for OLTP applications. But the situation where I've hit max_locks_per_transaction is when I've tried to create one table with thousands or partitions. Or rather, when I try to *drop* that table. In that situation, there's just one transaction acquiring all the locks, so the PROCLOCK / LOCK ratio is 1. We could parameterize it, but I feel that's probably overkill and exposing too much detail to users. At the end of the day, if you hit the limit, you just bump up max_locks_per_transactions. If there are two settings, it's more complicated; which one do you change? You probably don't mind wasting the few MB of memory that you could gain by carefully tuning the LOCK / PROCLOCK factor. - Heikki