diff --git a/contrib/postgres_fdw/connection.c b/contrib/postgres_fdw/connection.c
index 116be7d..d252eb1 100644
--- a/contrib/postgres_fdw/connection.c
+++ b/contrib/postgres_fdw/connection.c
@@ -44,11 +44,15 @@ typedef struct ConnCacheKey
 typedef struct ConnCacheEntry
 {
 	ConnCacheKey key;			/* hash key (must be first) */
-	PGconn	   *conn;			/* connection to foreign server, or NULL */
+	PGconn	   *conn;			/* primary connection to foreign server */
+	List	   *extraconns;		/* Other connection list of AdditionalConns */
 	int			xact_depth;		/* 0 = no xact open, 1 = main xact open, 2 =
 								 * one level of subxact open, etc */
+	char 	   *snapshot_id;	/* snapshot id for this server */
 	bool		have_prep_stmt; /* have we prepared any stmts in this xact? */
 	bool		have_error;		/* have any subxacts aborted in this xact? */
+	int			reserved_aux_conns; /* Number of reserved rooms for parallel
+									 * connection */
 } ConnCacheEntry;
 
 /*
@@ -75,31 +79,16 @@ static void pgfdw_subxact_callback(SubXactEvent event,
 					   SubTransactionId parentSubid,
 					   void *arg);
 
-
 /*
- * Get a PGconn which can be used to execute queries on the remote PostgreSQL
- * server with the user's authorization.  A new connection is established
- * if we don't already have a suitable one, and a transaction is opened at
- * the right subtransaction nesting depth if we didn't do that already.
- *
- * will_prep_stmt must be true if caller intends to create any prepared
- * statements.  Since those don't go away automatically at transaction end
- * (not even on error), we need this flag to cue manual cleanup.
- *
- * XXX Note that caching connections theoretically requires a mechanism to
- * detect change of FDW objects to invalidate already established connections.
- * We could manage that by watching for invalidation events on the relevant
- * syscaches.  For the moment, though, it's not clear that this would really
- * be useful and not mere pedantry.  We could not flush any active connections
- * mid-transaction anyway.
+ * Search connection cache entry for specified server and user. Creates new
+ * one if not exists.
  */
