diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c
index 5434826..59d8a83 100644
--- a/src/backend/postmaster/syslogger.c
+++ b/src/backend/postmaster/syslogger.c
@@ -1197,7 +1197,7 @@ static void
 logfile_rotate(bool time_based_rotation, int size_rotation_for)
 {
 	char	   *filename;
-	char	   *csvfilename = NULL;
+	char	   *csvfilename;
 	pg_time_t	fntime;
 	FILE	   *fh;
 	bool       rotate_csvlog;
@@ -1214,9 +1214,6 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
 		fntime = next_rotation_time;
 	else
 		fntime = time(NULL);
-	filename = logfile_getname(fntime, NULL);
-	if (csvlogFile != NULL)
-		csvfilename = logfile_getname(fntime, ".csv");
 
 	/*
 	 * Decide whether to overwrite or append.  We can overwrite if (a)
@@ -1230,6 +1227,8 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
 		(size_rotation_for & LOG_DESTINATION_STDERR);
 	if (rotate_stderr)
 	{
+		filename = logfile_getname(fntime, NULL);
+
 		if (Log_truncate_on_rotation && time_based_rotation &&
 			last_file_name != NULL &&
 			strcmp(filename, last_file_name) != 0)
@@ -1252,10 +1251,7 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
 				rotation_disabled = true;
 			}
 
-			if (filename)
-				pfree(filename);
-			if (csvfilename)
-				pfree(csvfilename);
+			pfree(filename);
 			return;
 		}
 
@@ -1266,7 +1262,6 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
 		if (last_file_name != NULL)
 			pfree(last_file_name);
 		last_file_name = filename;
-		filename = NULL;
 	}
 
 	/* Same as above, but for csv file. */
@@ -1274,6 +1269,9 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
 		(time_based_rotation || (size_rotation_for & LOG_DESTINATION_CSVLOG));
 	if (rotate_csvlog)
 	{
+		if (csvlogFile != NULL)
+			csvfilename = logfile_getname(fntime, ".csv");
+
 		if (Log_truncate_on_rotation && time_based_rotation &&
 			last_csv_file_name != NULL &&
 			strcmp(csvfilename, last_csv_file_name) != 0)
@@ -1296,10 +1294,7 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
 				rotation_disabled = true;
 			}
 
-			if (filename)
-				pfree(filename);
-			if (csvfilename)
-				pfree(csvfilename);
+			pfree(csvfilename);
 			return;
 		}
 
@@ -1310,15 +1305,10 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
 		if (last_csv_file_name != NULL)
 			pfree(last_csv_file_name);
 		last_csv_file_name = csvfilename;
-		csvfilename = NULL;
 	}
 
 	if (rotate_stderr || rotate_csvlog)
 		logfile_writename();
-	if (filename)
-		pfree(filename);
-	if (csvfilename)
-		pfree(csvfilename);
 
 	set_next_rotation_time();
 }
