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

Commit b8382c2

Browse files
committed
Have log_min_duration_statement = 0 always print duration/statement.
Change log line to be "duration: ms query:" Indent multi-line queries with a tab in the server logs.
1 parent cfd992e commit b8382c2

File tree

5 files changed

+58
-29
lines changed

5 files changed

+58
-29
lines changed

doc/src/sgml/runtime.sgml

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/runtime.sgml,v 1.210 2003/10/03 19:26:49 tgl Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/runtime.sgml,v 1.211 2003/10/08 03:49:37 momjian Exp $
33
-->
44

55
<Chapter Id="runtime">
@@ -1689,16 +1689,16 @@ SET ENABLE_SEQSCAN TO OFF;
16891689
<listitem>
16901690
<para>
16911691
Sets a minimum statement execution time (in milliseconds)
1692-
above which a statement will be logged. All SQL statements
1693-
that run longer than the time specified will be logged together
1694-
with their actual duration. Setting this to zero (the default)
1695-
disables time-based logging. For example, if you set it
1696-
to <literal>250</literal> then all SQL statements that run longer
1697-
than 250ms will be logged. Enabling this
1698-
option can be useful in tracking down unoptimized queries in
1699-
your applications.
1700-
Only superusers can increase this or set it to zero if this option
1701-
is set to non-zero by the administrator.
1692+
for statement to be logged. All SQL statements
1693+
that run in the time specified or longer will be logged with
1694+
their duration. Setting this to zero will print
1695+
all queries and their durations. Minus-one (the default)
1696+
disables this. For example, if you set it to
1697+
<literal>250</literal> then all SQL statements that run 250ms
1698+
or longer will be logged. Enabling this option can be
1699+
useful in tracking down unoptimized queries in your applications.
1700+
Only superusers can increase this or set it to minus-one if this
1701+
option is set by the administrator.
17021702
</para>
17031703
</listitem>
17041704
</varlistentry>

src/backend/tcop/postgres.c

+13-10
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.370 2003/10/04 02:47:04 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.371 2003/10/08 03:49:37 momjian Exp $
1212
*
1313
* NOTES
1414
* this is the "main" module of the postgres backend and
@@ -694,7 +694,7 @@ exec_simple_query(const char *query_string)
694694
* because gettimeofday() wasn't called. Similarly,
695695
* log_statement_stats has to be captured once.
696696
*/
697-
if (save_log_duration || save_log_min_duration_statement > 0)
697+
if (save_log_duration || save_log_min_duration_statement != -1)
698698
gettimeofday(&start_t, NULL);
699699

