From 52c8c5639d156763881660c872ad28cf63b491fa Mon Sep 17 00:00:00 2001
From: Bharath Rupireddy <bharath.rupireddy@enterprisedb.com>
Date: Fri, 29 Jan 2021 10:48:29 +0530
Subject: [PATCH v1] Fix connection closure issue in pgfdw_inval_callback

Do not close cached connections immediately in pgfdw_inval_callback
even though it's not being used, just mark it as invalidated so
that they get closed at the end of main txn in pgfdw_xact_callback.
---
 contrib/postgres_fdw/connection.c | 16 +---------------
 1 file changed, 1 insertion(+), 15 deletions(-)

diff --git a/contrib/postgres_fdw/connection.c b/contrib/postgres_fdw/connection.c
index ee0b4acf0b..3ef2c54e7d 100644
--- a/contrib/postgres_fdw/connection.c
+++ b/contrib/postgres_fdw/connection.c
@@ -1119,21 +1119,7 @@ pgfdw_inval_callback(Datum arg, int cacheid, uint32 hashvalue)
 			 entry->server_hashvalue == hashvalue) ||
 			(cacheid == USERMAPPINGOID &&
 			 entry->mapping_hashvalue == hashvalue))
-		{
-			/*
-			 * Close the connection immediately if it's not used yet in this
-			 * transaction. Otherwise mark it as invalid so that
-			 * pgfdw_xact_callback() can close it at the end of this
-			 * transaction.
-			 */
-			if (entry->xact_depth == 0)
-			{
-				elog(DEBUG3, "discarding connection %p", entry->conn);
-				disconnect_pg_server(entry);
-			}
-			else
-				entry->invalidated = true;
-		}
+			entry->invalidated = true;
 	}
 }
 
-- 
2.25.1

