Re: seg fault on dsm_create call

From: Max Fomichev <max(dot)fomitchev(at)gmail(dot)com>
To: "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:45:30
Message-ID: ed698b3a-9cbd-9179-6a2d-e8c957225523@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 28/06/16 19:24, Robert Haas wrote:
> 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.
>
Thanks.
It works now with CurrentResourceOwner = ResourceOwnerCreate(NULL, "name
of my extension")

I am a little bit confused about test/modules/test_shm_mq, where
CurrentResourceOwner is set up before dsm_attach, not dsm_create -

/*
* Connect to the dynamic shared memory segment.
*
* The backend that registered this worker passed us the ID of a shared
* memory segment to which we must attach for further instructions. In
* order to attach to dynamic shared memory, we need a resource owner.
* Once we've mapped the segment in our address space, attach to
the table
* of contents so we can locate the various data structures we'll
need to
* find within the segment.
*/
CurrentResourceOwner = ResourceOwnerCreate(NULL, "test_shm_mq worker");
seg = dsm_attach(DatumGetInt32(main_arg));

--
Best regards,
Max Fomichev

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Alvaro Herrera 2016-06-28 17:43:10 Re: primary_conninfo missing from pg_stat_wal_receiver
Previous Message Robert Haas 2016-06-28 16:24:13 Re: seg fault on dsm_create call