Index: src/backend/commands/prepare.c
===================================================================
--- src/backend/commands/prepare.c	(HEAD)
+++ src/backend/commands/prepare.c	(patched)
@@ -249,13 +249,9 @@
 		plan_list = cplan->stmt_list;
 	}
 
-	/*
-	 * Note: we don't bother to copy the source query string into the portal.
-	 * Any errors it might be useful for will already have been reported.
-	 */
 	PortalDefineQuery(portal,
 					  NULL,
-					  NULL,
+					  entry->plansource->query_string,
 					  entry->plansource->commandTag,
 					  plan_list,
 					  cplan);
Index: src/backend/executor/execMain.c
===================================================================
--- src/backend/executor/execMain.c	(HEAD)
+++ src/backend/executor/execMain.c	(patched)
@@ -58,6 +58,9 @@
 #include "utils/tqual.h"
 
 
+/* Hook for plugins to get control in ExecutorRun() */
+ExecutorRun_hook_type ExecutorRun_hook = NULL;
+
 typedef struct evalPlanQual
 {
 	Index		rti;
@@ -220,6 +223,19 @@
 ExecutorRun(QueryDesc *queryDesc,
 			ScanDirection direction, long count)
 {
+	TupleTableSlot *result;
+
+	if (ExecutorRun_hook)
+		result = ExecutorRun_hook(queryDesc, direction, count);
+	else
+		result = standard_ExecutorRun(queryDesc, direction, count);
+	return result;
+}
+
+TupleTableSlot *
+standard_ExecutorRun(QueryDesc *queryDesc,
+                     ScanDirection direction, long count)
+{
 	EState	   *estate;
 	CmdType		operation;
 	DestReceiver *dest;
Index: src/include/executor/executor.h
===================================================================
--- src/include/executor/executor.h	(HEAD)
+++ src/include/executor/executor.h	(patched)
@@ -133,9 +133,16 @@
 /*
  * prototypes from functions in execMain.c
  */
+typedef TupleTableSlot *(*ExecutorRun_hook_type) (QueryDesc *queryDesc,
+                                                  ScanDirection direction,
+												  long count);
+extern PGDLLIMPORT ExecutorRun_hook_type ExecutorRun_hook;
+
 extern void ExecutorStart(QueryDesc *queryDesc, int eflags);
 extern TupleTableSlot *ExecutorRun(QueryDesc *queryDesc,
 			ScanDirection direction, long count);
+extern TupleTableSlot *standard_ExecutorRun(QueryDesc *queryDesc,
+			ScanDirection direction, long count);
 extern void ExecutorEnd(QueryDesc *queryDesc);
 extern void ExecutorRewind(QueryDesc *queryDesc);
 extern void InitResultRelInfo(ResultRelInfo *resultRelInfo,
