Re: Better shared data structure management and resizable shared data structures
Matthias van de Meent <boekewurm+postgres@gmail.com>
On Sun, 5 Apr 2026 at 13:20, Ashutosh Bapat <ashutosh.bapat.oss@gmail.com> wrote: > > On Sun, Apr 5, 2026 at 2:36 PM Matthias van de Meent > <boekewurm+postgres@gmail.com> wrote: > > > > On Sun, 5 Apr 2026, 07:59 Ashutosh Bapat, <ashutosh.bapat.oss@gmail.com> wrote: > > > > > > On Sun, Apr 5, 2026 at 11:18 AM Ashutosh Bapat > > > <ashutosh.bapat.oss@gmail.com> wrote: > > > > > > > > I'm not opposed to HAVE_RESIZABLE_SHMEM, but is it universal enough on > > its platforms to make it part of the exposed ABI for Shmem? I think > > that we should expose the same functions and structs, and just have > > the shmem internals throw an error if the configuration used by the > > user implies the user wants to update shmem sizing when the system > > doesn't support it. That would avoid extensions having to recompile > > between have/have not systems that have an otherwise compatible ABI; > > especially when those extensions don't actually need the resizeable > > part of the shmem system. > > > > I don't think I understand this fully. An extension may want to > support a structure in both modes - fixed as well as resizable > depending upon whether the latter is supported. If the structure has > maximum_size always the extension code needs to set it to 0 when the > resizable shared structure is not supported and set to actual > maximum_size when the resizable structure is supported. Without a > macro or some flag they can not do that. The flag/macro then becomes > part ABI for shmem. Am I correct? That's not quite what I meant. With your patch, the size and field offsets in `struct ShmemStructOpts` changes depending only on HAVE_RESIZABLE_SHMEM, as does function's availability. This means that an extension that's built without HAVE_RESIZABLE_SHMEM (an otherwise identical system) can't correctly be loaded into a server that does have HAVE_RESIZABLE_SHMEM defined - or at least it'll misbehave when it tries to use the new shmem system without trying out resizeable areas. If instead the fields used for definining resizable shmem areas (and the relevant functions) are always defined, but with runtime checks to make sure that in !HAVE_RESIZEABLE_SHMEM nobody tries to use the resizing functionality, then that'd reduce the unchecked hidden incompatibility; assuming that no extension manually does memory management syscall operations on those shmem areas. > Since extension binaries need to be > built on different platforms anyway, that would automatically take > care of building with or without HAVE_RESIZABLE_SHMEM. I feel it makes > testing simpler since run time behaviour is fixed. Maybe I am missing > something. Maybe a code diff or some example platform might make it > more clear for me. I'm not entirely sure it would be automatic. Is it guaranteed that HAVE_RESIZABLE_SHMEM won't change over the lifetime of any distribution's platform? Because it's definitely not apparent to me that rebuilding the new server version against an upgraded platform (now possibly with HAVE_RESIZABLE_SHMEM) should also mean rebuilding the extensions that have been built against a previous minor version (without HAVE_RESIZABLE_SHMEM). > > > For now, it > > > seems only for the sanity checks, but it could be seen as a useful > > > safety feature. A difference in maximum_size and minimum_size would > > > indicate that the structure is resizable. > > > > I think that's the right approach. > > > I also think that introducing minimum_size is useful. Let's hear from > Heikki before implementing it, in case he has a different opinion. I > am not sure about min_allocated_space though - what use do you see for > it. reserved_space is useful in pg_shmem_allocations() C function > itself and gives impact to the fully grown structure. What would > min_allocated_space give us? If at all it would be min_allocated_size > not space since reserved space will never change. But even that I am > not sure about. I'd say it's mostly interesting for people looking at or debugging shmem allocations. Which isn't a huge group of developers or DBAs, but if we're exposing data like this, and are going to allow resizing, then someone could see some benefits from this. E.g., it may be useful to have the information to see how low the currently running server can scale down its memory usage, so that the admin can see whether a reboot is required if they want to allow it to scale it down further (assuming there's a lower limit for allocations - some shmem structs may have a lower scaling limit defined at startup, while others may be able to scale linearly from 0 to 100) Kind regards, Matthias van de Meent Databricks (https://www.databricks.com)
Commits
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Tidy up #ifdef USE_INJECTION_POINTS guards
- 9480c585df6c 19 (unreleased) landed
-
Convert all remaining subsystems to use the new shmem allocation API
- 9b5acad3f40f 19 (unreleased) landed
-
Convert buffer manager to use the new shmem allocation functions
- a4b6139dcceb 19 (unreleased) landed
-
Add alignment option to ShmemRequestStruct()
- dacfe81a0de5 19 (unreleased) landed
-
Convert AIO to use the new shmem allocation functions
- 58a1573385ed 19 (unreleased) landed
-
Convert SLRUs to use the new shmem allocation functions
- 2e0943a8597e 19 (unreleased) landed
-
Refactor shmem initialization code in predicate.c
- 4c9eca5afea0 19 (unreleased) landed
-
Use the new shmem allocation functions in a few core subsystems
- c6d55714ba4c 19 (unreleased) landed
-
Convert lwlock.c to use the new shmem allocation functions
- a006bc7b1699 19 (unreleased) landed
-
Introduce a registry of built-in shmem subsystems
- 1fc2e9fbc0a3 19 (unreleased) landed
-
Convert pg_stat_statements to use the new shmem allocation functions
- d4885af3d653 19 (unreleased) landed
-
Add a test module to test after-startup shmem allocations
- 6409994c7dd8 19 (unreleased) landed
-
Introduce a new mechanism for registering shared memory areas
- 283e823f9dcb 19 (unreleased) landed
-
Move some code from shmem.c and shmem.h
- 6ef9bee29310 19 (unreleased) landed
-
Improve test_lwlock_tranches
- 92a685e4070d 19 (unreleased) landed
-
Test pg_stat_statements across crash restart
- 148fe2b05df5 19 (unreleased) landed
-
Refactor PredicateLockShmemInit to not reuse var for different things
- 3fd057772827 19 (unreleased) landed
-
Refactor ShmemIndex initialization
- 6b8238cb6aa7 19 (unreleased) landed
-
Add a new shmem_request_hook hook.
- 4f2400cb3f10 15.0 cited