Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b952d61

Browse files
committed
Add log_min_duration_statement.
Christopher Kings-Lynne
1 parent 8bfe93c commit b952d61

File tree

6 files changed

+66
-14
lines changed

6 files changed

+66
-14
lines changed

doc/src/sgml/runtime.sgml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/runtime.sgml,v 1.182 2003/05/27 17:49:45 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/runtime.sgml,v 1.183 2003/06/11 18:01:13 momjian Exp $
33
-->
44

55
<Chapter Id="runtime">
@@ -1051,6 +1051,23 @@ SET ENABLE_SEQSCAN TO OFF;
10511051
</listitem>
10521052
</varlistentry>
10531053

1054+
<varlistentry>
1055+
<term><varname>LOG_MIN_DURATION_STATEMENT</varname> (<type>integer</type>)</term>
1056+
<listitem>
1057+
<para>
1058+
Sets a minimum statement execution time (in milliseconds)
1059+
above which a statement will be logged. All SQL statements
1060+
that run longer than the time specified will be logged together
1061+
with the duration, in seconds. The default is <literal>0</literal>
1062+
(turning this feature off). For example, if you set it
1063+
to <literal>250</literal> then all SQL statements that run longer
1064+
than 250ms will be logged along with the duration. Enabling this
1065+
option can be useful in tracking down unoptimized queries in
1066+
your applications.
1067+
</para>
1068+
</listitem>
1069+
</varlistentry>
1070+
10541071
<varlistentry>
10551072
<term><varname>LOG_MIN_ERROR_STATEMENT</varname> (<type>string</type>)</term>
10561073
<listitem>

src/backend/tcop/postgres.c

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
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)
663663
struct timeval start_t,
664664
stop_t;
665665
bool save_log_duration = log_duration;
666+
int save_log_min_duration_statement = log_min_duration_statement;
666667
bool save_log_statement_stats = log_statement_stats;
667668

668669
/*
@@ -673,11 +674,12 @@ exec_simple_query(const char *query_string)
673674
pgstat_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)
681683
gettimeofday(&start_t, NULL);
682684

683685
if (save_log_statement_stats)
@@ -915,19 +917,38 @@ exec_simple_query(const char *query_string)
915917
QueryContext = 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;
922926
gettimeofday(&stop_t, NULL);
923927
if (stop_t.tv_usec < start_t.tv_usec)
924928
{
925929
stop_t.tv_sec--;
926930
stop_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

933954
if (save_log_statement_stats)
@@ -2526,7 +2547,7 @@ PostgresMain(int argc, char *argv[], const char *username)
25262547
if (!IsUnderPostmaster)
25272548
{
25282549
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");
25302551
}
25312552

25322553
/*

src/backend/utils/misc/guc.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Written by Peter Eisentraut <peter_e@gmx.net>.
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/utils/misc/guc.c,v 1.128 2003/06/11 05:04:51 momjian Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/utils/misc/guc.c,v 1.129 2003/06/11 18:01:14 momjian Exp $
1414
*
1515
*--------------------------------------------------------------------
1616
*/
@@ -55,6 +55,8 @@
5555
#include "utils/pg_locale.h"
5656
#include "pgstat.h"
5757

58+
int log_min_duration_statement = 0;
59+
5860

5961
#ifndef PG_KRB_SRVTAB
6062
#define PG_KRB_SRVTAB ""
@@ -735,6 +737,11 @@ static struct config_int
735737
0, -15, 2, NULL, NULL
736738
},
737739

740+
{
741+
{"log_min_duration_statement", PGC_USERSET}, &log_min_duration_statement,
742+
0, 0, INT_MAX / 1000, NULL, NULL
743+
},
744+
738745
{
739746
{NULL, 0}, NULL, 0, 0, 0, NULL, NULL
740747
}

src/backend/utils/misc/postgresql.conf.sample

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@
140140
# debug5, debug4, debug3, debug2, debug1,
141141
# info, notice, warning, error, panic(off)
142142

143+
#log_min_duration_statement = 0 # Log all statements whose
144+
# execution time exceeds the value, in
145+
# milliseconds. Zero disables.
146+
143147
#debug_print_parse = false
144148
#debug_print_rewritten = false
145149
#debug_print_plan = false

src/bin/psql/tab-complete.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000-2002 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v 1.77 2003/05/14 03:26:02 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v 1.78 2003/06/11 18:01:14 momjian Exp $
77
*/
88

99
/*----------------------------------------------------------------------
@@ -538,6 +538,7 @@ psql_completion(char *text, int start, int end)
538538
"lc_time",
539539
"log_duration",
540540
"log_executor_stats",
541+
"log_min_duration_statement",
541542
"log_min_error_statement",
542543
"log_min_messages",
543544
"log_parser_stats",

src/include/utils/guc.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Copyright 2000-2003 by PostgreSQL Global Development Group
88
* Written by Peter Eisentraut <peter_e@gmx.net>.
99
*
10-
* $Id: guc.h,v 1.31 2003/05/06 20:26:28 tgl Exp $
10+
* $Id: guc.h,v 1.32 2003/06/11 18:01:14 momjian Exp $
1111
*--------------------------------------------------------------------
1212
*/
1313
#ifndef GUC_H
@@ -142,4 +142,6 @@ void write_nondefault_variables(GucContext context);
142142
void read_nondefault_variables(void);
143143
#endif
144144

145+
extern int log_min_duration_statement;
146+
145147
#endif /* GUC_H */

0 commit comments

Comments
 (0)