18
18
*
19
19
*
20
20
* 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 $
22
22
*
23
23
*-------------------------------------------------------------------------
24
24
*/
@@ -81,6 +81,8 @@ static bool pipe_eof_seen = false;
81
81
82
82
static FILE * syslogFile = NULL ;
83
83
84
+ static char * last_file_name = NULL ;
85
+
84
86
/* These must be exported for EXEC_BACKEND case ... annoying */
85
87
#ifndef WIN32
86
88
int syslogPipe [2 ] = {-1 , -1 };
@@ -761,7 +763,20 @@ logfile_rotate(bool time_based_rotation)
761
763
else
762
764
filename = logfile_getname (time (NULL ));
763
765
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 )
765
780
fh = fopen (filename , "w" );
766
781
else
767
782
fh = fopen (filename , "a" );
@@ -806,7 +821,10 @@ logfile_rotate(bool time_based_rotation)
806
821
807
822
set_next_rotation_time ();
808
823
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 ;
810
828
}
811
829
812
830
@@ -854,6 +872,7 @@ static void
854
872
set_next_rotation_time (void )
855
873
{
856
874
pg_time_t now ;
875
+ struct pg_tm * tm ;
857
876
int rotinterval ;
858
877
859
878
/* nothing to do if time-based rotation is disabled */
@@ -863,13 +882,16 @@ set_next_rotation_time(void)
863
882
/*
864
883
* The requirements here are to choose the next time > now that is a
865
884
* "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.
868
887
*/
869
888
rotinterval = Log_RotationAge * 60 ; /* convert to seconds */
870
889
now = time (NULL );
890
+ tm = pg_localtime (& now );
891
+ now += tm -> tm_gmtoff ;
871
892
now -= now % rotinterval ;
872
893
now += rotinterval ;
894
+ now -= tm -> tm_gmtoff ;
873
895
next_rotation_time = now ;
874
896
}
875
897
0 commit comments