Skip to content

Commit 95558bc

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 98b83b7 commit 95558bc

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
@@ -152,6 +152,7 @@ pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
152152
bool is_init)
153153
{
154154
PGOutputData *data = palloc0(sizeof(PGOutputData));
155+
static bool publication_callback_registered = false;
155156

156157
/* Create our memory context for private allocations. */
157158
data->context = AllocSetContextCreate(ctx->context,
@@ -196,9 +197,18 @@ pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
196197
/* Init publication state. */
197198
data->publications = NIL;
198199
publications_valid = false;
199-
CacheRegisterSyscacheCallback(PUBLICATIONOID,
200-
publication_invalidation_cb,
201-
(Datum) 0);
200+
201+
/*
202+
* Register callback for pg_publication if we didn't already do that
203+
* during some previous call in this process.
204+
*/
205+
if (!publication_callback_registered)
206+
{
207+
CacheRegisterSyscacheCallback(PUBLICATIONOID,
208+
publication_invalidation_cb,
209+
(Datum) 0);
210+
publication_callback_registered = true;
211+
}
202212

203213
/* Initialize relation schema cache. */
204214
init_rel_sync_cache(CacheMemoryContext);
@@ -503,8 +513,10 @@ static void
503513
init_rel_sync_cache(MemoryContext cachectx)
504514
{
505515
HASHCTL ctl;
516+
static bool relation_callbacks_registered = false;
506517
MemoryContext old_ctxt;
507518

519+
/* Nothing to do if hash table already exists */
508520
if (RelationSyncCache != NULL)
509521
return;
510522

@@ -522,10 +534,16 @@ init_rel_sync_cache(MemoryContext cachectx)
522534

523535
Assert(RelationSyncCache != NULL);
524536

537+
/* No more to do if we already registered callbacks */
538+
if (relation_callbacks_registered)
539+
return;
540+
525541
CacheRegisterRelcacheCallback(rel_sync_cache_relation_cb, (Datum) 0);
526542
CacheRegisterSyscacheCallback(PUBLICATIONRELMAP,
527543
rel_sync_cache_publication_cb,
528544
(Datum) 0);
545+
546+
relation_callbacks_registered = true;
529547
}
530548

531549
/*

0 commit comments

Comments
 (0)