From fc5173ad514a216dc93bc190dbba3751024a257d Mon Sep 17 00:00:00 2001
From: Andrew Dunstan <andrew@dunslane.net>
Date: Tue, 16 Feb 2010 22:19:59 +0000
Subject: [PATCH] Add query text to auto_explain output.

Still to be done: fix docs and fix regression failures under auto_explain.
---
 contrib/auto_explain/auto_explain.c |  6 ++++--
 src/backend/commands/explain.c      | 17 ++++++++++++++++-
 src/include/commands/explain.h      |  4 +++-
 3 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/contrib/auto_explain/auto_explain.c b/contrib/auto_explain/auto_explain.c
index a57cd162153..61406db6342 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 4195e2c6802..9100c404ad7 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 6684ac52c81..6f39f7a5692 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);
-- 
GitLab