Thread: help in allocating shared module within a module

help in allocating shared module within a module

From
Luca Ferrari
Date:
Hi all,
hope this is the right place to ask for, otherwise please point me in
the right resource.
I'm trying to develop a module that needs shared memory between
background workers.

The _PG_init calls a function to reserve the shared memory, which in
turn calls RequestAddinShmemSpace, which seems to fail. In fact, I
never got the debug line after the call to RequestAddinShmemSpaec and
_PG_init does not conitnue.

Here is my code skeleton:

void
_PG_init(void)
{

    if ( ! process_shared_preload_libraries_in_progress )
        return;



    // set up the GUCs
    _define_gucs();


    // set up the shared memory
    _create_shared_memory();

...
}


void
_create_shared_memory()
{
  Size estimated_size = 10136:

  elog( DEBUG2, "shared allocation %lu", estimated_size );

  RequestAddinShmemSpace( estimated_size );

  elog(DEBUG2, "never reached!" );
...
}

What am I missing here?

Thanks,
Luca



Re: help in allocating shared module within a module

From
Matthias van de Meent
Date:
On Mon, 23 Dec 2024, 18:32 Luca Ferrari, <fluca1978@gmail.com> wrote:
>
> Hi all,
> hope this is the right place to ask for, otherwise please point me in
> the right resource.
> I'm trying to develop a module that needs shared memory between
> background workers.
>
> The _PG_init calls a function to reserve the shared memory, which in
> turn calls RequestAddinShmemSpace, which seems to fail. In fact, I
> never got the debug line after the call to RequestAddinShmemSpaec and
> _PG_init does not conitnue.
>
> Here is my code skeleton:
>
> void
> _PG_init(void)
> {
[...]
>     // set up the shared memory
>     _create_shared_memory();
[...]
> void
> _create_shared_memory()
> {
>   Size estimated_size = 10136:
[...]
>   RequestAddinShmemSpace( estimated_size );
[...]
> What am I missing here?

Starting with PG15, shared memory should be allocated through a hook,
shmem_request_hook, and not through direct calls to
RequestAddinShmemSpace in _PG_init().

For specific info, see [0] and [1] which introduced
shmem_request_hook. PGPedia also has some info on how to deal with
older PG versions: [2]

I hope this helps.

Kind regards,

Matthias van de Meent
Neon (https://neon.tech)

[0] https://www.postgresql.org/docs/current/xfunc-c.html#XFUNC-SHARED-ADDIN
[1] https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=4f2400cb3f10aa79f99fba680c198237da28dd38
[2] https://pgpedia.info/s/shmem_request_hook.html