From 8ad0a2213d1794ad39c438c3fb947cd7d9f26a0d Mon Sep 17 00:00:00 2001 From: Shi Yu Date: Tue, 21 Feb 2023 10:55:46 +0800 Subject: [PATCH v3] Avoid duplicate registration of callbacks in pgoutput --- src/backend/replication/pgoutput/pgoutput.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/backend/replication/pgoutput/pgoutput.c b/src/backend/replication/pgoutput/pgoutput.c index 98377c094b..db6db44521 100644 --- a/src/backend/replication/pgoutput/pgoutput.c +++ b/src/backend/replication/pgoutput/pgoutput.c @@ -409,6 +409,7 @@ pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init) { PGOutputData *data = palloc0(sizeof(PGOutputData)); + static bool publication_callback_registered = false; /* Create our memory context for private allocations. */ data->context = AllocSetContextCreate(ctx->context, @@ -504,9 +505,14 @@ pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt, /* Init publication state. */ data->publications = NIL; publications_valid = false; - CacheRegisterSyscacheCallback(PUBLICATIONOID, - publication_invalidation_cb, - (Datum) 0); + /* Register callback for pg_publication if we didn't do that. */ + if (!publication_callback_registered) + { + CacheRegisterSyscacheCallback(PUBLICATIONOID, + publication_invalidation_cb, + (Datum) 0); + publication_callback_registered = true; + } /* Initialize relation schema cache. */ init_rel_sync_cache(CacheMemoryContext); @@ -1931,6 +1937,7 @@ static void init_rel_sync_cache(MemoryContext cachectx) { HASHCTL ctl; + static bool relation_callback_registered = false; if (RelationSyncCache != NULL) return; @@ -1946,6 +1953,10 @@ init_rel_sync_cache(MemoryContext cachectx) Assert(RelationSyncCache != NULL); + /* Fast return if we have registered callbacks. */ + if (relation_callback_registered) + return; + /* We must update the cache entry for a relation after a relcache flush */ CacheRegisterRelcacheCallback(rel_sync_cache_relation_cb, (Datum) 0); @@ -1968,6 +1979,8 @@ init_rel_sync_cache(MemoryContext cachectx) CacheRegisterSyscacheCallback(PUBLICATIONNAMESPACEMAP, rel_sync_cache_publication_cb, (Datum) 0); + + relation_callback_registered = true; } /* -- 2.30.0.windows.2