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

Commitf0c569d

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.This solution changes PGOutputData, which is fine for HEAD but it couldcause an ABI breakage in stable branches as the structure size wouldchange, so these are left out for now.Analyzed-by: Michael Paquier, Jeff DavisAuthor: Zhijie HouReviewed-by: Michael Paquier, Masahiko Sawada, Euler TaveiraDiscussion:https://postgr.es/m/Z0khf9EVMVLOc_YY@paquier.xyz
1 parent001a537 commitf0c569d

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,10 @@ pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
436436
"logical replication cache context",
437437
ALLOCSET_DEFAULT_SIZES);
438438

439+
data->pubctx=AllocSetContextCreate(ctx->context,
440+
"logical replication publication list context",
441+
ALLOCSET_SMALL_SIZES);
442+
439443
ctx->output_plugin_private=data;
440444

441445
/* This plugin uses binary protocol. */
@@ -1734,9 +1738,9 @@ pgoutput_origin_filter(LogicalDecodingContext *ctx,
17341738
/*
17351739
* Shutdown the output plugin.
17361740
*
1737-
* Note, we don't need to clean the data->context anddata->cachectx as
1738-
* they are child contexts of the ctx->context so they will be cleaned up by
1739-
* logical decoding machinery.
1741+
* Note, we don't need to clean the data->context,data->cachectx, and
1742+
*data->pubctx asthey are child contexts of the ctx->context so they
1743+
*will be cleaned up bylogical decoding machinery.
17401744
*/
17411745
staticvoid
17421746
pgoutput_shutdown(LogicalDecodingContext*ctx)
@@ -2063,12 +2067,9 @@ get_rel_sync_entry(PGOutputData *data, Relation relation)
20632067
/* Reload publications if needed before use. */
20642068
if (!publications_valid)
20652069
{
2066-
oldctx=MemoryContextSwitchTo(CacheMemoryContext);
2067-
if (data->publications)
2068-
{
2069-
list_free_deep(data->publications);
2070-
data->publications=NIL;
2071-
}
2070+
MemoryContextReset(data->pubctx);
2071+
2072+
oldctx=MemoryContextSwitchTo(data->pubctx);
20722073
data->publications=LoadPublications(data->publication_names);
20732074
MemoryContextSwitchTo(oldctx);
20742075
publications_valid= true;

‎src/include/replication/pgoutput.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ typedef struct PGOutputData
2020
MemoryContextcontext;/* private memory context for transient
2121
* allocations */
2222
MemoryContextcachectx;/* private memory context for cache data */
23+
MemoryContextpubctx;/* private memory context for publication data */
2324

2425
boolin_streaming;/* true if we are streaming a chunk of
2526
* transaction */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp