diff --git a/src/backend/distributed/connection/connection_management.c b/src/backend/distributed/connection/connection_management.c index a9a701988..e5080d6d7 100644 --- a/src/backend/distributed/connection/connection_management.c +++ b/src/backend/distributed/connection/connection_management.c @@ -320,6 +320,14 @@ StartNodeUserDatabaseConnection(uint32 flags, const char *hostname, int32 port, } + /* + * Either no caching desired, or no pre-established, non-claimed, + * connection present. Initiate connection establishment. + */ + MultiConnection *connection = MemoryContextAllocZero(ConnectionContext, + sizeof(MultiConnection)); + dlist_push_tail(entry->connections, &connection->connectionNode); + if (flags & WAIT_FOR_CONNECTION) { WaitLoopForSharedConnection(hostname, port); @@ -351,18 +359,13 @@ StartNodeUserDatabaseConnection(uint32 flags, const char *hostname, int32 port, IncrementSharedConnectionCounter(hostname, port); } + /* - * Either no caching desired, or no pre-established, non-claimed, - * connection present. Initiate connection establishment. + * We've already incremented the counter above, so we should decrement + * when we're done with the connection. */ - MultiConnection *connection = MemoryContextAllocZero(ConnectionContext, - sizeof(MultiConnection)); - - /* we've already incremented the counter above */ connection->sharedCounterIncremented = true; - dlist_push_tail(entry->connections, &connection->connectionNode); - StartConnectionEstablishment(connection, &key); ResetShardPlacementAssociation(connection);