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 1wFDc8-004qvC-0p for pgsql-hackers@arkaria.postgresql.org; Tue, 21 Apr 2026 16:06:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wFDc7-009ikA-0d for pgsql-hackers@arkaria.postgresql.org; Tue, 21 Apr 2026 16:06: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.96) (envelope-from ) id 1wFDc6-009ik0-2p for pgsql-hackers@lists.postgresql.org; Tue, 21 Apr 2026 16:06:14 +0000 Received: from mail-wr1-x435.google.com ([2a00:1450:4864:20::435]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wFDc4-00000002K5E-2WlJ for pgsql-hackers@postgresql.org; Tue, 21 Apr 2026 16:06:14 +0000 Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-43d75312379so3361718f8f.1 for ; Tue, 21 Apr 2026 09:06:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1776787571; cv=none; d=google.com; s=arc-20240605; b=CUBLVOsqf6540xlkd+xX1DES5xKv1Bc03/3OOBN3mLcpZtn+16VozCznhZJ9tKzdyu 6FIpLYeGYkdN7sgcRcLRXSoTGGReXM11tY7saOUQwZOPfzYTSQCLt1fot2rYFt/dR5iz cMO6VasE34e61gZ59beCWXQi601jK9ZgTmEm0LUe5RkCfDoNXibXuMsb/m6A2FiRn6EP kajH1Qsq7zqJk2eqHfx1PEZVsWrEF30rAIM41HfjMBTMC++UOFWw8gvVwagtttX1L8gJ CnS4nYVkBrtDb5voLV/0DbaWx1rzcQJ6zXKDXWZRKH/rPGLGqWUwMRKJZGHj3hW3sggr f4tA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=HxibRSBom+O57k1wYLkVHGe/XmJBoTmdyrJT4JUsY8E=; fh=x+CBDP6zVsupRN+/oOYgijCQexicy0SIA1PTTXFlVHU=; b=E50BY0ykwtRw0fu3yAtIzU2byQEybLwlVSR+zBKBhE6pyDG361T82UrdtueoVApc1J kz55YONuZtr3o9jOfK4tnhc7/+uCzbMglatpBZCW9iiFiaUlpVn4G2fDExowVZw2R8NK TLY/INzk8s5d3NpJ3KRhbLj1/3gUWbxXUFkayPjeHYeKFgfXpiop+PZ7xMyAb1L9Dqli wZdZ8+e5a8L5M0Xuwbflag+gI5VkPYAHzE36dRvCfubFS5gyp0AJD1juLbPCls9yYibP E6tA+bnXHDbaF/zPlRR3dXJ7WZAQPbmjbEEPBT33kWZRrXDN2e/eohcid+pxH/demLhd gA7w==; darn=postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776787571; x=1777392371; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=HxibRSBom+O57k1wYLkVHGe/XmJBoTmdyrJT4JUsY8E=; b=f6Fq/brSJl/4se0KaAFWhnuFXnc7ePc7VuZgcYi4kcR3kb7jZWo2b/F/4iDu5OZUKo ZX8oUXFinUFK5fZo9roD93cnzgfQ9zuvwQXMNqczMRZuda+RtkC7rIYt+zvrlyGbLBrB t0aEz44NX2f0FZ+SmsJQHyfxtK4M83ulPUv75gmRXmQvaNgiYPOllcyp4g2XErLZ62xa Puy02i5tti9JUCv1CN+WFfosBgLWZr20J+dicvFCpingpKxg1cFw0ShJFZqKRi9tDPnI sXXDQ8UEX6AGfI0wrnohdmCYQfF9apzP+eUz5WkpnVQowsyp2ts8Jo1sRQItTWuN+cEq B74Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776787571; x=1777392371; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=HxibRSBom+O57k1wYLkVHGe/XmJBoTmdyrJT4JUsY8E=; b=XOL12sw2ZGsokJ6Aue2PZMiHh16+5tG3FsbwVkYjuHAX6ZzbaG9j/wlsRIz6ExRUKD k+hxfZGUShqfhuwpe+ewjllqYEHM/eUduAysYOiHd9yIYSn5wIchWdGiZEiTDY+TABl4 rBoqau678DC9HoYdoK/WpX48sf5g/dm+qlwRL8eODYsWlICznV0xW5WVuD6egbs9z3Yl RH1iNihRqZPi+oFx84Coz/n4v6mhwbSZiLtp/5b+xGNJsx1IP2zeVpG83Jos8oTTe7V0 hS1ElvpBnCe0I2/S0DcuvedCWYVd5yIQftlX5oMkDC9/BNsi/xUNj92DSJ6uzNMLQqAn 4bLg== X-Forwarded-Encrypted: i=1; AFNElJ/7PcyKwQ5IY3SeaGh6mtN2H+BWBO5/RjoPeHoo0rRRlJNeGXqr5sUMRHduP2RNG0r7M/SQZTDfyaQtsWNb@postgresql.org X-Gm-Message-State: AOJu0Yz8UYGAPfIN7dIcb7/d4UVm13b1clQWpxj6rORj5r7p79Z1NQ/Z 5tctRdaeJjRcfeNtBL3hfP28z8kX2grGfauY32PCPXjGyVJa5P+jEAI+NBgOhPyPZ/5ws2Yxq6n hHwx0whVEpKthcPNIzMmDrIORdrHc8N4= X-Gm-Gg: AeBDietv2ebWZ+3/byxlw06Tr+yTgzsXQKi7CoLkLXqa1AzdDioMPqMrdFhtKdUa3L+ nHtSQkyUQ/VkC/sSq+dACotIVQ5pjed6+fYk3naQESzcA+Cj7mNg9kyiX8pY3KmWd0sVcs6T1TO oFZb70P9QwkHLk4D7qNVlsicOo1U2qW7ughLw+7CQ4JYhAvtsNBaZ/RA1NerwZy1YOkd3gaO1sm L7wGvDSBbe1Ob/JdI2nxt9s7M6vsQLJLvDXYa2wMTJc+ACwidGaOMC+44L2NqBApNoPz99X7RLG DuhOVLRC6HhME+LOYVWWju0Gdm03jhxmZXC0CFdmgLkOLsdIUGQhPn3KHGS3dKg= X-Received: by 2002:a05:6000:24c3:b0:43e:aa88:f1a8 with SMTP id ffacd0b85a97d-43fe4034446mr29701252f8f.6.1776787570817; Tue, 21 Apr 2026 09:06:10 -0700 (PDT) MIME-Version: 1.0 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> In-Reply-To: From: Ashutosh Bapat Date: Tue, 21 Apr 2026 21:35:56 +0530 X-Gm-Features: AQROBzB-BKdOe6uHer-Ydi-mWQa8eukFkaq6gUfiafNpxLwMZwHHaTUACpoCsFc Message-ID: Subject: Re: Better shared data structure management and resizable shared data structures To: Heikki Linnakangas Cc: =?UTF-8?Q?Dagfinn_Ilmari_Manns=C3=A5ker?= , Robert Haas , Andres Freund , pgsql-hackers , chaturvedipalak1911@gmail.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Apr 21, 2026 at 1:10=E2=80=AFPM 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? > > 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=3D_38Rw= 0aiv-wU4CGzHaBCRYzqA@mail.gmail.com --=20 Best Wishes, Ashutosh Bapat