From 0119274e7a05da4e31b1f7cfeccd14b90363829d Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Mon, 26 Jul 2021 10:10:21 -0500
Subject: [PATCH 2/2] justins changes

---
 src/backend/access/transam/xlog.c | 83 ++++++++++---------------------
 src/backend/storage/file/fd.c     | 12 ++---
 src/backend/storage/file/reinit.c |  9 ++--
 src/backend/utils/misc/guc.c      |  1 +
 src/include/access/xlog.h         |  7 ++-
 5 files changed, 40 insertions(+), 72 deletions(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 4f052050f3..6b6619a143 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -7378,7 +7378,7 @@ StartupXLOG(void)
 			{
 				bool		switchedTLI = false;
 
-				LogStartupProgress(STARTUP_PROCESS_OP_REDO, NULL);
+				LogStartupProgress(STARTUP_PROCESS_OP_REDO, NULL, false);
 
 #ifdef WAL_DEBUG
 				if (XLOG_DEBUG ||
@@ -13073,15 +13073,18 @@ InitStartupProgress(void)
 }
 
 /*
- * Logs the progress of the operations performed during startup process.
+ * Logs the status of operations performed during startup process.
  */
 void
-LogStartupProgress(StartupProcessOp operation, const char *path)
+LogStartupProgress(StartupProcessOp operation, const char *path, bool done)
 {
 	long        secs;
 	int         usecs;
 	int			elapsed_ms;
 	int			interval_in_ms;
+	int			swval = done ? -operation : operation;
+
+	Assert(operation != 0);
 
 	/* If not called from startup process then this feature is of no use. */
 	if (!AmStartupProcess())
@@ -13092,7 +13095,7 @@ LogStartupProgress(StartupProcessOp operation, const char *path)
 		return;
 
 	/* If the timeout has not occurred, then no need to log the details. */
-	if (!logStartupProgressTimeout)
+	if (!logStartupProgressTimeout && !done)
 		return;
 
 	/* Timeout has occurred. */
@@ -13100,16 +13103,7 @@ LogStartupProgress(StartupProcessOp operation, const char *path)
 						GetCurrentTimestamp(),
 						&secs, &usecs);
 
-	/*
-	 * Enable the timer for the next log message based on the time that current
-	 * log message timer was supposed to fire.
-	 */
-	elapsed_ms = (secs * 1000) + (usecs / 1000);
-	interval_in_ms = log_startup_progress_interval * 1000;
-	enable_timeout_after(LOG_STARTUP_PROGRESS_TIMEOUT,
-						 (interval_in_ms - (elapsed_ms % interval_in_ms)));
-
-	switch(operation)
+	switch (swval)
 	{
 		case STARTUP_PROCESS_OP_SYNCFS:
 			ereport(LOG,
@@ -13136,66 +13130,41 @@ LogStartupProgress(StartupProcessOp operation, const char *path)
 					(errmsg("resetting unlogged relations (cleanup), elapsed time: %ld.%02d s, current path: %s",
 							secs, (usecs / 10000), path)));
 			break;
-		default:
-			ereport(ERROR,
-					(errmsg("unrecognized operation (%d) in startup progress update",
-							operation)));
-			break;
-	}
-
-	logStartupProgressTimeout = false;
-}
-
-/*
- * Logs the completion of the operation performed during startup process and
- * disables the timeout used for logging the progress.
- */
-void
-LogStartupProgressComplete(StartupProcessOp operation)
-{
-	long        secs;
-	int         usecs;
-
-	/* If not called from startup process then this feature is of no use. */
-	if (!AmStartupProcess())
-		return;
 
-	/* If the feature is disabled, then no need to proceed further. */
-	if (log_startup_progress_interval < 0)
-		return;
-
-	TimestampDifference(startupProcessOpStartTime,
-						GetCurrentTimestamp(),
-						&secs, &usecs);
-
-	switch(operation)
-	{
-		case STARTUP_PROCESS_OP_SYNCFS:
+		case -STARTUP_PROCESS_OP_SYNCFS:
 			ereport(LOG,
 					(errmsg("data directory sync (syncfs) complete after %ld.%02d s",
 							secs, (usecs / 10000))));
 			break;
-		case STARTUP_PROCESS_OP_FSYNC:
+		case -STARTUP_PROCESS_OP_FSYNC:
 			ereport(LOG,
 					(errmsg("data directory sync (fsync) complete after %ld.%02d s",
 							secs, (usecs / 10000))));
 			break;
-		case STARTUP_PROCESS_OP_RESET_UNLOGGED_REL_INIT:
+		case -STARTUP_PROCESS_OP_RESET_UNLOGGED_REL_INIT:
 			ereport(LOG,
 					(errmsg("resetting unlogged relations (init) complete after %ld.%02d s",
 							secs, (usecs / 10000))));
 			break;
-		case STARTUP_PROCESS_OP_RESET_UNLOGGED_REL_CLEANUP:
+		case -STARTUP_PROCESS_OP_RESET_UNLOGGED_REL_CLEANUP:
 			ereport(LOG,
 					(errmsg("resetting unlogged relations (cleanup) complete after %ld.%02d s",
 							secs, (usecs / 10000))));
 			break;
-		default:
-			ereport(ERROR,
-					(errmsg("unrecognized operation (%d) in startup progress update",
-							operation)));
-			break;
 	}
 
-	disable_timeout(LOG_STARTUP_PROGRESS_TIMEOUT, false);
+	if (!done)
+	{
+		/*
+		 * Enable the timer for the next log message based on the time that
+		 * the current log message timer was supposed to fire.
+		 */
+		logStartupProgressTimeout = false;
+		elapsed_ms = (secs * 1000) + (usecs / 1000);
+		interval_in_ms = log_startup_progress_interval * 1000;
+		enable_timeout_after(LOG_STARTUP_PROGRESS_TIMEOUT,
+							 (interval_in_ms - (elapsed_ms % interval_in_ms)));
+	}
+	else
+		disable_timeout(LOG_STARTUP_PROGRESS_TIMEOUT, false);
 }
diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index 49ecbbd3b9..65e7de200e 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -3335,7 +3335,7 @@ do_syncfs(const char *path)
 {
 	int			fd;
 
-	LogStartupProgress(STARTUP_PROCESS_OP_SYNCFS, path);
+	LogStartupProgress(STARTUP_PROCESS_OP_SYNCFS, path, false);
 
 	fd = OpenTransientFile(path, O_RDONLY);
 	if (fd < 0)
@@ -3442,7 +3442,7 @@ SyncDataDirectory(void)
 		if (xlog_is_symlink)
 			do_syncfs("pg_wal");
 
-		LogStartupProgressComplete(STARTUP_PROCESS_OP_SYNCFS);
+		LogStartupProgress(STARTUP_PROCESS_OP_SYNCFS, NULL, true);
 		return;
 	}
 #endif							/* !HAVE_SYNCFS */
@@ -3475,7 +3475,7 @@ SyncDataDirectory(void)
 		walkdir("pg_wal", datadir_fsync_fname, false, LOG);
 	walkdir("pg_tblspc", datadir_fsync_fname, true, LOG);
 
-	LogStartupProgressComplete(STARTUP_PROCESS_OP_FSYNC);
+	LogStartupProgress(STARTUP_PROCESS_OP_FSYNC, NULL, true);
 }
 
 /*
@@ -3516,8 +3516,6 @@ walkdir(const char *path,
 
 		snprintf(subpath, sizeof(subpath), "%s/%s", path, de->d_name);
 
-		LogStartupProgress(STARTUP_PROCESS_OP_FSYNC, path);
-
 		switch (get_dirent_type(subpath, de, process_symlinks, elevel))
 		{
 			case PGFILETYPE_REG:
@@ -3596,8 +3594,10 @@ pre_sync_fname(const char *fname, bool isdir, int elevel)
 static void
 datadir_fsync_fname(const char *fname, bool isdir, int elevel)
 {
+	LogStartupProgress(STARTUP_PROCESS_OP_FSYNC, fname, false);
+
 	/*
-	 * We want to silently ignoring errors about unreadable files.  Pass that
+	 * We want to silently ignore errors about unreadable files.  Pass that
 	 * desire on to fsync_fname_ext().
 	 */
 	fsync_fname_ext(fname, isdir, true, elevel);
diff --git a/src/backend/storage/file/reinit.c b/src/backend/storage/file/reinit.c
index ba18996d98..21a414d434 100644
--- a/src/backend/storage/file/reinit.c
+++ b/src/backend/storage/file/reinit.c
@@ -79,7 +79,6 @@ ResetUnloggedRelations(int op)
 	 */
 	spc_dir = AllocateDir("pg_tblspc");
 
-
 	while ((spc_de = ReadDir(spc_dir, "pg_tblspc")) != NULL)
 	{
 		if (strcmp(spc_de->d_name, ".") == 0 ||
@@ -92,9 +91,9 @@ ResetUnloggedRelations(int op)
 	}
 
 	if (op & UNLOGGED_RELATION_INIT)
-		LogStartupProgressComplete(STARTUP_PROCESS_OP_RESET_UNLOGGED_REL_INIT);
+		LogStartupProgress(STARTUP_PROCESS_OP_RESET_UNLOGGED_REL_INIT, NULL, true);
 	else if (op & UNLOGGED_RELATION_CLEANUP)
-		LogStartupProgressComplete(STARTUP_PROCESS_OP_RESET_UNLOGGED_REL_CLEANUP);
+		LogStartupProgress(STARTUP_PROCESS_OP_RESET_UNLOGGED_REL_CLEANUP, NULL, true);
 
 	FreeDir(spc_dir);
 
@@ -149,10 +148,10 @@ ResetUnloggedRelationsInTablespaceDir(const char *tsdirname, int op)
 
 		if (op & UNLOGGED_RELATION_INIT)
 			LogStartupProgress(STARTUP_PROCESS_OP_RESET_UNLOGGED_REL_INIT,
-							   dbspace_path);
+							   dbspace_path, false);
 		else if (op & UNLOGGED_RELATION_CLEANUP)
 			LogStartupProgress(STARTUP_PROCESS_OP_RESET_UNLOGGED_REL_CLEANUP,
-							   dbspace_path);
+							   dbspace_path, false);
 
 		ResetUnloggedRelationsInDbspaceDir(dbspace_path, op);
 	}
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index d0ea7de8ff..82bbc880d8 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -679,6 +679,7 @@ static char *recovery_target_lsn_string;
 /* should be static, but commands/variable.c needs to get at this */
 char	   *role_string;
 
