1515#include "libpq-fe.h"
1616#include "pqexpbuffer.h"
1717#include "pgtar.h"
18+ #include "pgtime.h"
1819
1920#include <unistd.h>
2021#include <dirent.h>
@@ -46,6 +47,7 @@ static boolstreamwal = false;
4647static bool fastcheckpoint = false;
4748static bool writerecoveryconf = false;
4849static int standby_message_timeout = 10 * 1000 ;/* 10 sec = default */
50+ static pg_time_t last_progress_report = 0 ;
4951
5052/* Progress counters */
5153static uint64 totalsize ;
@@ -75,7 +77,7 @@ static PQExpBuffer recoveryconfcontents = NULL;
7577/* Function headers */
7678static void usage (void );
7779static void verify_dir_is_empty_or_create (char * dirname );
78- static void progress_report (int tablespacenum ,const char * filename );
80+ static void progress_report (int tablespacenum ,const char * filename , bool force );
7981
8082static void ReceiveTarFile (PGconn * conn ,PGresult * res ,int rownum );
8183static void ReceiveAndUnpackTarFile (PGconn * conn ,PGresult * res ,int rownum );
@@ -399,13 +401,27 @@ verify_dir_is_empty_or_create(char *dirname)
399401/*
400402 * Print a progress report based on the global variables. If verbose output
401403 * is enabled, also print the current file name.
404+ *
405+ * Progress report is written at maximum once per second, unless the
406+ * force parameter is set to true.
402407 */
403408static void
404- progress_report (int tablespacenum ,const char * filename )
409+ progress_report (int tablespacenum ,const char * filename , bool force )
405410{
406- int percent = ( int ) (( totaldone / 1024 ) * 100 / totalsize ) ;
411+ int percent ;
407412char totaldone_str [32 ];
408413char totalsize_str [32 ];
414+ pg_time_t now ;
415+
416+ if (!showprogress )
417+ return ;
418+
419+ now = time (NULL );
420+ if (now == last_progress_report && !force )
421+ return ;/* Max once per second */
422+
423+ last_progress_report = now ;
424+ percent = totalsize ? (int ) ((totaldone /1024 )* 100 /totalsize ) :0 ;
409425
410426/*
411427 * Avoid overflowing past 100% or the full size. This may make the total
@@ -853,9 +869,9 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
853869}
854870}
855871totaldone += r ;
856- if (showprogress )
857- progress_report (rownum ,filename );
872+ progress_report (rownum ,filename , false);
858873}/* while (1) */
874+ progress_report (rownum ,filename , true);
859875
860876if (copybuf != NULL )
861877PQfreemem (copybuf );
@@ -1080,8 +1096,7 @@ ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum)
10801096disconnect_and_exit (1 );
10811097}
10821098totaldone += r ;
1083- if (showprogress )
1084- progress_report (rownum ,filename );
1099+ progress_report (rownum ,filename , false);
10851100
10861101current_len_left -= r ;
10871102if (current_len_left == 0 && current_padding == 0 )
@@ -1097,6 +1112,7 @@ ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum)
10971112}
10981113}/* continuing data in existing file */
10991114}/* loop over all data blocks */
1115+ progress_report (rownum ,filename , true);
11001116
11011117if (file != NULL )
11021118{
@@ -1457,8 +1473,7 @@ BaseBackup(void)
14571473tablespacecount = PQntuples (res );
14581474for (i = 0 ;i < PQntuples (res );i ++ )
14591475{
1460- if (showprogress )
1461- totalsize += atol (PQgetvalue (res ,i ,2 ));
1476+ totalsize += atol (PQgetvalue (res ,i ,2 ));
14621477
14631478/*
14641479 * Verify tablespace directories are empty. Don't bother with the
@@ -1505,7 +1520,7 @@ BaseBackup(void)
15051520
15061521if (showprogress )
15071522{
1508- progress_report (PQntuples (res ),NULL );
1523+ progress_report (PQntuples (res ),NULL , true );
15091524fprintf (stderr ,"\n" );/* Need to move to next line */
15101525}
15111526PQclear (res );