88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.346 2003/05/27 17:49:46 momjian Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.347 2003/06/11 18:01:14 momjian Exp $
1212 *
1313 * NOTES
1414 * this is the "main" module of the postgres backend and
@@ -663,6 +663,7 @@ exec_simple_query(const char *query_string)
663663struct timeval start_t ,
664664stop_t ;
665665bool save_log_duration = log_duration ;
666+ int save_log_min_duration_statement = log_min_duration_statement ;
666667bool save_log_statement_stats = log_statement_stats ;
667668
668669/*
@@ -673,11 +674,12 @@ exec_simple_query(const char *query_string)
673674pgstat_report_activity (query_string );
674675
675676/*
676- * We use save_log_duration so "SET log_duration = true" doesn't
677- * report incorrect time because gettimeofday() wasn't called.
677+ * We use save_log_* so "SET log_duration = true" and
678+ * "SET log_min_duration_statement = true" don't report incorrect
679+ * time because gettimeofday() wasn't called.
678680 * Similarly, log_statement_stats has to be captured once.
679681 */
680- if (save_log_duration )
682+ if (save_log_duration || save_log_min_duration_statement > 0 )
681683gettimeofday (& start_t ,NULL );
682684
683685if (save_log_statement_stats )
@@ -915,19 +917,38 @@ exec_simple_query(const char *query_string)
915917QueryContext = NULL ;
916918
917919/*
918- * Finish up monitoring.
920+ * Combine processing here as we need to calculate the query
921+ * duration in both instances.
919922 */
920- if (save_log_duration )
923+ if (save_log_duration || save_log_min_duration_statement > 0 )
921924{
925+ long usecs ;
922926gettimeofday (& stop_t ,NULL );
923927if (stop_t .tv_usec < start_t .tv_usec )
924928{
925929stop_t .tv_sec -- ;
926930stop_t .tv_usec += 1000000 ;
927931}
928- elog (LOG ,"duration: %ld.%06ld sec" ,
929- (long ) (stop_t .tv_sec - start_t .tv_sec ),
930- (long ) (stop_t .tv_usec - start_t .tv_usec ));
932+ usecs = (long ) (stop_t .tv_sec - start_t .tv_sec )* 1000000 + (long ) (stop_t .tv_usec - start_t .tv_usec );
933+
934+ /*
935+ * Output a duration_query to the log if the query has exceeded the
936+ * min duration.
937+ */
938+ if (usecs >=save_log_min_duration_statement * 1000 )
939+ elog (LOG ,"duration_statement: %ld.%06ld %s" ,
940+ (long ) (stop_t .tv_sec - start_t .tv_sec ),
941+ (long ) (stop_t .tv_usec - start_t .tv_usec ),
942+ query_string );
943+
944+ /*
945+ * If the user is requesting logging of all durations, then log
946+ * that as well.
947+ */
948+ if (save_log_duration )
949+ elog (LOG ,"duration: %ld.%06ld sec" ,
950+ (long ) (stop_t .tv_sec - start_t .tv_sec ),
951+ (long ) (stop_t .tv_usec - start_t .tv_usec ));
931952}
932953
933954if (save_log_statement_stats )
@@ -2526,7 +2547,7 @@ PostgresMain(int argc, char *argv[], const char *username)
25262547if (!IsUnderPostmaster )
25272548{
25282549puts ("\nPOSTGRES backend interactive interface " );
2529- puts ("$Revision: 1.346 $ $Date: 2003/05/27 17:49:46 $\n" );
2550+ puts ("$Revision: 1.347 $ $Date: 2003/06/11 18:01:14 $\n" );
25302551}
25312552
25322553/*