From 059099c469c40ee9d91a3b37b8960b36557e6030 Mon Sep 17 00:00:00 2001
From: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Date: Wed, 4 May 2022 14:17:24 +0000
Subject: [PATCH v2] pgsql_tmp ereport_startup_progress

---
 src/backend/postmaster/postmaster.c | 11 +++++++++++
 src/backend/storage/file/fd.c       | 15 +++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 964a56dec4..261a5a5911 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -116,6 +116,7 @@
 #include "postmaster/interrupt.h"
 #include "postmaster/pgarch.h"
 #include "postmaster/postmaster.h"
+#include "postmaster/startup.h"
 #include "postmaster/syslogger.h"
 #include "replication/logicallauncher.h"
 #include "replication/walsender.h"
@@ -689,6 +690,11 @@ PostmasterMain(int argc, char *argv[])
 	pqsignal_pm(SIGXFSZ, SIG_IGN);	/* ignored */
 #endif
 
+	InitializeTimeouts();		/* establishes SIGALRM handler */
+
+	RegisterTimeout(STARTUP_PROGRESS_TIMEOUT,
+					startup_progress_timeout_handler);
+
 	/*
 	 * Options setup
 	 */
@@ -1104,6 +1110,9 @@ PostmasterMain(int argc, char *argv[])
 	/* Write out nondefault GUC settings for child processes to use */
 	write_nondefault_variables(PGC_POSTMASTER);
 
+	/* Prepare to report progress of the temporary files removal phase */
+	begin_startup_progress_phase();
+
 	/*
 	 * Clean out the temp directory used to transmit parameters to child
 	 * processes (see internal_forkexec, below).  We must do this before
@@ -1113,6 +1122,8 @@ PostmasterMain(int argc, char *argv[])
 	 * conflicting Postgres processes in this data directory.
 	 */
 	RemovePgTempFilesInDir(PG_TEMP_FILES_DIR, true, false);
+
+	disable_timeout(STARTUP_PROGRESS_TIMEOUT, false);
 #endif
 
 	/*
diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index 24704b6a02..c793303bd8 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -102,6 +102,7 @@
 #include "storage/ipc.h"
 #include "utils/guc.h"
 #include "utils/resowner_private.h"
+#include "utils/timeout.h"
 
 /* Define PG_FLUSH_DATA_WORKS if we have an implementation for pg_flush_data */
 #if defined(HAVE_SYNC_FILE_RANGE)
@@ -3156,6 +3157,12 @@ RemovePgTempFiles(void)
 	DIR		   *spc_dir;
 	struct dirent *spc_de;
 
+	/*
+	 * Prepare to report progress of the temporary and temporary relation files
+	 * removal phase.
+	 */
+	begin_startup_progress_phase();
+
 	/*
 	 * First process temp files in pg_default ($PGDATA/base)
 	 */
@@ -3185,6 +3192,8 @@ RemovePgTempFiles(void)
 
 	FreeDir(spc_dir);
 
+	disable_timeout(STARTUP_PROGRESS_TIMEOUT, false);
+
 	/*
 	 * In EXEC_BACKEND case there is a pgsql_tmp directory at the top level of
 	 * DataDir as well.  However, that is *not* cleaned here because doing so
@@ -3229,6 +3238,9 @@ RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok, bool unlink_all)
 		snprintf(rm_path, sizeof(rm_path), "%s/%s",
 				 tmpdirname, temp_de->d_name);
 
+		ereport_startup_progress("removing temporary files under pgsql_tmp directory, elapsed time: %ld.%02d s, current file: %s",
+								 rm_path);
+
 		if (unlink_all ||
 			strncmp(temp_de->d_name,
 					PG_TEMP_FILE_PREFIX,
@@ -3319,6 +3331,9 @@ RemovePgTempRelationFilesInDbspace(const char *dbspacedirname)
 		snprintf(rm_path, sizeof(rm_path), "%s/%s",
 				 dbspacedirname, de->d_name);
 
+		ereport_startup_progress("removing temporary relation files under pg_tblspc directory, elapsed time: %ld.%02d s, current file: %s",
+								 rm_path);
+
 		if (unlink(rm_path) < 0)
 			ereport(LOG,
 					(errcode_for_file_access(),
-- 
2.25.1

