From 681892208fe41459acf89ade0d91a78e55f1089a Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Thu, 19 Oct 2006 19:52:22 +0000
Subject: [PATCH] Make sure that debug_query_string contains the original query
 text, if available (which it usually should be), during processing of Bind
 and Execute protocol messages.  This improves usefulness of
 log_min_error_statement logging for extended query protocol.

---
 src/backend/tcop/postgres.c | 48 ++++++++++++++++++-------------------
 1 file changed, 24 insertions(+), 24 deletions(-)

diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 599bb8aa863..d019864fea4 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.515 2006/10/08 17:45:50 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.516 2006/10/19 19:52:22 tgl Exp $
  *
  * NOTES
  *	  this is the "main" module of the postgres backend and
@@ -1326,9 +1326,9 @@ exec_bind_message(StringInfo input_message)
 	/*
 	 * Report query to various monitoring facilities.
 	 */
-	debug_query_string = "bind message";
+	debug_query_string = pstmt->query_string ? pstmt->query_string : "<BIND>";
 
-	pgstat_report_activity(pstmt->query_string ? pstmt->query_string : "<BIND>");
+	pgstat_report_activity(debug_query_string);
 
 	set_ps_display("BIND", false);
 
@@ -1680,29 +1680,9 @@ exec_execute_message(const char *portal_name, long max_rows)
 		return;
 	}
 
-	/*
-	 * Report query to various monitoring facilities.
-	 */
-	debug_query_string = "execute message";
-
-	pgstat_report_activity(portal->sourceText ? portal->sourceText : "<EXECUTE>");
-
-	set_ps_display(portal->commandTag, false);
-
-	if (save_log_statement_stats)
-		ResetUsage();
-
 	/* Does the portal contain a transaction command? */
 	is_xact_command = IsTransactionStmtList(portal->parseTrees);
 
-	/*
-	 * If we re-issue an Execute protocol request against an existing portal,
-	 * then we are only fetching more rows rather than completely re-executing
-	 * the query from the start. atStart is never reset for a v3 portal, so we
-	 * are safe to use this check.
-	 */
-	execute_is_fetch = !portal->atStart;
-
 	/*
 	 * We must copy the sourceText and prepStmtName into MessageContext in
 	 * case the portal is destroyed during finish_xact_command. Can avoid the
@@ -1710,7 +1690,7 @@ exec_execute_message(const char *portal_name, long max_rows)
 	 */
 	if (is_xact_command)
 	{
-		sourceText = pstrdup(portal->sourceText);
+		sourceText = portal->sourceText ? pstrdup(portal->sourceText) : NULL;
 		if (portal->prepStmtName)
 			prepStmtName = pstrdup(portal->prepStmtName);
 		else
@@ -1732,6 +1712,18 @@ exec_execute_message(const char *portal_name, long max_rows)
 		portalParams = portal->portalParams;
 	}
 
+	/*
+	 * Report query to various monitoring facilities.
+	 */
+	debug_query_string = sourceText ? sourceText : "<EXECUTE>";
+
+	pgstat_report_activity(debug_query_string);
+
+	set_ps_display(portal->commandTag, false);
+
+	if (save_log_statement_stats)
+		ResetUsage();
+
 	BeginCommand(portal->commandTag, dest);
 
 	/*
@@ -1746,6 +1738,14 @@ exec_execute_message(const char *portal_name, long max_rows)
 	 */
 	start_xact_command();
 
+	/*
+	 * If we re-issue an Execute protocol request against an existing portal,
+	 * then we are only fetching more rows rather than completely re-executing
+	 * the query from the start. atStart is never reset for a v3 portal, so we
+	 * are safe to use this check.
+	 */
+	execute_is_fetch = !portal->atStart;
+
 	/* Log immediately if dictated by log_statement */
 	if (check_log_statement_cooked(portal->parseTrees))
 	{
-- 
GitLab