Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbdd6b62

Browse files
committed
Switch over to using the src/timezone functions for formatting timestamps
displayed in the postmaster log. This avoids Windows-specific problems withlocalized time zone names that are in the wrong encoding, and generally seemslike a good idea to forestall other potential platform-dependent issues.To preserve the existing behavior that all backends will log in the same timezone, create a new GUC variable log_timezone that can only be changed on asystem-wide basis, and reference log-related calculations to that zone insteadof the TimeZone variable.This fixes the issue reported by Hiroshi Saito that timestamps printed byxlog.c startup could be improperly localized on Windows. We still need asimpler patch for that problem in the back branches, however.
1 parent73852bd commitbdd6b62

File tree

16 files changed

+275
-148
lines changed

16 files changed

+275
-148
lines changed

‎doc/src/sgml/config.sgml

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.135 2007/08/02 23:39:43 adunstan Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.136 2007/08/04 01:26:53 tgl Exp $ -->
22

33
<chapter Id="runtime-config">
44
<title>Server Configuration</title>
@@ -2311,13 +2311,15 @@ SELECT * FROM parent WHERE key = 2400;
23112311
When <varname>redirect_stderr</varname> is enabled, this parameter
23122312
sets the file names of the created log files. The value
23132313
is treated as a <systemitem>strftime</systemitem> pattern,
2314-
so <literal>%</literal>-escapes
2315-
can be used to specify time-varying file names.
2314+
so <literal>%</literal>-escapes can be used to specify time-varying
2315+
file names. (Note that if there are
2316+
any time-zone-dependent <literal>%</literal>-escapes, the computation
2317+
is done in the zone specified by <xref linkend="guc-log-timezone">.)
23162318
If no <literal>%</literal>-escapes are present,
2317-
<productname>PostgreSQL</productname> will
2318-
append the epoch of the newlog file'sopen time. For example,
2319-
if<varname>log_filename</varname> were <literal>server_log</literal>, then the
2320-
chosen file name would be <literal>server_log.1093827753</literal>
2319+
<productname>PostgreSQL</productname> will append the epoch of the new
2320+
log file'screation time. For example, if
2321+
<varname>log_filename</varname> were <literal>server_log</literal>,
2322+
then thechosen file name would be <literal>server_log.1093827753</>
23212323
for a log starting at Sun Aug 29 19:02:33 2004 MST.
23222324
This parameter can only be set in the <filename>postgresql.conf</>
23232325
file or on the server command line.
@@ -2884,7 +2886,7 @@ SELECT * FROM parent WHERE key = 2400;
28842886
</row>
28852887
<row>
28862888
<entry><literal>%t</literal></entry>
2887-
<entry>Time stamp without milliseconds (no timezone either on Windows)</entry>
2889+
<entry>Time stamp without milliseconds</entry>
28882890
<entry>no</entry>
28892891
</row>
28902892
<row>
@@ -2909,7 +2911,7 @@ SELECT * FROM parent WHERE key = 2400;
29092911
</row>
29102912
<row>
29112913
<entry><literal>%s</literal></entry>
2912-
<entry>Session start time stamp</entry>
2914+
<entry>Process start time stamp</entry>
29132915
<entry>no</entry>
29142916
</row>
29152917
<row>
@@ -2935,7 +2937,7 @@ SELECT * FROM parent WHERE key = 2400;
29352937

29362938
The <literal>%c</> escape prints a quasi-unique session identifier,
29372939
consisting of two 4-byte hexadecimal numbers (without leading zeros)
2938-
separated by a dot. The numbers are thesession start time and the
2940+
separated by a dot. The numbers are theprocess start time and the
29392941
process ID, so <literal>%c</> can also be used as a space saving way
29402942
of printing those items.
29412943
</para>
@@ -3036,6 +3038,25 @@ SELECT * FROM parent WHERE key = 2400;
30363038
</listitem>
30373039
</varlistentry>
30383040

