Skip to content

Commit 27b02e0

Browse files
committed
pg_upgrade: Don't print progress status when output is not a tty.
Until this change pg_upgrade with output redirected to a file / pipe would end up printing all files in the cluster. This has made check-world output exceedingly verbose. Author: Andres Freund <andres@anarazel.de> Reviewed-By: Justin Pryzby <pryzby@telsasoft.com> Reviewed-By: Daniel Gustafsson <daniel@yesql.se> Discussion: https://postgr.es/m/CA+hUKGKjrV61ZVJ8OSag+3rKRmCZXPc03bDyWMqhXg3rdZ=fOw@mail.gmail.com
1 parent 3f64966 commit 27b02e0

File tree

6 files changed

+59
-16
lines changed

6 files changed

+59
-16
lines changed

src/bin/pg_upgrade/dump.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ generate_old_dump(void)
2929
GLOBALS_DUMP_FILE);
3030
check_ok();
3131

32-
prep_status("Creating dump of database schemas\n");
32+
prep_status_progress("Creating dump of database schemas");
3333

3434
/* create per-db dump files */
3535
for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)

src/bin/pg_upgrade/option.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ parseCommandLine(int argc, char *argv[])
207207
if (log_opts.verbose)
208208
pg_log(PG_REPORT, "Running in verbose mode\n");
209209

210+
log_opts.isatty = isatty(fileno(stdout));
211+
210212
/* Turn off read-only mode; add prefix to PGOPTIONS? */
211213
if (getenv("PGOPTIONS"))
212214
{

src/bin/pg_upgrade/pg_upgrade.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ create_new_objects(void)
381381
{
382382
int dbnum;
383383

384-
prep_status("Restoring database schemas in the new cluster\n");
384+
prep_status_progress("Restoring database schemas in the new cluster");
385385

386386
/*
387387
* We cannot process the template1 database concurrently with others,

src/bin/pg_upgrade/pg_upgrade.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ typedef struct
274274
char *basedir; /* Base output directory */
275275
char *dumpdir; /* Dumps */
276276
char *logdir; /* Log files */
277+
bool isatty; /* is stdout a tty */
277278
} LogOpts;
278279

279280

@@ -427,6 +428,7 @@ void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2, 3);
427428
void pg_fatal(const char *fmt,...) pg_attribute_printf(1, 2) pg_attribute_noreturn();
428429
void end_progress_output(void);
429430
void prep_status(const char *fmt,...) pg_attribute_printf(1, 2);
431+
void prep_status_progress(const char *fmt,...) pg_attribute_printf(1, 2);
430432
void check_ok(void);
431433
unsigned int str2uint(const char *str);
432434

src/bin/pg_upgrade/relfilenode.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ transfer_all_new_tablespaces(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
3232
switch (user_opts.transfer_mode)
3333
{
3434
case TRANSFER_MODE_CLONE:
35-
pg_log(PG_REPORT, "Cloning user relation files\n");
35+
prep_status_progress("Cloning user relation files");
3636
break;
3737
case TRANSFER_MODE_COPY:
38-
pg_log(PG_REPORT, "Copying user relation files\n");
38+
prep_status_progress("Copying user relation files");
3939
break;
4040
case TRANSFER_MODE_LINK:
41-
pg_log(PG_REPORT, "Linking user relation files\n");
41+
prep_status_progress("Linking user relation files");
4242
break;
4343
}
4444

src/bin/pg_upgrade/util.c

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,18 @@ report_status(eLogType type, const char *fmt,...)
3838
}
3939

4040

41-
/* force blank output for progress display */
4241
void
4342
end_progress_output(void)
4443
{
4544
/*
46-
* In case nothing printed; pass a space so gcc doesn't complain about
47-
* empty format string.
45+
* For output to a tty, erase prior contents of progress line. When either
46+
* tty or verbose, indent so that report_status() output will align
47+
* nicely.
4848
*/
49-
prep_status(" ");
49+
if (log_opts.isatty)
50+
pg_log(PG_REPORT, "\r%-*s", MESSAGE_WIDTH, "");
51+
else if (log_opts.verbose)
52+
pg_log(PG_REPORT, "%-*s", MESSAGE_WIDTH, "");
5053
}
5154

5255

@@ -75,14 +78,43 @@ prep_status(const char *fmt,...)
7578
vsnprintf(message, sizeof(message), fmt, args);
7679
va_end(args);
7780

78-
if (strlen(message) > 0 && message[strlen(message) - 1] == '\n')
79-
pg_log(PG_REPORT, "%s", message);
81+
/* trim strings */
82+
pg_log(PG_REPORT, "%-*s", MESSAGE_WIDTH, message);
83+
}
84+
85+
/*
86+
* prep_status_progress
87+
*
88+
* Like prep_status(), but for potentially longer running operations.
89+
* Details about what item is currently being processed can be displayed
90+
* with pg_log(PG_STATUS, ...). A typical sequence would look like this:
91+
*
92+
* prep_status_progress("copying files");
93+
* for (...)
94+
* pg_log(PG_STATUS, "%s", filename);
95+
* end_progress_output();
96+
* report_status(PG_REPORT, "ok");
97+
*/
98+
void
99+
prep_status_progress(const char *fmt,...)
100+
{
101+
va_list args;
102+
char message[MAX_STRING];
103+
104+
va_start(args, fmt);
105+
vsnprintf(message, sizeof(message), fmt, args);
106+
va_end(args);
107+
108+
/*
109+
* If outputting to a tty or in verbose, append newline. pg_log_v() will
110+
* put the individual progress items onto the next line.
111+
*/
112+
if (log_opts.isatty || log_opts.verbose)
113+
pg_log(PG_REPORT, "%-*s\n", MESSAGE_WIDTH, message);
80114
else
81-
/* trim strings that don't end in a newline */
82115
pg_log(PG_REPORT, "%-*s", MESSAGE_WIDTH, message);
83116
}
84117

85-
86118
static void
87119
pg_log_v(eLogType type, const char *fmt, va_list ap)
88120
{
@@ -111,8 +143,15 @@ pg_log_v(eLogType type, const char *fmt, va_list ap)
111143
break;
112144

113145
case PG_STATUS:
114-
/* for output to a display, do leading truncation and append \r */
115-
if (isatty(fileno(stdout)))
146+
/*
147+
* For output to a display, do leading truncation. Append \r so
148+
* that the next message is output at the start of the line.
149+
*
150+
* If going to non-interactive output, only display progress if
151+
* verbose is enabled. Otherwise the output gets unreasonably
152+
* large by default.
153+
*/
154+
if (log_opts.isatty)
116155
/* -2 because we use a 2-space indent */
117156
printf(" %s%-*.*s\r",
118157
/* prefix with "..." if we do leading truncation */
@@ -121,7 +160,7 @@ pg_log_v(eLogType type, const char *fmt, va_list ap)
121160
/* optional leading truncation */
122161
strlen(message) <= MESSAGE_WIDTH - 2 ? message :
123162
message + strlen(message) - MESSAGE_WIDTH + 3 + 2);
124-
else
163+
else if (log_opts.verbose)
125164
printf(" %s\n", message);
126165
break;
127166

0 commit comments

Comments
 (0)