public inbox for [email protected]  
help / color / mirror / Atom feed
From: Rui Zhao <[email protected]>
To: pgsql-hackers <[email protected]>
Cc: hlinnaka <[email protected]>
Cc: robertmhaas <[email protected]>
Cc: tgl <[email protected]>
Subject: Detach shared memory in Postmaster child if not needed
Date: Mon, 29 Jul 2024 17:57:06 +0800
Message-ID: <[email protected]> (raw)

Prior to PG16, postmaster children would manually detach from shared memory
if it was not needed. However, this behavior was removed in fork mode in
commit aafc05d.
Detaching shared memory when it is no longer needed is beneficial, as 
postmaster children (like syslogger) don't wish to take any risk of
accidentally corrupting shared memory. Additionally, any panic in these
processes will not reset shared memory.
The attached patch addresses this issue by detaching shared memory after
fork_process().
Best regard,
Rui Zhao


Attachments:

  [application/octet-stream] 0001-Detach-shared-memory-in-Postmaster-child-if-not-needed.patch (997B, ../[email protected]/3-0001-Detach-shared-memory-in-Postmaster-child-if-not-needed.patch)
  download | inline diff:
From 1b1c07f6681bf261f1c19ad8e7ad84f008a1adea Mon Sep 17 00:00:00 2001
From: "Rui Zhao" <[email protected]>
Date: Mon, 29 Jul 2024 16:55:04 +0800
Subject: [PATCH] Detach shared memory in Postmaster child if not needed

---
 src/backend/postmaster/launch_backend.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/src/backend/postmaster/launch_backend.c b/src/backend/postmaster/launch_backend.c
index fafe5feecc..9b018ef500 100644
--- a/src/backend/postmaster/launch_backend.c
+++ b/src/backend/postmaster/launch_backend.c
@@ -249,6 +249,13 @@ postmaster_child_launch(BackendType child_type,
 		/* Detangle from postmaster */
 		InitPostmasterChild();
 
+		/* Detach shared memory if not needed. */
+		if (!child_process_kinds[child_type].shmem_attach)
+		{
+			dsm_detach_all();
+			PGSharedMemoryDetach();
+		}
+
 		/*
 		 * Enter the Main function with TopMemoryContext.  The startup data is
 		 * allocated in PostmasterContext, so we cannot release it here yet.
-- 
2.39.3



view thread (2+ messages)

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected], [email protected]
  Subject: Re: Detach shared memory in Postmaster child if not needed
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox