diff --git a/src/backend/distributed/connection/connection_configuration.c b/src/backend/distributed/connection/connection_configuration.c index 206bb740b..9309cfa6d 100644 --- a/src/backend/distributed/connection/connection_configuration.c +++ b/src/backend/distributed/connection/connection_configuration.c @@ -89,7 +89,7 @@ ResetConnParams() InvalidateConnParamsHashEntries(); - AddConnParam("fallback_application_name", "citus"); + AddConnParam("fallback_application_name", CITUS_APPLICATION_NAME); } diff --git a/src/backend/distributed/connection/connection_management.c b/src/backend/distributed/connection/connection_management.c index 37866beaf..0f9d9e190 100644 --- a/src/backend/distributed/connection/connection_management.c +++ b/src/backend/distributed/connection/connection_management.c @@ -1046,6 +1046,18 @@ AfterXactHostConnectionHandling(ConnectionHashEntry *entry, bool isCommit) { dlist_mutable_iter iter; int cachedConnectionCount = 0; + bool isCitusInitiatedBackend = false; + + /* + * When we are in a backend that was created to service an internal connection + * from the coordinator or another worker, we disable connection caching to avoid + * escalating the number of cached connections. We can recognize such backends + * from their application name. + */ + if (application_name != NULL && strcmp(application_name, CITUS_APPLICATION_NAME) == 0) + { + isCitusInitiatedBackend = true; + } dlist_foreach_modify(iter, entry->connections) { @@ -1067,7 +1079,8 @@ AfterXactHostConnectionHandling(ConnectionHashEntry *entry, bool isCommit) /* * Preserve session lifespan connections if they are still healthy. */ - if (cachedConnectionCount >= MaxCachedConnectionsPerWorker || + if (isCitusInitiatedBackend || + cachedConnectionCount >= MaxCachedConnectionsPerWorker || connection->forceCloseAtTransactionEnd || PQstatus(connection->pgConn) != CONNECTION_OK || !RemoteTransactionIdle(connection)) diff --git a/src/include/distributed/connection_management.h b/src/include/distributed/connection_management.h index cb670508e..8d21e21cd 100644 --- a/src/include/distributed/connection_management.h +++ b/src/include/distributed/connection_management.h @@ -27,6 +27,9 @@ /* default notice level */ #define DEFAULT_CITUS_NOTICE_LEVEL DEBUG1 +/* application name used for internal connections in Citus */ +#define CITUS_APPLICATION_NAME "citus" + /* forward declare, to avoid forcing large headers on everyone */ struct pg_conn; /* target of the PGconn typedef */ struct MemoryContextData;