From 9f9da2ad7d1bc433bbef0ed44b2f37454d968ec2 Mon Sep 17 00:00:00 2001
From: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Date: Sat, 30 Apr 2022 05:35:22 +0000
Subject: [PATCH v1] Progress report removal of temp files and temp relation
 files

At times, there can be many temp files (under pgsql_tmp) and temp
relation files (under  removal which after crash may take longer
during which users have no clue about what's going on in the
server before it comes up online. This patch uses
ereport_startup_progress to report the progress of the file
removal.
---
 src/backend/storage/file/fd.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index 24704b6a02..08c333e514 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -3156,6 +3156,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)
 	 */
@@ -3229,6 +3235,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 +3328,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

