Skip to content

Commit 6f9ee74

Browse files
committed
Improve handling of psql \watch's interval argument
A failure in parsing the interval value defined in the \watch command was silently switched to 1s of interval between two queries, which can be confusing. This commit improves the error handling, and a couple of tests are added to check after: - An incorrect value. - An out-of-range value. - A negative value. A value of zero is able to work now, meaning that there is no interval of time between two queries in a \watch loop. No backpatch is done, as it could break existing applications. Author: Andrey Borodin Reviewed-by: Kyotaro Horiguchi, Nathan Bossart, Michael Paquier Discussion: https://postgr.es/m/CAAhFRxiZ2-n_L1ErMm9AZjgmUK=qS6VHb+0SaMn8sqqbhF7How@mail.gmail.com
1 parent dccb4d1 commit 6f9ee74

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/bin/psql/command.c

+15-3
Original file line numberDiff line numberDiff line change
@@ -2776,9 +2776,18 @@ exec_command_watch(PsqlScanState scan_state, bool active_branch,
27762776
/* Convert optional sleep-length argument */
27772777
if (opt)
27782778
{
2779-
sleep = strtod(opt, NULL);
2780-
if (sleep <= 0)
2781-
sleep = 1;
2779+
char *opt_end;
2780+
2781+
errno = 0;
2782+
sleep = strtod(opt, &opt_end);
2783+
if (sleep < 0 || *opt_end || errno == ERANGE)
2784+
{
2785+
pg_log_error("\\watch: incorrect interval value '%s'", opt);
2786+
free(opt);
2787+
resetPQExpBuffer(query_buf);
2788+
psql_scan_reset(scan_state);
2789+
return PSQL_CMD_ERROR;
2790+
}
27822791
free(opt);
27832792
}
27842793

@@ -5183,6 +5192,9 @@ do_watch(PQExpBuffer query_buf, double sleep)
51835192
if (pagerpipe && ferror(pagerpipe))
51845193
break;
51855194

5195+
if (sleep == 0)
5196+
continue;
5197+
51865198
#ifdef WIN32
51875199

51885200
/*

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

+17
Original file line numberDiff line numberDiff line change
@@ -350,4 +350,21 @@ sub psql_fails_like
350350
'\copy from with DEFAULT'
351351
);
352352

353+
# Check \watch errors
354+
psql_fails_like(
355+
$node,
356+
'SELECT 1;\watch -10',
357+
qr/incorrect interval value '-10'/,
358+
'\watch, negative interval');
359+
psql_fails_like(
360+
$node,
361+
'SELECT 1;\watch 10ab',
362+
qr/incorrect interval value '10ab'/,
363+
'\watch incorrect interval');
364+
psql_fails_like(
365+
$node,
366+
'SELECT 1;\watch 10e400',
367+
qr/incorrect interval value '10e400'/,
368+
'\watch out-of-range interval');
369+
353370
done_testing();

0 commit comments

Comments
 (0)