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

Commit8c603f2

Browse files
committed
Replace log_filename_prefix with more general log_filename parameter,
to allow DBA to choose the form in which log filenames reflect thecurrent time. Also allow for truncating instead of appending topre-existing files --- this is convenient when the log filename patternrewrites the same names cyclically. Per Ed L.
1 parent25aba1c commit8c603f2

File tree

5 files changed

+166
-67
lines changed

5 files changed

+166
-67
lines changed

‎doc/src/sgml/runtime.sgml

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/runtime.sgml,v 1.279 2004/08/24 00:06:50 neilc Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/runtime.sgml,v 1.280 2004/08/31 04:53:43 tgl Exp $
33
-->
44

55
<Chapter Id="runtime">
@@ -1925,14 +1925,21 @@ archive_command = 'copy "%p" /mnt/server/archivedir/"%f"' # Win32
19251925
</listitem>
19261926
</varlistentry>
19271927

1928-
<varlistentry id="guc-log-filename-prefix" xreflabel="log_filename_prefix">
1929-
<term><varname>log_filename_prefix</varname> (<type>string</type>)</term>
1928+
<varlistentry id="guc-log-filename" xreflabel="log_filename">
1929+
<term><varname>log_filename</varname> (<type>string</type>)</term>
19301930
<listitem>
19311931
<para>
19321932
When <varname>redirect_stderr</> is enabled, this option
1933-
sets the prefix of the file names of the created log files.
1934-
The postmaster PID and the current time are appended to this
1935-
prefix to form an exact log file name.
1933+
sets the file names of the created log files. The value
1934+
is treated as a <systemitem>strftime</> pattern,
1935+
so <literal>%</>-escapes
1936+
can be used to specify time-varying file names.
1937+
If no <literal>%</>-escapes are present,
1938+
<productname>PostgreSQL</productname> will
1939+
append the epoch of the new log file's open time. For example,
1940+
if <varname>log_filename</> were <literal>server_log</>, then the
1941+
chosen file name would be <literal>server_log.1093827753</>
1942+
for a log starting at Sun Aug 29 19:02:33 2004 MST.
19361943
This option can only be set at server start or in the
19371944
<filename>postgresql.conf</filename> configuration file.
19381945
</para>
@@ -1969,6 +1976,26 @@ archive_command = 'copy "%p" /mnt/server/archivedir/"%f"' # Win32
19691976
</listitem>
19701977
</varlistentry>
19711978

1979+
<varlistentry id="guc-log-truncate-on-rotation" xreflabel="log_truncate_on_rotation">
1980+
<term><varname>log_truncate_on_rotation</varname> (<type>boolean</type>)</term>
1981+
<listitem>
1982+
<para>
1983+
When <varname>redirect_stderr</> is enabled, this option will cause
1984+
<productname>PostgreSQL</productname> to truncate (overwrite),
1985+
rather than append to, any existing log file of the same name.
1986+
However, truncation will occur only when a new file is being opened
1987+
due to time-based rotation, not during server startup or size-based
1988+
rotation. When false, pre-existing files will be appended to in
1989+
all cases. For example, using this option in combination with
1990+
a <varname>log_filename</> like <literal>postgresql-%H.log</>
1991+
would result in generating twenty-four hourly log files and then
1992+
cyclically overwriting them.
1993+
This option can only be set at server start or in the
1994+
<filename>postgresql.conf</filename> configuration file.
1995+
</para>
1996+
</listitem>
1997+
</varlistentry>
1998+
19721999
<varlistentry id="guc-syslog-facility" xreflabel="syslog_facility">
19732000
<term><varname>syslog_facility</varname> (<type>string</type>)</term>
19742001
<listitem>

‎src/backend/postmaster/syslogger.c

Lines changed: 106 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
*
2020
* IDENTIFICATION
21-
* $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.7 2004/08/29 05:06:46 momjian Exp $
21+
* $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.8 2004/08/31 04:53:44 tgl Exp $
2222
*
2323
*-------------------------------------------------------------------------
2424
*/
@@ -61,8 +61,9 @@
6161
boolRedirect_stderr= false;
6262
intLog_RotationAge=24*60;
6363
intLog_RotationSize=10*1024;
64-
char*Log_directory="pg_log";
65-
char*Log_filename_prefix="postgresql-";
64+
char*Log_directory=NULL;
65+
char*Log_filename=NULL;
66+
boolLog_truncate_on_rotation= false;
6667

6768
/*
6869
* Globally visible state (used by elog.c)
@@ -72,7 +73,7 @@ boolam_syslogger = false;
7273
/*
7374
* Private state
7475
*/
75-
staticpg_time_tlast_rotation_time=0;
76+
staticpg_time_tnext_rotation_time;
7677

7778
staticboolredirection_done= false;
7879

@@ -109,8 +110,9 @@ static void write_syslogger_file_binary(const char *buffer, int count);
109110
#ifdefWIN32
110111
staticunsignedint __stdcallpipeThread(void*arg);
111112
#endif
112-
staticvoidlogfile_rotate(void);
113+
staticvoidlogfile_rotate(booltime_based_rotation);
113114
staticchar*logfile_getname(pg_time_ttimestamp);
115+
staticvoidset_next_rotation_time(void);
114116
staticvoidsigHupHandler(SIGNAL_ARGS);
115117

116118

@@ -121,7 +123,9 @@ static void sigHupHandler(SIGNAL_ARGS);
121123
NON_EXEC_STATICvoid
122124
SysLoggerMain(intargc,char*argv[])
123125
{
124-
charcurrentLogDir[MAXPGPATH];
126+
char*currentLogDir;
127+
char*currentLogFilename;
128+
intcurrentLogRotationAge;
125129

126130
IsUnderPostmaster= true;/* we are a postmaster subprocess now */
127131

@@ -218,15 +222,18 @@ SysLoggerMain(int argc, char *argv[])
218222
}
219223
#endif/* WIN32 */
220224

221-
/* remember age of initial logfile */
222-
last_rotation_time=time(NULL);
223-
/* remember active logfile directory */
224-
strncpy(currentLogDir,Log_directory,MAXPGPATH);
225+
/* remember active logfile parameters */
226+
currentLogDir=pstrdup(Log_directory);
227+
currentLogFilename=pstrdup(Log_filename);
228+
currentLogRotationAge=Log_RotationAge;
229+
/* set next planned rotation time */
230+
set_next_rotation_time();
225231

226232
/* main worker loop */
227233
for (;;)
228234
{
229235
boolrotation_requested= false;
236+
booltime_based_rotation= false;
230237

231238
#ifndefWIN32
232239
charlogbuffer[1024];
@@ -242,46 +249,51 @@ SysLoggerMain(int argc, char *argv[])
242249
ProcessConfigFile(PGC_SIGHUP);
243250

244251
/*
245-
* Check if the log directory changed in postgresql.conf. If
246-
* so, force rotation to make sure we're writing the logfiles
247-
* in the right place.
248-
*
249-
* XXX is it worth responding similarly to a change of
250-
* Log_filename_prefix?
252+
* Check if the log directory or filename pattern changed in
253+
* postgresql.conf. If so, force rotation to make sure we're
254+
* writing the logfiles in the right place.
251255
*/
252-
if (strncmp(Log_directory,currentLogDir,MAXPGPATH)!=0)
256+
if (strcmp(Log_directory,currentLogDir)!=0)
253257
{
254-
strncpy(currentLogDir,Log_directory,MAXPGPATH);
258+
pfree(currentLogDir);
259+
currentLogDir=pstrdup(Log_directory);
255260
rotation_requested= true;
256261
}
262+
if (strcmp(Log_filename,currentLogFilename)!=0)
263+
{
264+
pfree(currentLogFilename);
265+
currentLogFilename=pstrdup(Log_filename);
266+
rotation_requested= true;
267+
}
268+
/*
269+
* If rotation time parameter changed, reset next rotation time,
270+
* but don't immediately force a rotation.
271+
*/
272+
if (currentLogRotationAge!=Log_RotationAge)
273+
{
274+
currentLogRotationAge=Log_RotationAge;
275+
set_next_rotation_time();
276+
}
257277
}
258278

