Skip to content

Commit 79a4d24

Browse files
committed
Make it easy to detach completely from shared memory.
The new function dsm_detach_all() can be used either by postmaster children that don't wish to take any risk of accidentally corrupting shared memory; or by forked children of regular backends with the same need. This patch also updates the postmaster children that already do PGSharedMemoryDetach() to do dsm_detach_all() as well. Per discussion with Tom Lane.
1 parent 551fb5a commit 79a4d24

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

src/backend/postmaster/pgarch.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "postmaster/fork_process.h"
4141
#include "postmaster/pgarch.h"
4242
#include "postmaster/postmaster.h"
43+
#include "storage/dsm.h"
4344
#include "storage/fd.h"
4445
#include "storage/ipc.h"
4546
#include "storage/latch.h"
@@ -163,6 +164,7 @@ pgarch_start(void)
163164
on_exit_reset();
164165

165166
/* Drop our connection to postmaster's shared memory, as well */
167+
dsm_detach_all();
166168
PGSharedMemoryDetach();
167169

168170
PgArchiverMain(0, NULL);

src/backend/postmaster/pgstat.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "postmaster/postmaster.h"
5151
#include "storage/proc.h"
5252
#include "storage/backendid.h"
53+
#include "storage/dsm.h"
5354
#include "storage/fd.h"
5455
#include "storage/ipc.h"
5556
#include "storage/latch.h"
@@ -709,6 +710,7 @@ pgstat_start(void)
709710
on_exit_reset();
710711

711712
/* Drop our connection to postmaster's shared memory, as well */
713+
dsm_detach_all();
712714
PGSharedMemoryDetach();
713715

714716
PgstatCollectorMain(0, NULL);

src/backend/postmaster/syslogger.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "postmaster/fork_process.h"
4040
#include "postmaster/postmaster.h"
4141
#include "postmaster/syslogger.h"
42+
#include "storage/dsm.h"
4243
#include "storage/ipc.h"
4344
#include "storage/latch.h"
4445
#include "storage/pg_shmem.h"
@@ -626,6 +627,7 @@ SysLogger_Start(void)
626627
on_exit_reset();
627628

628629
/* Drop our connection to postmaster's shared memory, as well */
630+
dsm_detach_all();
629631
PGSharedMemoryDetach();
630632

631633
/* do the work */

src/backend/storage/ipc/dsm.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,8 @@ dsm_attach(dsm_handle h)
722722

723723
/*
724724
* At backend shutdown time, detach any segments that are still attached.
725+
* (This is similar to dsm_detach_all, except that there's no reason to
726+
* unmap the control segment before exiting, so we don't bother.)
725727
*/
726728
void
727729
dsm_backend_shutdown(void)
@@ -735,6 +737,31 @@ dsm_backend_shutdown(void)
735737
}
736738
}
737739

740+
/*
741+
* Detach all shared memory segments, including the control segments. This
742+
* should be called, along with PGSharedMemoryDetach, in processes that
743+
* might inherit mappings but are not intended to be connected to dynamic
744+
* shared memory.
745+
*/
746+
void
747+
dsm_detach_all(void)
748+
{
749+
void *control_address = dsm_control;
750+
751+
while (!dlist_is_empty(&dsm_segment_list))
752+
{
753+
dsm_segment *seg;
754+
755+
seg = dlist_head_element(dsm_segment, node, &dsm_segment_list);
756+
dsm_detach(seg);
757+
}
758+
759+
if (control_address != NULL)
760+
dsm_impl_op(DSM_OP_DETACH, dsm_control_handle, 0,
761+
&dsm_control_impl_private, &control_address,
762+
&dsm_control_mapped_size, ERROR);
763+
}
764+
738765
/*
739766
* Resize an existing shared memory segment.
740767
*

src/include/storage/dsm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ typedef struct dsm_segment dsm_segment;
2020
/* Startup and shutdown functions. */
2121
extern void dsm_postmaster_startup(void);
2222
extern void dsm_backend_shutdown(void);
23+
extern void dsm_detach_all(void);
2324

2425
/* Functions that create, update, or remove mappings. */
2526
extern dsm_segment *dsm_create(Size size);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy