diff --git a/src/backend/postmaster/pgarch.c b/src/backend/postmaster/pgarch.c index cf2ce46516b5c830a5cf7fcacb56d3317cdd484d..815316055a7124417c89d1537029048e355dd373 100644 --- a/src/backend/postmaster/pgarch.c +++ b/src/backend/postmaster/pgarch.c @@ -40,6 +40,7 @@ #include "postmaster/fork_process.h" #include "postmaster/pgarch.h" #include "postmaster/postmaster.h" +#include "storage/dsm.h" #include "storage/fd.h" #include "storage/ipc.h" #include "storage/latch.h" @@ -163,6 +164,7 @@ pgarch_start(void) on_exit_reset(); /* Drop our connection to postmaster's shared memory, as well */ + dsm_detach_all(); PGSharedMemoryDetach(); PgArchiverMain(0, NULL); diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c index 1ca5d130444c03676c764a248678d3b4c265bc87..3dc280a48d70c58f29e72840b90e9637a455c65a 100644 --- a/src/backend/postmaster/pgstat.c +++ b/src/backend/postmaster/pgstat.c @@ -50,6 +50,7 @@ #include "postmaster/postmaster.h" #include "storage/proc.h" #include "storage/backendid.h" +#include "storage/dsm.h" #include "storage/fd.h" #include "storage/ipc.h" #include "storage/latch.h" @@ -709,6 +710,7 @@ pgstat_start(void) on_exit_reset(); /* Drop our connection to postmaster's shared memory, as well */ + dsm_detach_all(); PGSharedMemoryDetach(); PgstatCollectorMain(0, NULL); diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c index e277a9a8a66e2a5b6024102e08e2841ca9d39991..4731ab73fed16d10aaf3afd8c2c7a7d98e597b35 100644 --- a/src/backend/postmaster/syslogger.c +++ b/src/backend/postmaster/syslogger.c @@ -39,6 +39,7 @@ #include "postmaster/fork_process.h" #include "postmaster/postmaster.h" #include "postmaster/syslogger.h" +#include "storage/dsm.h" #include "storage/ipc.h" #include "storage/latch.h" #include "storage/pg_shmem.h" @@ -626,6 +627,7 @@ SysLogger_Start(void) on_exit_reset(); /* Drop our connection to postmaster's shared memory, as well */ + dsm_detach_all(); PGSharedMemoryDetach(); /* do the work */ diff --git a/src/backend/storage/ipc/dsm.c b/src/backend/storage/ipc/dsm.c index 232c099c18acc88697f36664d268d8844d3659f8..c967177c4b17c4eefdfce60bf24e79cfa8da725c 100644 --- a/src/backend/storage/ipc/dsm.c +++ b/src/backend/storage/ipc/dsm.c @@ -722,6 +722,8 @@ dsm_attach(dsm_handle h) /* * At backend shutdown time, detach any segments that are still attached. + * (This is similar to dsm_detach_all, except that there's no reason to + * unmap the control segment before exiting, so we don't bother.) */ void dsm_backend_shutdown(void) @@ -735,6 +737,31 @@ dsm_backend_shutdown(void) } } +/* + * Detach all shared memory segments, including the control segments. This + * should be called, along with PGSharedMemoryDetach, in processes that + * might inherit mappings but are not intended to be connected to dynamic + * shared memory. + */ +void +dsm_detach_all(void) +{ + void *control_address = dsm_control; + + while (!dlist_is_empty(&dsm_segment_list)) + { + dsm_segment *seg; + + seg = dlist_head_element(dsm_segment, node, &dsm_segment_list); + dsm_detach(seg); + } + + if (control_address != NULL) + dsm_impl_op(DSM_OP_DETACH, dsm_control_handle, 0, + &dsm_control_impl_private, &control_address, + &dsm_control_mapped_size, ERROR); +} + /* * Resize an existing shared memory segment. * diff --git a/src/include/storage/dsm.h b/src/include/storage/dsm.h index 03dd7679ffde8d4538a328408c38f470feecd9b2..e4669a1e3c1038cc9ffad57d4721185ef0c4e04c 100644 --- a/src/include/storage/dsm.h +++ b/src/include/storage/dsm.h @@ -20,6 +20,7 @@ typedef struct dsm_segment dsm_segment; /* Startup and shutdown functions. */ extern void dsm_postmaster_startup(void); extern void dsm_backend_shutdown(void); +extern void dsm_detach_all(void); /* Functions that create, update, or remove mappings. */ extern dsm_segment *dsm_create(Size size);