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

Commit b1e1862

Browse files
committed
Coordinate log_line_prefix options 'm' and 'n' to share a timeval.
Commit f828654 introduced the 'n' option, but it invoked gettimeofday() independently of the 'm' option. If both options were in use (or multiple 'n' options), or if 'n' was in use along with csvlog, then the reported times could be different for the same log message. To fix, initialize a global variable with gettimeofday() once per log message, and use that for both formats. Don't bother coordinating the time for the 't' option, which has much lower resolution. Per complaint by Alvaro Herrera.
1 parent d94c36a commit b1e1862

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/backend/utils/error/elog.c

+23-9
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,14 @@ static int errordata_stack_depth = -1; /* index of topmost active frame */
143143

144144
static int recursion_depth = 0; /* to detect actual recursion */
145145

146-
/* buffers for formatted timestamps that might be used by both
147-
* log_line_prefix and csv logs.
146+
/*
147+
* Saved timeval and buffers for formatted timestamps that might be used by
148+
* both log_line_prefix and csv logs.
148149
*/
149150

151+
static struct timeval saved_timeval;
152+
static bool saved_timeval_set = false;
153+
150154
#define FORMATTED_TS_LEN 128
151155
static char formatted_start_time[FORMATTED_TS_LEN];
152156
static char formatted_log_time[FORMATTED_TS_LEN];
@@ -2195,12 +2199,16 @@ write_console(const char *line, int len)
21952199
static void
21962200
setup_formatted_log_time(void)
21972201
{
2198-
struct timeval tv;
21992202
pg_time_t stamp_time;
22002203
char msbuf[8];
22012204

2202-
gettimeofday(&tv, NULL);
2203-
stamp_time = (pg_time_t) tv.tv_sec;
2205+
if (!saved_timeval_set)
2206+
{
2207+
gettimeofday(&saved_timeval, NULL);
2208+
saved_timeval_set = true;
2209+
}
2210+
2211+
stamp_time = (pg_time_t) saved_timeval.tv_sec;
22042212

22052213
/*
22062214
* Note: we expect that guc.c will ensure that log_timezone is set up (at
@@ -2213,7 +2221,7 @@ setup_formatted_log_time(void)
22132221
pg_localtime(&stamp_time, log_timezone));
22142222

22152223
/* 'paste' milliseconds into place... */
2216-
sprintf(msbuf, ".%03d", (int) (tv.tv_usec / 1000));
2224+
sprintf(msbuf, ".%03d", (int) (saved_timeval.tv_usec / 1000));
22172225
memcpy(formatted_log_time + 19, msbuf, 4);
22182226
}
22192227

@@ -2440,11 +2448,16 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
24402448
break;
24412449
case 'n':
24422450
{
2443-
struct timeval tv;
24442451
char strfbuf[128];
24452452

2446-
gettimeofday(&tv, NULL);
2447-
sprintf(strfbuf, "%ld.%03d", tv.tv_sec, (int)(tv.tv_usec / 1000));
2453+
if (!saved_timeval_set)
2454+
{
2455+
gettimeofday(&saved_timeval, NULL);
2456+
saved_timeval_set = true;
2457+
}
2458+
2459+
sprintf(strfbuf, "%ld.%03d", saved_timeval.tv_sec,
2460+
(int)(saved_timeval.tv_usec / 1000));
24482461

24492462
if (padding != 0)
24502463
appendStringInfo(buf, "%*s", padding, strfbuf);
@@ -2825,6 +2838,7 @@ send_message_to_server_log(ErrorData *edata)
28252838

28262839
initStringInfo(&buf);
28272840

2841+
saved_timeval_set = false;
28282842
formatted_log_time[0] = '\0';
28292843

28302844
log_line_prefix(&buf, edata);

0 commit comments

Comments
 (0)