Skip to content

Commit 05172f1

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 0da243f commit 05172f1

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/backend/replication/pgoutput/pgoutput.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
409409
bool is_init)
410410
{
411411
PGOutputData *data = palloc0(sizeof(PGOutputData));
412+
static bool publication_callback_registered = false;
412413

413414
/* Create our memory context for private allocations. */
414415
data->context = AllocSetContextCreate(ctx->context,
@@ -504,9 +505,18 @@ pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
504505
/* Init publication state. */
505506
data->publications = NIL;
506507
publications_valid = false;
507-
CacheRegisterSyscacheCallback(PUBLICATIONOID,
508-
publication_invalidation_cb,
509-
(Datum) 0);
508+
509+
/*
510+
* Register callback for pg_publication if we didn't already do that
511+
* during some previous call in this process.
512+
*/
513+
if (!publication_callback_registered)
514+
{
515+
CacheRegisterSyscacheCallback(PUBLICATIONOID,
516+
publication_invalidation_cb,
517+
(Datum) 0);
518+
publication_callback_registered = true;
519+
}
510520

511521
/* Initialize relation schema cache. */
512522
init_rel_sync_cache(CacheMemoryContext);
@@ -1745,7 +1755,7 @@ pgoutput_origin_filter(LogicalDecodingContext *ctx,
17451755
* Shutdown the output plugin.
17461756
*
17471757
* Note, we don't need to clean the data->context and data->cachectx as
1748-
* they are child context of the ctx->context so it will be cleaned up by
1758+
* they are child contexts of the ctx->context so they will be cleaned up by
17491759
* logical decoding machinery.
17501760
*/
17511761
static void
@@ -1931,7 +1941,9 @@ static void
19311941
init_rel_sync_cache(MemoryContext cachectx)
19321942
{
19331943
HASHCTL ctl;
1944+
static bool relation_callbacks_registered = false;
19341945

1946+
/* Nothing to do if hash table already exists */
19351947
if (RelationSyncCache != NULL)
19361948
return;
19371949

@@ -1946,6 +1958,10 @@ init_rel_sync_cache(MemoryContext cachectx)
19461958

19471959
Assert(RelationSyncCache != NULL);
19481960

1961+
/* No more to do if we already registered callbacks */
1962+
if (relation_callbacks_registered)
1963+
return;
1964+
19491965
/* We must update the cache entry for a relation after a relcache flush */
19501966
CacheRegisterRelcacheCallback(rel_sync_cache_relation_cb, (Datum) 0);
19511967

@@ -1968,6 +1984,8 @@ init_rel_sync_cache(MemoryContext cachectx)
19681984
CacheRegisterSyscacheCallback(PUBLICATIONNAMESPACEMAP,
19691985
rel_sync_cache_publication_cb,
19701986
(Datum) 0);
1987+
1988+
relation_callbacks_registered = true;
19711989
}
19721990

19731991
/*

0 commit comments

Comments
 (0)