]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Don't repeatedly register cache callbacks in pgoutput plugin.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 23 Feb 2023 20:40:28 +0000 (15:40 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 23 Feb 2023 20:40:28 +0000 (15:40 -0500)
Multiple cycles of starting up and shutting down the plugin within a
single session would eventually lead to "out of relcache_callback_list
slots", because pgoutput_startup blindly re-registered its cache
callbacks each time.  Fix it to register them only once, as all other
users of cache callbacks already take care to do.

This has been broken all along, so back-patch to all supported branches.

Shi Yu

Discussion: https://postgr.es/m/OSZPR01MB631004A78D743D68921FFAD3FDA79@OSZPR01MB6310.jpnprd01.prod.outlook.com

src/backend/replication/pgoutput/pgoutput.c

index a059315c4f98c60a5a6b1ebbc2d74bb4a197aa2c..63a84424aac6e78962d0ac4f7df38d99979aae72 100644 (file)
@@ -393,6 +393,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,
@@ -481,9 +482,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);
@@ -1709,7 +1719,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
@@ -1891,7 +1901,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;
 
@@ -1906,6 +1918,10 @@ init_rel_sync_cache(MemoryContext cachectx)
 
        Assert(RelationSyncCache != NULL);
 
+       /* No more to do if we already registered callbacks */
+       if (relation_callbacks_registered)
+               return;
+
        CacheRegisterRelcacheCallback(rel_sync_cache_relation_cb, (Datum) 0);
        CacheRegisterSyscacheCallback(PUBLICATIONRELMAP,
                                                                  rel_sync_cache_publication_cb,
@@ -1913,6 +1929,8 @@ init_rel_sync_cache(MemoryContext cachectx)
        CacheRegisterSyscacheCallback(PUBLICATIONNAMESPACEMAP,
                                                                  rel_sync_cache_publication_cb,
                                                                  (Datum) 0);
+
+       relation_callbacks_registered = true;
 }
 
 /*