259-
if (!rotation_requested&&
260-
last_rotation_time!=0&&
261-
Log_RotationAge>0)
279+
if (!rotation_requested&&Log_RotationAge>0)
262280
{
263-
/*
264-
* Do a logfile rotation if too much time has elapsed since
265-
* the last one.
266-
*/
281+
/* Do a logfile rotation if it's time */
267282
pg_time_tnow=time(NULL);
268-
intelapsed_secs=now-last_rotation_time;
269283

270-
if (elapsed_secs >=Log_RotationAge*60)
271-
rotation_requested= true;
284+
if (now >=next_rotation_time)
285+
rotation_requested=time_based_rotation=true;
272286
}
273287

274288
if (!rotation_requested&&Log_RotationSize>0)
275289
{
276-
/*
277-
* Do a rotation if file is too big
278-
*/
290+
/* Do a rotation if file is too big */
279291
if (ftell(syslogFile) >=Log_RotationSize*1024L)
280292
rotation_requested= true;
281293
}
282294

283295
if (rotation_requested)
284-
logfile_rotate();
296+
logfile_rotate(time_based_rotation);
285297

286298
#ifndefWIN32
287299

@@ -365,7 +377,6 @@ int
365377
SysLogger_Start(void)
366378
{
367379
pid_tsysloggerPid;
368-
pg_time_tnow;
369380
char*filename;
370381

371382
if (!Redirect_stderr)
@@ -424,8 +435,7 @@ SysLogger_Start(void)
424435
* The initial logfile is created right in the postmaster, to verify
425436
* that the Log_directory is writable.
426437
*/
427-
now=time(NULL);
428-
filename=logfile_getname(now);
438+
filename=logfile_getname(time(NULL));
429439

430440
syslogFile=fopen(filename,"a");
431441

@@ -736,16 +746,26 @@ pipeThread(void *arg)
736746
* perform logfile rotation
737747
*/
738748
staticvoid
739-
logfile_rotate(void)
749+
logfile_rotate(booltime_based_rotation)
740750
{
741751
char*filename;
742-
pg_time_tnow;
743752
FILE*fh;
744753

745-
now=time(NULL);
746-
filename=logfile_getname(now);
754+
/*
755+
* When doing a time-based rotation, invent the new logfile name based
756+
* on the planned rotation time, not current time, to avoid "slippage"
757+
* in the file name when we don't do the rotation immediately.
758+
*/
759+
if (time_based_rotation)
760+
filename=logfile_getname(next_rotation_time);
761+
else
762+
filename=logfile_getname(time(NULL));
763+
764+
if (Log_truncate_on_rotation&&time_based_rotation)
765+
fh=fopen(filename,"w");
766+
else
767+
fh=fopen(filename,"a");
747768

748-
fh=fopen(filename,"a");
749769
if (!fh)
750770
{
751771
intsaveerrno=errno;
@@ -784,7 +804,7 @@ logfile_rotate(void)
784804
LeaveCriticalSection(&sysfileSection);
785805
#endif
786806

787-
last_rotation_time=now;
807+
set_next_rotation_time();
788808

789809
pfree(filename);
790810
}
@@ -799,25 +819,60 @@ static char *
799819
logfile_getname(pg_time_ttimestamp)
800820
{
801821
char*filename;
802-
charstamptext[128];
803-
804-
pg_strftime(stamptext,sizeof(stamptext),"%Y-%m-%d_%H%M%S",
805-
pg_localtime(&timestamp));
822+
intlen;
823+
structpg_tm*tm;
806824

807825
filename=palloc(MAXPGPATH);
808826

809827
if (is_absolute_path(Log_directory))
810-
snprintf(filename,MAXPGPATH,"%s/%s%05u_%s.log",
811-
Log_directory,Log_filename_prefix,
812-
(unsignedint)PostmasterPid,stamptext);
828+
snprintf(filename,MAXPGPATH,"%s/",Log_directory);
813829
else
814-
snprintf(filename,MAXPGPATH,"%s/%s/%s%05u_%s.log",
815-
DataDir,Log_directory,Log_filename_prefix,
816-
(unsignedint)PostmasterPid,stamptext);
830+
snprintf(filename,MAXPGPATH,"%s/%s/",DataDir,Log_directory);
831+
832+
len=strlen(filename);
833+
834+
if (strchr(Log_filename,'%'))
835+
{
836+
/* treat it as a strftime pattern */
837+
tm=pg_localtime(&timestamp);
838+
pg_strftime(filename+len,MAXPGPATH-len,Log_filename,tm);
839+
}
840+
else
841+
{
842+
/* no strftime escapes, so append timestamp to new filename */
843+
snprintf(filename+len,MAXPGPATH-len,"%s.%lu",
844+
Log_filename, (unsigned long)timestamp);
845+
}
817846

818847
returnfilename;
819848
}
820849

850+
/*
851+
* Determine the next planned rotation time, and store in next_rotation_time.
852+
*/
853+
staticvoid
854+
set_next_rotation_time(void)
855+
{
856+
pg_time_tnow;
857+
introtinterval;
858+
859+
/* nothing to do if time-based rotation is disabled */
860+
if (Log_RotationAge <=0)
861+
return;
862+
863+
/*
864+
* The requirements here are to choose the next time > now that is a
865+
* "multiple" of the log rotation interval. "Multiple" can be interpreted
866+
* fairly loosely --- in particular, for intervals larger than an hour,
867+
* it might be interesting to align to local time instead of GMT.
868+
*/
869+
rotinterval=Log_RotationAge*60;/* convert to seconds */
870+
now=time(NULL);
871+
now-=now %rotinterval;
872+
now+=rotinterval;
873+
next_rotation_time=now;
874+
}
875+
821876
/* --------------------------------
822877
*signal handler routines
823878
* --------------------------------

‎src/backend/utils/misc/guc.c

Lines changed: 14 additions & 6 deletions
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-
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.235 2004/08/30 02:54:40 momjian Exp $
13+
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.236 2004/08/31 04:53:44 tgl Exp $
1414
*
1515
*--------------------------------------------------------------------
1616
*/
@@ -812,6 +812,14 @@ static struct config_bool ConfigureNamesBool[] =
812812
&Redirect_stderr,
813813
false,NULL,NULL
814814
},
815+
{
816+
{"log_truncate_on_rotation",PGC_SIGHUP,LOGGING_WHERE,
817+
gettext_noop("Truncate existing log files of same name during log rotation"),
818+
NULL
819+
},
820+
&Log_truncate_on_rotation,
821+
false,NULL,NULL
822+
},
815823

816824
#ifdefWAL_DEBUG
817825
{
@@ -1665,20 +1673,20 @@ static struct config_string ConfigureNamesString[] =
16651673
},
16661674
{
16671675
{"log_directory",PGC_SIGHUP,LOGGING_WHERE,
1668-
gettext_noop("Sets the destination directory forlogfiles."),
1676+
gettext_noop("Sets the destination directory forlog files."),
16691677
gettext_noop("May be specified as relative to the cluster directory "
16701678
"or as absolute path.")
16711679
},
16721680
&Log_directory,
16731681
"pg_log",NULL,NULL
16741682
},
16751683
{
1676-
{"log_filename_prefix",PGC_SIGHUP,LOGGING_WHERE,
1677-
gettext_noop("Prefix for filenames created in the log_directory."),
1684+
{"log_filename",PGC_SIGHUP,LOGGING_WHERE,
1685+
gettext_noop("Sets the filename pattern for log files."),
16781686
NULL
16791687
},
1680-
&Log_filename_prefix,
1681-
"postgresql-",NULL,NULL
1688+
&Log_filename,
1689+
"postgresql-%Y-%m-%d_%H%M%S.log",NULL,NULL
16821690
},
16831691

16841692
#ifdefHAVE_SYSLOG

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp