Skip to content

Commit cfb6b72

Browse files
RekGRpthalvherre
authored andcommitted
Allow DSM segments to be created as pinned
dsm_create and dsm_attach assumed that a current resource owner was always in place. Exploration with the API show that this is inconvenient: sometimes one must create a dummy resowner, create/attach the DSM, only to pin the mapping later, which is wasteful. Change create/attach so that if there is no current resowner, the dsm is effectively pinned right from the start. Discussion: https://postgr.es/m/[email protected] Reviewed by Thomas Munro. This is backport of commit 767bc02 Co-authored-by: Alvaro Herrera <[email protected]>
1 parent ac7e76a commit cfb6b72

File tree

1 file changed

+16
-2
lines changed
  • src/backend/storage/ipc

1 file changed

+16
-2
lines changed

src/backend/storage/ipc/dsm.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,13 @@ dsm_set_control_handle(dsm_handle h)
453453

454454
/*
455455
* Create a new dynamic shared memory segment.
456+
*
457+
* If there is a non-NULL CurrentResourceOwner, the new segment is associated
458+
* with it and must be detached before the resource owner releases, or a
459+
* warning will be logged. If CurrentResourceOwner is NULL, the segment
460+
* remains attached until explicitely detached or the session ends.
461+
* Creating with a NULL CurrentResourceOwner is equivalent to creating
462+
* with a non-NULL CurrentResourceOwner and then calling dsm_pin_mapping.
456463
*/
457464
dsm_segment *
458465
dsm_create(Size size)
@@ -535,6 +542,11 @@ dsm_create(Size size)
535542
* This can happen if we're asked to attach the segment, but then everyone
536543
* else detaches it (causing it to be destroyed) before we get around to
537544
* attaching it.
545+
*
546+
* If there is a non-NULL CurrentResourceOwner, the attached segment is
547+
* associated with it and must be detached before the resource owner releases,
548+
* or a warning will be logged. Otherwise the segment remains attached until
549+
* explicitely detached or the session ends. See the note atop dsm_create().
538550
*/
539551
dsm_segment *
540552
dsm_attach(dsm_handle h)
@@ -1055,7 +1067,8 @@ dsm_create_descriptor(void)
10551067
{
10561068
dsm_segment *seg;
10571069

1058-
ResourceOwnerEnlargeDSMs(CurrentResourceOwner);
1070+
if (CurrentResourceOwner)
1071+
ResourceOwnerEnlargeDSMs(CurrentResourceOwner);
10591072

10601073
seg = MemoryContextAlloc(TopMemoryContext, sizeof(dsm_segment));
10611074
dlist_push_head(&dsm_segment_list, &seg->node);
@@ -1067,7 +1080,8 @@ dsm_create_descriptor(void)
10671080
seg->mapped_size = 0;
10681081

10691082
seg->resowner = CurrentResourceOwner;
1070-
ResourceOwnerRememberDSM(CurrentResourceOwner, seg);
1083+
if (CurrentResourceOwner)
1084+
ResourceOwnerRememberDSM(CurrentResourceOwner, seg);
10711085

10721086
slist_init(&seg->on_detach);
10731087

0 commit comments

Comments
 (0)