Christopher Tyler

psql Watch Improvements

I saw this tip at Luca’s website

To use \watch:

1
2
3
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.

1
2
3
4
5
6
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:

<< Previous Post

|

Next Post >>

#Psql #Postgresql #Luca Ferrari