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 1wYywN-000fwx-32 for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Jun 2026 04:28:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wYywL-009ihp-1F for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Jun 2026 04:28:49 +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 1wYywL-009ihf-0A for pgsql-hackers@lists.postgresql.org; Mon, 15 Jun 2026 04:28:49 +0000 Received: from mail-wr1-x432.google.com ([2a00:1450:4864:20::432]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wYywD-00000000SHk-2l5Q for pgsql-hackers@postgresql.org; Mon, 15 Jun 2026 04:28:43 +0000 Received: by mail-wr1-x432.google.com with SMTP id ffacd0b85a97d-4619990ca5fso271481f8f.1 for ; Sun, 14 Jun 2026 21:28:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1781497719; cv=none; d=google.com; s=arc-20240605; b=ckTVTcB4UU//ztIdxUpFt/2xY+w6FZJcvSzFtBN+gPgCfL/D7BaQaSjTzxkEbcCa8L YdgLey0p6Hmi3yG4W8uloXYZKGOHk+T7abdvkBJZ7oCqMZ26Dd8kiAklIISantn/2PrG PZunFCVkB6TBgIxBxO1lCAyR9QXhGa0IFj+d7u4hOOaR6QD/i4ZRF5ioeViHDgmmesIU 2WQfqDwv4dsM2Y3lcy6WCX/PXjAucAkFobhfpWAnDAIl0q4tF5S7m1Uz93x5r1w0iEGV FfgdczsocahBLW9OdYzjomYp1RnMranZsAscsLnV8CetC7zQzkH6EcERzT4eKOI20RCT 8sKQ== 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=hcGKCKxREqXE8ZPLBPqE3Vln67gcMzZr0eoILxAMvVE=; fh=w0XJuRBBYgqBoBNlC445pw45u5e8TOvm3aKqLKOBFgU=; b=UVZc0e0BlmcOWZzVPis0kuqWiqRQ13eg2I9OvOM+zFovNaXE5apLHP4S/PHJ/fzZAd u0y9nVAD6+68NXMuRfEMZX8DKAF6PWGzvG3MkQUTRgctOseZ88WU/xBceeekob3jtuAr lH1e/0nUoAxHOXEp1rJ7Nn5+bTq/fgr0KYo4nmDauOO+to86cgnxXZuJeU6Ev/sfeWad cw2zlYl67pZFPgT2xN4bx3FR9Numh4rtZ9Tmau1FcaoWMyOy5zH9u0CHqXml6dGxANxf 7a+g1KZEWv2H3FqGheUvbWrr20L+xToAkOlkgBKpM8nOsNDhFvPVerhF7e6VDgT3r+JP oTCg==; 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=1781497719; x=1782102519; 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=hcGKCKxREqXE8ZPLBPqE3Vln67gcMzZr0eoILxAMvVE=; b=Zc4DoSFC1sLIuFsf4/DB1929t9IZnOFCHLMGKuAfoLG2eH+Vc3X/HjVGWSzBJ1VOUF i9xyLlLBz5hjXkkCErBbSAFJ0AyNpi5dmWthO4h99ouXoNpjQzA/VAXzqN8A7MlsDpSq kJBSaxx2fx6yRmwboe7xyop9IfQH3Rt+ct2dE3lhAAG8uGXph0eOaCnxfjPi4QMVmm1K tZoSgrp++WbvyRuDqJSH0EUC7i2NVtaBGymyIwuxziejokX4Ndvx+VZhnIKidvvV6jl/ YAckcfBvYEM8h7ZLjgCmnXafSUAzVKsq92dCEw66ewBIB126EpuFdlUBGkFIm8QQIYuo 9mPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781497719; x=1782102519; 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=hcGKCKxREqXE8ZPLBPqE3Vln67gcMzZr0eoILxAMvVE=; b=B3hAtH/Jl6kVAYlF+0Gl+JDJueaLqia3A1PAD8fRZSERr5Vw1JTHhYczvG/IOwszJx 1WHKqklQsqqpng6Q+IZfmVoaEiDr+jyLC8z9kCFqtJcoOyD2W0Zkfh28BibXje8W2NHA jTekJKA7aKJ8W74D75MFf51tMgU8tS7frUWIp6E4T5MDmY0X3iXK3c5gRwBTmDbYvWdI 66Dtr2YwMCYVDqP+IBj93XCXYbZyKe9SQGHd7itqL/LxBmYTLjrxxBEhUjyWEpbPbW6K IIMBPOWDTekvqrhR93x4ZbV2tq3h++asVuqDUJ8iiyEJkzHfsAP9v6rAYTICdT2qmEHh hroQ== X-Forwarded-Encrypted: i=1; AFNElJ8dPCe/XpfHjpOz+F/knkt2Oo6PvPbgLWmEVKjxOiAIdmdft4dNpSdHkCixdDquOYt9iPU+0CVDAc1wXMQ2@postgresql.org X-Gm-Message-State: AOJu0Yw8iBBDLcx3hbd/2ZKYnzofHmjEhxqq/Ki6NEJLFlWkIK+AsAe0 0wUvyJMbrim77BOkaAM9aB8k14TpEYscJgd5IxNcDB9fNTLw2PlRZDIaSGJVVV3lqpKLVSm2jox 2rqDqFiiGlOxZYHnHD2zsZpT+Q67M86c= X-Gm-Gg: Acq92OEhPnjiRhDxAahAh1pvFOskMLFvdIJpiQ5ieVEFvMrcblSnYPFBHEUNkU9fgAk 3Lz50/NZurrKdWdd3EumU3R8cc5wZHIpeSbBfzU4KvSbJZw5+a5mZNP9EWufC6thEloKDOoVIZT 5z0AowwfkM9s/133NjyW+clsJG7cUXf0LE3MKp2kpY+iN9okFOZ47hvh/geIqTxNfHTCcwk/z20 RhR5KrsGzWNKmWmE0HgCKBpxreqVuAsUOxkkOpiPY0ubdLKaOH12qld9KxMS+bp0+KqwzYBtw1D I+X80s8WH3kyctjp07rP0n1HC5YBrPAWcPSE+zSGWwPAH69jsSkLQbrrRyouZrQsns9wNt9xmfD eusV9jghCYszZv3tPxIC9x01yaTez9H+65ECXYoJhWyXYW/rjrI7q8b03E03a X-Received: by 2002:a5d:59ad:0:b0:460:3234:77d5 with SMTP id ffacd0b85a97d-46074b946d4mr11351356f8f.43.1781497719260; Sun, 14 Jun 2026 21:28:39 -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> <555608f6-63bb-4dda-b1e4-f378f0febd38@iki.fi> In-Reply-To: <555608f6-63bb-4dda-b1e4-f378f0febd38@iki.fi> From: Ashutosh Bapat Date: Mon, 15 Jun 2026 09:58:26 +0530 X-Gm-Features: AVVi8Ccupt6AGb-vGCNgMkRbs4CA2mI_wVs1W4_AW9xh_9E5wFV1mzJJanFc67o 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 Fri, Jun 12, 2026 at 9:07=E2=80=AFPM Heikki Linnakangas wrote: > > On 21/04/2026 19:05, Ashutosh Bapat wrote: > > 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 a= fter > >> 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 th= e > >> 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 anoth= er > >> 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_= 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. The patch helps to set the expectations right. ShmemIndexLock is for protecting entries in ShmemIndex; I didn't expect it to protect the Shared structures as well. I thought a shared structure specific lock, which usually every shared structure is expected to have, would protect its initialization and content. But I see that I was wrong. Even those locks need to be initialized; so they can't be used here. ShmemIndexLock works here with the proposed comment changes. -- Best Wishes, Ashutosh Bapat