700700
if (save_log_statement_stats)
@@ -940,7 +940,7 @@ exec_simple_query(const char *query_string)
940940
* Combine processing here as we need to calculate the query duration
941941
* in both instances.
942942
*/
943-
if (save_log_duration || save_log_min_duration_statement > 0)
943+
if (save_log_duration || save_log_min_duration_statement != -1)
944944
{
945945
long usecs;
946946
bool print_statement;
@@ -957,15 +957,17 @@ exec_simple_query(const char *query_string)
957957
* Output a duration_statement to the log if the query has exceeded
958958
* the min duration, or if we are to print all durations.
959959
*/
960-
print_statement = (save_log_min_duration_statement > 0 &&
961-
usecs >= save_log_min_duration_statement * 1000);
960+
print_statement = (save_log_min_duration_statement == 0 ||
961+
(save_log_min_duration_statement > 0 &&
962+
usecs >= save_log_min_duration_statement * 1000));
962963

963964
if (save_log_duration || print_statement)
964965
ereport(LOG,
965-
(errmsg("duration(secs): %ld.%06ld%s%s",
966-
(long) (stop_t.tv_sec - start_t.tv_sec),
967-
(long) (stop_t.tv_usec - start_t.tv_usec),
968-
print_statement ? " " : "",
966+
(errmsg("duration: %ld.%03ld ms%s%s",
967+
(long) ((stop_t.tv_sec - start_t.tv_sec) * 1000 +
968+
(stop_t.tv_usec - start_t.tv_usec) / 1000),
969+
(long) (stop_t.tv_usec - start_t.tv_usec) % 1000,
970+
print_statement ? " query: " : "",
969971
print_statement ? query_string : "")));
970972
}
971973

@@ -2655,7 +2657,7 @@ PostgresMain(int argc, char *argv[], const char *username)
26552657
if (!IsUnderPostmaster)
26562658
{
26572659
puts("\nPOSTGRES backend interactive interface ");
2658-
puts("$Revision: 1.370 $ $Date: 2003/10/04 02:47:04 $\n");
2660+
puts("$Revision: 1.371 $ $Date: 2003/10/08 03:49:37 $\n");
26592661
}
26602662

26612663
/*
@@ -3196,3 +3198,4 @@ ShowUsage(const char *title)
31963198

31973199
pfree(str.data);
31983200
}
3201+

src/backend/utils/error/elog.c

+27-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $Header: /cvsroot/pgsql/src/backend/utils/error/elog.c,v 1.123 2003/09/25 06:58:05 petere Exp $
40+
* $Header: /cvsroot/pgsql/src/backend/utils/error/elog.c,v 1.124 2003/10/08 03:49:38 momjian Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -145,6 +145,7 @@ static const char *useful_strerror(int errnum);
145145
static const char *error_severity(int elevel);
146146
static const char *print_timestamp(void);
147147
static const char *print_pid(void);
148+
static char *str_prepend_tabs(const char *str);
148149

149150

150151
/*
@@ -1135,14 +1136,17 @@ send_message_to_server_log(ErrorData *edata)
11351136
/* Write to stderr, if enabled */
11361137
if (Use_syslog <= 1 || whereToSendOutput == Debug)
11371138
{
1139+
char *p = str_prepend_tabs(buf.data);
1140+
11381141
/*
11391142
* Timestamp and PID are only used for stderr output --- we assume
11401143
* the syslog daemon will supply them for us in the other case.
11411144
*/
11421145
fprintf(stderr, "%s%s%s",
11431146
Log_timestamp ? print_timestamp() : "",
11441147
Log_pid ? print_pid() : "",
1145-
buf.data);
1148+
p);
1149+
pfree(p);
11461150
}
11471151

11481152
pfree(buf.data);
@@ -1450,3 +1454,24 @@ print_pid(void)
14501454
snprintf(buf, sizeof(buf), "[%d] ", (int) MyProcPid);
14511455
return buf;
14521456
}
1457+
1458+
/*
1459+
* str_prepend_tabs
1460+
*
1461+
* This string prepends a tab to message continuation lines.
1462+
*/
1463+
static char *str_prepend_tabs(const char *str)
1464+
{
1465+
char *outstr = palloc(strlen(str) * 2 + 1);
1466+
int len = strlen(str);
1467+
int i, outlen = 0;
1468+
1469+
for (i = 0; i < len; i++)
1470+
{
1471+
outstr[outlen++] = str[i];
1472+
if (str[i] == '\n' && str[i+1] != '\0' )
1473+
outstr[outlen++] = '\t';
1474+
}
1475+
outstr[outlen++] = '\0';
1476+
return outstr;
1477+
}

src/backend/utils/misc/guc.c

+4-4
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.162 2003/10/03 19:26:49 tgl Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/utils/misc/guc.c,v 1.163 2003/10/08 03:49:38 momjian Exp $
1414
*
1515
*--------------------------------------------------------------------
1616
*/
@@ -128,7 +128,7 @@ int log_min_error_statement = PANIC;
128128
int log_min_messages = NOTICE;
129129
int client_min_messages = NOTICE;
130130

131-
int log_min_duration_statement = 0;
131+
int log_min_duration_statement = -1;
132132

133133

134134
/*
@@ -1185,10 +1185,10 @@ static struct config_int ConfigureNamesInt[] =
11851185
{"log_min_duration_statement", PGC_USERLIMIT, LOGGING_WHEN,
11861186
gettext_noop("minimum execution time in milliseconds above which statements will "
11871187
"be logged"),
1188-
gettext_noop("The default is 0 (turning this feature off).")
1188+
gettext_noop("Zero prints all queries. The default is -1 (turning this feature off).")
11891189
},
11901190
&log_min_duration_statement,
1191-
0, 0, INT_MAX / 1000, NULL, NULL
1191+
-1, -1, INT_MAX / 1000, NULL, NULL
11921192
},
11931193

11941194
/* End-of-list marker */

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,10 @@
157157
# debug5, debug4, debug3, debug2, debug1,
158158
# info, notice, warning, error, panic(off)
159159

160-
#log_min_duration_statement = 0 # Log all statements whose
160+
#log_min_duration_statement = -1 # Log all statements whose
161161
# execution time exceeds the value, in
162-
# milliseconds. Zero disables.
162+
# milliseconds. Zero prints all queries.
163+
# Minus-one disables.
163164

164165
#silent_mode = false # DO NOT USE without Syslog!
165166

0 commit comments

Comments
 (0)