@@ -146,6 +146,7 @@ static char *logfile_getname(pg_time_t timestamp, const char *suffix);
146146static void set_next_rotation_time (void );
147147static void sigHupHandler (SIGNAL_ARGS );
148148static void sigUsr1Handler (SIGNAL_ARGS );
149+ static void update_metainfo_datafile (void );
149150
150151
151152/*
@@ -282,6 +283,7 @@ SysLoggerMain(int argc, char *argv[])
282283 currentLogRotationAge = Log_RotationAge ;
283284 /* set next planned rotation time */
284285 set_next_rotation_time ();
286+ update_metainfo_datafile ();
285287
286288 /* main worker loop */
287289 for (;;)
@@ -348,6 +350,13 @@ SysLoggerMain(int argc, char *argv[])
348350 rotation_disabled = false;
349351 rotation_requested = true;
350352 }
353+
354+ /*
355+ * Force rewriting last log filename when reloading configuration.
356+ * Even if rotation_requested is false, log_destination may have
357+ * been changed and we don't want to wait the next file rotation.
358+ */
359+ update_metainfo_datafile ();
351360 }
352361
353362 if (Log_RotationAge > 0 && !rotation_disabled )
@@ -1098,6 +1107,8 @@ open_csvlogfile(void)
10981107 pfree (last_csv_file_name );
10991108
11001109 last_csv_file_name = filename ;
1110+
1111+ update_metainfo_datafile ();
11011112}
11021113
11031114/*
@@ -1268,6 +1279,8 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
12681279 if (csvfilename )
12691280 pfree (csvfilename );
12701281
1282+ update_metainfo_datafile ();
1283+
12711284 set_next_rotation_time ();
12721285}
12731286
@@ -1337,6 +1350,72 @@ set_next_rotation_time(void)
13371350 next_rotation_time = now ;
13381351}
13391352
1353+ /*
1354+ * Store the name of the file(s) where the log collector, when enabled, writes
1355+ * log messages. Useful for finding the name(s) of the current log file(s)
1356+ * when there is time-based logfile rotation. Filenames are stored in a
1357+ * temporary file and which is renamed into the final destination for
1358+ * atomicity.
1359+ */
1360+ static void
1361+ update_metainfo_datafile (void )
1362+ {
1363+ FILE * fh ;
1364+
1365+ if (!(Log_destination & LOG_DESTINATION_STDERR ) &&
1366+ !(Log_destination & LOG_DESTINATION_CSVLOG ))
1367+ {
1368+ if (unlink (LOG_METAINFO_DATAFILE ) < 0 && errno != ENOENT )
1369+ ereport (LOG ,
1370+ (errcode_for_file_access (),
1371+ errmsg ("could not remove file \"%s\": %m" ,
1372+ LOG_METAINFO_DATAFILE )));
1373+ return ;
1374+ }
1375+
1376+ if ((fh = logfile_open (LOG_METAINFO_DATAFILE_TMP , "w" , true)) == NULL )
1377+ {
1378+ ereport (LOG ,
1379+ (errcode_for_file_access (),
1380+ errmsg ("could not open file \"%s\": %m" ,
1381+ LOG_METAINFO_DATAFILE_TMP )));
1382+ return ;
1383+ }
1384+
1385+ if (last_file_name && (Log_destination & LOG_DESTINATION_STDERR ))
1386+ {
1387+ if (fprintf (fh , "stderr %s\n" , last_file_name ) < 0 )
1388+ {
1389+ ereport (LOG ,
1390+ (errcode_for_file_access (),
1391+ errmsg ("could not write file \"%s\": %m" ,
1392+ LOG_METAINFO_DATAFILE_TMP )));
1393+ fclose (fh );
1394+ return ;
1395+ }
1396+ }
1397+
1398+ if (last_csv_file_name && (Log_destination & LOG_DESTINATION_CSVLOG ))
1399+ {
1400+ if (fprintf (fh , "csvlog %s\n" , last_csv_file_name ) < 0 )
1401+ {
1402+ ereport (LOG ,
1403+ (errcode_for_file_access (),
1404+ errmsg ("could not write file \"%s\": %m" ,
1405+ LOG_METAINFO_DATAFILE_TMP )));
1406+ fclose (fh );
1407+ return ;
1408+ }
1409+ }
1410+ fclose (fh );
1411+
1412+ if (rename (LOG_METAINFO_DATAFILE_TMP , LOG_METAINFO_DATAFILE ) != 0 )
1413+ ereport (LOG ,
1414+ (errcode_for_file_access (),
1415+ errmsg ("could not rename file \"%s\" to \"%s\": %m" ,
1416+ LOG_METAINFO_DATAFILE_TMP , LOG_METAINFO_DATAFILE )));
1417+ }
1418+
13401419/* --------------------------------
13411420 * signal handler routines
13421421 * --------------------------------
0 commit comments