diff options
author | Heikki Linnakangas <heikki.linnakangas@iki.fi> | 2020-08-17 09:27:29 +0300 |
---|---|---|
committer | Heikki Linnakangas <heikki.linnakangas@iki.fi> | 2020-08-17 09:27:29 +0300 |
commit | d7ec8337f9093b097f08f94e5ecec36303ad73fd (patch) | |
tree | 7e0e01c5b5ebb926d0229ef07c79a2dd5f4b7332 /src/bin/pg_basebackup/pg_basebackup.c | |
parent | b4f16397af460d9d6ead31b86cb3e7f562806866 (diff) | |
download | postgresql-d7ec8337f9093b097f08f94e5ecec36303ad73fd.tar.gz postgresql-d7ec8337f9093b097f08f94e5ecec36303ad73fd.zip |
Fix printing last progress report line in client programs.
A number of client programs have a "--progress" option that when printing
to a TTY, updates the current line by printing a '\r' and overwriting it.
After the last line, '\n' needs to be printed to move the cursor to the
next line. pg_basebackup and pgbench got this right, but pg_rewind and
pg_checksums were slightly wrong. pg_rewind printed the newline to stdout
instead of stderr, and pg_checksums printed the newline even when not
printing to a TTY. Fix them, and also add a 'finished' argument to
pg_basebackup's progress_report() function, to keep it consistent with
the other programs.
Backpatch to v12. pg_rewind's newline was broken with the logging changes
in commit cc8d415117 in v12, and pg_checksums was introduced in v12.
Discussion: https://www.postgresql.org/message-id/82b539e5-ae33-34b0-1aee-22b3379fd3eb@iki.fi
Diffstat (limited to 'src/bin/pg_basebackup/pg_basebackup.c')
-rw-r--r-- | src/bin/pg_basebackup/pg_basebackup.c | 38 |
1 files changed, 20 insertions, 18 deletions
diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index 4f29671d0cd..8158c8e4195 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -188,7 +188,8 @@ static PQExpBuffer recoveryconfcontents = NULL; /* Function headers */ static void usage(void); static void verify_dir_is_empty_or_create(char *dirname, bool *created, bool *found); -static void progress_report(int tablespacenum, const char *filename, bool force); +static void progress_report(int tablespacenum, const char *filename, bool force, + bool finished); static void ReceiveTarFile(PGconn *conn, PGresult *res, int rownum); static void ReceiveTarCopyChunk(size_t r, char *copybuf, void *callback_data); @@ -765,11 +766,15 @@ verify_dir_is_empty_or_create(char *dirname, bool *created, bool *found) * Print a progress report based on the global variables. If verbose output * is enabled, also print the current file name. * - * Progress report is written at maximum once per second, unless the - * force parameter is set to true. + * Progress report is written at maximum once per second, unless the force + * parameter is set to true. + * + * If finished is set to true, this is the last progress report. The cursor + * is moved to the next line. */ static void -progress_report(int tablespacenum, const char *filename, bool force) +progress_report(int tablespacenum, const char *filename, + bool force, bool finished) { int percent; char totaldone_str[32]; @@ -780,7 +785,7 @@ progress_report(int tablespacenum, const char *filename, bool force) return; now = time(NULL); - if (now == last_progress_report && !force) + if (now == last_progress_report && !force && !finished) return; /* Max once per second */ last_progress_report = now; @@ -851,10 +856,11 @@ progress_report(int tablespacenum, const char *filename, bool force) totaldone_str, totalsize_str, percent, tablespacenum, tablespacecount); - if (isatty(fileno(stderr))) - fprintf(stderr, "\r"); - else - fprintf(stderr, "\n"); + /* + * Stay on the same line if reporting to a terminal and we're not done + * yet. + */ + fprintf(stderr, (!finished && isatty(fileno(stderr))) ? "\r" : "\n"); } static int32 @@ -1277,7 +1283,7 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum) } } - progress_report(rownum, state.filename, true); + progress_report(rownum, state.filename, true, false); /* * Do not sync the resulting tar file yet, all files are synced once at @@ -1470,7 +1476,7 @@ ReceiveTarCopyChunk(size_t r, char *copybuf, void *callback_data) } } totaldone += r; - progress_report(state->tablespacenum, state->filename, false); + progress_report(state->tablespacenum, state->filename, false, false); } @@ -1528,7 +1534,7 @@ ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum) if (state.file) fclose(state.file); - progress_report(rownum, state.filename, true); + progress_report(rownum, state.filename, true, false); if (state.file != NULL) { @@ -1709,7 +1715,7 @@ ReceiveTarAndUnpackCopyChunk(size_t r, char *copybuf, void *callback_data) exit(1); } totaldone += r; - progress_report(state->tablespacenum, state->filename, false); + progress_report(state->tablespacenum, state->filename, false, false); state->current_len_left -= r; if (state->current_len_left == 0 && state->current_padding == 0) @@ -2027,11 +2033,7 @@ BaseBackup(void) ReceiveBackupManifest(conn); if (showprogress) - { - progress_report(PQntuples(res), NULL, true); - if (isatty(fileno(stderr))) - fprintf(stderr, "\n"); /* Need to move to next line */ - } + progress_report(PQntuples(res), NULL, true, true); PQclear(res); |