88 *
99 *
1010 * IDENTIFICATION
11- * $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.499 2006/08/15 18:26:58 tgl Exp $
11+ * $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.500 2006/08/29 02:11:29 momjian Exp $
1212 *
1313 * NOTES
1414 * this is the "main" module of the postgres backend and
@@ -583,6 +583,7 @@ log_after_parse(List *raw_parsetree_list, const char *query_string,
583583 * For the first EXECUTE we find, record the client statement used by
584584 * the PREPARE. PREPARE doesn't save the parse tree so we have no
585585 * way to conditionally output based on the type of query prepared.
586+ * Parse does save the command tag, so perhaps we can use that.
586587 */
587588if (IsA (parsetree ,ExecuteStmt ))
588589{
@@ -592,20 +593,16 @@ log_after_parse(List *raw_parsetree_list, const char *query_string,
592593if (* prepare_string == NULL &&
593594(entry = FetchPreparedStatement (stmt -> name , false))!= NULL &&
594595entry -> query_string )
595- {
596- * prepare_string = palloc (strlen (entry -> query_string )+
597- strlen (" [PREPARE: %s]" )- 2 + 1 );
598- sprintf (* prepare_string ," [PREPARE: %s]" ,
599- entry -> query_string );
600- }
596+ * prepare_string = pstrdup (entry -> query_string );
601597}
602598}
603599
604600if (log_this_statement )
605601{
606602ereport (LOG ,
607- (errmsg ("statement: %s%s" ,query_string ,
608- * prepare_string ?* prepare_string :"" )));
603+ (errmsg ("statement: %s" ,query_string ),
604+ * prepare_string ?errdetail ("prepare: %s" ,
605+ * prepare_string ) :0 ));
609606return true;
610607}
611608else
@@ -874,9 +871,7 @@ exec_simple_query(const char *query_string)
874871parsetree_list = pg_parse_query (query_string );
875872
876873/* Log immediately if dictated by log_statement */
877- if (log_statement != LOGSTMT_NONE )
878- was_logged = log_after_parse (parsetree_list ,query_string ,
879- & prepare_string );
874+ was_logged = log_after_parse (parsetree_list ,query_string ,& prepare_string );
880875
881876/*
882877 * Switch back to transaction context to enter the loop.
@@ -957,6 +952,7 @@ exec_simple_query(const char *query_string)
957952PortalDefineQuery (portal ,
958953NULL ,
959954query_string ,
955+ NULL ,
960956commandTag ,
961957querytree_list ,
962958plantree_list ,
@@ -1097,10 +1093,11 @@ exec_simple_query(const char *query_string)
10971093secs * 1000 + msecs ,usecs %1000 )));
10981094else
10991095ereport (LOG ,
1100- (errmsg ("duration: %ld.%03d ms statement: %s%s " ,
1096+ (errmsg ("duration: %ld.%03d ms statement: %s" ,
11011097secs * 1000 + msecs ,usecs %1000 ,
1102- query_string ,
1103- prepare_string ?prepare_string :"" )));
1098+ query_string ),
1099+ prepare_string ?errdetail ("prepare: %s" ,
1100+ prepare_string ) :0 ));
11041101}
11051102}
11061103
@@ -1147,7 +1144,7 @@ exec_parse_message(const char *query_string,/* string to execute */
11471144
11481145if (log_statement == LOGSTMT_ALL )
11491146ereport (LOG ,
1150- (errmsg ("prepare %s: %s" ,
1147+ (errmsg ("statement: prepare %s, %s" ,
11511148* stmt_name ?stmt_name :"<unnamed>" ,
11521149query_string )));
11531150
@@ -1384,8 +1381,7 @@ exec_bind_message(StringInfo input_message)
13841381/* Switch back to message context */
13851382MemoryContextSwitchTo (MessageContext );
13861383
1387- if (log_statement == LOGSTMT_ALL )
1388- initStringInfo (& bind_values_str );
1384+ initStringInfo (& bind_values_str );
13891385
13901386/* Get the fixed part of the message */
13911387portal_name = pq_getmsgstring (input_message );
@@ -1521,7 +1517,7 @@ exec_bind_message(StringInfo input_message)
15211517{
15221518Oid typinput ;
15231519Oid typioparam ;
1524- char * pstring ;
1520+ char * pstring , * p ;
15251521
15261522getTypeInputInfo (ptype ,& typinput ,& typioparam );
15271523
@@ -1540,12 +1536,17 @@ exec_bind_message(StringInfo input_message)
15401536typioparam ,
15411537-1 );
15421538
1543- /* Log the parameter value if needed */
1544- if (log_statement == LOGSTMT_ALL )
1545- appendStringInfo (& bind_values_str ,"%s$%d = \"%s\"" ,
1546- bind_values_str .len ?", " :"" ,
1547- paramno + 1 ,
1548- pstring );
1539+ /* Save the parameter values */
1540+ appendStringInfo (& bind_values_str ,"%s$%d = '" ,
1541+ bind_values_str .len ?", " :"" ,
1542+ paramno + 1 );
1543+ for (p = pstring ;* p ;p ++ )
1544+ {
1545+ if (* p == '\'' )/* double single quotes */
1546+ appendStringInfoChar (& bind_values_str ,* p );
1547+ appendStringInfoChar (& bind_values_str ,* p );
1548+ }
1549+ appendStringInfoChar (& bind_values_str ,'\'' );
15491550
15501551/* Free result of encoding conversion, if any */
15511552if (pstring && pstring != pbuf .data )
@@ -1607,13 +1608,14 @@ exec_bind_message(StringInfo input_message)
16071608if (log_statement == LOGSTMT_ALL )
16081609{
16091610ereport (LOG ,
1610- (errmsg ("bind %s%s%s: %s" ,
1611+ (errmsg ("statement: bind %s%s%s%s %s" ,
16111612* stmt_name ?stmt_name :"<unnamed>" ,
16121613* portal -> name ?"/" :"" ,
16131614* portal -> name ?portal -> name :"" ,
1614- pstmt -> query_string ?pstmt -> query_string :"" ),
1615- bind_values_str .len ?errdetail (bind_values_str .data ) :0 ));
1616- pfree (bind_values_str .data );
1615+ /* print bind parameters if we have them */
1616+ bind_values_str .len ?", " :"" ,
1617+ bind_values_str .len ?bind_values_str .data :"" ),
1618+ errdetail ("prepare: %s" ,pstmt -> query_string )));
16171619}
16181620
16191621/* Get the result format codes */
@@ -1651,6 +1653,7 @@ exec_bind_message(StringInfo input_message)
16511653PortalDefineQuery (portal ,
16521654* stmt_name ?pstrdup (stmt_name ) :NULL ,
16531655pstmt -> query_string ,
1656+ bind_values_str .len ?pstrdup (bind_values_str .data ) :NULL ,
16541657pstmt -> commandTag ,
16551658pstmt -> query_list ,
16561659pstmt -> plan_list ,
@@ -1684,6 +1687,7 @@ exec_execute_message(const char *portal_name, long max_rows)
16841687bool completed ;
16851688char completionTag [COMPLETION_TAG_BUFSIZE ];
16861689const char * sourceText = NULL ;
1690+ const char * bindText = NULL ;
16871691const char * prepStmtName ;
16881692bool save_log_statement_stats = log_statement_stats ;
16891693bool is_xact_command ;
@@ -1728,21 +1732,6 @@ exec_execute_message(const char *portal_name, long max_rows)
17281732debug_query_string = "fetch message" ;
17291733pgstat_report_activity ("<FETCH>" );
17301734}
1731- else if (portal -> sourceText )
1732- {
1733- /*
1734- * We must copy the sourceText into MessageContext in case the
1735- * portal is destroyed during finish_xact_command. Can avoid
1736- * the copy if it's not an xact command, though.
1737- */
1738- if (is_xact_command )
1739- sourceText = pstrdup (portal -> sourceText );
1740- else
1741- sourceText = portal -> sourceText ;
1742-
1743- debug_query_string = sourceText ;
1744- pgstat_report_activity (sourceText );
1745- }
17461735else
17471736{
17481737debug_query_string = "execute message" ;
@@ -1757,6 +1746,24 @@ exec_execute_message(const char *portal_name, long max_rows)
17571746else
17581747prepStmtName = "<unnamed>" ;
17591748
1749+ /*
1750+ * We must copy the sourceText and bindText into MessageContext
1751+ * in case the portal is destroyed during finish_xact_command.
1752+ * Can avoid the copy if it's not an xact command, though.
1753+ */
1754+ if (is_xact_command )
1755+ sourceText = pstrdup (portal -> sourceText );
1756+ else
1757+ sourceText = portal -> sourceText ;
1758+
1759+ if (portal -> bindText )
1760+ {
1761+ if (is_xact_command )
1762+ bindText = pstrdup (portal -> bindText );
1763+ else
1764+ bindText = portal -> bindText ;
1765+ }
1766+
17601767/*
17611768 * We use save_log_statement_stats so ShowUsage doesn't report incorrect
17621769 * results because ResetUsage wasn't called.
@@ -1766,12 +1773,15 @@ exec_execute_message(const char *portal_name, long max_rows)
17661773
17671774if (log_statement == LOGSTMT_ALL )
17681775ereport (LOG ,
1769- (errmsg ("execute %s%s%s%s: %s" ,
1776+ (errmsg ("statement: execute %s%s%s%s" ,
17701777execute_is_fetch ?"fetch from " :"" ,
17711778prepStmtName ,
17721779* portal_name ?"/" :"" ,
1773- * portal_name ?portal_name :"" ,
1774- sourceText ?sourceText :"" )));
1780+ * portal_name ?portal_name :"" ),
1781+ errdetail ("prepare: %s%s%s" ,sourceText ,
1782+ /* optionally print bind parameters */
1783+ bindText ?" bind: " :"" ,
1784+ bindText ?bindText :"" )));
17751785
17761786BeginCommand (portal -> commandTag ,dest );
17771787
@@ -1876,13 +1886,16 @@ exec_execute_message(const char *portal_name, long max_rows)
18761886secs * 1000 + msecs ,usecs %1000 )));
18771887else
18781888ereport (LOG ,
1879- (errmsg ("duration: %ld.%03d ms execute %s%s%s%s: %s " ,
1889+ (errmsg ("duration: %ld.%03d ms execute %s%s%s%s" ,
18801890secs * 1000 + msecs ,usecs %1000 ,
18811891execute_is_fetch ?"fetch from " :"" ,
18821892prepStmtName ,
18831893* portal_name ?"/" :"" ,
1884- * portal_name ?portal_name :"" ,
1885- sourceText ?sourceText :"" )));
1894+ * portal_name ?portal_name :"" ),
1895+ errdetail ("prepare: %s%s%s" ,sourceText ,
1896+ /* optionally print bind parameters */
1897+ bindText ?" bind: " :"" ,
1898+ bindText ?bindText :"" )));
18861899}
18871900}
18881901