3041+
<varlistentry id="guc-log-timezone" xreflabel="log_timezone">
3042+
<term><varname>log_timezone</varname> (<type>string</type>)</term>
3043+
<indexterm>
3044+
<primary><varname>log_timezone</> configuration parameter</primary>
3045+
</indexterm>
3046+
<listitem>
3047+
<para>
3048+
Sets the time zone used for timestamps written in the log.
3049+
Unlike <xref linkend="guc-timezone">, this value is cluster-wide,
3050+
so that all sessions will report timestamps consistently.
3051+
The default is <literal>unknown</>, which means to use whatever
3052+
the system environment specifies as the time zone. See <xref
3053+
linkend="datatype-timezones"> for more information.
3054+
This parameter can only be set in the <filename>postgresql.conf</>
3055+
file or on the server command line.
3056+
</para>
3057+
</listitem>
3058+
</varlistentry>
3059+
30393060
</variablelist>
30403061
</sect2>
30413062
</sect1>
@@ -3822,9 +3843,9 @@ SET XML OPTION { DOCUMENT | CONTENT };
38223843
<listitem>
38233844
<para>
38243845
Sets the time zone for displaying and interpreting time stamps.
3825-
The default is <literal>'unknown'</>, which means to use whatever
3846+
The default is <literal>unknown</>, which means to use whatever
38263847
the system environment specifies as the time zone. See <xref
3827-
linkend="datatype-datetime"> for more
3848+
linkend="datatype-timezones"> for more
38283849
information.
38293850
</para>
38303851
</listitem>

‎doc/src/sgml/datatype.sgml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/datatype.sgml,v 1.205 2007/07/27 10:37:52 petere Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/datatype.sgml,v 1.206 2007/08/04 01:26:53 tgl Exp $ -->
22

33
<chapter id="datatype">
44
<title id="datatype-title">Data Types</title>
@@ -2234,7 +2234,8 @@ January 8 04:05:06 1999 PST
22342234
savings transition-date rules as well. The recognized abbreviations
22352235
are listed in the <literal>pg_timezone_abbrevs</> view (see <xref
22362236
linkend="view-pg-timezone-abbrevs">). You cannot set the
2237-
configuration parameter <xref linkend="guc-timezone"> using a time
2237+
configuration parameters <xref linkend="guc-timezone"> or
2238+
<xref linkend="guc-log-timezone"> using a time
22382239
zone abbreviation, but you can use abbreviations in
22392240
date/time input values and with the <literal>AT TIME ZONE</>
22402241
operator.
@@ -2316,6 +2317,8 @@ January 8 04:05:06 1999 PST
23162317
behavior of the C library function <literal>localtime()</>. The
23172318
default time zone is selected as the closest match among
23182319
<productname>PostgreSQL</productname>'s known time zones.
2320+
(These rules are also used to choose the default value of
2321+
<xref linkend="guc-log-timezone">, if it is not specified.)
23192322
</para>
23202323
</listitem>
23212324

‎src/backend/access/transam/xlog.c

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.276 2007/08/01 22:45:08 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.277 2007/08/04 01:26:53 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -437,7 +437,7 @@ static void writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI,
437437
uint32endLogId,uint32endLogSeg);
438438
staticvoidWriteControlFile(void);
439439
staticvoidReadControlFile(void);
440-
staticchar*str_time(time_ttnow);
440+
staticchar*str_time(pg_time_ttnow);
441441
staticvoidissue_xlog_fsync(void);
442442

443443
#ifdefWAL_DEBUG
@@ -4266,13 +4266,13 @@ BootStrapXLOG(void)
42664266
}
42674267

