diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index b5c70286183..0e4d1e515c3 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -2980,6 +2980,26 @@ CleanupTempFiles(bool isProcExit)
 		FreeDesc(&allocatedDescs[0]);
 }
 
+/*
+ * Check if a given temporary directory exists.
+ */
+static bool
+temporary_dir_exists(const char *path)
+{
+	struct stat statbuf;
+
+	if (lstat(path, &statbuf) < 0)
+	{
+		if (errno == ENOENT)
+			return false;
+
+		ereport(ERROR,
+				(errcode_for_file_access(),
+				 errmsg("cannot stat temporary directory \"%s\": %m",
+						path)));
+	}
+	return true;
+}
 
 /*
  * Remove temporary and temporary relation files left over from a prior
@@ -3010,7 +3030,8 @@ RemovePgTempFiles(void)
 	 * First process temp files in pg_default ($PGDATA/base)
 	 */
 	snprintf(temp_path, sizeof(temp_path), "base/%s", PG_TEMP_FILES_DIR);
-	RemovePgTempFilesInDir(temp_path, false);
+	if (temporary_dir_exists(temp_path))
+		RemovePgTempFilesInDir(temp_path, false);
 	RemovePgTempRelationFiles("base");
 
 	/*
@@ -3026,7 +3047,8 @@ RemovePgTempFiles(void)
 
 		snprintf(temp_path, sizeof(temp_path), "pg_tblspc/%s/%s/%s",
 				 spc_de->d_name, TABLESPACE_VERSION_DIRECTORY, PG_TEMP_FILES_DIR);
-		RemovePgTempFilesInDir(temp_path, false);
+		if (temporary_dir_exists(temp_path))
+			RemovePgTempFilesInDir(temp_path, false);
 
 		snprintf(temp_path, sizeof(temp_path), "pg_tblspc/%s/%s",
 				 spc_de->d_name, TABLESPACE_VERSION_DIRECTORY);