+
 /*
  * Displayable names for context types (enum GucContext)
  *
diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h
index f17d91bf9c..22ede3baa9 100644
--- a/src/include/access/xlog.h
+++ b/src/include/access/xlog.h
@@ -301,11 +301,11 @@ typedef enum WALAvailability
  */
 typedef enum StartupProcessOp
 {
-	STARTUP_PROCESS_OP_SYNCFS,
+	STARTUP_PROCESS_OP_SYNCFS = 1,
 	STARTUP_PROCESS_OP_FSYNC,
 	STARTUP_PROCESS_OP_REDO,
 	STARTUP_PROCESS_OP_RESET_UNLOGGED_REL_INIT,
-	STARTUP_PROCESS_OP_RESET_UNLOGGED_REL_CLEANUP
+	STARTUP_PROCESS_OP_RESET_UNLOGGED_REL_CLEANUP,
 } StartupProcessOp;
 
 struct XLogRecData;
@@ -413,8 +413,7 @@ extern void register_persistent_abort_backup_handler(void);
 extern SessionBackupState get_backup_status(void);
 
 extern void InitStartupProgress(void);
-extern void LogStartupProgress(StartupProcessOp operation, const char *path);
-extern void LogStartupProgressComplete(StartupProcessOp operation);
+extern void LogStartupProgress(StartupProcessOp operation, const char *path, bool done);
 extern void LogStartupProgressTimeoutHandler(void);
 
 /* File path names (all relative to $PGDATA) */
-- 
2.17.0

