Re: seg fault on dsm_create call

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Max Fomichev <max(dot)fomitchev(at)gmail(dot)com>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: seg fault on dsm_create call
Date: 2016-06-28 16:24:13
Message-ID: CA+TgmobcV1FEmaT_Ykrv6_apDHRiKJMsRvF-HG45r88FoMGwSg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Jun 28, 2016 at 10:11 AM, Max Fomichev <max(dot)fomitchev(at)gmail(dot)com> wrote:
> Hello,
> sorry for my repost from psql-novice, probably it was not a right place for
> my question.
>
> I'm trying to understand how to work with dynamic shared memory, message
> queues and workers.
> The problem is I can not initialize any dsm segment -
>
> void _PG_init() {
> ...
> dsm_segment *seg = dsm_create(32768, 0); // Segmentation fault here
> ...
> BackgroundWorker worker;
> sprintf(worker.bgw_name, "mystem wrapper process");
> worker.bgw_flags = BGWORKER_SHMEM_ACCESS;
> worker.bgw_start_time = BgWorkerStart_RecoveryFinished;
> worker.bgw_restart_time = BGW_NEVER_RESTART;
> worker.bgw_main = mainProc;
> worker.bgw_notify_pid = 0;
> RegisterBackgroundWorker(&worker);
> }
>
> Also I was trying to move dsm_create call to a worker, but with the same
> result -
>
> static void mainProc(Datum) {
> ...
> dsm_segment *seg = dsm_create(32768, 0); // Segmentation fault here
> ...
> pqsignal(SIGTERM, mystemSigterm);
> BackgroundWorkerUnblockSignals();
> ...
>
> What could be a reason and what am I doing wrong?

I think there are two problems.

1. You need to set up a ResourceOwner before you can attach to a DSM
segment. Something like: CurrentResourceOwner =
ResourceOwnerCreate(NULL, "name of my extension").

2. You can't do this from inside a _PG_init() block. That will run in
every process that loads this, which is probably not what you want,
and it will run in the postmaster also, which will not work: the
postmaster cannot use DSM.

Actually, I'd like to change #1 at some point, so that if
CurrentResourceOwner = NULL and you create or attach to a DSM, you
just get a backend-lifespan mapping. The current setup is annoying
rather than helpful. But currently that's how it is.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Max Fomichev 2016-06-28 16:45:30 Re: seg fault on dsm_create call
Previous Message Alvaro Herrera 2016-06-28 16:16:49 Re: primary_conninfo missing from pg_stat_wal_receiver