public inbox for [email protected]  
help / color / mirror / Atom feed
Re: Release postmaster working memory context in slotsync worker
3+ messages / 2 participants
[nested] [flat]

* Re: Release postmaster working memory context in slotsync worker
@ 2026-02-27 17:58 Tom Lane <[email protected]>
  2026-03-18 06:31 ` Re: Release postmaster working memory context in slotsync worker Fujii Masao <[email protected]>
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2026-02-27 17:58 UTC (permalink / raw)
  To: Andres Freund <[email protected]>; +Cc: Fujii Masao <[email protected]>; PostgreSQL Hackers <[email protected]>

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.  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.

			regards, tom lane






^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Release postmaster working memory context in slotsync worker
  2026-02-27 17:58 Re: Release postmaster working memory context in slotsync worker Tom Lane <[email protected]>
@ 2026-03-18 06:31 ` Fujii Masao <[email protected]>
  2026-04-06 14:09   ` Re: Release postmaster working memory context in slotsync worker Fujii Masao <[email protected]>
  0 siblings, 1 reply; 3+ messages in thread

From: Fujii Masao @ 2026-03-18 06:31 UTC (permalink / raw)
  To: Tom Lane <[email protected]>; +Cc: Andres Freund <[email protected]>; PostgreSQL Hackers <[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



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Release postmaster working memory context in slotsync worker
  2026-02-27 17:58 Re: Release postmaster working memory context in slotsync worker Tom Lane <[email protected]>
  2026-03-18 06:31 ` Re: Release postmaster working memory context in slotsync worker Fujii Masao <[email protected]>
@ 2026-04-06 14:09   ` Fujii Masao <[email protected]>
  0 siblings, 0 replies; 3+ messages in thread

From: Fujii Masao @ 2026-04-06 14:09 UTC (permalink / raw)
  To: Chao Li <[email protected]>; +Cc: getiancheng <[email protected]>; Tom Lane <[email protected]>; Andres Freund <[email protected]>; PostgreSQL Hackers <[email protected]>

On Thu, Mar 19, 2026 at 5:16 PM Chao Li <[email protected]> wrote:
> Still, the current phrasing seems a bit too strong to me. Strictly speaking, the memory is already inherited at fork time, so this is not making access impossible in an absolute sense. What this patch really does is remove the inherited PostmasterContext, so the child no longer retains that postmaster-private data through that context.
>
> Maybe we could phrase in some way like: "so that it no longer retains access to postmaster-private data through PostmasterContext”.

Thanks for the review! I've pushed the patch.
Per your and Tiancheng's suggestions, I just used "prevent unintended use"
in the commit message.

Regards,

-- 
Fujii Masao





^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2026-04-06 14:09 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-02-27 17:58 Re: Release postmaster working memory context in slotsync worker Tom Lane <[email protected]>
2026-03-18 06:31 ` Fujii Masao <[email protected]>
2026-04-06 14:09   ` Fujii Masao <[email protected]>

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