Re: Fix for Extra Parenthesis in pgbench progress message - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Fix for Extra Parenthesis in pgbench progress message
Date
Msg-id 1457693.1738951804@sss.pgh.pa.us
Whole thread Raw
In response to Re: Fix for Extra Parenthesis in pgbench progress message  (Nathan Bossart <nathandbossart@gmail.com>)
Responses Re: Fix for Extra Parenthesis in pgbench progress message
List pgsql-hackers
Nathan Bossart <nathandbossart@gmail.com> writes:
> On Fri, Feb 07, 2025 at 12:58:38PM -0500, Tom Lane wrote:
>> Let me have a go at fixing it, and if it turns out to be harder
>> than I think, I'll revert it instead.

> Oops, I was already taking a look at this.  I figured it'd just be
> something like the following, although maybe there's a more elegant way.

Well, the stuff with prev_chars really ought to be skipped as well.
(Yeah, it's probably a no-op, but readers shouldn't have to figure
that out.)

My thought was that duplicating the logic isn't so bad, as attached.

            regards, tom lane

diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index 40592e6260..f303bdeec8 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -5011,6 +5011,16 @@ initPopulateTable(PGconn *con, const char *table, int64 base,
                             j, total,
                             (int) ((j * 100) / total),
                             table, elapsed_sec, remaining_sec);
+
+            /*
+             * If the previous progress message is longer than the current
+             * one, add spaces to the current line to fully overwrite any
+             * remaining characters from the previous message.
+             */
+            if (prev_chars > chars)
+                fprintf(stderr, "%*c", prev_chars - chars, ' ');
+            fputc(eol, stderr);
+            prev_chars = chars;
         }
         /* let's not call the timing for each row, but only each 100 rows */
         else if (use_quiet && (j % 100 == 0))
@@ -5026,20 +5036,20 @@ initPopulateTable(PGconn *con, const char *table, int64 base,
                                 (int) ((j * 100) / total),
                                 table, elapsed_sec, remaining_sec);

+                /*
+                 * If the previous progress message is longer than the current
+                 * one, add spaces to the current line to fully overwrite any
+                 * remaining characters from the previous message.
+                 */
+                if (prev_chars > chars)
+                    fprintf(stderr, "%*c", prev_chars - chars, ' ');
+                fputc(eol, stderr);
+                prev_chars = chars;
+
                 /* skip to the next interval */
                 log_interval = (int) ceil(elapsed_sec / LOG_STEP_SECONDS);
             }
         }
-
-        /*
-         * If the previous progress message is longer than the current one,
-         * add spaces to the current line to fully overwrite any remaining
-         * characters from the previous message.
-         */
-        if (prev_chars > chars)
-            fprintf(stderr, "%*c", prev_chars - chars, ' ');
-        fputc(eol, stderr);
-        prev_chars = chars;
     }

     if (chars != 0 && eol != '\n')

pgsql-hackers by date:

Previous
From: Nathan Bossart
Date:
Subject: Re: Fix for Extra Parenthesis in pgbench progress message
Next
From: Andres Freund
Date:
Subject: Re: Fix for Extra Parenthesis in pgbench progress message