diff --git a/contrib/auto_explain/auto_explain.c b/contrib/auto_explain/auto_explain.c
index a57cd1621531d551f876c2dad2fa05d8dc7d1730..61406db634245be15e29101003256b842b3348bf 100644
--- a/contrib/auto_explain/auto_explain.c
+++ b/contrib/auto_explain/auto_explain.c
@@ -6,7 +6,7 @@
  * Copyright (c) 2008-2010, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/contrib/auto_explain/auto_explain.c,v 1.12 2010/01/06 18:07:19 tgl Exp $
+ *	  $PostgreSQL: pgsql/contrib/auto_explain/auto_explain.c,v 1.13 2010/02/16 22:19:59 adunstan Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -240,6 +240,7 @@ explain_ExecutorEnd(QueryDesc *queryDesc)
 			es.format = auto_explain_log_format;
 
 			ExplainBeginOutput(&es);
+			ExplainQueryText(&es, queryDesc);
 			ExplainPrintPlan(&es, queryDesc);
 			ExplainEndOutput(&es);
 
@@ -255,7 +256,8 @@ explain_ExecutorEnd(QueryDesc *queryDesc)
 			 */
 			ereport(LOG,
 					(errmsg("duration: %.3f ms  plan:\n%s",
-							msec, es.str->data)));
+							msec, es.str->data),
+						errhidestmt(true)));
 
 			pfree(es.str->data);
 		}
diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c
index 4195e2c6802d6df920a7b4f2fdd4916fedd32b37..9100c404ad71aefc119e08a5c7a2734bebd741d5 100644
--- a/src/backend/commands/explain.c
+++ b/src/backend/commands/explain.c
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1994-5, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.202 2010/02/16 20:07:13 stark Exp $
+ *	  $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.203 2010/02/16 22:19:59 adunstan Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -487,6 +487,21 @@ ExplainPrintPlan(ExplainState *es, QueryDesc *queryDesc)
 				NULL, NULL, NULL, es);
 }
 
+/*
+ * ExplainQueryText -
+ *    add a "Query Text" node that contains the actual text of the query
+ * 
+ * The caller should have set up the options fields of *es, as well as
+ * initializing the output buffer es->str. 
+ *
+ */
+void
+ExplainQueryText(ExplainState *es, QueryDesc *queryDesc)
+{
+	if (queryDesc->sourceText)
+		ExplainPropertyText("Query Text", queryDesc->sourceText, es);
+}
+
 /*
  * report_triggers -
  *		report execution stats for a single relation's triggers
diff --git a/src/include/commands/explain.h b/src/include/commands/explain.h
index 6684ac52c81160e365895eeab34e18a1ab2e446e..6f39f7a5692a1767d9e2cf019d0358acc72cf6c6 100644
--- a/src/include/commands/explain.h
+++ b/src/include/commands/explain.h
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994-5, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/commands/explain.h,v 1.45 2010/01/02 16:58:03 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/commands/explain.h,v 1.46 2010/02/16 22:19:59 adunstan Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -66,6 +66,8 @@ extern void ExplainOnePlan(PlannedStmt *plannedstmt, ExplainState *es,
 
 extern void ExplainPrintPlan(ExplainState *es, QueryDesc *queryDesc);
 
+extern void ExplainQueryText(ExplainState *es, QueryDesc *queryDesc);
+
 extern void ExplainBeginOutput(ExplainState *es);
 extern void ExplainEndOutput(ExplainState *es);
 extern void ExplainSeparatePlans(ExplainState *es);