Re: Better shared data structure management and resizable shared data structures

From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: Matthias van de Meent <boekewurm+postgres(at)gmail(dot)com>
Cc: Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>, Robert Haas <robertmhaas(at)gmail(dot)com>, Andres Freund <andres(at)anarazel(dot)de>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>, chaturvedipalak1911(at)gmail(dot)com
Subject: Re: Better shared data structure management and resizable shared data structures
Date: 2026-04-05 15:07:23
Message-ID: a37ddc16-2892-40c6-bea6-a556f6011b7d@iki.fi
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2026-04-05 15:10:52 Re: PG 19 release notes and authors
Previous Message Andrew Dunstan 2026-04-05 15:06:09 Re: pg_get__*_ddl consolidation