8
8
*
9
9
*
10
10
* 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 $
12
12
*
13
13
* NOTES
14
14
* this is the "main" module of the postgres backend and
@@ -663,6 +663,7 @@ exec_simple_query(const char *query_string)
663
663
struct timeval start_t ,
664
664
stop_t ;
665
665
bool save_log_duration = log_duration ;
666
+ int save_log_min_duration_statement = log_min_duration_statement ;
666
667
bool save_log_statement_stats = log_statement_stats ;
667
668
668
669
/*
@@ -673,11 +674,12 @@ exec_simple_query(const char *query_string)
673
674
pgstat_report_activity (query_string );
674
675
675
676
/*
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.
678
680
* Similarly, log_statement_stats has to be captured once.
679
681
*/
680
- if (save_log_duration )
682
+ if (save_log_duration || save_log_min_duration_statement > 0 )
681
683
gettimeofday (& start_t ,NULL );
682
684
683
685
if (save_log_statement_stats )
@@ -915,19 +917,38 @@ exec_simple_query(const char *query_string)
915
917
QueryContext = NULL ;
916
918
917
919
/*
918
- * Finish up monitoring.
920
+ * Combine processing here as we need to calculate the query
921
+ * duration in both instances.
919
922
*/
920
- if (save_log_duration )
923
+ if (save_log_duration || save_log_min_duration_statement > 0 )
921
924
{
925
+ long usecs ;
922
926
gettimeofday (& stop_t ,NULL );
923
927
if (stop_t .tv_usec < start_t .tv_usec )
924
928
{
925
929
stop_t .tv_sec -- ;
926
930
stop_t .tv_usec += 1000000 ;
927
931
}
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 ));
931
952
}
932
953
933
954
if (save_log_statement_stats )
@@ -2526,7 +2547,7 @@ PostgresMain(int argc, char *argv[], const char *username)
2526
2547
if (!IsUnderPostmaster )
2527
2548
{
2528
2549
puts ("\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" );
2530
2551
}
2531
2552
2532
2553
/*