public inbox for [email protected]  
help / color / mirror / Atom feed
From: Fujii Masao <[email protected]>
To: Tom Lane <[email protected]>
Cc: Andres Freund <[email protected]>
Cc: PostgreSQL Hackers <[email protected]>
Subject: Re: Release postmaster working memory context in slotsync worker
Date: Wed, 18 Mar 2026 15:31:23 +0900
Message-ID: <CAHGQGwFu0tam9i2UxRHY+mrB-0YZVfWZPheKYy5V2KfB-WxuGQ@mail.gmail.com> (raw)
In-Reply-To: <[email protected]>
References: <CAHGQGwHO05JaUpgKF8FBDmPdBUJsK22axRRcgmAUc2Jyi8OK8g@mail.gmail.com>
	<ukpuo66v3k5z5cpwfbofa3zwdgdb53thbxemgqvtsnf7gdf5ew@ixgj5hs4bwu6>
	<[email protected]>

On Sat, Feb 28, 2026 at 2:58 AM Tom Lane <[email protected]> wrote:
>
> Andres Freund <[email protected]> writes:
> > On 2026-02-28 01:25:12 +0900, Fujii Masao wrote:
> >> Child processes do not need the postmaster's working memory context and
> >> release it at the start of their main function. However, the slotsync worker
> >> appears to have missed this step.
>
> > Obviously this inconsistency is not good.  However:
> > I think we should consider *not* releasing postmaster memory. Freeing the
> > memory actually can lead to an *increase* in memory usage and a slight
> > *decrease* in connection startup performance. The reason for that is that with
> > fork, memory allocated in postmaster is handled by copy-on-write in the
> > children.
>
> Meh.  I think that's optimizing for the wrong thing.  To my mind the
> point of releasing that context is to be sure that child processes
> don't have access to postmaster-private data.

Okay, I've included this point in the commit message of the patch.


> Admittedly, we're not
> doing anything as drastic as zeroing out the memory, but it'll soon
> be overwritten as the child starts up and populates its caches.

Yes.

Attached is a rebased version of the patch. I'm thinking to commit it.

Regards,

-- 
Fujii Masao


Attachments:

  [application/octet-stream] v2-0001-Release-postmaster-working-memory-context-in-slot.patch (1.3K, 2-v2-0001-Release-postmaster-working-memory-context-in-slot.patch)
  download | inline diff:
From ca15c3802be2abac58d288a4d1c91943a867a46d Mon Sep 17 00:00:00 2001
From: Fujii Masao <[email protected]>
Date: Wed, 18 Mar 2026 15:26:00 +0900
Subject: [PATCH v2] Release postmaster working memory context in slotsync
 worker

Child processes do not need the postmaster's working memory context and
normally release it at the start of their main entry point. However,
the slotsync worker forgot to do so.

This commit makes the slotsync worker release the postmaster's working
memory context at startup, ensuring it does not have access to
postmaster-private data.
---
 src/backend/replication/logical/slotsync.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/src/backend/replication/logical/slotsync.c b/src/backend/replication/logical/slotsync.c
index e75db69e3f6..d103223c21f 100644
--- a/src/backend/replication/logical/slotsync.c
+++ b/src/backend/replication/logical/slotsync.c
@@ -1485,6 +1485,13 @@ ReplSlotSyncWorkerMain(const void *startup_data, size_t startup_data_len)
 
 	Assert(startup_data_len == 0);
 
+	/* Release postmaster's working memory context */
+	if (PostmasterContext)
+	{
+		MemoryContextDelete(PostmasterContext);
+		PostmasterContext = NULL;
+	}
+
 	init_ps_display(NULL);
 
 	Assert(GetProcessingMode() == InitProcessing);
-- 
2.51.2



view thread (3+ messages)  latest in thread

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: Release postmaster working memory context in slotsync worker
  In-Reply-To: <CAHGQGwFu0tam9i2UxRHY+mrB-0YZVfWZPheKYy5V2KfB-WxuGQ@mail.gmail.com>

* 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