Sometimes we want to execute a query more than once, repeated at regular intervals; in this recipe we see an interesting psql command that does exactly that.
Repeatedly executing a query in psql
How to do it...
The \watch meta-command allows psql users to automatically (and continuously) re-execute a query.
This behavior is similar to the watch utility of some Linux and Unix environments.
In the following example, we run a simple query on pg_stat_activity and ask psql to repeat it every 5 seconds. You can exit at any time by pressing Ctrl + C:
gabriele=> SELECT count(*) FROM pg_stat_activity;...
count
-------
1
(1 row)
gabriele=> \watch 5
Watch every 5s Tue Aug 27 21:47:24 2013
count
-------
1
(1 row)
<snip>