dsm_registry: Add detach and destroy features

Started by Sungwoo Changover 1 year ago14 messageshackers

Thread notes

1 note#feature#moderate#enhancement
hackorum-bot#feature#moderate#enhancement
hackorum-bot
2 days ago

@global #feature #moderate #enhancement

#feature#moderate#enhancement
Jump to latest
#1Sungwoo Chang
swchangdev@gmail.com

Hi,

I had use cases for manually detaching and destroying dsm segment while
developing my extension (emulating Oracle's DBMS_PIPE package). In this
extension user can call user defined procedures that destroy the resources
in the shared memory. There were mentions about adding detach features in
the original discussion (introduce dynamic shared memory registry
</messages/by-id/20231205034647.GA2705267@nathanxps13>),
but it seems like it didn't expand from there.

In the attached patch, dsm_registry not provides two more interfaces:
DetachNamedDSMSegment, and DestroyNamedDSMSegment. Detach function simply
detaches the corresponding dsm_segment. Destroy function calls
dsm_unpin_segment and deletes the dshash entry from the dsm_registry_table.
Destroy function may not "destroy" the dsm segment immediately if there are
other processes attached to the dsm segment, but it will eventually cause
the dsm segment to be destroyed when there are no more processes attached
to it. Because Destroy function deletes the entry from the
dsm_registry_table, it will block new attachment. It will create a new dsm
segment with the same name.

Detach and Destroy functions allows on_detach_callback, which will be
passed on to the dsm segment by calling on_dsm_detach. Because
on_dsm_detach requires the callback function to have dsm_segment as input,
we wrap the library user callback with a function matching to the required
signature.

I also made some fix in GetNamedDSMSegment function where it throws an
exception if the found entry's size field does not match the user input. It
looks like dshash_release_lock needs to be called and MemoryContext needs
to be switched back to the old one.

It's my first time submitting a patch so please let me know if I missed on
something.

Best regards,

Sungwoo Chang

Attachments:

v1-0001-add-detach-and-destroy-for-dsm-registry.patchtext/x-patch; charset=US-ASCII; name=v1-0001-add-detach-and-destroy-for-dsm-registry.patchDownload+221-2
#2Nathan Bossart
nathandbossart@gmail.com
In reply to: Sungwoo Chang (#1)
Re: dsm_registry: Add detach and destroy features

On Fri, Mar 14, 2025 at 10:46:58AM +0900, Sungwoo Chang wrote:

I also made some fix in GetNamedDSMSegment function where it throws an
exception if the found entry's size field does not match the user input. It
looks like dshash_release_lock needs to be called and MemoryContext needs
to be switched back to the old one.

My expectation is that the error handling takes care of these things. Are
there cases where it does not? In any case, I would expect these errors to
be extremely rare to the point of virtually never happening in practice.

It's my first time submitting a patch so please let me know if I missed on
something.

I'm delighted that folks want to expand on this feature, and I'm very
interested in helping. Other things folks have asked for are a view into
the DSM registry [0]/messages/by-id/4D445D3E-81C5-4135-95BB-D414204A0AB4@gmail.com and a simple way to allocate a DSA or dshash table
with the DSM registry, all of which seem like good improvements to me.

That being said, we are at the very end of the v18 development cycle, and
most folks are probably busy trying to finish their ongoing projects. So
it may be a while before anyone can give this a meaningful review. Please
be sure to add a commitfest entry [1]https://commitfest.postgresql.org/ so it doesn't get lost.

[0]: /messages/by-id/4D445D3E-81C5-4135-95BB-D414204A0AB4@gmail.com
[1]: https://commitfest.postgresql.org/

--
nathan

#3Sungwoo Chang
swchangdev@gmail.com
In reply to: Nathan Bossart (#2)
Re: dsm_registry: Add detach and destroy features

2025년 3월 14일 (금) 오후 11:36, Nathan Bossart <nathandbossart@gmail.com>님이 작성:

My expectation is that the error handling takes care of these things. Are
there cases where it does not? In any case, I would expect these errors to
be extremely rare to the point of virtually never happening in practice.

I don't see any logic in ereport where it switches back the original
memory context.
I guess not switching back to the original memory context is not so
problematic because
we set it to TopMemoryContext and throw will cause the user to restart a query.
Not releasing the dshash lock could be problematic, so it's better to keep that.

You are right about these errors to be rare, especially the one where
we can't find a
dsm_segment given the dsm handle. However, user faults can cause the
other error,
where the input name and size for dshash lookup don't match.

I'm delighted that folks want to expand on this feature, and I'm very
interested in helping. Other things folks have asked for are a view into
the DSM registry [0] and a simple way to allocate a DSA or dshash table
with the DSM registry, all of which seem like good improvements to me.

I didn't know about the discussions on the view for DSM registry. I'm
happy to work on that.
I think what I can also add along with the view is the user command
for detaching/destroying
the dsm segment (like pg_backend_terminate function).

That being said, we are at the very end of the v18 development cycle, and
most folks are probably busy trying to finish their ongoing projects. So
it may be a while before anyone can give this a meaningful review. Please
be sure to add a commitfest entry [1] so it doesn't get lost.

[0] /messages/by-id/4D445D3E-81C5-4135-95BB-D414204A0AB4@gmail.com
[1] https://commitfest.postgresql.org/

Thanks for the heads up. I will add an entry to the commitfest as you advised.

Best regards,

Sungwoo Chang

#4Sungwoo Chang
swchangdev@gmail.com
In reply to: Sungwoo Chang (#3)
Re: dsm_registry: Add detach and destroy features

There was a minor typo in the test module. I built and ran the
test_dsm_registry extension before submitting the patch but perhaps it
got mixed up with an older version.

Attachments:

v2-0001-add-detach-and-destroy-for-dsm-registry.patchtext/x-patch; charset=US-ASCII; name=v2-0001-add-detach-and-destroy-for-dsm-registry.patchDownload+221-2
#5Sungwoo Chang
swchangdev@gmail.com
In reply to: Sungwoo Chang (#4)
Re: dsm_registry: Add detach and destroy features

Regression test failed because I didn't add an extra new line in the
expected file.

2025년 3월 17일 (월) 오전 9:55, Sungwoo Chang <swchangdev@gmail.com>님이 작성:

Show quoted text

There was a minor typo in the test module. I built and ran the
test_dsm_registry extension before submitting the patch but perhaps it
got mixed up with an older version.

Attachments:

v3-0001-add-detach-and-destroy-for-dsm-registry.patchtext/x-patch; charset=US-ASCII; name=v3-0001-add-detach-and-destroy-for-dsm-registry.patchDownload+222-2
#6Nathan Bossart
nathandbossart@gmail.com
In reply to: Sungwoo Chang (#5)
Re: dsm_registry: Add detach and destroy features

On Tue, Mar 18, 2025 at 10:05:07AM +0900, Sungwoo Chang wrote:

+/*
+ * Attempt to destroy a named DSM segment
+ *
+ * This routine attempts to destroy the DSM segment. We unpin the dsm_segment
+ * and delete the entry from dsm_registry_table. This may not destroy the
+ * dsm_segment instantly, but it would die out once all the other processes
+ * attached to this dsm_segment either exit or manually detach from the
+ * dsm_segment.
+ *
+ * Because we deleted the key from dsm_registry_table, calling
+ * GetNamedDSMSegment with the same key would result into creating a new
+ * dsm_segment instead of retrieving the old unpinned dsm_segment.
+ */

One of the reasons I avoided adding detach/destroy functionality originally
is because this seems difficult to do correctly. How would an extension
ensure that it doesn't end up with one set of backends attached to a new
segment and another attached to an old one that is pending deletion?

--
nathan

#7Sungwoo Chang
swchangdev@gmail.com
In reply to: Sungwoo Chang (#1)
Fwd: dsm_registry: Add detach and destroy features

---------- Forwarded message ---------
보낸사람: Sungwoo Chang <swchangdev@gmail.com>
Date: 2025년 6월 13일 (금) 오전 8:03
Subject: Re: dsm_registry: Add detach and destroy features
To: Nathan Bossart <nathandbossart@gmail.com>

One of the reasons I avoided adding detach/destroy functionality originally
is because this seems difficult to do correctly. How would an extension
ensure that it doesn't end up with one set of backends attached to a new
segment and another attached to an old one that is pending deletion?

Sorry for the late response.

I used this patch for my extension in a way that you should always
detach after you are done using the shmem segment. So the situation
you described would happen in a brief moment, but once the extension
finishes its task, the shmem segment will be destroyed naturally as
all processes detach from it.

Would this not be applicable in other extensions?

#8Nathan Bossart
nathandbossart@gmail.com
In reply to: Sungwoo Chang (#7)
Re: Fwd: dsm_registry: Add detach and destroy features

On Fri, Jun 13, 2025 at 08:03:00AM +0900, Sungwoo Chang wrote:

One of the reasons I avoided adding detach/destroy functionality originally
is because this seems difficult to do correctly. How would an extension
ensure that it doesn't end up with one set of backends attached to a new
segment and another attached to an old one that is pending deletion?

I used this patch for my extension in a way that you should always detach
after you are done using the shmem segment. So the situation you described
would happen in a brief moment, but once the extension finishes its task,
the shmem segment will be destroyed naturally as all processes detach from
it.

Would this not be applicable in other extensions?

I suspect detaching/destroying segments would be applicable elsewhere, I'm
just not sure about providing something with the aforementioned behavior.

Could your use-case be handled with a DSA? On the other thread [0]/messages/by-id/aEyX-9k5vlK2lxjz@nathan, we're
talking about adding a GetNamedDSA() function, which returns a DSA that you
can use to allocate and free shared memory as needed. In theory you could
even detach the DSA if you no longer needed it in a backend, although
that's probably unnecessary.

[0]: /messages/by-id/aEyX-9k5vlK2lxjz@nathan

--
nathan

#9Sungwoo Chang
swchangdev@gmail.com
In reply to: Nathan Bossart (#8)
Re: Fwd: dsm_registry: Add detach and destroy features

2025년 6월 14일 (토) 오전 6:50, Nathan Bossart <nathandbossart@gmail.com>님이 작성:

I suspect detaching/destroying segments would be applicable elsewhere, I'm
just not sure about providing something with the aforementioned behavior.

Could your use-case be handled with a DSA? On the other thread [0], we're
talking about adding a GetNamedDSA() function, which returns a DSA that you
can use to allocate and free shared memory as needed. In theory you could
even detach the DSA if you no longer needed it in a backend, although
that's probably unnecessary.

[0] /messages/by-id/aEyX-9k5vlK2lxjz@nathan

--
nathan

My use-case requires access to the shared memory object through a named key.
Even if we migrate the code to NamedDSA, within the DSA we will need some sort
of a map between the named key and the object to access. So, I think NamedDSA
won't be the solution.

How about when we call destroy, we check if there are other processes
attached to it
and if so, we throw an exception? I checked C++ boost interprocess
library [0]https://www.boost.org/doc/libs/1_55_0/doc/html/interprocess/sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.removing, and it
looks like that's the way boost does. This way, we can avoid the aforementioned
"partitioned" scenario.

[0]: https://www.boost.org/doc/libs/1_55_0/doc/html/interprocess/sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.removing

#10Nathan Bossart
nathandbossart@gmail.com
In reply to: Sungwoo Chang (#9)
Re: Fwd: dsm_registry: Add detach and destroy features

On Mon, Jun 16, 2025 at 09:02:22AM +0900, Sungwoo Chang wrote:

2025년 6월 14일 (토) 오전 6:50, Nathan Bossart <nathandbossart@gmail.com>님이 작성:

Could your use-case be handled with a DSA? On the other thread [0], we're
talking about adding a GetNamedDSA() function, which returns a DSA that you
can use to allocate and free shared memory as needed. In theory you could
even detach the DSA if you no longer needed it in a backend, although
that's probably unnecessary.

My use-case requires access to the shared memory object through a named key.
Even if we migrate the code to NamedDSA, within the DSA we will need some sort
of a map between the named key and the object to access. So, I think NamedDSA
won't be the solution.

Right, you'd need some other shared space for the DSA pointers. In the
other thread, I'm using a dshash table (created via GetNamedDSMHash()) to
store those for test_dsm_registry [0]/messages/by-id/aEyX-9k5vlK2lxjz@nathan. There are probably other ways to do
this.

How about when we call destroy, we check if there are other processes
attached to it and if so, we throw an exception? I checked C++ boost
interprocess library [0], and it looks like that's the way boost does.
This way, we can avoid the aforementioned "partitioned" scenario.

That might work.

[0]: /messages/by-id/aEyX-9k5vlK2lxjz@nathan

--
nathan

#11Sungwoo Chang
swchangdev@gmail.com
In reply to: Nathan Bossart (#10)
Re: Fwd: dsm_registry: Add detach and destroy features

Hi,

I created a new patch that throws an exception if we try to call
destroy on a dsm segment that is still in use.
I added a function in dsm.h that returns a refcnt of a given dsm
segment. The destroy function uses that refcnt
getter to check if the dsm segment is in use.

2025년 6월 16일 (월) 오후 11:40, Nathan Bossart <nathandbossart@gmail.com>님이 작성:

Show quoted text

On Mon, Jun 16, 2025 at 09:02:22AM +0900, Sungwoo Chang wrote:

2025년 6월 14일 (토) 오전 6:50, Nathan Bossart <nathandbossart@gmail.com>님이 작성:

Could your use-case be handled with a DSA? On the other thread [0], we're
talking about adding a GetNamedDSA() function, which returns a DSA that you
can use to allocate and free shared memory as needed. In theory you could
even detach the DSA if you no longer needed it in a backend, although
that's probably unnecessary.

My use-case requires access to the shared memory object through a named key.
Even if we migrate the code to NamedDSA, within the DSA we will need some sort
of a map between the named key and the object to access. So, I think NamedDSA
won't be the solution.

Right, you'd need some other shared space for the DSA pointers. In the
other thread, I'm using a dshash table (created via GetNamedDSMHash()) to
store those for test_dsm_registry [0]. There are probably other ways to do
this.

How about when we call destroy, we check if there are other processes
attached to it and if so, we throw an exception? I checked C++ boost
interprocess library [0], and it looks like that's the way boost does.
This way, we can avoid the aforementioned "partitioned" scenario.

That might work.

[0] /messages/by-id/aEyX-9k5vlK2lxjz@nathan

--
nathan

Attachments:

t51241_11
v4-0001-add-detach-and-destroy-for-dsm_registry.patchapplication/octet-stream; name=v4-0001-add-detach-and-destroy-for-dsm_registry.patchDownload+253-2
#12Nathan Bossart
nathandbossart@gmail.com
In reply to: Sungwoo Chang (#11)
Re: Fwd: dsm_registry: Add detach and destroy features

On Wed, Jun 18, 2025 at 09:03:59AM +0900, Sungwoo Chang wrote:

I created a new patch that throws an exception if we try to call
destroy on a dsm segment that is still in use.
I added a function in dsm.h that returns a refcnt of a given dsm
segment. The destroy function uses that refcnt
getter to check if the dsm segment is in use.

Thanks for the patch. I'm still interested to hear whether the proposed
GetNamedDSA() feature could work for your use-case, as I described upthread
[0]: /messages/by-id/aFAszMKD69AyA4c1@nathan

[0]: /messages/by-id/aFAszMKD69AyA4c1@nathan

--
nathan

#13Rahila Syed
rahilasyed90@gmail.com
In reply to: Nathan Bossart (#12)
Re: Fwd: dsm_registry: Add detach and destroy features

Hi,

Thanks for the patch. I'm still interested to hear whether the proposed
GetNamedDSA() feature could work for your use-case, as I described upthread
[0].

[0] /messages/by-id/aFAszMKD69AyA4c1@nathan

Is this improvement still being worked on? I think it will be a useful
addition to the dsm_registry,
if we could delete the entries from the dsm_registry_table after they
are no longer used by any process.

Right, you'd need some other shared space for the DSA pointers. In the
other thread, I'm using a dshash table (created via GetNamedDSMHash()) to
store those for test_dsm_registry [0]. There are probably other ways to do
this.

It would be nice if we could store the dsa_pointers generated by
dsa_allocate calls
in the dsm_registry itself, this way each module using dsa won't need
to define its
own table for storing the pointers. I am not yet sure how it will fit
in the existing structure
of the registry table, though.

Please find a few high level comments on the v4 patch below.

+typedef struct DSMDetachCallbackContext
+{
+ void (*on_detach_callback) (void *);
+ void *arg;
+} DSMDetachCallbackContext;
+

I am wondering why we need to add the ability to define on_dsm_detach
callbacks in the dsm_registry.
Isn't the existing on_dsm_detach functionality sufficient?

+extern void DetachNamedDSMSegment(const char *name, size_t size,
+   void (*on_detach_callback) (void *),
+   void *arg);
+
+extern void DestroyNamedDSMSegment(const char *name, size_t size,
+    void (*on_detach_callback) (void *),
+    void *arg);

Why do we need two separate functions? Since DestroyNamedDSMSegment()
also ensures
that the segment is detached, having just one function seems sufficient.
For cases where we only need to detach the segments, can't we use
dsm_detach() directly?

Thank you,
Rahila Syed

#14Sungwoo Chang
swchangdev@gmail.com
In reply to: Rahila Syed (#13)
Re: Fwd: dsm_registry: Add detach and destroy features

Hi Rahila,

I'm sorry for the late response. This patch has been long forgotten and
also might need some effort to rebase in the current main so if we want to
revisit this idea, that's what it takes.

As for your questions, you do need both detach and destroy functions
because the first process that creates the DSM will pin it (i.e., increase
the ref count by 1) so that even if the creator detaches from it, the DSM
will stay alive. Once some process calls destroy on this DSM, then this pin
is dropped and will be destroyed once all the ref counts are dropped.

I would like to put this in the next revision, but I can't do this by my
own. I need a reviewer and the community's willingness to push this. Let me
know if we come to that situation.

Best regards,

Sungwoo

2026년 6월 18일 (목) 13:20, Rahila Syed <rahilasyed90@gmail.com>님이 작성:

Show quoted text

Hi,

Thanks for the patch. I'm still interested to hear whether the proposed
GetNamedDSA() feature could work for your use-case, as I described

upthread

[0].

[0] /messages/by-id/aFAszMKD69AyA4c1@nathan

Is this improvement still being worked on? I think it will be a useful
addition to the dsm_registry,
if we could delete the entries from the dsm_registry_table after they
are no longer used by any process.

Right, you'd need some other shared space for the DSA pointers. In the
other thread, I'm using a dshash table (created via GetNamedDSMHash()) to
store those for test_dsm_registry [0]. There are probably other ways to

do

this.

It would be nice if we could store the dsa_pointers generated by
dsa_allocate calls
in the dsm_registry itself, this way each module using dsa won't need
to define its
own table for storing the pointers. I am not yet sure how it will fit
in the existing structure
of the registry table, though.

Please find a few high level comments on the v4 patch below.

+typedef struct DSMDetachCallbackContext
+{
+ void (*on_detach_callback) (void *);
+ void *arg;
+} DSMDetachCallbackContext;
+

I am wondering why we need to add the ability to define on_dsm_detach
callbacks in the dsm_registry.
Isn't the existing on_dsm_detach functionality sufficient?

+extern void DetachNamedDSMSegment(const char *name, size_t size,
+   void (*on_detach_callback) (void *),
+   void *arg);
+
+extern void DestroyNamedDSMSegment(const char *name, size_t size,
+    void (*on_detach_callback) (void *),
+    void *arg);

Why do we need two separate functions? Since DestroyNamedDSMSegment()
also ensures
that the segment is detached, having just one function seems sufficient.
For cases where we only need to detach the segments, can't we use
dsm_detach() directly?

Thank you,
Rahila Syed