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

Commit 95f20b9

Browse files
committed
Rotate on time boundaries that are sensible per local time rather than GMT.
Also, avoid truncating the file we just wrote into, which might otherwise easily happen at DST boundaries. Ed L. and Tom Lane.
1 parent 6da0c43 commit 95f20b9

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

src/backend/postmaster/syslogger.c

Lines changed: 27 additions & 5 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.8 2004/08/31 04:53:44 tgl Exp $
21+
* $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.9 2004/09/21 00:21:25 tgl Exp $
2222
*
2323
*-------------------------------------------------------------------------
2424
*/
@@ -81,6 +81,8 @@ static bool pipe_eof_seen = false;
8181

8282
static FILE *syslogFile = NULL;
8383

84+
static char *last_file_name = NULL;
85+
8486
/* These must be exported for EXEC_BACKEND case ... annoying */
8587
#ifndef WIN32
8688
int syslogPipe[2] = {-1, -1};
@@ -761,7 +763,20 @@ logfile_rotate(bool time_based_rotation)
761763
else
762764
filename = logfile_getname(time(NULL));
763765

764-
if (Log_truncate_on_rotation && time_based_rotation)
766+
/*
767+
* Decide whether to overwrite or append. We can overwrite if (a)
768+
* Log_truncate_on_rotation is set, (b) the rotation was triggered by
769+
* elapsed time and not something else, and (c) the computed file name
770+
* is different from what we were previously logging into.
771+
*
772+
* Note: during the first rotation after forking off from the postmaster,
773+
* last_file_name will be NULL. (We don't bother to set it in the
774+
* postmaster because it ain't gonna work in the EXEC_BACKEND case.)
775+
* So we will always append in that situation, even though truncating
776+
* would usually be safe.
777+
*/
778+
if (Log_truncate_on_rotation && time_based_rotation &&
779+
last_file_name != NULL && strcmp(filename, last_file_name) != 0)
765780
fh = fopen(filename, "w");
766781
else
767782
fh = fopen(filename, "a");
@@ -806,7 +821,10 @@ logfile_rotate(bool time_based_rotation)
806821

807822
set_next_rotation_time();
808823

809-
pfree(filename);
824+
/* instead of pfree'ing filename, remember it for next time */
825+
if (last_file_name != NULL)
826+
pfree(last_file_name);
827+
last_file_name = filename;
810828
}
811829

812830

@@ -854,6 +872,7 @@ static void
854872
set_next_rotation_time(void)
855873
{
856874
pg_time_t now;
875+
struct pg_tm *tm;
857876
int rotinterval;
858877

859878
/* nothing to do if time-based rotation is disabled */
@@ -863,13 +882,16 @@ set_next_rotation_time(void)
863882
/*
864883
* The requirements here are to choose the next time > now that is a
865884
* "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.
885+
* fairly loosely. In this version we align to local time rather than
886+
* GMT.
868887
*/
869888
rotinterval = Log_RotationAge * 60; /* convert to seconds */
870889
now = time(NULL);
890+
tm = pg_localtime(&now);
891+
now += tm->tm_gmtoff;
871892
now -= now % rotinterval;
872893
now += rotinterval;
894+
now -= tm->tm_gmtoff;
873895
next_rotation_time = now;
874896
}
875897

0 commit comments

Comments
 (0)