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 1w9Sfx-001SCp-0X for pgsql-hackers@arkaria.postgresql.org; Sun, 05 Apr 2026 18:58:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w9Sfv-004TIe-1s for pgsql-hackers@arkaria.postgresql.org; Sun, 05 Apr 2026 18:58:24 +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 1w9Sfv-004TIW-0w for pgsql-hackers@lists.postgresql.org; Sun, 05 Apr 2026 18:58:23 +0000 Received: from lahtoruutu.iki.fi ([185.185.170.37]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1w9Sft-00000000lpC-1xGi for pgsql-hackers@postgresql.org; Sun, 05 Apr 2026 18:58:23 +0000 Received: from [10.0.2.15] (unknown [130.41.208.1]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: hlinnaka) by lahtoruutu.iki.fi (Postfix) with ESMTPSA id 4fphYl430Pz49Pxf; Sun, 05 Apr 2026 21:58:19 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1775415500; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tbZ6QDBCHmQE+pCoEP70Q0VF02JhEag2f749MQOeE1o=; b=YMzN6IrYJvZYaRspmqCE8QRgIbehDsu6qcLCzQJbO10rHXtTu/xRShl9AAGPEJm9Vi1LXu v3AQjTjyG2nMxh3S0sWFWbUDtknLj4WaCyaehoFTJOo4mOF8SSu7hguSCcjoL4f2YPwXR0 h6wQmL1+Q7bKmI2mD8hfQq4igfThpy34uEWSx8aGF3TGvKm6G3CD9nEFPuhjlssgbREBw/ GnRy3uRO+0QOgKNTelLnGIXrTazGmIgDZ+EwlB/o+KoWjf1K618nFeQ/SUKNR9ZVQIwlBJ Zi7jTyLu/AXTLg+JAfAZU0TCzSoRxqCG9aOEPKa16eRcG6rOd/aEgDK7qC7D8A== ARC-Seal: i=1; a=rsa-sha256; d=iki.fi; s=lahtoruutu; cv=none; t=1775415500; b=Ea/Z5qilBC1wsCjrOdLVYGLHrKa4ltRzGcFZiaIJehc2T0Q191/2cg1KekOHk4ieyd4y31 DnMbf9wsoBPSa7Ta/3lGSPOm4Sklwp9Ulc23PPor4yMdgCYc7P0P/MiO1fPVfc3dEJTY2w K3uo+g0UZZevQ8Tblr/YD1Ma8IVkh4ta/wRGLdg+SiF1k/cHq4ritb1oesctbyYL4+MV/y lOPirWtzY5wEMHELT+wV5kBambeE9UTKaEMPO/Ys2FpkqKPDI/l7OgJ38L1+MsxoOtopFe esW2UgPEzayr9RhpC/2erLOwVVbbYE7gVo4G4MppwYHXtOTo6wRhNdHFYVSnDQ== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1775415500; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tbZ6QDBCHmQE+pCoEP70Q0VF02JhEag2f749MQOeE1o=; b=hy3R1g2MiijpO939H5ykcDUP4YrGWU9m0C7FkOgn+yul8MexmYJKynlcxP1UZY6pM+9jYh x9j9DSyQme+wmruIWOZzNTZXt0zTuSfpMyoiAUmD6NqZJ3zqUaxxwEsW8pVq3RfQb7obA6 HhVrDY+9v3eHj9FgQJdPKIjrtj+32ibYDnAOLlvmoG9wi/VaQ8C34fs0ji/ni6BGRbv9WJ eSht0zHinG0VfxFGyJ+2WQoM+mqm5XhOMrBRkRJ0kMS2x90hAObo62359PxRXyvzD2mKKk fV2QxfDoIO4YdMo1VAXs4HYufttYwaz6JnjQL/YlYqugctaML9qBn7Cw1XAlfw== Message-ID: Date: Sun, 5 Apr 2026 21:58:15 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Better shared data structure management and resizable shared data structures To: Matthias van de Meent Cc: Ashutosh Bapat , Robert Haas , Andres Freund , pgsql-hackers , chaturvedipalak1911@gmail.com 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> Content-Language: en-US From: Heikki Linnakangas In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 03/04/2026 01:10, Matthias van de Meent wrote: > While I do think it's an improvement over the current APIs, the > improvement seems to be mostly concentrated in the RequestStruct/Hash > department, with only marginal improvements in RegisterShmemCallbacks. > I feel like it's missing the important part: I'd like > direct-from-_PG_init() ShmemRequestStruct/Hash calls. If > ShmemRequestStruct/Hash had a size callback as alternative to the size > field (which would then be called after preload_libraries finishes) > then that would be sufficient for most shmem allocations, and it'd > simplify shmem management for most subsystems. > We'd still need the shmem lifecycle hooks/RegisterShmemCallbacks to > allow conditionally allocated shmem areas (e.g. those used in aio), > but I think that, in general, we shouldn't need a separate callback > function just to get started registering shmem structures. I kind of started from that thought too, but the design has since evolved to what it is. Robert in particular was skeptical of that approach, and I think I've come around on most of his feedback. A per-struct size callback isn't very ergonomic in places like LockManagerShmemRequest(), which derives the size of two different things from the same calculated value. You'd need to repeat the calculation for each one, or pass it through global variables or something. PredicateLockShmemInit() is another extreme example. It already has that problem to some extent, and already uses global variables (I'm all ears if you have suggestions to improve it!) but I feel that with a size callback this kind of stuff would get even harder. Another reason is that it's good to have only one way of doing the initialization, instead of one simple way and a different way for more complex scenarios. If the simple way was vastly simpler, it might be worth it, but I don't think there's that much difference here. BTW, I also considered another way of initializing structs for simple cases: instead of providing an init callback function, you could provide the struct contents directly in the ShmemRequestStruct() call. To pick a random example, slotsync.c currently looks like this: static void SlotSyncShmemRequest(void *arg) { ShmemRequestStruct(.name = "Slot Sync Data", .size = sizeof(SlotSyncCtxStruct), .ptr = (void **) &SlotSyncCtx, ); } static void SlotSyncShmemInit(void *arg) { memset(SlotSyncCtx, 0, sizeof(SlotSyncCtxStruct)); SlotSyncCtx->pid = InvalidPid; SpinLockInit(&SlotSyncCtx->mutex); } But it could look like this instead: static void SlotSyncShmemRequest(void *arg) { SlotSyncCtxStruct init_content; memset(SlotSyncCtx, 0, sizeof(SlotSyncCtxStruct)); init_content.pid SpinLockInit(&SlotSyncCtx->mutex); ShmemRequestStruct(.name = "Slot Sync Data", .size = sizeof(SlotSyncCtxStruct), .ptr = (void **) &SlotSyncCtx, .init_content = &init_content, ); } That'd only work for small, simple structs, though. Since the initial contents would be copied in this model, it won't work for anything with pointers to itself, for example. And it's not much less code after all. - Heikki