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 1w91PK-0017e2-1T for pgsql-hackers@arkaria.postgresql.org; Sat, 04 Apr 2026 13:51:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w91PI-00GW9t-31 for pgsql-hackers@arkaria.postgresql.org; Sat, 04 Apr 2026 13:51:25 +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 1w91PI-00GW3k-1h for pgsql-hackers@lists.postgresql.org; Sat, 04 Apr 2026 13:51:24 +0000 Received: from mail-lj1-x229.google.com ([2a00:1450:4864:20::229]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w91PG-00000000Z98-12QW for pgsql-hackers@postgresql.org; Sat, 04 Apr 2026 13:51:24 +0000 Received: by mail-lj1-x229.google.com with SMTP id 38308e7fff4ca-38dd5f28a4cso10008631fa.2 for ; Sat, 04 Apr 2026 06:51:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1775310681; cv=none; d=google.com; s=arc-20240605; b=jls5uZzbjExaNM40pab9ruSTzFX9bK+A75iF6/0PLRcawiy7ghTBvXzwA3J1/90HHN NMW9jKSZTf9wteYHwNvU70s8EzQNfqSMaiH5AeB19PfAeqzIBHHiVMV9vCajhJ3uhDMX W9rey04F9Uog8QpZzbLUEJ5g/94KvngmiukpUFiGxIT7HthKc/W3xt1oI6oE4HiVGaAq YOaXp8TeSzujvyxykL2OQtIV0lERg73eF2f0Fi6sx8K+LN8wnaRCfIVoPHKYUK4TpAUl IP6n/xaV8j/GcLZcDI990MPWgagTjWypnOsp5NF2WgbrXErCBSZFRBkEBVKwXxrzP2N6 Cjlg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=PW7D4yvl4hJ61MJn7NknocHmVxnfSbfCDIv5q12lr0o=; fh=my4sjLY8t5ANC3h9sgFijSL4lvlkgLzFFFAn3/niFSI=; b=HDCXBJrHve/AoKTh+rRCNDR/BJV0/zCfB6bg6njvtGrM58Xg5tQW7uAKI2OFNUS4wn ywCjqr6kW0oHdmbWEvMGmjDr9xYfJ6xoMEL99c4NmlVh+xqyvQTQ/qiQkOQHmKnYlm+q 8LsrSo2i333skTANKYrFo29U83tdugjgU16Rb05KIwRvrrwRNSaSEUA33GfVLK6zRjGT OuCGLCnYlEMZPHRk0/K6MXKcn4dD+8ZSq/kQJ9soommdpuQmEhDHyh66wh3vTcDYki+d llfv8PUV8TfB7evTY1qX3s5SW9SyXpmpagFhb+8Z3YJ5yOFKy2kXimceLaVBId8MP9I8 Llrw==; 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=1775310681; x=1775915481; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=PW7D4yvl4hJ61MJn7NknocHmVxnfSbfCDIv5q12lr0o=; b=lpt1mW/0TVaaZZ1w9pMDjKC3Fv/+23FiCAbpDH146yVCsOtUBLoIntJvzCai7cflx4 6rJli8gWgCtwFZ0izp+Zbcf2ss0xp7mfL0PsOp1nEHtfuTH9xrWqEzXVRiPBai90i+hf +PBdR30C3Ygz1iphJL5RttDUdSv5YixDWolF4HEw/Tc+3Lue8hXegcS0PF/ajsVa/eR4 VgchfvzXlKhnET4zEgHhTrNkvuhVQi3H7jBG3FJjyDwNbLAWer7kXyJTL8pVzJRV9wf6 iYtQtwvy1wJ1NdpeMTY1LeWWjeuMrW5E1DaNaDD1lLg2EkmOp5VPlS9XWgLvRHyhYZLx eseA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775310681; x=1775915481; h=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=PW7D4yvl4hJ61MJn7NknocHmVxnfSbfCDIv5q12lr0o=; b=T7+/f/mU2znkd3NZ4eF5GQKVODEvbnDG9XTKpKtauq9ufYXzbaelukevKYCLvsSjw2 0UOUsgFozH93GqCJBuPUIsZN4vEF29M26gdYOu/J4Ng1n9DpBCzp6oVrfQhvH57E6dAR MQAJa+ZbIvsOThxm8EygqFQiXf9B5/EirWHVazgYxLnXxC1bNZJAguZkj8QHVEreV6G4 V4T9nHIWYNVZoeSNabcywNSXr5fUwMGq1ERLKoFP7kqhhhJLU+88GFuwKUkWzIuPZvS4 I+qZu3Z2ikmFbcivzlZjXOUh1dhieUfQbFw4xgbTv0KTMzoAYeJIY4HBJUGeA1h/d2ob Ny6Q== X-Forwarded-Encrypted: i=1; AJvYcCVBmE2u9ktdTAvn+oh4nPMI/EovhgOldSM3mFeJdENE7xpdEBAa5zdMttYJGxhpzlBjN/QgHp4lq6dtOUHl@postgresql.org X-Gm-Message-State: AOJu0YzKHKtgj6B1gAd8Oqj1U8I+XQCR1VuAaxOkENOdpoon2tW4IQS+ ongis7UQlD++oY/ulxii7CCC18a08eH2FF0J+V5vi4IJ9kUysZGZF2V77W4AySuchTdzp5pPdy0 HxQabvOFvv1Ey1/oOex1FBj+JJmRORFc= X-Gm-Gg: AeBDiesCtPAwivAb3kH3OBhvHoKS+41oVrjy/jlX0xfm8kG7ta0GWHcazqR7MpTkvP+ 7AoONmocUxGdw0UTFa7Il0Jv34DI0Vo2ip1gp5te4BeyMooT7MVuCL3vfrJICQ1TESjwVVnutZD CgHEj8aHYtecxaLVEFDiKog3/sGf7yCoMx1Vho4T1KuwRKhJW8G5GP6odA+OwrFbB69qzQ7obrR cpgkz89TSqio7hEgl9l6ZDQh8SLOthO8r8aSnK2bIpbCcQqoOCLHob9jgWobTtxtAnMIKRk2Dxm GD8mfgR5z5Wda/BqDGK9nM7amJdaY9waUP3mONFXBi7qPHInSivHSu20ABOheHBBmpveXpwwxdS Ob9R7 X-Received: by 2002:a2e:be8c:0:b0:38b:f454:8569 with SMTP id 38308e7fff4ca-38d8d1dc4d4mr20176121fa.0.1775310680939; Sat, 04 Apr 2026 06:51:20 -0700 (PDT) MIME-Version: 1.0 References: <8a6799be-bd42-49fb-8914-856c97bb1977@iki.fi> <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> In-Reply-To: <470e7ebe-0971-49f6-8e46-9b8f6395f88b@iki.fi> From: Matthias van de Meent Date: Sat, 4 Apr 2026 15:51:09 +0200 X-Gm-Features: AQROBzBL8_juuMVX8HzWxwT8zj5RRJ93O8FhOcRUHOLP1fgXGvIKGDLx_XEkbGs Message-ID: Subject: Re: Better shared data structure management and resizable shared data structures To: Heikki Linnakangas Cc: Ashutosh Bapat , Robert Haas , Andres Freund , pgsql-hackers , chaturvedipalak1911@gmail.com Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sat, 4 Apr 2026 at 02:49, Heikki Linnakangas wrote: > Those are now committed, and here's a new version rebased over those > changes. The hash options is now called 'nelems', and the 'extra_size' > in ShmemStructOpts is gone. > > Plus a bunch of other fixes and cleanups. I also reordered and > re-grouped the patches a little, into more logical increments I hope. 0001: LGTM 0002: > +++ b/src/backend/storage/ipc/shmem.c > + * Nowadays, there is also a third way to allocate shared memory called There's no clear indicator of the second way to allocate shared memory, nor is the first one clearly defined in the new verson of the comment block. > + * item is deleted. However, if one hash table grows very large and then > + * shrinks, its space cannot be redistributed to other tables. We could build > + * a simple hash bucket garbage collector if need be. Right now, it seems > + * unnecessary. I think this new text is outdated, given that we don't have growing hash tables anymore. I also think it should've referred to elements, not buckets; dynahash's buckets cannot readily be deallocated as they're generally always "in use" (they might be NULL, but they're still accessed in read operations on missing keys). Elements are put in the freelist if not used, and those could be released into a memory pool if so desired (and coded). > + * In builtin PostgreSQL code, add the callbacks to the list in > + * src/include/storage/subsystemlist.h. This refers to an automation system that's introduced a few commits later, in commit 0005, and therefore probably should be added only in that commit. > + * Legacy ShmemInitStruct()/ShmemInitHash() functions > + * -------------------------------------------------- Should we have checks in place to avoid calls to new APIs from old callbacks, and vice versa? > ShmemRequestInternal(... > + ShmemRequest *request; [...] > + foreach_ptr(ShmemStructDesc, existing, pending_shmem_requests) [...] > + request = palloc(sizeof(ShmemRequest)); [...] > + pending_shmem_requests = lappend(pending_shmem_requests, request); It looks like you missed my earlier comment about type confusion. Here, pending_shmem_requests is a List of ShmemRequest pointers, while the foreach_ptr() uses ShmemStructDesc, which is a type confusion. The loop checks the 'char *name' field of ShmemStructDesc, which in a ShmemRequest is the 'ShmemStructDesc *desc'. This bug would cause issues if different ShmemStructDescs are registered by the same name, as the ShmemStructDescs wouldn't (necessarily) be strcmp()-equal for the same name. > ShmemAttachRequested(void) > + /* Call attach callbacks */ > + foreach(lc, registered_shmem_callbacks) > + { > + const ShmemCallbacks *callbacks = (const ShmemCallbacks *) lfirst(lc); This would be more concise with foreach_ptr(const ShmemCallbacks, callbacks, registered_shmem_callbacks), like in ShmemInitRequested. > +++ b/src/include/storage/shmem.h > +/* > + * Shared memory is reserved and allocated in stages at postmaster startup, > + * and in EXEC_BACKEND mode, there's some extra work done to "attach" to them > + * at backend startup. ShmemCallbacks holds callback functions that are > + * called at different stages. > + */ > +typedef struct ShmemCallbacks Maybe this should also have the opportunity for a (before_)shmem_exit callback? > + * on-demaind in a backend. If a subsystem sets this flag, the callbacks are > + * called immediately after registration, to initialize or attach to the > + * requested shared memory areas. Ideally we only immediately call the callbacks if we're under postmaster, or in a standalone backend; we shouldn't allocate shmem for some preloaded libraries that set this flag, at least not ahead of loading all preload libraries. 0003: Maybe this could also test that the protections we're putting in place against double-registration of shmem areas actually detect the duplication issue? Otherwise, LGTM 0004-0014: TBD While it's mostly mechanical changes, it did make me notice the rather annoying allocation patterns by XLOGShmemRequest. It allocates various types of data in one go (which, in principle, is fine) but in doing so it adds its own alignment tricks etc, and I'm not super stoked about that. If time allows, could we clean that up? Kind regards, Matthias van de Meent Databricks (https://www.databricks.com)