>From 249920b4d812127535731c3d6ced714506879241 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Fri, 10 Jul 2015 15:02:59 +0900
Subject: [PATCH 6/6] Debug message for async execution of postgres_fdw.

---
 contrib/postgres_fdw/postgres_fdw.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c
index dc60bcc..5d62e36 100644
--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -385,6 +385,25 @@ postgres_fdw_handler(PG_FUNCTION_ARGS)
 	PG_RETURN_POINTER(routine);
 }
 
+static void
+postgresDebugLog(PgFdwScanState *fsstate, char *msg, void* ptr)
+{
+	ForeignTable *table = GetForeignTable(RelationGetRelid(fsstate->rel));
+	ForeignServer *server = GetForeignServer(table->serverid);
+
+	if (fsstate->conn)
+		ereport(LOG,
+				(errmsg ("pg_fdw: [%s/%s/%p] %s",
+						 get_rel_name(table->relid), server->servername,
+						 fsstate->conn, msg),
+				 errhidestmt(true)));
+	else
+		ereport(LOG,
+				(errmsg ("pg_fdw: [%s/%s/--] %s",
+						 get_rel_name(table->relid), server->servername, msg),
+				 errhidestmt(true)));
+}
+
 /*
  * Read boolean server/table options
  * 0 is false, 1 is true, -1 is not specified
@@ -928,7 +947,10 @@ postgresStartForeignScan(ForeignScanState *node)
 	PgFdwScanState *fsstate = (PgFdwScanState *)node->fdw_state;
 
 	if (!fsstate->allow_async)
+	{
+		postgresDebugLog(fsstate, "Async start admistratively denied.", NULL);
 		return false;
+	}
 
 	/*
 	 * On the current implemnt, scans can run asynchronously if it is the
@@ -943,9 +965,11 @@ postgresStartForeignScan(ForeignScanState *node)
 		 * for details
 		 */
 		fetch_more_data(fsstate, START_ONLY);
+		postgresDebugLog(fsstate, "Async exec started.", fsstate->conn);
 		return true;
 	}
 
+	postgresDebugLog(fsstate, "Async exec denied.", NULL);
 	return false;
 }
 
@@ -2162,6 +2186,9 @@ fetch_more_data(PgFdwScanState *fsstate, fetch_mode cmd)
 			 */
 			if (fsstate != PFCgetAsyncScan(conn))
 			{
+				postgresDebugLog(fsstate,
+								 "Changed to sync fetch (different scan)",
+								 fsstate->conn);
 				fetch_more_data(PFCgetAsyncScan(conn), EXIT_ASYNC);
 				res = PFCexec(conn, sql);
 			}
@@ -2196,6 +2223,7 @@ fetch_more_data(PgFdwScanState *fsstate, fetch_mode cmd)
 			}
 
 			/* Elsewise do synchronous query execution */
+			postgresDebugLog(fsstate, "Sync fetch.", conn);
 			PFCsetAsyncScan(conn, NULL);
 			res = PFCexec(conn, sql);
 		}
-- 
1.8.3.1

