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 1w24OW-000quT-1n for pgsql-hackers@arkaria.postgresql.org; Mon, 16 Mar 2026 09:37:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w24OV-008vkA-1v for pgsql-hackers@arkaria.postgresql.org; Mon, 16 Mar 2026 09:37:52 +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 1w24OV-008vjy-0h for pgsql-hackers@lists.postgresql.org; Mon, 16 Mar 2026 09:37:52 +0000 Received: from mail-wr1-x42a.google.com ([2a00:1450:4864:20::42a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w24OT-00000000Ou2-1nBc for pgsql-hackers@postgresql.org; Mon, 16 Mar 2026 09:37:51 +0000 Received: by mail-wr1-x42a.google.com with SMTP id ffacd0b85a97d-439d8df7620so3139981f8f.0 for ; Mon, 16 Mar 2026 02:37:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1773653867; cv=none; d=google.com; s=arc-20240605; b=BuHvjQ1lsR5GL1A4WKHNoMFchYYmoBut2xtn8GgWFbCtcwOVx5WMgj8/bl9YpilZeg HUPTqa01A5drCk77icgWmKi4z3WA8XtMXd59ibj7Qwggc2duy3tfZJOifTPLLT631b3D vJZ13Qq93v3IrXP2CwcQmPv5a63LAATA6Jv5FXTKD7dDMipiNtQ4BK9sX6Z1FEsTvIh/ rGtZIxanZyK1bJI3l66dOQzb2K2DTMRHcq6qCQPjotbk7AtaitCJ0YDkKlWio0phWiaR 3NlyCh2ObbslPiBpbsvRf6YY+nUmtGQWVyvp3fAoMCRSe1RgiVTdwJ+eJJsJqZITKKOX S+tw== 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=umumBGcZHNb8zgXCo2vb1Slka8Cbulhc4IOUssU7ZAQ=; fh=DO7fXy07wj9azGG1QWwfOT4ELWxS3AF0ZWiE0DqeDVE=; b=i4bj0LJTiLZcly4qGUqXb2XSsENkg1QiboUUieowFdOdEqHTPh+5j3ftd6J8NgyZ6O 6sVgd46O5iK49eTs8V1L6FwpR+vfL6TkgjawuUamAt1ZSDJpTaIidrv+Nmv1c7+pTUDX aIN4bDerhsMejV8PVg4Q9WJpZErOUTg4kI5YQ62KEf0NdAt3FirtXBMxMXzkIiXwCYum jXTJnM8zuoStUdt1u3FwsHC3xyXIOlmGwRb/NK5kIq/OG1iqRF+5NOi/3fzfQOAUmu+u v5y3YQROXPDA65IAgdJsoWwP9B6+MhJuDr6EVbSxfy++8sIlbKWhAoeDhIUNOiGROxnQ oNiA==; 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=20230601; t=1773653867; x=1774258667; 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=umumBGcZHNb8zgXCo2vb1Slka8Cbulhc4IOUssU7ZAQ=; b=UCe9U0RZ9wezm6hAahHpJeE3hoXpTNQ4L9vmYm2LlqLnDVnMIEE32rLlUGHm/kU5b9 H/SZKdAOaSJYyixe2JOddvSfr9/LRisAp5Q3FGza9LfmJtYna5CIMKzR0ymNjQ1yAOH8 ewQpPk5g0l3F8KtLfruCT/qF22LeL/6+JBKgngHZARQFiYFQ76RGwbMdg7TVExRzmfxC FHurkAhJYxTRZmO6AhI2LYnWZXvSLZVRPyA5rrXqe5yCamyL7SrwLKHCXnsLoTvZX1ou TJbV0Abi/HhTitEfnZzklPwvd2XM/L9+s0RlmP7wb5BxQpS4ZmZ8oQF4sCiOAawSI4oi GopA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773653867; x=1774258667; 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=umumBGcZHNb8zgXCo2vb1Slka8Cbulhc4IOUssU7ZAQ=; b=sqlDtJfH8m6AJbjTJ4tnRpQxCd8toSvWSTnbw5Yk58MgpDRiIAqZ29KzVZ5EECeCl8 pcVyYFgTtX458EXV9LoOTRkn17kbHx4lXD9SPhp+IcjRBYz6QonKPI1t3HXO1IfaQ/6G DvrWiOoAmWGxu9Afg3P+b8QHwi7EPcQq9QPY3bMpi9dLDHexr3zMcdsKXdDvVj9d7PJp dFcfYEM2Ls34vYnnLWpKUhVFbNRK6M3JqPaSh3paNSaWmaVrqKk4+hh+9h4gYPncf3Np 0lJxTLs7ymrKioQDHXnesEP53fwUNm7yOdH3eq/HiXa71J7GXUD5vMjRVUof1cKGQlTY omeQ== X-Forwarded-Encrypted: i=1; AJvYcCWPqphyqU9WBRgCJcGFPL9nglvZyyRL8NeUfuE3GdE8OhfuE6CKwb9YY4PcVTkH+KlRVr2YUxDlZljZjx8M@postgresql.org X-Gm-Message-State: AOJu0YyerLGNo5qympGBaLMmjQjREuSNw6QEJ1yNdZFPYi0DzFvlcyPj 6yq8wMApqREhrxeVMgB/M+LK+Qo3+ilvDkrIPZCmKzm6856zJcdCDkt3NBKSgMZCvid22WKgn6c oEtOoK6IGb4Fbgii6OZvVRbMtqyC0t3g= X-Gm-Gg: ATEYQzwwissfhTYVhbv5TzvSe6xkUXDNqANgf6Ko4Xjhbm/TXsKTCXec0gri8gF+W/n +DlKzYhmiGfFRiZTt+F53blIOYQGD3FEbmQ0dIcNlQJ2jMyLofN6oNjfYevBza74u3TUGPLAAP/ ELabtoL/qvPYygt2tBnn23YPdt0kpZoLXMGDaGFWgK4WQaZ9cPMj9Vxaiyi8OdI4Lq9H/XEuF/E ea7S2PeAcGLDkwq8Ho42OlPYDVxvnp5CvNDOdaYRoD/Zmi9ucv/gx7NMiyELorhShW+8RIzu9+e x5WyB8KD61VvNC1RcwX4Z752+BKgkCsUTh+rhAVDDdK9LbnlurIN X-Received: by 2002:a5d:64c4:0:b0:439:ac98:751a with SMTP id ffacd0b85a97d-43a04dbecf8mr19524312f8f.34.1773653866442; Mon, 16 Mar 2026 02:37:46 -0700 (PDT) MIME-Version: 1.0 References: <5a37c2e3-619d-4816-84d7-0b27e3e6797f@iki.fi> <26c766d6-db0f-43d3-a618-44f8d40a3121@iki.fi> <62b8dc23-8f6a-4cac-91ff-f74bb5bc159a@iki.fi> In-Reply-To: From: Ashutosh Bapat Date: Mon, 16 Mar 2026 15:07:34 +0530 X-Gm-Features: AaiRm53nyPQpazfcvUWyoVs6FdP3A60pcy-Dm0LWxrVM0WNwc2uHCLAyum_IBNI Message-ID: Subject: Re: Better shared data structure management and resizable shared data structures To: Heikki Linnakangas Cc: 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, Mar 13, 2026 at 5:11=E2=80=AFPM Heikki Linnakangas wrote: > > On 12/03/2026 22:05, Robert Haas wrote: > > On Thu, Mar 12, 2026 at 3:21=E2=80=AFPM Heikki Linnakangas wrote: > >>>> I'm currently leaning towards _PG_init(), except for allocations tha= t > >>>> depend on MaxBackends. For those, you can install a shmem_request_ho= ok > >>>> that sets the size in the descriptor. In other words, you can leave = the > >>>> 'size' as empty in _PG_init(), but set it later in the shmem_request= _hook. > >>> > >>> Why can't you just do the whole thing later? > >> > >> shmem_request_hook won't work in EXEC_BACKEND mode, because in > >> EXEC_BACKEND mode, ShmemRegisterStruct() also needs to be called at > >> backend startup. > >> > >> One of my design goals is to avoid EXEC_BACKEND specific steps so that > >> if you write your extension oblivious to EXEC_BACKEND mode, it will > >> still usually work with EXEC_BACKEND. For example, if it was necessary > >> to call a separate AttachShmem() function for every shmem struct in > >> EXEC_BACKEND mode, but which was not needed on Unix, that would be bad= . > > > > That's *definitely* a good goal. A less important but still valuable > > goal is to maximize the notational simplicity of the mechanism. Your > > callback idea is elegant in theory but in practice it seems like it > > might make it harder for people to get started quickly on a new > > module, and having to create the object in one place and then fill in > > the size in another sort of has the same problem. I don't really know > > what to do about that, but it's something to think about. The > > complexity of getting the details right is annoyingly high in this > > area. > > Yeah. IMHO the existing shmem_request/startup_hook mechanism is pretty > awkward too, and in most cases, the new mechanism is more convenient. It > might be slightly less convenient for some things, but mostly it's > better. Would you agree with that, or do you actually like the old hooks > and ShmemInitStruct() better? FWIW, I like the new way. If your goal is to get rid of ShmemInitStruct, ShmemInitHash, shmem_request/startup_hook, we could replace the hooks by another hook which gets called after MaxBackends is initialized but before CalculateShmemSize() gets called. All StructShmemRegister() can be called in that hook. _PG_init() is used to register the hook as it's done today. If we are going to deprecate the old hooks in a couple of releases, we will need to maintain three hooks but given that we don't have to wait for several releases for the extensions to adapt to the new hooks, it should be only a temporary measure. > > One such wrinkle with ShmemRegisterStruct() in the patch now is that > it's harder to do initialization that touches multiple structs or hash > tables. Currently the callbacks are called in the same order that the > structs are registered, so you can do all the initialization in the last > struct's callback. The single pair of shmem_request/startup_hooks per > module was more clear in that aspect. Fortunately, that kind of > cross-struct dependencies are pretty rare. So I think it's fine. (The > order that the callbacks are called needs be documented explicitly though= ). > > If we want to improve on that, one idea would be to introduce a > ShmemRegisterCallbacks() function to register callbacks that are not > tied to any particular struct and are called after all the per-struct > callbacks. > I think as long as we stick to calling the init/attach functions in the order of their registration (or any well defined suitable order), the module can use perform initization/setup of individual structures to which the callback is attached and also modify/use previously registered structures or do the setup in the last registered structure's init. I think this is more flexible that required; maybe overwhelmingly flexible. > >>> Yeah, I think RequestNamedLWLockTranche() might be fine if you just > >>> need LWLocks, but if you need a bunch of resources, putting them all > >>> into the same chunk of memory seems cleaner. > >> > >> Agreed. Then again, how often do you need just a LWLock (or multiple > >> LWLocks)? Surely you have a struct you want to protect with the lock. = I > >> guess having shmem hash table but no struct would be pretty common, th= ough. > > > > Yeah, we've developed an annoying number of different ways to do this > > stuff. I don't entirely know how to fix that. > > Here's a new version that doubles down on the > LWLockNewTrancheId+LWLockInitialize method, by changing the example in > the docs, and contrib/pg_stat_statements, to use that method. > RequestNamedLWLockTranche() still works, there are no changes to it, > it's just not as convenient to use with ShmemRegisterStruct(). This has > the advantage that we don't introduce yet another way of allocating LWLoc= ks. The new hook could be used to request LWLockTranche as well. --=20 Best Wishes, Ashutosh Bapat