diff --git a/contrib/postgres_fdw/connection.c b/contrib/postgres_fdw/connection.c index 304f3c20f83..c47f9efa378 100644 --- a/contrib/postgres_fdw/connection.c +++ b/contrib/postgres_fdw/connection.c @@ -138,6 +138,7 @@ static PGconn *connect_pg_server(ForeignServer *server, UserMapping *user); static void disconnect_pg_server(ConnCacheEntry *entry); static void check_conn_params(const char **keywords, const char **values, UserMapping *user); static void configure_remote_session(PGconn *conn); +static void reset_remote_session(PGconn *conn); static void do_sql_command_begin(PGconn *conn, const char *sql); static void do_sql_command_end(PGconn *conn, const char *sql, bool consume_input); @@ -204,6 +205,7 @@ GetConnection(UserMapping *user, bool will_prep_stmt, PgFdwConnState **state) { bool found; bool retry = false; + bool new_connection = false; ConnCacheEntry *entry; ConnCacheKey key; MemoryContext ccxt = CurrentMemoryContext; @@ -274,7 +276,10 @@ GetConnection(UserMapping *user, bool will_prep_stmt, PgFdwConnState **state) * will remain in a valid empty state, ie conn == NULL.) */ if (entry->conn == NULL) + { make_new_connection(entry, user); + new_connection = true; + } /* * We check the health of the cached connection here when using it. In @@ -283,6 +288,10 @@ GetConnection(UserMapping *user, bool will_prep_stmt, PgFdwConnState **state) */ PG_TRY(); { + /* Prepare the cached connection for use */ + if (!new_connection) + configure_remote_session(entry->conn); + /* Process a pending asynchronous request if any. */ if (entry->state.pendingAreq) process_pending_request(entry->state.pendingAreq); @@ -798,6 +807,12 @@ configure_remote_session(PGconn *conn) do_sql_command(conn, "SET extra_float_digits = 2"); } +static void +reset_remote_session(PGconn *conn) +{ + do_sql_command(conn, "RESET search_path"); +} + /* * Convenience subroutine to issue a non-data-returning SQL command to remote */ @@ -895,6 +910,8 @@ ReleaseConnection(PGconn *conn) * cleanup is managed on a transaction or subtransaction basis instead. So * there's nothing to do here. */ + + reset_remote_session(conn); } /*