Re: Better shared data structure management and resizable shared data structures
Heikki Linnakangas <hlinnaka@iki.fi>
On 05/04/2026 02:17, Matthias van de Meent wrote:
> Formatting:
>> + ShmemRequestHash(.name = "pg_stat_statements hash",
>> + .nelems = pgss_max,
>> + .hash_info.keysize = sizeof(pgssHashKey),
>> + .hash_info.entrysize = sizeof(pgssEntry),
>> + .hash_flags = HASH_ELEM | HASH_BLOBS,
>> + .ptr = &pgss_hash,
>> + );
> (note that additional unit of indentation for the closing bracket)
>
> Is this malformatting caused by pgindent? If so, could you see if
> there's a better way of defining ShmemRequestHash/Struct that doesn't
> have this indent as output?
Yeah, that's pgindent. Matter of taste, but I think that looks fine. An
alternative is to put the closing bracket on the same line with the last
argument and drop the trailing comma:
ShmemRequestStruct(.name = "pg_stat_statements",
.size = sizeof(pgssSharedState),
.ptr = (void **) &pgss);
That looks OK to me too.
>> + pgss->extent = 0;
>> + pgss->n_writers = 0;
>> + pgss->gc_count = 0;
>> + pgss->stats.dealloc = 0;
>
> Shmem is said to be zero-initialized, should we remove the manual
> zero-initialization?
>
>> + on_shmem_exit(pgss_shmem_shutdown, (Datum) 0);
> See my upthread comment about adding optional on_shmem_exit callbacks
> to ShmemCallbacks.
>
> 0005: LGTM
>
> 0006: I don't think it is a great idea to make the LwLock machinery
> the first to get allocation requests:
> It has the RequestNamedLWLockTranche infrastructure, which can only
> register new requests while process_shmem_requests_in_progress, and
> making it request its memory ahead of everything else is likely to
> cause an undersized tranche to be allocated.
Good catch. I think the easiest fix is to call process_shmem_requests()
before ShmemCallRequestCallbacks() at postmaster startup. That's kind of
how it was before: process_shmem_requests() was called before all the
*ShmemSize() and *ShmemInit() functions in core.
> You could make sure that this isn't an issue by maintaining a flag
> in lwlock.c that's set when the shmem request is made (and reset on
> shmem exit), which must be false when RequestNamedLWLockTranche() is
> called, and if not then it should throw an error.
I'll change it so that the number of locks calculated in
LWLockShmemRequest() is stored in a global variable, and
LWLockShmemInit() has an Assert() to cross-checks with that. That
catches the bug and seems like a good cross-check in general.
This isn't the only place where we need to pass information from the
request callback to the init callback. I've used a global variable for
that here, and also between ProcGlobalShmemRequest() and
ProcGlobalShmemRequest(). An alternative might be to use the callback
arg pointers that are currently unused, but I'm not sure how to make
that ergonomic. The current 'arg' isn't very helpful for that, so
perhaps the signatures should look like this instead:
static void
LWLockShmemRequest(Datum *init_arg)
{
int numLocks;
numLocks = NUM_FIXED_LWLOCKS + NumLWLocksForNamedTranches();
/* pass the calculated numLocks value to LWLockShmemInit() */
*init_arg = Int32GetDatum(numLocks);
...
}
static void
LWLockShmemInit(Datum init_arg)
{
int numLocks = DatumGetIn32(numLocks);
...
}
If you need to pass more than a single Datum, you can allocate a struct
and pass it via PointerGetDatum(). At that point global variables might
feel simpler again though.
> 0010: Not looked at everything yet, but a few comment:
>
>> +++ b/src/include/access/slru.h
>
> With the changes in the signatures for most/all SLRU functions from a
> hidden-by-typedef pointer to a visible pointer type, maybe this could
> be an opportunity to swap them to `const SlruDesc *ctl` wherever
> possible? I don't think there are many backend-local changes that
> happen to SlruDescs once we've properly started the backend. I'm happy
> to provide an incremental patch if you'd like me to spend cycles on it
> if you're busy.
Yeah sounds like a good idea.
>> +++ b/src/backend/access/transam/clog.c
>
>> + SimpleLruRequest(.desc = &XactSlruDesc,
>> + .name = "transaction",
>> + .Dir = "pg_xact",
>> + .long_segment_names = false,
>> +
>> + .nslots = CLOGShmemBuffers(),
>> + .nlsns = CLOG_LSNS_PER_PAGE,
>> +
>> + .sync_handler = SYNC_HANDLER_CLOG,
>> + .PagePrecedes = CLOGPagePrecedes,
>> + .errdetail_for_io_error = clog_errdetail_for_io_error,
>
> That awfully inconsistent field name styling is ... awful, but not
> this patch's fault. If something can be done about it in a cheap
> fashion in this patch, that'd be great, but I won't hold it against
> you if that's skipped.
:-D.
- Heikki
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