On Sat, Nov 30, 2024 at 09:28:31AM +0100, Alvaro Herrera wrote:
> I'm not sure about your proposed fix.  Isn't it simpler to have another
> memory context which we can reset instead of doing list_free_deep()?  It
> doesn't have to be a global memory context -- since this is not
> reentrant and not referenced anywhere else, it can be a simple static
> variable in that block, as in the attached.  I ran the stock tests (no
> sysbench) and at least it doesn't crash.
> 
> This should be easily backpatchable also, since there's no ABI change.
Yeah.  Using more memory contexts around these API calls done without
the cache manipulation is something I've also mentioned to Amit a few
days ago, and I'm feeling that this concept should be applied to a
broader area than just the cached publication list in light of this
thread and ea792bfd93ab.  That's a discussion for later, likely.  I'm
not sure how broad this should be and if this should be redesign to
make the whole context tracking easier.  What I am sure of at this
stage is that for this workload we don't have any garbage left behind.

> +                     static MemoryContext pubmemcxt = NULL;
> +
> +                     if (pubmemcxt == NULL)
> +                             pubmemcxt = 
> AllocSetContextCreate(CacheMemoryContext,
> +                                                                             
>                   "publication list context",
> +                                                                             
>                   ALLOCSET_DEFAULT_SIZES);
> +                     else
> +                             MemoryContextReset(pubmemcxt);
> +                     oldctx = MemoryContextSwitchTo(pubmemcxt);
> +
>                       data->publications = 
> LoadPublications(data->publication_names);
>                       MemoryContextSwitchTo(oldctx);
>                       publications_valid = true;

Something like that works for me in stable branches, removing the need
for the list free as there is only one caller of LoadPublications()
currently.  I've checked with my previous script, with the aggressive
invalidation tweak, in the case I'm missing something, and the memory
numbers are stable.

I am slightly concerned about the current design of GetPublication()
in the long-term, TBH.  LoadPublications() has hidden the leak behind
two layers of routines in the WAL sender, and that's easy to miss once
you call anything that loads a Publication depending on how the caller
caches its data.  So I would still choose for modifying the structure
on HEAD removing the pstrdup() for the publication name.  Anyway, your 
suggestion is also OK by me on top of that, that's less conflicts in
all the branches.

May I suggest the attached then?  0001 is your backpatch, 0002 would
be my HEAD-only suggestion, if that's OK for you and others of course.
--
Michael
From 22795bd2c7a8c7934d4ac77cfe1cd2ef36ae9fd3 Mon Sep 17 00:00:00 2001
From: Michael Paquier <mich...@paquier.xyz>
Date: Mon, 2 Dec 2024 09:50:01 +0900
Subject: [PATCH v2 1/2] Fix memory leak in pgoutput with publication list
 cache

Blah.

Analyzed-by: Jeff Davis, Michael Paquier
Author: Alvaro Herrera
Discussion: https://postgr.es/m/z0khf9evmvloc...@paquier.xyz
Backpatch-through: 13
---
 src/backend/replication/pgoutput/pgoutput.c | 22 ++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/src/backend/replication/pgoutput/pgoutput.c b/src/backend/replication/pgoutput/pgoutput.c
index 5e23453f07..41d204ac3b 100644
--- a/src/backend/replication/pgoutput/pgoutput.c
+++ b/src/backend/replication/pgoutput/pgoutput.c
@@ -2060,15 +2060,23 @@ get_rel_sync_entry(PGOutputData *data, Relation relation)
 		char		relkind = get_rel_relkind(relid);
 		List	   *rel_publications = NIL;
 
-		/* Reload publications if needed before use. */
+		/*
+		 * Reload publications if needed before use.  A dedicated memory
+		 * context is used to hold the publication information, so as any
+		 * memory allocated within LoadPublications() is freed.
+		 */
 		if (!publications_valid)
 		{
-			oldctx = MemoryContextSwitchTo(CacheMemoryContext);
-			if (data->publications)
-			{
-				list_free_deep(data->publications);
-				data->publications = NIL;
-			}
+			static MemoryContext pubmemcxt = NULL;
+
+			if (pubmemcxt == NULL)
+				pubmemcxt = AllocSetContextCreate(CacheMemoryContext,
+												  "publication list context",
+												  ALLOCSET_DEFAULT_SIZES);
+			else
+				MemoryContextReset(pubmemcxt);
+			oldctx = MemoryContextSwitchTo(pubmemcxt);
+
 			data->publications = LoadPublications(data->publication_names);
 			MemoryContextSwitchTo(oldctx);
 			publications_valid = true;
-- 
2.45.2

From 22cb03ab8e0fe024f8c3bb36b1581a32eff04ebc Mon Sep 17 00:00:00 2001
From: Michael Paquier <mich...@paquier.xyz>
Date: Mon, 2 Dec 2024 10:36:52 +0900
Subject: [PATCH v2 2/2] Avoid extra pstrdup() in Publication

The structure is changed so as the publication name is included in a
single memory allocation, knowing that the publication name is limited
by NAMEDATALEN.

No backpatch is required, as this is an ABI breakage.
---
 src/include/catalog/pg_publication.h | 2 +-
 src/backend/catalog/pg_publication.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/include/catalog/pg_publication.h b/src/include/catalog/pg_publication.h
index 9a83a72d6b..809bd1f0a6 100644
--- a/src/include/catalog/pg_publication.h
+++ b/src/include/catalog/pg_publication.h
@@ -103,7 +103,7 @@ typedef struct PublicationDesc
 typedef struct Publication
 {
 	Oid			oid;
-	char	   *name;
+	char		name[NAMEDATALEN];
 	bool		alltables;
 	bool		pubviaroot;
 	bool		pubgencols;
diff --git a/src/backend/catalog/pg_publication.c b/src/backend/catalog/pg_publication.c
index 9bbb60463f..8f4e3b83f1 100644
--- a/src/backend/catalog/pg_publication.c
+++ b/src/backend/catalog/pg_publication.c
@@ -1061,7 +1061,7 @@ GetPublication(Oid pubid)
 
 	pub = (Publication *) palloc(sizeof(Publication));
 	pub->oid = pubid;
-	pub->name = pstrdup(NameStr(pubform->pubname));
+	strlcpy(pub->name, NameStr(pubform->pubname), NAMEDATALEN);
 	pub->alltables = pubform->puballtables;
 	pub->pubactions.pubinsert = pubform->pubinsert;
 	pub->pubactions.pubupdate = pubform->pubupdate;
-- 
2.45.2

Attachment: signature.asc
Description: PGP signature

Reply via email to