42684268
staticchar*
4269-
str_time(time_ttnow)
4269+
str_time(pg_time_ttnow)
42704270
{
42714271
staticcharbuf[128];
42724272

4273-
strftime(buf,sizeof(buf),
4274-
"%Y-%m-%d %H:%M:%S %Z",
4275-
localtime(&tnow));
4273+
pg_strftime(buf,sizeof(buf),
4274+
"%Y-%m-%d %H:%M:%S %Z",
4275+
pg_localtime(&tnow,log_timezone));
42764276

42774277
returnbuf;
42784278
}
@@ -6290,7 +6290,7 @@ pg_start_backup(PG_FUNCTION_ARGS)
62906290
char*backupidstr;
62916291
XLogRecPtrcheckpointloc;
62926292
XLogRecPtrstartpoint;
6293-
time_tstamp_time;
6293+
pg_time_tstamp_time;
62946294
charstrfbuf[128];
62956295
charxlogfilename[MAXFNAMELEN];
62966296
uint32_logId;
@@ -6368,16 +6368,11 @@ pg_start_backup(PG_FUNCTION_ARGS)
63686368
XLByteToSeg(startpoint,_logId,_logSeg);
63696369
XLogFileName(xlogfilename,ThisTimeLineID,_logId,_logSeg);
63706370

6371-
/*
6372-
* We deliberately use strftime/localtime not the src/timezone
6373-
* functions, so that backup labels will consistently be recorded in
6374-
* the same timezone regardless of TimeZone setting. This matches
6375-
* elog.c's practice.
6376-
*/
6377-
stamp_time=time(NULL);
6378-
strftime(strfbuf,sizeof(strfbuf),
6379-
"%Y-%m-%d %H:%M:%S %Z",
6380-
localtime(&stamp_time));
6371+
/* Use the log timezone here, not the session timezone */
6372+
stamp_time= (pg_time_t)time(NULL);
6373+
pg_strftime(strfbuf,sizeof(strfbuf),
6374+
"%Y-%m-%d %H:%M:%S %Z",
6375+
pg_localtime(&stamp_time,log_timezone));
63816376

63826377
/*
63836378
* Check for existing backup label --- implies a backup is already
@@ -6455,7 +6450,7 @@ pg_stop_backup(PG_FUNCTION_ARGS)
64556450
text*result;
64566451
XLogRecPtrstartpoint;
64576452
XLogRecPtrstoppoint;
6458-
time_tstamp_time;
6453+
pg_time_tstamp_time;
64596454
charstrfbuf[128];
64606455
charhistfilepath[MAXPGPATH];
64616456
charstartxlogfilename[MAXFNAMELEN];
@@ -6489,16 +6484,11 @@ pg_stop_backup(PG_FUNCTION_ARGS)
64896484
XLByteToSeg(stoppoint,_logId,_logSeg);
64906485
XLogFileName(stopxlogfilename,ThisTimeLineID,_logId,_logSeg);
64916486

6492-
/*
6493-
* We deliberately use strftime/localtime not the src/timezone functions,
6494-
* so that backup labels will consistently be recorded in the same
6495-
* timezone regardless of TimeZone setting. This matches elog.c's
6496-
* practice.
6497-
*/
6498-
stamp_time=time(NULL);
6499-
strftime(strfbuf,sizeof(strfbuf),
6500-
"%Y-%m-%d %H:%M:%S %Z",
6501-
localtime(&stamp_time));
6487+
/* Use the log timezone here, not the session timezone */
6488+
stamp_time= (pg_time_t)time(NULL);
6489+
pg_strftime(strfbuf,sizeof(strfbuf),
6490+
"%Y-%m-%d %H:%M:%S %Z",
6491+
pg_localtime(&stamp_time,log_timezone));
65026492

65036493
/*
65046494
* Open the existing label file

‎src/backend/commands/variable.c

Lines changed: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/commands/variable.c,v 1.120 2007/01/05 22:19:27 momjian Exp $
12+
* $PostgreSQL: pgsql/src/backend/commands/variable.c,v 1.121 2007/08/04 01:26:53 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -344,7 +344,7 @@ assign_timezone(const char *value, bool doit, GucSource source)
344344
*/
345345
if (doit)
346346
{
347-
constchar*curzone=pg_get_timezone_name(global_timezone);
347+
constchar*curzone=pg_get_timezone_name(session_timezone);
348348

349349
if (curzone)
350350
value=curzone;
@@ -381,7 +381,7 @@ assign_timezone(const char *value, bool doit, GucSource source)
381381
if (doit)
382382
{
383383
/* Save the changed TZ */
384-
global_timezone=new_tz;
384+
session_timezone=new_tz;
385385
HasCTZSet= false;
386386
}
387387
}
@@ -434,7 +434,112 @@ show_timezone(void)
434434
IntervalPGetDatum(&interval)));
435435
}
436436
else
437-
tzn=pg_get_timezone_name(global_timezone);
437+
tzn=pg_get_timezone_name(session_timezone);
438+
439+
if (tzn!=NULL)
440+
returntzn;
441+
442+
return"unknown";
443+
}
444+
445+
446+
/*
447+
* LOG_TIMEZONE
448+
*
449+
* For log_timezone, we don't support the interval-based methods of setting a
450+
* zone, which are only there for SQL spec compliance not because they're
451+
* actually useful.
452+
*/
453+
454+
/*
455+
* assign_log_timezone: GUC assign_hook for log_timezone
456+
*/
457+
constchar*
458+
assign_log_timezone(constchar*value,booldoit,GucSourcesource)
459+
{
460+
char*result;
461+
462+
if (pg_strcasecmp(value,"UNKNOWN")==0)
463+
{
464+
/*
465+
* UNKNOWN is the value shown as the "default" for log_timezone in
466+
* guc.c. We interpret it as being a complete no-op; we don't
467+
* change the timezone setting. Note that if there is a known
468+
* timezone setting, we will return that name rather than UNKNOWN
469+
* as the canonical spelling.
470+
*
471+
* During GUC initialization, since the timezone library isn't set
472+
* up yet, pg_get_timezone_name will return NULL and we will leave
473+
* the setting as UNKNOWN.If this isn't overridden from the
474+
* config file then pg_timezone_initialize() will eventually
475+
* select a default value from the environment.
476+
*/
477+
if (doit)
478+
{
479+
constchar*curzone=pg_get_timezone_name(log_timezone);
480+
481+
if (curzone)
482+
value=curzone;
483+
}
484+
}
485+
else
486+
{
487+
/*
488+
* Otherwise assume it is a timezone name, and try to load it.
489+
*/
490+
pg_tz*new_tz;
491+
492+
new_tz=pg_tzset(value);
493+
494+
if (!new_tz)
495+
{
496+
ereport((source >=PGC_S_INTERACTIVE) ?ERROR :LOG,
497+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
498+
errmsg("unrecognized time zone name: \"%s\"",
499+
value)));
500+
returnNULL;
501+
}
502+
503+
if (!tz_acceptable(new_tz))
504+
{
505+
ereport((source >=PGC_S_INTERACTIVE) ?ERROR :LOG,
506+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
507+
errmsg("time zone \"%s\" appears to use leap seconds",
508+
value),
509+
errdetail("PostgreSQL does not support leap seconds.")));
510+
returnNULL;
511+
}
512+
513+
if (doit)
514+
{
515+
/* Save the changed TZ */
516+
log_timezone=new_tz;
517+
}
518+
}
519+
520+
/*
521+
* If we aren't going to do the assignment, just return OK indicator.
522+
*/
523+
if (!doit)
524+
returnvalue;
525+
526+
/*
527+
* Prepare the canonical string to return.GUC wants it malloc'd.
528+
*/
529+
result=strdup(value);
530+
531+
returnresult;
532+
}
533+
534+
/*
535+
* show_log_timezone: GUC show_hook for log_timezone
536+
*/
537+
constchar*
538+
show_log_timezone(void)
539+
{
540+
constchar*tzn;
541+
542+
tzn=pg_get_timezone_name(log_timezone);
438543

439544
if (tzn!=NULL)
440545
returntzn;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp