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 1wY3wp-0002hp-2p for pgsql-hackers@arkaria.postgresql.org; Fri, 12 Jun 2026 15:37:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wY3wn-000vnz-0t for pgsql-hackers@arkaria.postgresql.org; Fri, 12 Jun 2026 15:37:29 +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 1wY3wm-000vnp-2n for pgsql-hackers@lists.postgresql.org; Fri, 12 Jun 2026 15:37:28 +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 1wY3wk-000000002W2-2C8E for pgsql-hackers@postgresql.org; Fri, 12 Jun 2026 15:37:28 +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 4gcNtV5fv1z49Q3P; Fri, 12 Jun 2026 18:37:22 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1781278643; 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: in-reply-to:in-reply-to:references:references; bh=HaQ9iElzp5BwbWC0PQ8vS5EtzF2JK9N0qsNM8VFGwYQ=; b=BJ5Rl745YVzsi5Nk+C83SEtn2zjyXnZhluEe4jrQZdnCdMQ+VcT6Rp1+H31vTrwCXdnrgX 6apbC50iwbuDntGtcUhrdMZ2Uwy4x2CXT/7Mhnlj/cEb0OKq172lo3B2u+2czdoMzJGA6s 1G7YO0O/B+8vZKvjiZRuXee5RoSYRvl5BlLwEIhmkDdHXbHOfAiPqz9yu9ZFOa6V7oBFS6 ccriiFE9K1gTOi6fcwiV/GiM7fEgfS3RtaWm9gvHLX9VTOHh+KrIVdbppGFxwwL7YcW9PB 1gtNbAGYIqL4l8EUmzOMFdbnq7FAx6IR2bq8Qi3tBXIBrMzmyuc6NeqYB/Fr+g== ARC-Seal: i=1; a=rsa-sha256; d=iki.fi; s=lahtoruutu; cv=none; t=1781278643; b=DcP/VkXPcr7Quqk3gMHy3NaUAyuVdPqKfSKAKUhDzvDICKqJYUBaO8Z0CUmM3PYnuCfosb dvYi2UcXpskrWwnir6NiiKJabJE6aV0Oyi0I19lNtXQcXbBlBYq/c0LkQZ74iBrxsd7eRx Oe5Ufsu6TvO9KetFCDJzGuYPiYfQVmLzyEkIaxdxpytmPUSxpBAH3oHK48e84AGyOifNsC WqqCuZ2tKt3nPhDdiinXpbRg7scc/Hj2ZUpLZGBY13OGAnKcZLefGUzJG+zybztCwODOMY ROrVnYbIxyw6MJPk3Tcuiyna9RrwhpSJpI7FJxEUymc4w5GS6OI2kEmWtzbYrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1781278643; 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: in-reply-to:in-reply-to:references:references; bh=HaQ9iElzp5BwbWC0PQ8vS5EtzF2JK9N0qsNM8VFGwYQ=; b=suXDsE70fHogCP/vmkszJYwh8eCmba+02NZZbVLy7/Ih8j62GPS3w1Tw61ttc1jlrDTIiC 6JMMocMEtopNV7a99GgHxRrebXksBcF643quJVyp+/Oa5TeN7aKsfIO0R60WjjmDc6bnPM u6SO+u1Gg0NdfBvEiBWsZONxoGL9zHQvSykmWKTfsW6wH4FCEobBy+r3+Gyk5wPTSwoHuN trd+h4sU3QURLPI6uCbACzNf7orvscyCNHu9bAJn9oD6hTBelcN0zAovqRyvL3yHSWHjRf 5fa5rL1+pxNj235lJyQ+M1SihofDl+UvsHZn3NVNxYgCJypglhVdvm7Qbhp7Gw== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi Content-Type: multipart/mixed; boundary="------------grpQeMCQxKwFtDoP0jwnhuSq" Message-ID: <555608f6-63bb-4dda-b1e4-f378f0febd38@iki.fi> Date: Fri, 12 Jun 2026 18:37:21 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Better shared data structure management and resizable shared data structures To: Ashutosh Bapat Cc: =?UTF-8?Q?Dagfinn_Ilmari_Manns=C3=A5ker?= , Robert Haas , Andres Freund , pgsql-hackers , chaturvedipalak1911@gmail.com References: <113724ab-0028-493f-9605-6e8570f0939f@iki.fi> <791c3f18-f4de-4d84-ac6b-c7ccc074dd38@iki.fi> <9d919bd9-94dd-4bda-8ccf-ebced4178c53@iki.fi> <470e7ebe-0971-49f6-8e46-9b8f6395f88b@iki.fi> <87y0iz2c1v.fsf@wibble.ilmari.org> <4297b4ee-5248-415f-ae12-abf2a23f4ab6@iki.fi> Content-Language: en-US From: Heikki Linnakangas In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------grpQeMCQxKwFtDoP0jwnhuSq Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 21/04/2026 19:05, Ashutosh Bapat wrote: > On Tue, Apr 21, 2026 at 1:10 PM Heikki Linnakangas wrote: >> >> On 07/04/2026 17:19, Ashutosh Bapat wrote: >>> Hi Heikki, >>> CallShmemCallbacksAfterStartup() holds ShmemIndexLock while invoking >>> init_fn/attach_fn callbacks. That looks wrong. Before this commit, >>> init or attach code was not run with the lock held. Any reason the >>> lock is held while calling init and attach callbacks. Since these >>> function can come from extensions, we don't have control on what goes >>> in those functions, and thus looks problematic. Further, it will >>> serialize all the attach_fn executions across backends, since each >>> will be run under the lock. >> >> This was intentional, I added a note in the docs about it: >> >> When RegisterShmemCallbacks() is called after >> startup, it will immediately call the appropriate callbacks, >> depending >> on whether the requested memory areas were already initialized by >> another backend. The callbacks will be called while holding an >> internal >> lock, which prevents concurrent two backends from initializing the >> memory area concurrently. >> >> That "internal lock" is ShmemIndexLock. I piggybacked on that since the >> code needs to acquire it anyway for the hash table lookups. > > I had read this part, but didn't realize it's ShmemIndexLock. The > document and the code are placed far apart and the comments in the > code do not help connecting these two. The comment before > LWLockAcquire() call doesn't say anything about init functions. > /* Hold ShmemIndexLock while we allocate all the shmem entries */ > >> With the old ShmemInitStruct() interface, extensions needed to do the >> locking themselves, usually by holding AddinShmemInitLock. >> >> (Now that I read that again, the grammar on the last sentence sounds >> awkward...) > > Given that the init_fn is called in only one backend which requests > the structures first, do we need a lock? If two backends request the same structure concurrently, which one is "first"? That's what the lock determines. It's not safe to release the lock before the init callback has finished. Otherwise, another backend might attach to the struct before it's fully initialized and read uninitialized values. >>> In my case, the init_fn was performing ShmemIndex lookup which >>> deadlocked. It's questionable whether init function should lookup >>> ShmemIndex but, it's not something that needs to be prohibited >>> either. >> Yeah I'm curious what the use case is. We could easily introduce another >> lock or reuse AddinShmemInitLock for this. > > In case of resizable shared memory structures, I was adding mprotect > to make sure that the part of the shared address space which is > reserved but not used is protected from inadvertent access. The > mprotect is wrapped in a shmem API which fetches the ShmemIndex entry > of the shared structure, figures out the part of the address space to > protect using maximum_size and current size and calls mprotect > appropriately. To fetch the ShmemIndex entry it acquires a ShmemIndex > lock. The shmem API was supposed to be called from init_fn() and > attach_fn() to protect the address spaces as soon as the structure is > attached to. See patches attached to [1] for code. > > [1] https://www.postgresql.org/message-id/CAExHW5v5muT_SKV2NCxxVmvC=_38Rw0aiv-wU4CGzHaBCRYzqA@mail.gmail.com Ok. So if I understand correctly, holding ShmemIndexLock is not a actual problem per se, you just didn't expect it. Right? I propose the attached to improve the wording a little on the docs, comments, and error message. - Heikki --------------grpQeMCQxKwFtDoP0jwnhuSq Content-Type: text/x-patch; charset=UTF-8; name="improve-shmem-init-callback-locking-docs.patch" Content-Disposition: attachment; filename="improve-shmem-init-callback-locking-docs.patch" Content-Transfer-Encoding: base64 ZGlmZiAtLWdpdCBhL2RvYy9zcmMvc2dtbC94ZnVuYy5zZ21sIGIvZG9jL3NyYy9zZ21sL3hm dW5jLnNnbWwKaW5kZXggYmFlMTZkN2ZiNTMuLmNiM2NjMDlmMTZkIDEwMDY0NAotLS0gYS9k b2Mvc3JjL3NnbWwveGZ1bmMuc2dtbAorKysgYi9kb2Mvc3JjL3NnbWwveGZ1bmMuc2dtbApA QCAtMzczOSw4ICszNzM5LDggQEAgbXlfc2htZW1faW5pdCh2b2lkICphcmcpCiAgICAgICBz dGFydHVwLCBpdCB3aWxsIGltbWVkaWF0ZWx5IGNhbGwgdGhlIGFwcHJvcHJpYXRlIGNhbGxi YWNrcywgZGVwZW5kaW5nCiAgICAgICBvbiB3aGV0aGVyIHRoZSByZXF1ZXN0ZWQgbWVtb3J5 IGFyZWFzIHdlcmUgYWxyZWFkeSBpbml0aWFsaXplZCBieQogICAgICAgYW5vdGhlciBiYWNr ZW5kLiBUaGUgY2FsbGJhY2tzIHdpbGwgYmUgY2FsbGVkIHdoaWxlIGhvbGRpbmcgYW4gaW50 ZXJuYWwKLSAgICAgIGxvY2ssIHdoaWNoIHByZXZlbnRzIGNvbmN1cnJlbnQgdHdvIGJhY2tl bmRzIGZyb20gaW5pdGlhbGl6aW5nIHRoZQotICAgICAgbWVtb3J5IGFyZWEgY29uY3VycmVu dGx5LgorICAgICAgbG9jayAoU2htZW1JbmRleExvY2spLCB3aGljaCBwcmV2ZW50cyB0aGUg cmFjZSBjb25kaXRp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