Re: [HACKERS] Regarding Postgres Dynamic Shared Memory (DSA) - Mailing list pgsql-hackers
From | Mahendranath Gurram |
---|---|
Subject | Re: [HACKERS] Regarding Postgres Dynamic Shared Memory (DSA) |
Date | |
Msg-id | 15cc99b2a1b.127a695611477.972517809289130986@zohocorp.com Whole thread Raw |
In response to | Re: [HACKERS] Regarding Postgres Dynamic Shared Memory (DSA) (Thomas Munro <thomas.munro@enterprisedb.com>) |
Responses |
Re: [HACKERS] Regarding Postgres Dynamic Shared Memory (DSA)
|
List | pgsql-hackers |
Hi Thomas,
I like the whole idea.
In fact, i understood this in your very first response itself.
Only thing is every time i have to check for dsa_attached or not. I mean get_my_shared_state() is NULL or not.
To avoid that check, i tried creating it in _PG_Init(postmaster process itself) all that stuff :(
Anyways the solution you suggested will work for me. I'll live with that if check.
Thanks a lot for all your help.
I'll implement this and update the status.
Cheers :)
Thanks & Best Regards,
-Mahi
Teamwork divides the task and multiplies the success.
Teamwork divides the task and multiplies the success.
---- On Wed, 21 Jun 2017 12:02:30 +0530 Thomas Munro <thomas.munro@enterprisedb.com> wrote ----
On Wed, Jun 21, 2017 at 5:27 PM, Mahendranath Gurram<mahendranath@zohocorp.com> wrote:> Initially i tried to design the same way.> I mean, i have created a background worker and created dsa in it.> I tried to attach/detach to the same dsa/dsm by all the backends(postgres> clients/connections) during backend(client/connection) init/destroy.> I didn't find any triggers or callbacks during backend init/close to> attach/detach the dsa/dsm. Hence, i took this approach.> If postgres have any such triggers/callbacks available, please let me know,> that is of great great help for me.>> Anyways now i understood, i have taken a wrong approach to use dsa. I'll try> to figure out any other way to build my in-memory index over postgres.You definitely can use DSA or DSM for this. As a matter of factshared in-memory indexing was one of our target use cases. You justhave to jump through a few hoops... Here's one approach:1. Use _PG_init and the shmem hook to reserve a little bit oftraditional shared memory and initialise it to zero. This will beused just to share the DSA handle, but you can't actually create theDSA area in postmaster. In other words, this little bit of sharedmemory is for "discovery", since it can be looked up by name from anybackend.2. In each backend that wants to use your new in-memory index system,you need to be able to attach or create the DSA area on-demand.Perhaps you could have a get_my_shared_state() function (insert bettername) that uses a static local variable to hold a pointer to somestate. If it's NULL, you know you need to create the state. Thatshould happen only once in each backend, the first time through thefunction. In that case you need to create or attach to the DSA areaas appropriate, which you should wrap inLWLockAcquire(AddinShmemInitLock,LW_EXCLUSIVE)/LWLockRelease(AddinShmemInitLock) to serialise the codeblock. First, look up the bit of traditional shared memory to see ifthere is a DSA handle published in it already. If there is you canattach. If there isn't, you are the first so you need to create, andpublish the handle for others to attach to. Remember whatever stateyou need to remember, such as the dsa_area, in static local variablesso that all future calls to get_my_shared_state() in that backend willbe fast.If you do it that way, then it doesn't matter whether it's backgroundworkers or foreground processes: whoever is first to callget_my_shared_state() will create the DSA area (and whatever else youwant to do to set things up). All later callers will attach to theexisting one. But each backend only ever has to enter the locked codepath once, and from then on it's fast and there's no lock.Note that you could skip step 1 and not require a preload sharedlibrary. Then you'd be creating the 'discovery' shmem region ondemand too! Just make sure it's in the locked region and payattention to the 'found' output variable to decide whether you'refirst and need to initialise it. Skipping step 1 is very slightlyagainst the rules though, because you'd be using a little piece ofmemory that you didn't tell the postmaster to reserve space for withRequestAddinShmemSpace. It's very small though, so you might decidethat's OK...If you don't like the idea of creating that shmem stuff on demand, youcould look into using session_preload_libraries as a way to get a'backend init' hook.--Thomas Munro--Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)To make changes to your subscription:
pgsql-hackers by date: