psql Watch Improvements

psql Watch Improvements

November 10, 2023

I saw this tip at Luca’s website

To use \watch:

testdb=# select * from pg_stat_progess_cluster;
...
testdb=# \watch 5

The above example will show me what is going on as CLUSTER or VACUUM with a refresh ratio of 5 seconds. One problem of the \watch command is that it loops forever, meaning you need to manually stop it (e.g., CTRL-c).

With PostgreSQL 16, you can now include a count so it will stop.

testdb=> \?
General
...
  \watch [[i=]SEC] [c=N] execute query every SEC seconds, up to N times
testdb=> \watch i=2 c=7
...

This will watch every 2 seconds and stop after 7 times. The following is also correct:

  • \watch 2 will go forever every 2 seconds.
  • \watch 2 c=7 will go every 2 seconds and stop after 7 times.
    • \watch i=2 c=7
  • \watch 2 7 is not valid.