Index: src/backend/tcop/postgres.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/tcop/postgres.c,v
retrieving revision 1.458
diff -c -c -r1.458 postgres.c
*** src/backend/tcop/postgres.c	2 Sep 2005 21:50:54 -0000	1.458
--- src/backend/tcop/postgres.c	9 Sep 2005 10:06:46 -0000
***************
*** 1162,1173 ****
  	if (save_log_statement_stats)
  		ResetUsage();
  
- 	if (log_statement == LOGSTMT_ALL)
- 		ereport(LOG,
- 				(errmsg("statement: PREPARE %s AS %s",
- 						(*stmt_name != '\0') ? stmt_name : "<unnamed>",
- 						query_string)));
- 
  	/*
  	 * Start up a transaction command so we can run parse analysis etc.
  	 * (Note that this will normally change current memory context.)
--- 1162,1167 ----
***************
*** 1217,1222 ****
--- 1211,1222 ----
  
  	QueryContext = CurrentMemoryContext;
  
+ 
+     /* Log parse only when using named statements, unless in debug1 mode */
+    	if (log_statement == LOGSTMT_ALL)
+    		ereport((is_named ? DEBUG1 : LOG),
+ 				(errmsg("statement: PREPARE %s AS %s", stmt_name, query_string)));
+ 
  	/*
  	 * Do basic parsing of the query or queries (this should be safe even
  	 * if we are in aborted transaction state!)
***************
*** 1467,1473 ****
  		portal = CreatePortal(portal_name, false, false);
  
  	if (log_statement == LOGSTMT_ALL)
! 		ereport(LOG,
  				(errmsg("statement: <BIND> %s", portal_name)));
  
  	/*
--- 1467,1473 ----
  		portal = CreatePortal(portal_name, false, false);
  
  	if (log_statement == LOGSTMT_ALL)
! 		ereport(DEBUG1,
  				(errmsg("statement: <BIND> %s", portal_name)));
  
  	/*
***************
*** 1683,1688 ****
--- 1683,1689 ----
  	bool		save_log_duration = log_duration;
  	int			save_log_min_duration_statement = log_min_duration_statement;
  	bool		save_log_statement_stats = log_statement_stats;
+     bool        subsequent_fetch = false;
  
  	/* Adjust destination to tell printtup.c what to do */
  	dest = whereToSendOutput;
***************
*** 1695,1700 ****
--- 1696,1710 ----
  				(errcode(ERRCODE_UNDEFINED_CURSOR),
  				 errmsg("portal \"%s\" does not exist", portal_name)));
  
+     /*
+      * 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.
+      */
+     if (!portal->atStart)
+         subsequent_fetch = true;
+ 
  	/*
  	 * If the original query was a null string, just return
  	 * EmptyQueryResponse.
***************
*** 1706,1712 ****
  		return;
  	}
  
! 	if (portal->sourceText)
  	{
  		debug_query_string = portal->sourceText;
  		pgstat_report_activity(portal->sourceText);
--- 1716,1727 ----
  		return;
  	}
  
!     if (subsequent_fetch)
!     {
! 		debug_query_string = "fetch message";
! 		pgstat_report_activity("<FETCH>");
!     }
!     else if (portal->sourceText)
  	{
  		debug_query_string = portal->sourceText;
  		pgstat_report_activity(portal->sourceText);
***************
*** 1731,1742 ****
  	if (save_log_statement_stats)
  		ResetUsage();
  
  	if (log_statement == LOGSTMT_ALL)
! 		/* We have the portal, so output the source query. */
! 		ereport(LOG,
! 				(errmsg("statement: EXECUTE %s  [PREPARE:  %s]",
! 						(*portal_name != '\0') ? portal_name : "<unnamed>",
  						portal->sourceText ? portal->sourceText : "")));
  
  	BeginCommand(portal->commandTag, dest);
  
--- 1746,1774 ----
  	if (save_log_statement_stats)
  		ResetUsage();
  
+     /* 
+      * log statement details - when using log_statement=all we log the
+      * phrase MAXROWS because this occurs prior to execution
+      */
  	if (log_statement == LOGSTMT_ALL)
!     {
!         if (subsequent_fetch)
!     		ereport(LOG,
! 				(errmsg("statement: FETCH %s MAXROWS %ld", portal_name,
!                         max_rows)));
!         else
!     		/* We have the portal, so output the source query. */
!             if (max_rows <= 0)
!         		ereport(LOG,
! 	       			(errmsg("statement: EXECUTE %s [PREPARE:  %s]", 
!                         portal_name,
  						portal->sourceText ? portal->sourceText : "")));
+             else
+            		ereport(LOG,
+ 	       			(errmsg("statement: EXECUTE %s MAXROWS %ld [PREPARE:  %s]", 
+                         portal_name, max_rows,
+ 						portal->sourceText ? portal->sourceText : "")));
+     }
  
  	BeginCommand(portal->commandTag, dest);
  
***************
*** 1865,1877 ****
  		if (save_log_min_duration_statement == 0 ||
  			(save_log_min_duration_statement > 0 &&
  			 usecs >= save_log_min_duration_statement * 1000))
! 			ereport(LOG,
! 					(errmsg("duration: %ld.%03ld ms  statement: EXECUTE %s  [PREPARE:  %s]",
  						(long) ((stop_t.tv_sec - start_t.tv_sec) * 1000 +
  							  (stop_t.tv_usec - start_t.tv_usec) / 1000),
  						(long) (stop_t.tv_usec - start_t.tv_usec) % 1000,
! 							(*portal_name != '\0') ? portal_name : "<unnamed>",
! 							portal->sourceText ? portal->sourceText : "")));
  	}
  
  	if (save_log_statement_stats)
--- 1897,1929 ----
  		if (save_log_min_duration_statement == 0 ||
  			(save_log_min_duration_statement > 0 &&
  			 usecs >= save_log_min_duration_statement * 1000))
!         {
!             if (subsequent_fetch)
!         		ereport(LOG,
! 					(errmsg("duration: %ld.%03ld ms  statement: FETCH %s ROWS %ld ",
  						(long) ((stop_t.tv_sec - start_t.tv_sec) * 1000 +
  							  (stop_t.tv_usec - start_t.tv_usec) / 1000),
  						(long) (stop_t.tv_usec - start_t.tv_usec) % 1000,
! 							portal_name, 
!                             max_rows)));
!             else
!                 if (completed)
!         			ereport(LOG,
! 	       				(errmsg("duration: %ld.%03ld ms  statement: EXECUTE %s [PREPARE:  %s]",
! 	       					(long) ((stop_t.tv_sec - start_t.tv_sec) * 1000 +
! 	       						  (stop_t.tv_usec - start_t.tv_usec) / 1000),
! 	       					(long) (stop_t.tv_usec - start_t.tv_usec) % 1000,
! 	       						portal_name,
! 	       						portal->sourceText ? portal->sourceText : "")));
!                 else
!         			ereport(LOG,
! 	       				(errmsg("duration: %ld.%03ld ms  statement: EXECUTE %s ROWS %ld [PREPARE:  %s]",
! 	       					(long) ((stop_t.tv_sec - start_t.tv_sec) * 1000 +
! 	       						  (stop_t.tv_usec - start_t.tv_usec) / 1000),
! 	       					(long) (stop_t.tv_usec - start_t.tv_usec) % 1000,
! 	       						portal_name, max_rows,
! 	       						portal->sourceText ? portal->sourceText : "")));
!         }
  	}
  
  	if (save_log_statement_stats)
