2

PostgreSQL for MySQL DBAs: Watch Command

 1 year ago
source link: https://www.percona.com/blog/postgresql-for-mysql-dbas-watch-command/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

PostgreSQL for MySQL DBAs: Watch Command

Those new to the realm of PostgreSQL from other databases will find little gems sprinkled liberally through the software.  One of those gems is the watch command. It can be used to repeatedly run a query over and over again.

Pretend you are watching the progress of the importation of a CSV file and want to sample the progress every so often.  Before you start the import, type query to sample the incoming data.

Shell
blog=# SELECT MAX(id) FROM x1;
------
(1 row)

To get updates three times a minute, you would then do the following while the import runs.

Shell
\watch 20
8/24/2022 11:22:34 AM (every 20s)
------
(1 row)

For MySQL-ers who miss the information from SHOW PROCESSLIST, try instead this combination.  First, check the latest queries from the various sessions.

Shell
blog=# SELECT datname, query, usename FROM pg_stat_activity ORDER BY query_start;
 datname |                                   query                                    | usename
---------+----------------------------------------------------------------------------+----------
 blog    | insert into x1 (id) values (generate_series(1300,1499));                   | percona
 blog    | SELECT datname, query, usename FROM pg_stat_activity ORDER BY query_start; | percona

Then set a fifteen-second timer!

Shell
blog=# \watch 15
8/24/2022 11:39:00 AM (every 15s)
datname | query | usename
---------+----------------------------------------------------------------------------+----------
blog | insert into x1 (id) values (generate_series(1300,1499)); | percona
blog | SELECT datname, query, usename FROM pg_stat_activity ORDER BY query_start; | percona
8/24/2022 11:39:15 AM (every 15s)
datname | query | usename
---------+----------------------------------------------------------------------------+----------
blog | insert into x1 (id) values (generate_series(2100,2499)); | percona
blog | SELECT datname, query, usename FROM pg_stat_activity ORDER BY query_start; | percona

The watch command comes in handy whenever you repetitively need to run a query.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK