Skip to content

Commit ba230ce

Browse files
committed
Fix memory leak in pgoutput with publication list cache
The pgoutput module caches publication names in a list and frees it upon invalidation. However, the code forgot to free the actual publication names within the list elements, as publication names are pstrdup()'d in GetPublication(). This would cause memory to leak in CacheMemoryContext, bloating it over time as this context is not cleaned. This is a problem for WAL senders running for a long time, as an accumulation of invalidation requests would bloat its cache memory usage. A second case, where this leak is easier to see, involves a backend calling SQL functions like pg_logical_slot_{get,peek}_changes() which create a new decoding context with each execution. More publications create more bloat. To address this, this commit adds a new memory context within the logical decoding context and resets it each time the publication names cache is invalidated, based on a suggestion from Amit Kapila. This ensures that the lifespan of the publication names aligns with that of the logical decoding context. Contrary to the HEAD-only commit f0c569d that has changed PGOutputData to track this new child memory context, the context is tracked with a static variable whose state is reset with a MemoryContext reset callback attached to PGOutputData->context, so as ABI compatibility is preserved in stable branches. This approach is based on an suggestion from Amit Kapila. Analyzed-by: Michael Paquier, Jeff Davis Author: Masahiko Sawada Reviewed-by: Amit Kapila, Michael Paquier, Euler Taveira, Hou Zhijie Discussion: https://postgr.es/m/Z0khf9EVMVLOc_YY@paquier.xyz Backpatch-through: 13
1 parent 9c1afd3 commit ba230ce

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

src/backend/replication/pgoutput/pgoutput.c

+36-5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ static bool pgoutput_origin_filter(LogicalDecodingContext *ctx,
4949

5050
static bool publications_valid;
5151

52+
/*
53+
* Private memory context for publication data, created in
54+
* PGOutputData->context when starting pgoutput, and set to NULL when its
55+
* parent context is reset via a dedicated MemoryContextCallback.
56+
*/
57+
static MemoryContext pubctx = NULL;
58+
5259
static List *LoadPublications(List *pubnames);
5360
static void publication_invalidation_cb(Datum arg, int cacheid,
5461
uint32 hashvalue);
@@ -174,6 +181,15 @@ parse_output_parameters(List *options, uint32 *protocol_version,
174181
}
175182
}
176183

184+
/*
185+
* Callback of PGOutputData->context in charge of cleaning pubctx.
186+
*/
187+
static void
188+
pgoutput_pubctx_reset_callback(void *arg)
189+
{
190+
pubctx = NULL;
191+
}
192+
177193
/*
178194
* Initialize this plugin
179195
*/
@@ -183,12 +199,22 @@ pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
183199
{
184200
PGOutputData *data = palloc0(sizeof(PGOutputData));
185201
static bool publication_callback_registered = false;
202+
MemoryContextCallback *mcallback;
186203

187204
/* Create our memory context for private allocations. */
188205
data->context = AllocSetContextCreate(ctx->context,
189206
"logical replication output context",
190207
ALLOCSET_DEFAULT_SIZES);
191208

209+
Assert(pubctx == NULL);
210+
pubctx = AllocSetContextCreate(ctx->context,
211+
"logical replication publication list context",
212+
ALLOCSET_SMALL_SIZES);
213+
214+
mcallback = palloc0(sizeof(MemoryContextCallback));
215+
mcallback->func = pgoutput_pubctx_reset_callback;
216+
MemoryContextRegisterResetCallback(ctx->context, mcallback);
217+
192218
ctx->output_plugin_private = data;
193219

194220
/* This plugin uses binary protocol. */
@@ -587,8 +613,9 @@ pgoutput_origin_filter(LogicalDecodingContext *ctx,
587613
/*
588614
* Shutdown the output plugin.
589615
*
590-
* Note, we don't need to clean the data->context as it's child context
591-
* of the ctx->context so it will be cleaned up by logical decoding machinery.
616+
* Note, we don't need to clean the data->context and pubctx as they are
617+
* child contexts of the ctx->context so they will be cleaned up by logical
618+
* decoding machinery.
592619
*/
593620
static void
594621
pgoutput_shutdown(LogicalDecodingContext *ctx)
@@ -598,6 +625,9 @@ pgoutput_shutdown(LogicalDecodingContext *ctx)
598625
hash_destroy(RelationSyncCache);
599626
RelationSyncCache = NULL;
600627
}
628+
629+
/* Better safe than sorry */
630+
pubctx = NULL;
601631
}
602632

603633
/*
@@ -731,9 +761,10 @@ get_rel_sync_entry(PGOutputData *data, Oid relid)
731761
/* Reload publications if needed before use. */
732762
if (!publications_valid)
733763
{
734-
oldctx = MemoryContextSwitchTo(CacheMemoryContext);
735-
if (data->publications)
736-
list_free_deep(data->publications);
764+
Assert(pubctx);
765+
766+
MemoryContextReset(pubctx);
767+
oldctx = MemoryContextSwitchTo(pubctx);
737768

738769
data->publications = LoadPublications(data->publication_names);
739770
MemoryContextSwitchTo(oldctx);

0 commit comments

Comments
 (0)