diff --git a/src/backend/replication/pgoutput/pgoutput.c b/src/backend/replication/pgoutput/pgoutput.c index 98377c094b..0df1acbb7a 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,18 @@ 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 already do that + * during some previous call in this process. + */ + if (!publication_callback_registered) + { + CacheRegisterSyscacheCallback(PUBLICATIONOID, + publication_invalidation_cb, + (Datum) 0); + publication_callback_registered = true; + } /* Initialize relation schema cache. */ init_rel_sync_cache(CacheMemoryContext); @@ -1745,7 +1755,7 @@ pgoutput_origin_filter(LogicalDecodingContext *ctx, * Shutdown the output plugin. * * Note, we don't need to clean the data->context and data->cachectx as - * they are child context of the ctx->context so it will be cleaned up by + * they are child contexts of the ctx->context so they will be cleaned up by * logical decoding machinery. */ static void @@ -1931,7 +1941,9 @@ static void init_rel_sync_cache(MemoryContext cachectx) { HASHCTL ctl; + static bool relation_callbacks_registered = false; + /* Nothing to do if hash table already exists */ if (RelationSyncCache != NULL) return; @@ -1946,6 +1958,10 @@ init_rel_sync_cache(MemoryContext cachectx) Assert(RelationSyncCache != NULL); + /* No more to do if we already registered callbacks */ + if (relation_callbacks_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 +1984,8 @@ init_rel_sync_cache(MemoryContext cachectx) CacheRegisterSyscacheCallback(PUBLICATIONNAMESPACEMAP, rel_sync_cache_publication_cb, (Datum) 0); + + relation_callbacks_registered = true; } /*