Hi,

from time to time I need to correlate PostgreSQL logs to other logs,
containing numeric timestamps - a prime example of that is pgbench. With
%t and %m that's not quite trivial, because of timezones etc.

I propose adding two new log_line_prefix escape sequences - %T and %M,
doing the same thing as %t and %m, but formatting the value as a number.

Patch attached, I'll add it to CF 2015-06.

regards

-- 
Tomas Vondra                http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index b30c68d..7f39b18 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -4636,11 +4636,21 @@ local0.*    /var/log/postgresql
              <entry>no</entry>
             </row>
             <row>
+             <entry><literal>%T</literal></entry>
+             <entry>Time stamp without milliseconds (as a numer)</entry>
+             <entry>no</entry>
+            </row>
+            <row>
              <entry><literal>%m</literal></entry>
              <entry>Time stamp with milliseconds</entry>
              <entry>no</entry>
             </row>
             <row>
+             <entry><literal>%M</literal></entry>
+             <entry>Time stamp with milliseconds (as a number)</entry>
+             <entry>no</entry>
+            </row>
+            <row>
              <entry><literal>%i</literal></entry>
              <entry>Command tag: type of session's current command</entry>
              <entry>yes</entry>
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index b952c7c..abafdd9 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -2428,6 +2428,19 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
 				else
 					appendStringInfoString(buf, formatted_log_time);
 				break;
+			case 'M':
+				{
+					struct timeval tv;
+					char timestamp_str[FORMATTED_TS_LEN];
+
+					gettimeofday(&tv, NULL);
+
+					sprintf(timestamp_str, "%ld.%.03d",
+							tv.tv_sec, (int)(tv.tv_usec / 1000));
+
+					appendStringInfoString(buf, timestamp_str);
+				}
+				break;
 			case 't':
 				{
 					pg_time_t	stamp_time = (pg_time_t) time(NULL);
@@ -2442,6 +2455,18 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
 						appendStringInfoString(buf, strfbuf);
 				}
 				break;
+			case 'T':
+				{
+					struct timeval tv;
+					char timestamp_str[FORMATTED_TS_LEN];
+
+					gettimeofday(&tv, NULL);
+
+					sprintf(timestamp_str, "%ld", tv.tv_sec);
+
+					appendStringInfoString(buf, timestamp_str);
+				}
+				break;
 			case 's':
 				if (formatted_start_time[0] == '\0')
 					setup_formatted_start_time();
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
index 110983f..e448dd0 100644
--- a/src/backend/utils/misc/postgresql.conf.sample
+++ b/src/backend/utils/misc/postgresql.conf.sample
@@ -425,7 +425,9 @@
 					#   %h = remote host
 					#   %p = process ID
 					#   %t = timestamp without milliseconds
+					#   %T = timestamp without milliseconds (as a number)
 					#   %m = timestamp with milliseconds
+					#   %M = timestamp with milliseconds (as a number)
 					#   %i = command tag
 					#   %e = SQL state
 					#   %c = session ID
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to