Index: doc/src/sgml/runtime.sgml =================================================================== RCS file: /projects/cvsroot/pgsql-server/doc/src/sgml/runtime.sgml,v retrieving revision 1.177 diff -c -r1.177 runtime.sgml *** doc/src/sgml/runtime.sgml 2003/04/04 03:03:53 1.177 --- doc/src/sgml/runtime.sgml 2003/04/22 15:35:22 *************** *** 1091,1096 **** --- 1091,1114 ---- + LOG_MIN_DURATION_STATEMENT (integer) + + + Sets a minimum statement execution time (in milliseconds) + above which any statement will be logged. All SQL statements + that run longer than the time specified will be logged together + with the duration. The default is 0 + (turning this feature off). Valid values are integers from + 0 to MAX_INT. For example, if you set this + to 250 then all SQL statements that run longer + than 250ms will be logged along with the duration. Enabling this + option can be helpful in tracking down unoptimized queries in + your application. + + + + + LOG_MIN_MESSAGES (string) Index: src/backend/tcop/postgres.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/tcop/postgres.c,v retrieving revision 1.323 diff -c -r1.323 postgres.c *** src/backend/tcop/postgres.c 2003/04/22 00:08:07 1.323 --- src/backend/tcop/postgres.c 2003/04/22 15:35:23 *************** *** 581,594 **** struct timeval start_t, stop_t; bool save_log_duration = log_duration; debug_query_string = query_string->data; /* ! * We use save_log_duration so "SET log_duration = true" doesn't ! * report incorrect time because gettimeofday() wasn't called. */ ! if (save_log_duration) gettimeofday(&start_t, NULL); /* --- 581,596 ---- struct timeval start_t, stop_t; bool save_log_duration = log_duration; + int save_log_min_duration_statement = log_min_duration_statement; debug_query_string = query_string->data; /* ! * We use save_log_* so "SET log_duration = true" and ! * "SET log_min_duration_statement = true" don't report incorrect ! * time because gettimeofday() wasn't called. */ ! if (save_log_duration || save_log_min_duration_statement > 0) gettimeofday(&start_t, NULL); /* *************** *** 959,975 **** if (xact_started) finish_xact_command(false); ! if (save_log_duration) { gettimeofday(&stop_t, NULL); if (stop_t.tv_usec < start_t.tv_usec) { stop_t.tv_sec--; stop_t.tv_usec += 1000000; } ! elog(LOG, "duration: %ld.%06ld sec", ! (long) (stop_t.tv_sec - start_t.tv_sec), ! (long) (stop_t.tv_usec - start_t.tv_usec)); } debug_query_string = NULL; --- 961,999 ---- if (xact_started) finish_xact_command(false); ! /* ! * Combine processing here as we need to calculate the query ! * duration in both instances ! */ ! if (save_log_duration || save_log_min_duration_statement > 0) { + long usecs; gettimeofday(&stop_t, NULL); if (stop_t.tv_usec < start_t.tv_usec) { stop_t.tv_sec--; stop_t.tv_usec += 1000000; } ! usecs = (long) (stop_t.tv_sec - start_t.tv_sec) * 1000000 + (long) (stop_t.tv_usec - start_t.tv_usec); ! ! /* ! * Output a duration_query to the log if the query has exceeded the ! * min duration. ! */ ! if (usecs >= save_log_min_duration_statement * 1000) ! elog(LOG, "duration_query: %s duration: %ld.%06ld sec", ! query_string->data, ! (long) (stop_t.tv_sec - start_t.tv_sec), ! (long) (stop_t.tv_usec - start_t.tv_usec)); ! ! /* ! * If the user is requesting logging of all durations, then log ! * that as well. ! */ ! if (save_log_duration) ! elog(LOG, "duration: %ld.%06ld sec", ! (long) (stop_t.tv_sec - start_t.tv_sec), ! (long) (stop_t.tv_usec - start_t.tv_usec)); } debug_query_string = NULL; Index: src/backend/utils/misc/guc.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/utils/misc/guc.c,v retrieving revision 1.118 diff -c -r1.118 guc.c *** src/backend/utils/misc/guc.c 2003/03/28 20:17:13 1.118 --- src/backend/utils/misc/guc.c 2003/04/22 15:35:24 *************** *** 113,119 **** --- 113,121 ---- char *client_min_messages_str = NULL; const char client_min_messages_str_default[] = "notice"; + int log_min_duration_statement = 0; + #ifndef PG_KRB_SRVTAB #define PG_KRB_SRVTAB "" #endif *************** *** 709,714 **** --- 711,721 ---- { {"extra_float_digits", PGC_USERSET}, &extra_float_digits, 0, -15, 2, NULL, NULL + }, + + { + {"log_min_duration_statement", PGC_USERSET}, &log_min_duration_statement, + 0, 0, INT_MAX, NULL, NULL }, { Index: src/backend/utils/misc/postgresql.conf.sample =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/utils/misc/postgresql.conf.sample,v retrieving revision 1.77 diff -c -r1.77 postgresql.conf.sample *** src/backend/utils/misc/postgresql.conf.sample 2003/04/19 00:37:28 1.77 --- src/backend/utils/misc/postgresql.conf.sample 2003/04/22 15:35:24 *************** *** 140,145 **** --- 140,149 ---- # debug5, debug4, debug3, debug2, debug1, # info, notice, warning, error, panic(off) + #log_min_duration_statement = 0 # Log all statements whose + # execution time exceeds the value, in + # milliseconds. Zero disables. + #debug_print_parse = false #debug_print_rewritten = false #debug_print_plan = false Index: src/bin/psql/tab-complete.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/bin/psql/tab-complete.c,v retrieving revision 1.76 diff -c -r1.76 tab-complete.c *** src/bin/psql/tab-complete.c 2003/04/03 20:18:16 1.76 --- src/bin/psql/tab-complete.c 2003/04/22 15:35:25 *************** *** 540,545 **** --- 540,546 ---- "log_duration", "log_executor_stats", "log_min_error_statement", + "log_min_duration_statement", "log_min_messages", "log_parser_stats", "log_planner_stats", Index: src/include/utils/guc.h =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/include/utils/guc.h,v retrieving revision 1.26 diff -c -r1.26 guc.h *** src/include/utils/guc.h 2002/11/15 00:47:22 1.26 --- src/include/utils/guc.h 2003/04/22 15:35:26 *************** *** 138,141 **** --- 138,143 ---- extern const char client_min_messages_str_default[]; + extern int log_min_duration_statement; + #endif /* GUC_H */