diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index 309378ae54..873ad57b79 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -4955,24 +4955,21 @@ pgstat_write_statsfiles(bool permanent, bool allDbs)
 				(errcode_for_file_access(),
 				 errmsg("could not write temporary statistics file \"%s\": %m",
 						tmpfile)));
-		FreeFile(fpout);
-		unlink(tmpfile);
 	}
-	else if (FreeFile(fpout) < 0)
+	if (FreeFile(fpout) < 0)
 	{
 		ereport(LOG,
 				(errcode_for_file_access(),
 				 errmsg("could not close temporary statistics file \"%s\": %m",
 						tmpfile)));
-		unlink(tmpfile);
 	}
-	else if (rename(tmpfile, statfile) < 0)
+	if (rename(tmpfile, statfile) < 0)
 	{
 		ereport(LOG,
 				(errcode_for_file_access(),
 				 errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
 						tmpfile, statfile)));
-		unlink(tmpfile);
+	    unlink(tmpfile);
 	}
 
 	if (permanent)
@@ -5090,25 +5087,22 @@ pgstat_write_db_statsfile(PgStat_StatDBEntry *dbentry, bool permanent)
 				(errcode_for_file_access(),
 				 errmsg("could not write temporary statistics file \"%s\": %m",
 						tmpfile)));
-		FreeFile(fpout);
-		unlink(tmpfile);
 	}
-	else if (FreeFile(fpout) < 0)
+	if (FreeFile(fpout) < 0)
 	{
 		ereport(LOG,
 				(errcode_for_file_access(),
 				 errmsg("could not close temporary statistics file \"%s\": %m",
 						tmpfile)));
-		unlink(tmpfile);
 	}
-	else if (rename(tmpfile, statfile) < 0)
+	if (rename(tmpfile, statfile) < 0)
 	{
 		ereport(LOG,
 				(errcode_for_file_access(),
 				 errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
 						tmpfile, statfile)));
-		unlink(tmpfile);
-	}
+	    unlink(tmpfile);
+	} 
 
 	if (permanent)
 	{