-PGconn *
-GetConnection(ForeignServer *server, UserMapping *user,
-			  bool will_prep_stmt)
+static ConnCacheEntry*
+search_cache_entry(ForeignServer *server, UserMapping *user)
 {
-	bool		found;
-	ConnCacheEntry *entry;
 	ConnCacheKey key;
+	ConnCacheEntry *entry;
+	bool	found;
 
 	/* First time through, initialize connection cache hashtable */
 	if (ConnectionHash == NULL)
@@ -124,9 +113,6 @@ GetConnection(ForeignServer *server, UserMapping *user,
 		RegisterSubXactCallback(pgfdw_subxact_callback, NULL);
 	}
 
-	/* Set flag that we did GetConnection during the current transaction */
-	xact_got_connection = true;
-
 	/* Create hash key for the entry.  Assume no pad bytes in key struct */
 	key.serverid = server->serverid;
 	key.userid = user->userid;
@@ -139,11 +125,52 @@ GetConnection(ForeignServer *server, UserMapping *user,
 	{
 		/* initialize new hashtable entry (key is already filled in) */
 		entry->conn = NULL;
+		entry->extraconns = NIL;
 		entry->xact_depth = 0;
 		entry->have_prep_stmt = false;
 		entry->have_error = false;
+		entry->reserved_aux_conns = 0;
+		entry->snapshot_id = NULL;
 	}
 
+	return entry;
+}
+
+/*
+ * Get a PGconn which can be used to execute queries on the remote PostgreSQL
+ * server with the user's authorization.  A new connection is established
+ * if we don't already have a suitable one, and a transaction is opened at
+ * the right subtransaction nesting depth if we didn't do that already.
+ *
+ * will_prep_stmt must be true if caller intends to create any prepared
+ * statements.  Since those don't go away automatically at transaction end
+ * (not even on error), we need this flag to cue manual cleanup.
+ *
+ * Create new auxiliary connection to be used for parallel query if parallel
+ * is true. The number of auxiliary connections assumed to be limited by the
+ * caller using ReserveParallelConnection().
+ *
+ * XXX Note that caching connections theoretically requires a mechanism to
+ * detect change of FDW objects to invalidate already established connections.
+ * We could manage that by watching for invalidation events on the relevant
+ * syscaches.  For the moment, though, it's not clear that this would really
+ * be useful and not mere pedantry.  We could not flush any active connections
+ * mid-transaction anyway.
+ */
+PGconn *
+GetConnection(ForeignServer *server, UserMapping *user,
+			  bool will_prep_stmt, bool parallel)
+{
+	ConnCacheEntry *entry;
+	PGconn		*retconn;
+
+	Assert(!(will_prep_stmt && parallel));
+
+	entry = search_cache_entry(server, user);
+
+	/* Set flag that we did GetConnection during the current transaction */
+	xact_got_connection = true;
+
 	/*
 	 * We don't check the health of cached connection here, because it would
 	 * require some overhead.  Broken connection will be detected when the
@@ -160,20 +187,102 @@ GetConnection(ForeignServer *server, UserMapping *user,
 		entry->xact_depth = 0;	/* just to be sure */
 		entry->have_prep_stmt = false;
 		entry->have_error = false;
+		entry->snapshot_id = NULL;
 		entry->conn = connect_pg_server(server, user);
 		elog(DEBUG3, "new postgres_fdw connection %p for server \"%s\"",
 			 entry->conn, server->servername);
+
 	}
 
-	/*
-	 * Start a new transaction or subtransaction if needed.
-	 */
 	begin_remote_xact(entry);
 
 	/* Remember if caller will prepare statements */
 	entry->have_prep_stmt |= will_prep_stmt;
 
-	return entry->conn;
+	retconn = entry->conn;
+	if (parallel)
+	{
+		PGconn		*conn;
+		PGresult	*res;
+		char		 sql[64];
+		MemoryContext oldcontext;
+
+		/* Parallel query needs exported snapshot. */
+		if (entry->snapshot_id == NULL)
+			return NULL;
+
+		/* Make new connection and setup it. */
+		conn = connect_pg_server(server, user);
+		if (IsolationIsSerializable())
+			strncpy(sql, "START TRANSACTION ISOLATION LEVEL SERIALIZABLE", 64);
+		else
+			strncpy(sql, "START TRANSACTION ISOLATION LEVEL REPEATABLE READ", 64);
+		res = PQexec(conn, sql);
+		if (PQresultStatus(res) != PGRES_COMMAND_OK)
+			pgfdw_report_error(ERROR, res, conn, true, sql);
+		PQclear(res);
+
+		/*
+		 * Snapshot setup for parallel query. If the snapshot id could be get
+		 * from this server, this should succeed on the same server.
+		 */
+		snprintf(sql, 64, "SET TRANSACTION SNAPSHOT \'%s\'", entry->snapshot_id);
+		res = PQexec(conn, sql);
+		if (PQresultStatus(res) != PGRES_COMMAND_OK)
+			pgfdw_report_error(ERROR, res, conn, true, sql);
+		PQclear(res);
+
+		/* This list should be in the same context with connection cache */
+		oldcontext = MemoryContextSwitchTo(CacheMemoryContext);
+		entry->extraconns = lappend(entry->extraconns, conn);
+		MemoryContextSwitchTo(oldcontext);
+		retconn = conn;
+	}
+
+	elog(DEBUG3, "Get %s connection (parallels %d): %p",
+		 parallel ? "parallel" : "base", list_length(entry->extraconns),
+		 retconn);
+
+	return retconn;
+}
+
+
+/* 
+ * Reserve a room for parallel connection up to aux_conn_limit.
+ */
+bool
+ReserveParallelConnection(ForeignServer *server, UserMapping *user,
+						  int aux_conn_limit)
+{
+	ConnCacheEntry *entry;
+
+	if (!user)
+		return false;
+
+	entry = search_cache_entry(server, user);
+
+	if (entry->reserved_aux_conns >= aux_conn_limit)
+		return false;
+
+	entry->reserved_aux_conns++;
+	return true;
+}
+
+/*
+ * Reset reserved number of parallel connections
+ */
+void
+ResetReservedParallelConnections(ForeignServer *server, UserMapping *user)
+{
+	ConnCacheEntry *entry;
+
+	if (!user)
+		return false;
+
+	entry = search_cache_entry(server, user);
+
+	entry->reserved_aux_conns = 0;
+	return;
 }
 
 /*
@@ -378,6 +487,7 @@ begin_remote_xact(ConnCacheEntry *entry)
 	if (entry->xact_depth <= 0)
 	{
 		const char *sql;
+		PGresult *res;
 
 		elog(DEBUG3, "starting remote transaction on connection %p",
 			 entry->conn);
@@ -388,6 +498,30 @@ begin_remote_xact(ConnCacheEntry *entry)
 			sql = "START TRANSACTION ISOLATION LEVEL REPEATABLE READ";
 		do_sql_command(entry->conn, sql);
 		entry->xact_depth = 1;
+
+		/* 
+		 * To avoid error on the remote server, check if we can call
+		 * pg_export_snapshot() previously.
+		 */
+		sql = "SELECT count(*) FROM pg_proc p JOIN pg_namespace n ON "
+  			       "(n.oid = p.pronamespace AND n.nspname = 'pg_catalog')"
+			"WHERE p.proname = 'pg_export_snapshot' AND pronargs = 0";
+		res = PQexec(entry->conn, sql);
+		if (PQresultStatus(res) != PGRES_TUPLES_OK)
+			pgfdw_report_error(ERROR, res, entry->conn, true, sql);
+		if (strcmp(PQgetvalue(res, 0, 0), "1") == 0)
+		{
+			/* Get transaction snapshot if we can */
+			PQclear(res);
+			sql = "SELECT pg_export_snapshot()";
+			res = PQexec(entry->conn, sql);
+			if (PQresultStatus(res) != PGRES_TUPLES_OK)
+				pgfdw_report_error(ERROR, res, entry->conn, true, sql);
+			entry->snapshot_id = strdup(PQgetvalue(res, 0, 0));
+			PQclear(res);
+		}
+		else
+			PQclear(res);
 	}
 
 	/*
@@ -406,16 +540,92 @@ begin_remote_xact(ConnCacheEntry *entry)
 }
 
 /*
- * Release connection reference count created by calling GetConnection.
+ * Close all auxiliary connections if any.
+ */
+static void
+ReleaseAllAuxConnections(ConnCacheEntry *entry)
+{
+	ListCell *lc;
+
+	foreach(lc, entry->extraconns)
+	{
+		PGconn *c = (PGconn *) lfirst(lc);
+		PQfinish(c);
+	}
+	list_free(entry->extraconns);
+	entry->extraconns = NIL;
+}
+
+/*
+ * Release connection if required. All connections but the base connection
+ * should be release immediately.
  */
 void
-ReleaseConnection(PGconn *conn)
+ReleaseConnection(ForeignServer *server, UserMapping *user, PGconn *conn)
 {
+	ConnCacheEntry *entry;
+	ConnCacheKey key;
+
+	/* Clean up current asynchronous query if any */
+	while (PQtransactionStatus(conn) == PQTRANS_ACTIVE)
+	{
+		PGresult *res = PQgetResult(conn);
+		if (res)
+			PQclear(res);
+	}		
+
+	/* Create hash key for the entry.  Assume no pad bytes in key struct */
+	key.serverid = server->serverid;
+	key.userid = user->userid;
+
 	/*
-	 * Currently, we don't actually track connection references because all
-	 * cleanup is managed on a transaction or subtransaction basis instead. So
-	 * there's nothing to do here.
+	 * Find cached entry for the connection.
 	 */
+	entry = hash_search(ConnectionHash, &key, HASH_FIND, NULL);
+
+	if (!entry)
+		elog(WARNING, "Inconsistent connection release in postgres_fdw.");
+	else
+	{
+		if (entry->conn == conn) {
+			ReleaseAllAuxConnections(entry);
+
+			/*
+			 * Don't reset reserved number before auxiliary connections are
+			 * actually made
+			 */
+			if (entry->extraconns)
+				entry->reserved_aux_conns = 0;
+		}
+		else
+		{
+			ListCell *lc;
+			PGconn   *c = NULL;
+
+			/* Find  */
+			foreach(lc, entry->extraconns)
+			{
+				c = (PGconn *) lfirst(lc);
+				if (conn == c) break;
+			}
+
+			if (!lc)
+			{
+				/* XXXX: This is basically a bug, but simply ignore it... */
+				elog(WARNING, "Unknown connection is tried to release. Ignore it.");
+			}
+			else
+			{
+				/* Close the found connection and remove it from the list */
+				do_sql_command(c, "COMMIT TRANSACTION");
+				PQfinish(c);
+				elog(DEBUG3, "Parallel connection closed: %p", c);
+				entry->extraconns = list_delete_ptr(entry->extraconns, c);
+				if (entry->reserved_aux_conns > 0) 
+					entry->reserved_aux_conns--;
+			}
+		}
+	}
 }
 
 /*
@@ -571,6 +781,7 @@ pgfdw_xact_callback(XactEvent event, void *arg)
 						res = PQexec(entry->conn, "DEALLOCATE ALL");
 						PQclear(res);
 					}
+					entry->snapshot_id = NULL;
 					entry->have_prep_stmt = false;
 					entry->have_error = false;
 					break;
@@ -612,6 +823,7 @@ pgfdw_xact_callback(XactEvent event, void *arg)
 							res = PQexec(entry->conn, "DEALLOCATE ALL");
 							PQclear(res);
 						}
+						entry->snapshot_id = NULL;
 						entry->have_prep_stmt = false;
 						entry->have_error = false;
 					}
@@ -619,6 +831,7 @@ pgfdw_xact_callback(XactEvent event, void *arg)
 			}
 		}
 
+		ReleaseAllAuxConnections(entry);
 		/* Reset state to show we're out of a transaction */
 		entry->xact_depth = 0;
 
