On Tue, Jun 28, 2016 at 10:11 AM, Max Fomichev <max.fomitchev@gmail.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