From 4c3b054d94c08b5d867e5f714c56e0963fe619e5 Mon Sep 17 00:00:00 2001
From: Yura Sokolov <y.sokolov@postgrespro.ru>
Date: Sun, 6 Dec 2026 15:51:53 +0300
Subject: [PATCH v1 1/8] Allow fractional seconds in pgbench --progress

The progress interval of -P/--progress is a double now; it is parsed
by a new option_parse_double() helper mirroring option_parse_int(),
so fractions of a second are accepted.  The allowed range is 0.001
to INT_MAX seconds, and the messages for garbage and out-of-range
values are the same as for integer options.

When the interval is not integer, the progress report timestamp
is printed with three decimals to keep the fraction visible, and
next_report is advanced by the fractional interval converted to
microseconds with an explicit cast.
---
 doc/src/sgml/ref/pgbench.sgml              |  4 +-
 src/bin/pgbench/pgbench.c                  | 62 +++++++++++++++++++---
 src/bin/pgbench/t/002_pgbench_no_server.pl |  2 +-
 3 files changed, 60 insertions(+), 8 deletions(-)

diff --git a/doc/src/sgml/ref/pgbench.sgml b/doc/src/sgml/ref/pgbench.sgml
index 9688527c955..fb6e1d4a516 100644
--- a/doc/src/sgml/ref/pgbench.sgml
+++ b/doc/src/sgml/ref/pgbench.sgml
@@ -606,7 +606,9 @@ pgbench <optional> <replaceable>options</replaceable> </optional> <replaceable>d
       <term><option>--progress=</option><replaceable>sec</replaceable></term>
       <listitem>
        <para>
-        Show progress report every <replaceable>sec</replaceable> seconds.  The report
+        Show progress report every <replaceable>sec</replaceable> seconds.
+        The value may include a fractional part, for example 0.5 for a
+        half-second interval.  The report
         includes the time since the beginning of the run, the TPS since the
         last report, and the transaction latency average, standard deviation,
         and the number of failed transactions since the last report. Under
diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index 06c43fd6942..b13d4996688 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -260,7 +260,7 @@ static bool use_quiet;			/* quiet logging onto stderr */
 static int	agg_interval;		/* log aggregates instead of individual
 								 * transactions */
 static bool per_script_stats = false;	/* whether to collect stats per script */
-static int	progress = 0;		/* thread progress report every this seconds */
+static double progress = 0;		/* thread progress report every this seconds */
 static bool progress_timestamp = false; /* progress report with Unix time */
 static int	nclients = 1;		/* number of clients */
 static int	nthreads = 1;		/* number of threads */
@@ -6329,7 +6329,8 @@ printProgressReport(TState *threads, int64 test_start, pg_time_usec_t now,
 				latency,
 				sqlat,
 				lag,
-				stdev;
+				stdev,
+				intp;
 	char		tbuf[315];
 	StatsData	cur;
 
@@ -6381,6 +6382,11 @@ printProgressReport(TState *threads, int64 test_start, pg_time_usec_t now,
 		snprintf(tbuf, sizeof(tbuf), "%.3f s",
 				 PG_TIME_GET_DOUBLE(now + epoch_shift));
 	}
+	else if (modf(progress, &intp) != 0)
+	{
+		/* fractional progress interval: keep sub-second precision visible */
+		snprintf(tbuf, sizeof(tbuf), "%.3f s", total_run);
+	}
 	else
 	{
 		/* round seconds are expected, but the thread may be late */
@@ -6734,6 +6740,50 @@ set_random_seed(const char *seed)
 	return true;
 }
 
+/*
+ * option_parse_double
+ *
+ * Parse double value for an option, in the same way option_parse_int() does
+ * for integers.  If the parsing is successful, returns true and stores the
+ * result in *result if that's given; if parsing fails, returns false.
+ */
+static bool
+option_parse_double(const char *optarg, const char *optname,
+					double min_range, double max_range,
+					double *result)
+{
+	char	   *endptr;
+	double		val;
+
+	errno = 0;
+	val = strtod(optarg, &endptr);
+
+	/*
+	 * Skip any trailing whitespace; if anything but whitespace remains before
+	 * the terminating character, fail.
+	 */
+	while (*endptr != '\0' && isspace((unsigned char) *endptr))
+		endptr++;
+
+	if (*endptr != '\0')
+	{
+		pg_log_error("invalid value \"%s\" for option %s",
+					 optarg, optname);
+		return false;
+	}
+
+	if (errno == ERANGE || isnan(val) || val < min_range || val > max_range)
+	{
+		pg_log_error("%s must be in range %.17g..%.17g",
+					 optname, min_range, max_range);
+		return false;
+	}
+
+	if (result)
+		*result = val;
+	return true;
+}
+
 int
 main(int argc, char **argv)
 {
@@ -6995,8 +7045,8 @@ main(int argc, char **argv)
 				break;
 			case 'P':
 				benchmarking_option_set = true;
-				if (!option_parse_int(optarg, "-P/--progress", 1, INT_MAX,
-									  &progress))
+				if (!option_parse_double(optarg, "-P/--progress", 0.001,
+										 (double) INT_MAX, &progress))
 					exit(1);
 				break;
 			case 'q':
@@ -7577,7 +7627,7 @@ threadRun(void *arg)
 	thread->started_time = thread_start;
 	thread->conn_duration = 0;
 	last_report = thread_start;
-	next_report = last_report + (int64) 1000000 * progress;
+	next_report = last_report + (int64) (1000000.0 * progress);
 
 	/* STEADY */
 	if (!is_connect)
@@ -7795,7 +7845,7 @@ threadRun(void *arg)
 				 */
 				do
 				{
-					next_report += (int64) 1000000 * progress;
+					next_report += (int64) (1000000.0 * progress);
 				} while (now2 >= next_report);
 			}
 		}
diff --git a/src/bin/pgbench/t/002_pgbench_no_server.pl b/src/bin/pgbench/t/002_pgbench_no_server.pl
index 0e8d92f8abd..679abbebe54 100644
--- a/src/bin/pgbench/t/002_pgbench_no_server.pl
+++ b/src/bin/pgbench/t/002_pgbench_no_server.pl
@@ -117,7 +117,7 @@ my @options = (
 	],
 	[
 		'-T XOR -t',
-		'-P 1 --progress-timestamp -l --sampling-rate=0.001 -T 10 -t 1000',
+		'-P 1.5 --progress-timestamp -l --sampling-rate=0.001 -T 10 -t 1000',
 		[qr{specify either }]
 	],
 	[ 'bad variable', '--define foobla', [qr{invalid variable definition}] ],
-- 
2.43.0