diff --git a/contrib/postgres_fdw/option.c b/contrib/postgres_fdw/option.c
index 65e7b89..5323d10 100644
--- a/contrib/postgres_fdw/option.c
+++ b/contrib/postgres_fdw/option.c
@@ -150,6 +150,7 @@ InitPgFdwOptions(void)
 		/* cost factors */
 		{"fdw_startup_cost", ForeignServerRelationId, false},
 		{"fdw_tuple_cost", ForeignServerRelationId, false},
+		{"max_aux_connections", ForeignServerRelationId, false},
 		/* updatable is available on both server and table */
 		{"updatable", ForeignServerRelationId, false},
 		{"updatable", ForeignTableRelationId, false},
diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c
index 4c49776..3fd60e1 100644
--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -76,7 +76,8 @@ typedef struct PgFdwRelationInfo
 	/* Cached catalog information. */
 	ForeignTable *table;
 	ForeignServer *server;
-	UserMapping *user;			/* only set in use_remote_estimate mode */
+	UserMapping  *user;			/* only set in use_remote_estimate mode */
+	bool		  parallel;		/* true if this rel can be scanned in parallel */
 } PgFdwRelationInfo;
 
 /*
@@ -136,6 +137,10 @@ typedef struct PgFdwScanState
 
 	/* for remote query execution */
 	PGconn	   *conn;			/* connection for the scan */
+	ForeignServer *server;		/* The foreign server this scan based on */
+	UserMapping	  *user;		/* The user this scan done by */
+	bool		parallel;		/* ture if this scan is allowed to run in
+								 * parallel */
 	unsigned int cursor_number; /* quasi-unique ID for my cursor */
 	bool		cursor_exists;	/* have we created the cursor? */
 	int			numParams;		/* number of parameters passed to query */
@@ -145,6 +150,7 @@ typedef struct PgFdwScanState
 
 	/* for storing result tuples */
 	HeapTuple  *tuples;			/* array of currently-retrieved tuples */
+	int			tupplane;		/* Which tuples currently read */
 	int			num_tuples;		/* # of tuples in array */
 	int			next_tuple;		/* index of next one to return */
 
@@ -168,6 +174,8 @@ typedef struct PgFdwModifyState
 	/* for remote query execution */
 	PGconn	   *conn;			/* connection for the scan */
 	char	   *p_name;			/* name of prepared statement, if created */
+	ForeignServer *server;		/* The foreign server this scan based on */
+	UserMapping	  *user;		/* The user this scan done by */
 
 	/* extracted fdw_private data */
 	char	   *query;			/* text of INSERT/UPDATE/DELETE command */
@@ -306,7 +314,7 @@ static bool ec_member_matches_foreign(PlannerInfo *root, RelOptInfo *rel,
 						  EquivalenceClass *ec, EquivalenceMember *em,
 						  void *arg);
 static void create_cursor(ForeignScanState *node);
-static void fetch_more_data(ForeignScanState *node);
+static void fetch_more_data(ForeignScanState *node, bool async);
 static void close_cursor(PGconn *conn, unsigned int cursor_number);
 static void prepare_foreign_modify(PgFdwModifyState *fmstate);
 static const char **convert_prep_stmt_params(PgFdwModifyState *fmstate,
@@ -328,6 +336,10 @@ static HeapTuple make_tuple_from_result_row(PGresult *res,
 						   MemoryContext temp_context);
 static void conversion_error_callback(void *arg);
 
+/* PQtransactionStatus returns 2 state for IDLE condition */
+#define CONN_IS_IDLE(conn) \
+	(PQtransactionStatus(conn) == PQTRANS_IDLE || \
+	 PQtransactionStatus(conn) == PQTRANS_INTRANS)
 
 /*
  * Foreign-data wrapper handler function: return a struct with pointers
@@ -384,6 +396,7 @@ postgresGetForeignRelSize(PlannerInfo *root,
 {
 	PgFdwRelationInfo *fpinfo;
 	ListCell   *lc;
+	unsigned long max_aux_connections = 0;
 
 	/*
 	 * We use PgFdwRelationInfo to pass various information to subsequent
@@ -414,6 +427,8 @@ postgresGetForeignRelSize(PlannerInfo *root,
 			fpinfo->fdw_startup_cost = strtod(defGetString(def), NULL);
 		else if (strcmp(def->defname, "fdw_tuple_cost") == 0)
 			fpinfo->fdw_tuple_cost = strtod(defGetString(def), NULL);
+		else if (strcmp(def->defname, "max_aux_connections") == 0)
+			max_aux_connections = strtol(defGetString(def), NULL, 10);
 	}
 	foreach(lc, fpinfo->table->options)
 	{
@@ -442,6 +457,11 @@ postgresGetForeignRelSize(PlannerInfo *root,
 	else
 		fpinfo->user = NULL;
 
+	/* Check if this foregn relation has the auxiliary connection available.*/
+	fpinfo->parallel =
+		ReserveParallelConnection(fpinfo->server, fpinfo->user,
+								  max_aux_connections);
+
 	/*
 	 * Identify which baserestrictinfo clauses can be sent to the remote
 	 * server and which can't.
@@ -558,6 +578,7 @@ postgresGetForeignPaths(PlannerInfo *root,
 								   fpinfo->total_cost,
 								   NIL, /* no pathkeys */
 								   NULL,		/* no outer rel either */
+								   fpinfo->parallel,
 								   NIL);		/* no fdw_private list */
 	add_path(baserel, (Path *) path);
 
@@ -725,6 +746,7 @@ postgresGetForeignPaths(PlannerInfo *root,
 									   total_cost,
 									   NIL,		/* no pathkeys */
 									   param_info->ppi_req_outer,
+									   fpinfo->parallel,
 									   NIL);	/* no fdw_private list */
 		add_path(baserel, (Path *) path);
 	}
