@@ -146,6 +146,7 @@ static char *logfile_getname(pg_time_t timestamp, const char *suffix);
146
146
static void set_next_rotation_time (void );
147
147
static void sigHupHandler (SIGNAL_ARGS );
148
148
static void sigUsr1Handler (SIGNAL_ARGS );
149
+ static void update_metainfo_datafile (void );
149
150
150
151
151
152
/*
@@ -282,6 +283,7 @@ SysLoggerMain(int argc, char *argv[])
282
283
currentLogRotationAge = Log_RotationAge ;
283
284
/* set next planned rotation time */
284
285
set_next_rotation_time ();
286
+ update_metainfo_datafile ();
285
287
286
288
/* main worker loop */
287
289
for (;;)
@@ -348,6 +350,13 @@ SysLoggerMain(int argc, char *argv[])
348
350
rotation_disabled = false;
349
351
rotation_requested = true;
350
352
}
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 ();
351
360
}
352
361
353
362
if (Log_RotationAge > 0 && !rotation_disabled )
@@ -1098,6 +1107,8 @@ open_csvlogfile(void)
1098
1107
pfree (last_csv_file_name );
1099
1108
1100
1109
last_csv_file_name = filename ;
1110
+
1111
+ update_metainfo_datafile ();
1101
1112
}
1102
1113
1103
1114
/*
@@ -1268,6 +1279,8 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
1268
1279
if (csvfilename )
1269
1280
pfree (csvfilename );
1270
1281
1282
+ update_metainfo_datafile ();
1283
+
1271
1284
set_next_rotation_time ();
1272
1285
}
1273
1286
@@ -1337,6 +1350,72 @@ set_next_rotation_time(void)
1337
1350
next_rotation_time = now ;
1338
1351
}
1339
1352
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
+
1340
1419
/* --------------------------------
1341
1420
*signal handler routines
1342
1421
* --------------------------------