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 1w3F8N-0012a0-1J for pgsql-hackers@arkaria.postgresql.org; Thu, 19 Mar 2026 15:18:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w3F8L-000j3Z-2W for pgsql-hackers@arkaria.postgresql.org; Thu, 19 Mar 2026 15:18:02 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w3F8L-000j3R-1C for pgsql-hackers@lists.postgresql.org; Thu, 19 Mar 2026 15:18:01 +0000 Received: from mail-ej1-x636.google.com ([2a00:1450:4864:20::636]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w3F8J-000000001D8-3pzp for pgsql-hackers@postgresql.org; Thu, 19 Mar 2026 15:18:00 +0000 Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-b97e6e48b24so174493366b.2 for ; Thu, 19 Mar 2026 08:17:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1773933478; cv=none; d=google.com; s=arc-20240605; b=ERNvOCftvHy55tyCnAjX6BrL74VmrBlBnRC1KkPHXwkbiyDgNVpeUGASHVkr85Hggo 2D7Z5LiqpQtyGz+5m0c+UqF5Fre4NQW+uK8PibM0dzquEipZn3Ed5qe94AbIP6gC1aiG 6rvcZo54z4sjwwO3n4TFyOxu+ZrJV8NrRvOGBQAWb9SK3nNM3M783FEKbLFmwlHwxkZc fOm77OkQvxCnbE5lIeeYmwrP6KQXJ9AiH93eVL+PeNPSXTsZidYcz+mt6h+oz65o8MOq DYQQ8unX+FlANu+ufmGAXE2cEbOofnBqmkVB3Zbc3HPFUn7gI9l6wX/7ZKA+gKxFPSrQ v+GA== 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=tgRrXHfZrxjaCRxz44W/s6siSzjTii3TPZ7oQksxNZg=; fh=XHare2Kpibi1seD5L3tezuEL7c7pUbcaaE+d0E7B7xY=; b=VBhfoJa2l8DKV0/U3l9x8m7VAy5oNseYjFsbZN8qpy4KRoY11GtwHqSA+sH2L3xOaU RK5M6bDWEwXOBkTN7x+UzRN1sw1fJZP6SoGjtuslRQfibRDsbF0QhH8jkvr30PxB6+b7 SQHQcrSh7FTpZtOXFoIvMUHwn4Phy2fCjnyv1iGG1KgKzT9Atezv/BaRyQAtTLTjewsC FyXEaM0FarVuEEAWg2uX2tPiKTPXkISGSG02rLb9M2CfaUtrTwpAM9ti6mItH2Z706lu 0ZKOgKSbxtlM2acabIOgofgF8zPv4aw3Wr/hdShXUXHDSmsCb3kJSwMj6Krd9QBuHRAL xiCA==; 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=1773933478; x=1774538278; 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=tgRrXHfZrxjaCRxz44W/s6siSzjTii3TPZ7oQksxNZg=; b=LtfhWPIiDWegk37q4eBsVgiaWZPgpxopHLe+p/2yUa2tN+7kBXA0Y36AjIp3rbX+aO aMt2ZEufuMg92p+EfEgYzZYAertTaCHQfuign0P/zDSiNgjFou1oktxlG0vt4lmd0hX1 GsWHQcf9XR/bHWon1vTcZ2kfxi9cfDT5RxVCgAEpHbO2VIjfIglCt4IfI1nMwiSFe2VN 7P+xtkNw12ZEXKJ9LL/UYE+/3DTWodfFz6Vt0V4Ju73QCjR0tCf//X13qM1lSgYWypnf nGwaRzYd1/UhDuQRdKpoY4oYLDonVOF2S0fgLrj2/xcaWrIjRlgfJxhCUPHxi6w+ySE2 pm1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773933478; x=1774538278; 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=tgRrXHfZrxjaCRxz44W/s6siSzjTii3TPZ7oQksxNZg=; b=jRXIQbowi/KG+yk/nONlrLpQaqh5c7b1UgVIYoF+FtbdOg9stLbGwNtQ/BIMES2d3Y yp3aR+R9VcgemPt80XckTo6gEQ1BHu1/9sSiHv0OXzLDc+gvRa2uvYezHW2pOogqroy8 88R+4wwYlYgiIBWguX7b8do7itltcgYXs/Vs4HcR0nv5ct2vH1pOKHrlwjnWII9FUc5D 8CXNe70B6zc7Hhj1rDXmVCK+OIWHvy0mrPyMDaFbpREuy2j2HQt58oexh563UyyCw/P5 DjshQ/aIqY/5CxFThPYkEwwi+osd9CTymMgygtHSKgkQEm219i/rPNw9Rh67ktODHMoT bFDA== X-Forwarded-Encrypted: i=1; AJvYcCVW8aJsTIcBJz4bBw+PxeSomVJPow07hKDAFC/Id2HZa8fkQrA+7FClhQDLd+4a3lPug3OgPMRXO0u/pfsd@postgresql.org X-Gm-Message-State: AOJu0YxvImo1hDhMhcdCP99Lnk1IkLX903hONftdTgXVkqugN6FUraCd NSdmM7uiKl7sv7BZbaVxvI9njm+h4QXwTD9pTkOBXsIv/o6Q9Dbb0qeP2etdwD1edWfY7Um/7Pw To/ilvAroFPlJK5FiUXL9urWcmrbaFTk= X-Gm-Gg: ATEYQzzXkbI3K2FXRQMn98+rSSdhKBif2A/sbSoNo6UY8zANPnDlEmzz7N7UCWVQTeW lNj7yiAgLBfD67cviq2uSNg2e/6sWtJoIDjdxJidhkcdyDNV+FFPpnTXXX+IVZ8NfEUc8kvjsRv dbIpYqJZfeK+RxsE/HCuJ9Rtwk9FhamRf5gEPBHf376JWhbE/8E307TRjbP/q+uvIrbxTTU1Ltw xB+n9dwxjOF92kAwtyxW9pZzguyeJK02NjR14LEKyuMRYpW41cet04+e4JnklRm1FazyOw/aR/S qfZsg0GPwv7vNncqlC4g/hm906T5IJYnA/J+5wI= X-Received: by 2002:a17:907:d08:b0:b88:6542:86a0 with SMTP id a640c23a62f3a-b97f4a358b4mr490439866b.54.1773933477946; Thu, 19 Mar 2026 08:17:57 -0700 (PDT) MIME-Version: 1.0 References: <26c766d6-db0f-43d3-a618-44f8d40a3121@iki.fi> <62b8dc23-8f6a-4cac-91ff-f74bb5bc159a@iki.fi> <8a6799be-bd42-49fb-8914-856c97bb1977@iki.fi> In-Reply-To: From: Robert Haas Date: Thu, 19 Mar 2026 11:17:45 -0400 X-Gm-Features: AaiRm53tXxoLNffKc4g0vO9Xy_ux7vSR0etC3DLV3d0S861L6seWsAGGux1-MwQ Message-ID: Subject: Re: Better shared data structure management and resizable shared data structures To: Heikki Linnakangas Cc: Ashutosh Bapat , 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 Thu, Mar 19, 2026 at 10:34=E2=80=AFAM Heikki Linnakangas wrote: > > I suspect > > that issue is also why pg_stat_statements has the > > process_shared_preload_libraries_in_progress check at the top, because > > it looks to me like everything else that the function does would be > > completely fine to do later. > > I think a bigger problem is loading and saving the statistics file. The > file needs to be saved on postmaster exit, where do you do that if the > library was not in shared_preload_libraries? Well, there's no way to install a hook in the postmaster in that case, so you can't. But I'm not sure that justifies skipping everything _PG_init() would have done. A problem with the status quo is that every module author makes their own decision about how to handle the s_p_l problem, and they don't all decide differently, even in our own tree. For example, autoprewarm chooses to register the GUC that it can while skipping the other one, while pg_stat_statements skips everything, including hook installation. Maybe that's properly considered flexibility that should be left to each individual author, but to me it seems more like happenstance than anything else. I'd favor a design that emphasizes severability - i.e. you always try to do as much as possible, and defer errors until later. So you always create the GUCs but then restrict setting them to values that you can't support without a restart, instead of not creating them. You install the hooks and then maybe they will have to no-op out. It's just weird if you load a library and the GUCs aren't defined and there's not even a diagnostic telling you why. > If you squint a little, this is pretty much the same as my descriptor > design. Let's start from your DefineShmemRegion function, but in order > to have some flexibility to add optional optional in the future, without > having to create DefineShmemRegionNew(), DefineShmemRegionExt() or > similar functions, let's pass the arguments in a struct. So instead of: > > DefineShmemRegion("pg_stat_statements", sizeof(pgssSharedState), &pgss, > &pgss,pgss_shmem_init, 0); > > you would call it like this: > > DefineShmemRegion(&(ShmemStructDesc) { > .name =3D "pg_stat_statements", > .size =3D sizeof(pgssSharedState), > .ptr =3D (void **) &pgss, > .init_fn =3D pgss_shmem_init, > .flags =3D 0, > }); > > This flexibility will come handy as soon as we add the ability to resize. I see your point. I'm not really convinced, though. In practice, what's now going to happen is that you're probably going to move that struct out of _PG_init() and define it elsewhere, so the logic is getting split between multiple places, which does not improve readability, and it also becomes much more worrying to what degree the struct needs to be const, whereas if you just pass a bunch of parameters by value you kind of understand what has to be happening. Also, what flexibility have you really purchased? Sure, now you can add arguments to the function call without breaking existing call sites, but (1) there are other ways to do that, like by creating an object first and then using methods to assign properties to it afterward (i.e. RegisterCallbackOnShmemRegion) and (2) adding arguments without breaking existing callers is not an unmixed blessing in the first place. I know the world won't end if you go with this style, but I guess I'm not much of a fan. I find this sort of thing hard to read. > Ok, we could add GetShmemRegion() in either design. Do we have any place > where we'd use that though, instead the backend-private pointer global > variable? I can't think of any examples where we currently call > ShmemInitStruct() to get a pointer "on demand" like that. > > In pg_stat_statements, this would replace these tests: > > if (!pgss || !pgss_hash) > ereport(ERROR, > (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), > errmsg("pg_stat_statements must be loaded via > \"shared_preload_libraries\""))); > > But I don't think pg_stat_statements could still allocate the region > after postmaster startup. GetShmemRegion() would just be a different way > of throwing that error. In that case, yes. But something like autoprewarm only needs a small amount of shared memory, and can potentially initialize itself on the fly. The not-yet-committed pg_collect_advice module has similar needs, which it currently satisfies using GetNamedDSMSegment(); see in particular pg_collect_advice_attach() if you feel like wandering over to the pg_plan_advice thread. > Thanks, this hasn't changed my opinions, but I really appreciate > pressure-testing the design. I don't want to rewrite this again in a > year, because we didn't get it quite right. Yeah, I'm somewhat concerned about an ever-proliferating number of ways to do things that all sort of suck in different ways. --=20 Robert Haas EDB: http://www.enterprisedb.com