@@ -752,6 +774,9 @@ postgresGetForeignPlan(PlannerInfo *root,
 	StringInfoData sql;
 	ListCell   *lc;
 
+	/* reset reserved number of auxiliary connections */
+	ResetReservedParallelConnections(fpinfo->server, fpinfo->user);
+
 	/*
 	 * Separate the scan_clauses into those that can be executed remotely and
 	 * those that can't.  baserestrictinfo clauses that were previously
@@ -866,6 +891,7 @@ postgresGetForeignPlan(PlannerInfo *root,
 	return make_foreignscan(tlist,
 							local_exprs,
 							scan_relid,
+							best_path->path.parallel,
 							params_list,
 							fdw_private);
 }
@@ -888,6 +914,7 @@ postgresBeginForeignScan(ForeignScanState *node, int eflags)
 	int			numParams;
 	int			i;
 	ListCell   *lc;
+	bool		parallel = fsplan->scan.parallel;
 
 	/*
 	 * Do nothing in EXPLAIN (no ANALYZE) case.  node->fdw_state stays NULL.
@@ -918,7 +945,20 @@ postgresBeginForeignScan(ForeignScanState *node, int eflags)
 	 * Get connection to the foreign server.  Connection manager will
 	 * establish new connection if necessary.
 	 */
-	fsstate->conn = GetConnection(server, user, false);
+	fsstate->conn = GetConnection(server, user, false, parallel);
+	if (parallel && fsstate->conn == NULL)
+	{
+		/*
+		 * Somehow no more auxiliary connection available, so this relation is
+		 * scanned in the base connection sequentially.
+		 */
+		elog(WARNING, "Failed to get parallel connection for foreign relation %d. Fall back to base connection.",
+			 RelationGetRelid(fsstate->rel));
+		parallel = false;
+		fsstate->conn = GetConnection(server, user, false, false);		
+	}
+	fsstate->server = server;
+	fsstate->user = user;
 
 	/* Assign a unique ID for my cursor */
 	fsstate->cursor_number = GetCursorNumber(fsstate->conn);
@@ -981,6 +1021,17 @@ postgresBeginForeignScan(ForeignScanState *node, int eflags)
 		fsstate->param_values = (const char **) palloc0(numParams * sizeof(char *));
 	else
 		fsstate->param_values = NULL;
+
+	fsstate->parallel = parallel;
+	if (parallel)
+	{
+		/* 
+		 * This connection is allowed asynchronous query execution, so start
+		 * it just now. 
+		 */
+		create_cursor(node);
+		fetch_more_data(node, true);
+	}
 }
 
 /*
@@ -1008,7 +1059,7 @@ postgresIterateForeignScan(ForeignScanState *node)
 	{
 		/* No point in another fetch if we already detected EOF, though. */
 		if (!fsstate->eof_reached)
-			fetch_more_data(node);
+			fetch_more_data(node, fsstate->parallel);
 		/* If we didn't get any tuples, must be end of data. */
 		if (fsstate->next_tuple >= fsstate->num_tuples)
 			return ExecClearTuple(slot);
@@ -1099,7 +1150,7 @@ postgresEndForeignScan(ForeignScanState *node)
 		close_cursor(fsstate->conn, fsstate->cursor_number);
 
 	/* Release remote connection */
-	ReleaseConnection(fsstate->conn);
+	ReleaseConnection(fsstate->server, fsstate->user, fsstate->conn);
 	fsstate->conn = NULL;
 
 	/* MemoryContexts will be deleted automatically. */
@@ -1301,7 +1352,9 @@ postgresBeginForeignModify(ModifyTableState *mtstate,
 	user = GetUserMapping(userid, server->serverid);
 
 	/* Open connection; report that we'll create a prepared statement. */
-	fmstate->conn = GetConnection(server, user, true);
+	fmstate->conn = GetConnection(server, user, true, false);
+	fmstate->server = server;
+	fmstate->user = user;
 	fmstate->p_name = NULL;		/* prepared statement not made yet */
 
 	/* Deconstruct fdw_private data. */
@@ -1599,7 +1652,7 @@ postgresEndForeignModify(EState *estate,
 	}
 
 	/* Release remote connection */
-	ReleaseConnection(fmstate->conn);
+	ReleaseConnection(fmstate->server, fmstate->user, fmstate->conn);
 	fmstate->conn = NULL;
 }
 
@@ -1751,10 +1804,11 @@ estimate_path_cost_size(PlannerInfo *root,
 							  (fpinfo->remote_conds == NIL), NULL);
 
 		/* Get the remote estimate */
-		conn = GetConnection(fpinfo->server, fpinfo->user, false);
+		conn = GetConnection(fpinfo->server, fpinfo->user, false, false);
+
 		get_remote_estimate(sql.data, conn, &rows, &width,
 							&startup_cost, &total_cost);
-		ReleaseConnection(conn);
+		ReleaseConnection(fpinfo->server, fpinfo->user, conn);
 
 		retrieved_rows = rows;
 
