Is there any way to request unique lwlock inside a background worker in PG9.4?

Started by Hubert Zhangabout 7 years ago5 messages
#1Hubert Zhang
hzhang@pivotal.io

Hi all,

I want to init SHM in a background worker, which is supported in PG9.4.
Also I need to use lwlock to protect the share memory inside the worker
code.

RequestNamedLWLockTranche is the way to handle it, but it's not supported
in PG 9.4, is there any way I could request an unique lock inside worker
init code in PG 9.4?

--
Thanks

Hubert Zhang

#2Amit Kapila
amit.kapila16@gmail.com
In reply to: Hubert Zhang (#1)
Re: Is there any way to request unique lwlock inside a background worker in PG9.4?

On Wed, Oct 17, 2018 at 3:49 PM Hubert Zhang <hzhang@pivotal.io> wrote:

Hi all,

I want to init SHM in a background worker, which is supported in PG9.4. Also I need to use lwlock to protect the share memory inside the worker code.

RequestNamedLWLockTranche is the way to handle it, but it's not supported in PG 9.4, is there any way I could request an unique lock inside worker init code in PG 9.4?

You might want to look at section "Shared Memory and LWLocks" in the docs [1]https://www.postgresql.org/docs/9.4/static/xfunc-c.html.

[1]: https://www.postgresql.org/docs/9.4/static/xfunc-c.html

--
With Regards,
Amit Kapila.
EnterpriseDB: http://www.enterprisedb.com

#3Hubert Zhang
hzhang@pivotal.io
In reply to: Amit Kapila (#2)
Re: Is there any way to request unique lwlock inside a background worker in PG9.4?

Hi Amit

The section "Share Memory and LWLocks" describe the AddinShmemInitLock which
is used to protect the ShmemInitStruct() when backend workers initialize
their shm. My requirement is to how to protect the shm access within the
bgworkers(not at init stage). This lock should be bgworkers specific.

On Wed, Oct 17, 2018 at 7:51 PM Amit Kapila <amit.kapila16@gmail.com> wrote:

On Wed, Oct 17, 2018 at 3:49 PM Hubert Zhang <hzhang@pivotal.io> wrote:

Hi all,

I want to init SHM in a background worker, which is supported in PG9.4.

Also I need to use lwlock to protect the share memory inside the worker
code.

RequestNamedLWLockTranche is the way to handle it, but it's not

supported in PG 9.4, is there any way I could request an unique lock inside
worker init code in PG 9.4?

You might want to look at section "Shared Memory and LWLocks" in the docs
[1].

[1] - https://www.postgresql.org/docs/9.4/static/xfunc-c.html

--
With Regards,
Amit Kapila.
EnterpriseDB: http://www.enterprisedb.com

--
Thanks

Hubert Zhang

#4Andres Freund
andres@anarazel.de
In reply to: Hubert Zhang (#3)
Re: Is there any way to request unique lwlock inside a background worker in PG9.4?

Hi,

On 2018-10-17 23:11:26 +0800, Hubert Zhang wrote:

The section "Share Memory and LWLocks" describe the AddinShmemInitLock which
is used to protect the ShmemInitStruct() when backend workers initialize
their shm. My requirement is to how to protect the shm access within the
bgworkers(not at init stage). This lock should be bgworkers specific.

Rereead the page please. After you RequestAddinLWLocks() during
initialization, you can use LWLockAssign() to get an lwlock. Which you
then can use as is your pleasure.

Greetings,

Andres Freund

#5Hubert Zhang
hzhang@pivotal.io
In reply to: Andres Freund (#4)
Re: Is there any way to request unique lwlock inside a background worker in PG9.4?

Thanks a lot.

On Wed, Oct 17, 2018 at 11:21 PM Andres Freund <andres@anarazel.de> wrote:

Hi,

On 2018-10-17 23:11:26 +0800, Hubert Zhang wrote:

The section "Share Memory and LWLocks" describe the AddinShmemInitLock

which

is used to protect the ShmemInitStruct() when backend workers initialize
their shm. My requirement is to how to protect the shm access within the
bgworkers(not at init stage). This lock should be bgworkers specific.

Rereead the page please. After you RequestAddinLWLocks() during
initialization, you can use LWLockAssign() to get an lwlock. Which you
then can use as is your pleasure.

Greetings,

Andres Freund

--
Thanks

Hubert Zhang