Skip to content

Commit 0f78df7

Browse files
committed
Don't repeatedly register cache callbacks in pgoutput plugin.
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
1 parent f0423be commit 0f78df7

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/backend/replication/pgoutput/pgoutput.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
260260
bool is_init)
261261
{
262262
PGOutputData *data = palloc0(sizeof(PGOutputData));
263+
static bool publication_callback_registered = false;
263264

264265
/* Create our memory context for private allocations. */
265266
data->context = AllocSetContextCreate(ctx->context,
@@ -323,9 +324,18 @@ pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
323324
/* Init publication state. */
324325
data->publications = NIL;
325326
publications_valid = false;
326-
CacheRegisterSyscacheCallback(PUBLICATIONOID,
327-
publication_invalidation_cb,
328-
(Datum) 0);
327+
328+
/*
329+
* Register callback for pg_publication if we didn't already do that
330+
* during some previous call in this process.
331+
*/
332+
if (!publication_callback_registered)
333+
{
334+
CacheRegisterSyscacheCallback(PUBLICATIONOID,
335+
publication_invalidation_cb,
336+
(Datum) 0);
337+
publication_callback_registered = true;
338+
}
329339

330340
/* Initialize relation schema cache. */
331341
init_rel_sync_cache(CacheMemoryContext);
@@ -948,7 +958,9 @@ static void
948958
init_rel_sync_cache(MemoryContext cachectx)
949959
{
950960
HASHCTL ctl;
961+
static bool relation_callbacks_registered = false;
951962

963+
/* Nothing to do if hash table already exists */
952964
if (RelationSyncCache != NULL)
953965
return;
954966

@@ -963,10 +975,16 @@ init_rel_sync_cache(MemoryContext cachectx)
963975

964976
Assert(RelationSyncCache != NULL);
965977

978+
/* No more to do if we already registered callbacks */
979+
if (relation_callbacks_registered)
980+
return;
981+
966982
CacheRegisterRelcacheCallback(rel_sync_cache_relation_cb, (Datum) 0);
967983
CacheRegisterSyscacheCallback(PUBLICATIONRELMAP,
968984
rel_sync_cache_publication_cb,
969985
(Datum) 0);
986+
987+
relation_callbacks_registered = true;
970988
}
971989

972990
/*

0 commit comments

Comments
 (0)