Hi everyone!

A situation has been discovered in which empty .log files are being created.

--Reproduce:

1) You need to set the following parameters in the "postgresql.conf" file:

log_destination = 'csvlog'
logging_collector = on
log_rotation_age = 1min
log_directory = '/home/user/builds/postgresql/log'

2) After starting the server, two files are being created in the directory with logs:

-rw------- 1 user user  1,4K oct 10 13:29 postgresql-2024-10-10_132907.csv
-rw------- 1 user user  172 oct 10 13:29 postgresql-2024-10-10_132907.log

3) The .log file is created anyway, regardless of the log_destination parameter. The following message is displayed in the .log file (due to the fact that the log_destination
parameter is set = 'csvlog'):
|
|2024-10-10 13:05:52.539 MSK [1629065] LOG:  ending log output to stderr
2024-10-10 13:05:52.539 MSK [1629065] HINT:  Future log output will go to log destination "csvlog".

Next, the stderr stream is redirected to a .csv file.

4) Now, logs are rotated once a minute (due to the set log_rotation_age parameter). Moreover, all open log files are rotated, including the .log file that I wrote about above. New ones are being created .csv and .log files. Logs themselves are sent to .csv, and nothing else is output to .log
and it remains empty:

-rw------- 1 user user 1,4K oct 10 13:29 postgresql-2024-10-10_132907.csv
-rw------- 1 user user  172 oct 10 13:29 postgresql-2024-10-10_132907.log
-rw------- 1 user user 1,4K oct 10 13:30 postgresql-2024-10-10_133000.csv
-rw------- 1 user user    0 oct 10 13:30 postgresql-2024-10-10_133000.log

--Fix:

To correct the situation, you can add a check for the "Log_destination" parameter in the "logfile_rotate()" function of the "syslogger.c" module. Then only the logs specified in this parameter will be rotated.Thisis doneinthe proposedpatch.


Best regards,
Arseny Kositsin.
From 3b33349ada0ab50c334a2801a2ba41d4ba3d77e7 Mon Sep 17 00:00:00 2001
From: Arseny Kositsin <arseny.p...@gmail.com>
Date: Mon, 28 Oct 2024 18:08:38 +0300
Subject: [PATCH v1] Fixed creation of an empty .log file

When logs are rotated, if the log_destination = 'csvlog', besides .csv
file, an empty .log file was created. In order to correct this behavior,
an additional check has been added to the log file_rotate() function.
---
 src/backend/postmaster/syslogger.c | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c
index 7951599fa8..cd29e863ee 100644
--- a/src/backend/postmaster/syslogger.c
+++ b/src/backend/postmaster/syslogger.c
@@ -1374,22 +1374,25 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
 		fntime = time(NULL);
 
 	/* file rotation for stderr */
-	if (!logfile_rotate_dest(time_based_rotation, size_rotation_for, fntime,
-							 LOG_DESTINATION_STDERR, &last_sys_file_name,
-							 &syslogFile))
-		return;
+	if ((Log_destination & LOG_DESTINATION_STDERR) != 0)
+		if (!logfile_rotate_dest(time_based_rotation, size_rotation_for, fntime,
+								 LOG_DESTINATION_STDERR, &last_sys_file_name,
+								 &syslogFile))
+			return;
 
 	/* file rotation for csvlog */
-	if (!logfile_rotate_dest(time_based_rotation, size_rotation_for, fntime,
-							 LOG_DESTINATION_CSVLOG, &last_csv_file_name,
-							 &csvlogFile))
-		return;
+	if ((Log_destination & LOG_DESTINATION_CSVLOG) != 0)
+		if (!logfile_rotate_dest(time_based_rotation, size_rotation_for, fntime,
+								 LOG_DESTINATION_CSVLOG, &last_csv_file_name,
+								 &csvlogFile))
+			return;
 
 	/* file rotation for jsonlog */
-	if (!logfile_rotate_dest(time_based_rotation, size_rotation_for, fntime,
-							 LOG_DESTINATION_JSONLOG, &last_json_file_name,
-							 &jsonlogFile))
-		return;
+	if ((Log_destination & LOG_DESTINATION_JSONLOG) != 0)
+		if (!logfile_rotate_dest(time_based_rotation, size_rotation_for, fntime,
+								 LOG_DESTINATION_JSONLOG, &last_json_file_name,
+								 &jsonlogFile))
+			return;
 
 	update_metainfo_datafile();
 
-- 
2.34.1

Reply via email to