@@ -2001,10 +2055,12 @@ create_cursor(ForeignScanState *node)
 }
 
 /*
- * Fetch some more rows from the node's cursor.
+ * Fetch some more rows from the node's cursor. async indicates that this
+ * query runs on the dedicated connection and requested asynchronous query
+ * execution.
  */
 static void
-fetch_more_data(ForeignScanState *node)
+fetch_more_data(ForeignScanState *node, bool async)
 {
 	PgFdwScanState *fsstate = (PgFdwScanState *) node->fdw_state;
 	PGresult   *volatile res = NULL;
@@ -2026,6 +2082,7 @@ fetch_more_data(ForeignScanState *node)
 		int			fetch_size;
 		int			numrows;
 		int			i;
+		bool		skip_get_result = false;
 
 		/* The fetch size is arbitrary, but shouldn't be enormous. */
 		fetch_size = 100;
@@ -2033,36 +2090,81 @@ fetch_more_data(ForeignScanState *node)
 		snprintf(sql, sizeof(sql), "FETCH %d FROM c%u",
 				 fetch_size, fsstate->cursor_number);
 
-		res = PQexec(conn, sql);
-		/* On error, report the original query, not the FETCH. */
-		if (PQresultStatus(res) != PGRES_TUPLES_OK)
-			pgfdw_report_error(ERROR, res, conn, false, fsstate->query);
+		if (async && CONN_IS_IDLE(conn))
+		{
+			if (!PQsendQuery(conn, sql))
+				pgfdw_report_error(ERROR, res, conn, false, fsstate->query);
 
-		/* Convert the data into HeapTuples */
-		numrows = PQntuples(res);
-		fsstate->tuples = (HeapTuple *) palloc0(numrows * sizeof(HeapTuple));
-		fsstate->num_tuples = numrows;
-		fsstate->next_tuple = 0;
+			/*
+			 * IDLE connection state on async query means that this is the
+			 * first call for this query, so return immediately. See
+			 * postgresBeginForeignScan()
+			 */
+			skip_get_result = true;
+		}
 
-		for (i = 0; i < numrows; i++)
+		if (!skip_get_result)
 		{
-			fsstate->tuples[i] =
-				make_tuple_from_result_row(res, i,
-										   fsstate->rel,
-										   fsstate->attinmeta,
-										   fsstate->retrieved_attrs,
-										   fsstate->temp_cxt);
-		}
+			if (async)
+			{
+				res = PQgetResult(conn);
+				if (PQntuples(res) == fetch_size)
+				{
+					/*
+					 * Connection state doesn't go to IDLE even if all data
+					 * has been sent to client for asynchronous query. One
+					 * more PQgetResult() is needed to reset the state to
+					 * IDLE.  See PQexecFinish() for details.
+					 */
+					if (PQgetResult(conn) != NULL)
+						elog(ERROR, "Connection status error.");
+						
+				}
+			}
+			else
+				res = PQexec(conn, sql);
 
-		/* Update fetch_ct_2 */
-		if (fsstate->fetch_ct_2 < 2)
-			fsstate->fetch_ct_2++;
+			/* On error, report the original query, not the FETCH. */
+			if (PQresultStatus(res) != PGRES_TUPLES_OK)
+				pgfdw_report_error(ERROR, res, conn, false, fsstate->query);
 
-		/* Must be EOF if we didn't get as many tuples as we asked for. */
-		fsstate->eof_reached = (numrows < fetch_size);
+			/* Convert the data into HeapTuples */
+			numrows = PQntuples(res);
+			fsstate->tuples =
+					(HeapTuple *) palloc0(numrows * sizeof(HeapTuple));
+			fsstate->num_tuples = numrows;
+			fsstate->next_tuple = 0;
 
-		PQclear(res);
-		res = NULL;
+			for (i = 0; i < numrows; i++)
+			{
+				fsstate->tuples[i] =
+					make_tuple_from_result_row(res, i,
+											   fsstate->rel,
+											   fsstate->attinmeta,
+											   fsstate->retrieved_attrs,
+											   fsstate->temp_cxt);
+			}
+
+			/* Update fetch_ct_2 */
+			if (fsstate->fetch_ct_2 < 2)
+				fsstate->fetch_ct_2++;
+
+			/* Must be EOF if we didn't get as many tuples as we asked for. */
+			fsstate->eof_reached = (numrows < fetch_size);
+
+			PQclear(res);
+			res = NULL;
+		}
+
+		if (async && !skip_get_result && !fsstate->eof_reached)
+		{
+			/*
+			 * We can immediately request the next bunch of tuples if we're on
+			 * asynchronous connection.
+			 */
+			if (!PQsendQuery(conn, sql))
+				pgfdw_report_error(ERROR, res, conn, false, fsstate->query);
+		}
 	}
 	PG_CATCH();
 	{
@@ -2315,8 +2417,7 @@ postgresAnalyzeForeignTable(Relation relation,
 	table = GetForeignTable(RelationGetRelid(relation));
 	server = GetForeignServer(table->serverid);
 	user = GetUserMapping(relation->rd_rel->relowner, server->serverid);
-	conn = GetConnection(server, user, false);
-
+	conn = GetConnection(server, user, false, false);
 	/*
 	 * Construct command to get page count for relation.
 	 */
@@ -2345,7 +2446,7 @@ postgresAnalyzeForeignTable(Relation relation,
 	}
 	PG_END_TRY();
 
-	ReleaseConnection(conn);
+	ReleaseConnection(server, user, conn);
 
 	return true;
 }
@@ -2407,7 +2508,7 @@ postgresAcquireSampleRowsFunc(Relation relation, int elevel,
 	table = GetForeignTable(RelationGetRelid(relation));
 	server = GetForeignServer(table->serverid);
 	user = GetUserMapping(relation->rd_rel->relowner, server->serverid);
-	conn = GetConnection(server, user, false);
+	conn = GetConnection(server, user, false, false);
 
 	/*
 	 * Construct cursor that retrieves whole rows from remote.
@@ -2479,7 +2580,7 @@ postgresAcquireSampleRowsFunc(Relation relation, int elevel,
 	}
 	PG_END_TRY();
 
-	ReleaseConnection(conn);
+	ReleaseConnection(server, user, conn);
 
 	/* We assume that we have no dead tuple. */
 	*totaldeadrows = 0.0;
@@ -2609,7 +2710,7 @@ postgresImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid)
 	 */
 	server = GetForeignServer(serverOid);
 	mapping = GetUserMapping(GetUserId(), server->serverid);
-	conn = GetConnection(server, mapping, false);
+	conn = GetConnection(server, mapping, false, false);
 
 	/* Don't attempt to import collation if remote server hasn't got it */
 	if (PQserverVersion(conn) < 90100)
@@ -2826,7 +2927,7 @@ postgresImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid)
 	}
 	PG_END_TRY();
 
-	ReleaseConnection(conn);
+	ReleaseConnection(server, mapping, conn);
 
 	return commands;
 }
diff --git a/contrib/postgres_fdw/postgres_fdw.h b/contrib/postgres_fdw/postgres_fdw.h
index 94eadae..387b2ff 100644
--- a/contrib/postgres_fdw/postgres_fdw.h
+++ b/contrib/postgres_fdw/postgres_fdw.h
@@ -25,10 +25,16 @@ extern int	set_transmission_modes(void);
 extern void reset_transmission_modes(int nestlevel);
 
 /* in connection.c */
+extern bool ReserveParallelConnection(ForeignServer *server, UserMapping *user,
+									  int aux_conn_limit);
+extern void ResetReservedParallelConnections(ForeignServer *server,
+											 UserMapping *user);
 extern PGconn *GetConnection(ForeignServer *server, UserMapping *user,
-			  bool will_prep_stmt);
-extern void ReleaseConnection(PGconn *conn);
+							 bool will_prep_stmt, bool parallel);
+extern void ReleaseConnection(ForeignServer *server, UserMapping *user,
+							  PGconn *conn);
 extern unsigned int GetCursorNumber(PGconn *conn);
+extern int GetServerOrdinate(Oid serverid);
 extern unsigned int GetPrepStmtNumber(PGconn *conn);
 extern void pgfdw_report_error(int elevel, PGresult *res, PGconn *conn,
 				   bool clear, const char *sql);
