Re: help in allocating shared module within a module

From: Matthias van de Meent <boekewurm+postgres(at)gmail(dot)com>
To: Luca Ferrari <fluca1978(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: help in allocating shared module within a module
Date: 2024-12-23 18:08:37
Message-ID: CAEze2WjocCocF3pyLL7U5ndggtvV_VJdS-VS2=7S8jAcVQC8Uw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, 23 Dec 2024, 18:32 Luca Ferrari, <fluca1978(at)gmail(dot)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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Marcos Pegoraro 2024-12-23 18:38:49 Re: Document NULL
Previous Message Jan Wieck 2024-12-23 18:06:49 Re: transaction lost when delete clog file after normal shutdown