Re: Regarding Postgres Dynamic Shared Memory (DSA)

From: Mahendranath Gurram <mahendranath(at)zohocorp(dot)com>
To: "Thomas Munro" <thomas(dot)munro(at)enterprisedb(dot)com>
Cc: "Mahi Gurram" <teckymahi(at)gmail(dot)com>, "Pg Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Regarding Postgres Dynamic Shared Memory (DSA)
Date: 2017-06-21 05:27:19
Message-ID: 15cc91e836b.12362f6336537.3402818166899386386@zohocorp.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Thomas,

Thanks for taking time and explaining the things.

Postgres extensions can't rely on backends inheriting the postmaster's

memory map like this (other than the main shared memory areas which

the core code looks after). For one thing, new backends aren't

created with fork() on all platforms (mainly Windows AFAIK, but also

any build with EXEC_BACKEND defined). The next problem is that dsa.c

and dsm.c work with reference counts that will be wrong if you try to

use memory map inheritance like this. Another problem is that the

postmaster isn't allowed to create DSM segment: if it's working for

you then I think you must be building with asserts disabled?

Now I understood, i took wrong approach.

That's simply not how DSA is

designed to be used: you need to create DSA areas in a non-postmaster

backend, and then attach explicitly from every other backend that

wants to access the area. Each backend needs to get its own dsa_area

object (either by creating or attaching).

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.

Once again thanks a lot for taking time to help me.

@Dilip thank you for your response :)

Thanks &amp; Best Regards,

-Mahi
Teamwork divides the task and multiplies the success.

---- On Wed, 21 Jun 2017 04:26:45 +0530 Thomas Munro &lt;thomas(dot)munro(at)enterprisedb(dot)com&gt; wrote ----

On Sat, Jun 17, 2017 at 1:17 AM, Mahi Gurram &lt;teckymahi(at)gmail(dot)com&gt; wrote:

&gt;&gt; 3. Whether you are the backend that created it or a backend that

&gt;&gt; attached to it, I think you'll need to store the dsa_area in a global

&gt;&gt; variable for your UDFs to access. Note that the dsa_area object will

&gt;&gt; be different in each backend: there is no point in storing that

&gt;&gt; address itself in shared memory, as you have it, as you certainly

&gt;&gt; can't use it in any other backend. In other words, each backend that

&gt;&gt; attached has its own dsa_area object that it can use to access the

&gt;&gt; common dynamic shared memory area.

&gt;

&gt;

&gt; In case of forked processes, the OS actually does share the pages initially,

&gt; because fork implements copy-on-write semantics. which means that provided

&gt; none of the processes modifies the pages, they both points to same address

&gt; and the same data.

&gt;

&gt; Based on above theory, assume i have created dsa_area object in postmaster

&gt; process(_PG_Init) and is a global variable, all the backends/forked

&gt; processes can able to access/share the same dsa_area object and it's

&gt; members.

&gt;

&gt; Hence theoretically, the code should work with out any issues. But i'm sure

&gt; why it is not working as expected :(

&gt;

&gt; I tried debugging by putting prints, and observed the below things:

&gt; 1. dsa_area_control address is different among postmaster process and

&gt; backends.

&gt; 2. After restarting, they seems to be same and hence it is working after

&gt; that.

Postgres extensions can't rely on backends inheriting the postmaster's

memory map like this (other than the main shared memory areas which

the core code looks after). For one thing, new backends aren't

created with fork() on all platforms (mainly Windows AFAIK, but also

any build with EXEC_BACKEND defined). The next problem is that dsa.c

and dsm.c work with reference counts that will be wrong if you try to

use memory map inheritance like this. Another problem is that the

postmaster isn't allowed to create DSM segment: if it's working for

you then I think you must be building with asserts disabled?

I'm not sure exactly why you're seeing the symptoms you're seeing

(working on one flavour of Unix and not another, and then working

after a crash-restart -- I guess it has something to do with

coincidences of mapped address). That's simply not how DSA is

designed to be used: you need to create DSA areas in a non-postmaster

backend, and then attach explicitly from every other backend that

wants to access the area. Each backend needs to get its own dsa_area

object (either by creating or attaching).

--

Thomas Munro

http://www.enterprisedb.com

--

Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)

To make changes to your subscription:

http://www.postgresql.org/mailpref/pgsql-hackers

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message sanyam jain 2017-06-21 05:28:25 Re: Logical decoding on standby
Previous Message Amit Kapila 2017-06-21 04:20:36 Re: Getting server crash on Windows when using ICU collation