Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitba230ce

Browse files
committed
Fix memory leak in pgoutput with publication list cache
The pgoutput module caches publication names in a list and frees it uponinvalidation. However, the code forgot to free the actual publicationnames within the list elements, as publication names are pstrdup()'d inGetPublication(). This would cause memory to leak inCacheMemoryContext, bloating it over time as this context is notcleaned.This is a problem for WAL senders running for a long time, as anaccumulation of invalidation requests would bloat its cache memoryusage. A second case, where this leak is easier to see, involves abackend calling SQL functions like pg_logical_slot_{get,peek}_changes()which create a new decoding context with each execution. Morepublications create more bloat.To address this, this commit adds a new memory context within thelogical decoding context and resets it each time the publication namescache is invalidated, based on a suggestion from Amit Kapila. Thisensures that the lifespan of the publication names aligns with that ofthe logical decoding context.Contrary to the HEAD-only commitf0c569d that has changedPGOutputData to track this new child memory context, the context istracked with a static variable whose state is reset with a MemoryContextreset callback attached to PGOutputData->context, so as ABIcompatibility is preserved in stable branches. This approach is basedon an suggestion from Amit Kapila.Analyzed-by: Michael Paquier, Jeff DavisAuthor: Masahiko SawadaReviewed-by: Amit Kapila, Michael Paquier, Euler Taveira, Hou ZhijieDiscussion:https://postgr.es/m/Z0khf9EVMVLOc_YY@paquier.xyzBackpatch-through: 13
1 parent9c1afd3 commitba230ce

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

‎src/backend/replication/pgoutput/pgoutput.c

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ static bool pgoutput_origin_filter(LogicalDecodingContext *ctx,
4949

5050
staticboolpublications_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+
staticMemoryContextpubctx=NULL;
58+
5259
staticList*LoadPublications(List*pubnames);
5360
staticvoidpublication_invalidation_cb(Datumarg,intcacheid,
5461
uint32hashvalue);
@@ -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+
staticvoid
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
staticboolpublication_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
staticvoid
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)

[8]ページ先頭

©2009-2025 Movatter.jp