Skip to content

Commit 9f34cae

Browse files
committed
psql: Fix \watch when using interval values less than 1ms
Attempting to use an interval of time less than 1ms would cause \watch to hang. This was confusing, so let's change the logic so as an interval lower than 1ms behaves the same as 0. Comments are added to mention that the internals of do_watch() had better rely on "sleep_ms", the interval value in milliseconds. While on it, this commit adds a test to check the behavior of interval values less than 1ms. \watch hanging for interval values less than 1ms existed before 6f9ee74, that has changed the code to support an interval value of 0. Reported-by: Heikki Linnakangas Author: Andrey M. Borodin, Michael Paquier Discussion: https://postgr.es/m/[email protected] Backpatch-through: 16
1 parent 35a015a commit 9f34cae

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/bin/psql/command.c

+8-3
Original file line numberDiff line numberDiff line change
@@ -5359,6 +5359,10 @@ do_shell(const char *command)
53595359
*
53605360
* We break this out of exec_command to avoid having to plaster "volatile"
53615361
* onto a bunch of exec_command's variables to silence stupider compilers.
5362+
*
5363+
* "sleep" is the amount of time to sleep during each loop, measured in
5364+
* seconds. The internals of this function should use "sleep_ms" for
5365+
* precise sleep time calculations.
53625366
*/
53635367
static bool
53645368
do_watch(PQExpBuffer query_buf, double sleep, int iter, int min_rows)
@@ -5484,10 +5488,10 @@ do_watch(PQExpBuffer query_buf, double sleep, int iter, int min_rows)
54845488

54855489
if (user_title)
54865490
snprintf(title, title_len, _("%s\t%s (every %gs)\n"),
5487-
user_title, timebuf, sleep);
5491+
user_title, timebuf, sleep_ms / 1000.0);
54885492
else
54895493
snprintf(title, title_len, _("%s (every %gs)\n"),
5490-
timebuf, sleep);
5494+
timebuf, sleep_ms / 1000.0);
54915495
myopt.title = title;
54925496

54935497
/* Run the query and print out the result */
@@ -5508,7 +5512,8 @@ do_watch(PQExpBuffer query_buf, double sleep, int iter, int min_rows)
55085512
if (pagerpipe && ferror(pagerpipe))
55095513
break;
55105514

5511-
if (sleep == 0)
5515+
/* Tight loop, no wait needed */
5516+
if (sleep_ms == 0)
55125517
continue;
55135518

55145519
#ifdef WIN32

src/bin/psql/t/001_basic.pl

+8-2
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,14 @@ sub psql_fails_like
352352

353353
# Check \watch
354354
# Note: the interval value is parsed with locale-aware strtod()
355-
psql_like($node, sprintf('SELECT 1 \watch c=3 i=%g', 0.01),
356-
qr/1\n1\n1/, '\watch with 3 iterations');
355+
psql_like(
356+
$node, sprintf('SELECT 1 \watch c=3 i=%g', 0.01),
357+
qr/1\n1\n1/, '\watch with 3 iterations, interval of 0.01');
358+
359+
# Sub-millisecond wait works, equivalent to 0.
360+
psql_like(
361+
$node, sprintf('SELECT 1 \watch c=3 i=%g', 0.0001),
362+
qr/1\n1\n1/, '\watch with 3 iterations, interval of 0.0001');
357363

358364
# Check \watch minimum row count
359365
psql_fails_like(

0 commit comments

Comments
 (0)