During an incident, how do psql's \watch, \timing and \conninfo help you, and what exactly does each one show or measure?
answer
- rerun the last query
- a client-side stopwatch
- two seconds unless told otherwise
- which server am I on
- 18 prints a table
basics
~20 s\watch reruns the current or last query every 2 seconds until cancelled or a count or row floor stops it. \timing prints each statement's elapsed time measured by psql, network included. \conninfo shows the database, host, user and backend you are on.
solid answer
~40 s`\watch` re-executes the query buffer, or the most recently sent query if the buffer is empty, every 2 seconds until you press Ctrl+C or the query fails. `i=` sets the interval, `c=` a run count, `m=` (`min_rows`, since 17) stops once fewer rows come back, and psql 18 adds the `WATCH_INTERVAL` variable for the default. Pointed at a `pg_stat_activity` query it becomes a live view of an incident. `\timing on` prints `Time: ... ms` after each statement; psql measures it on the client, so it includes the network round trip and the result transfer, not just execution. `\conninfo` answers 'where am I?': in 18 it prints a table with database, client user, host, port, backend PID, SSL details, whether you are a superuser and whether the server is a hot standby.
code
sql · 11 lines\conninfo
\x auto
\timing on
SELECT pid, state, wait_event_type, wait_event,
now() - xact_start AS xact_age,
left(query, 60) AS query
FROM pg_stat_activity
WHERE datname = current_database()
AND state <> 'idle'
ORDER BY xact_age DESC NULLS LAST;
\watch i=5 c=60go deeper
Know that \timing shows how long statements take and \watch reruns a query every few seconds until you stop it.
Explain what \timing measures, the \watch options (i=, c=, m=) and the 2-second default, and what \conninfo reports in psql 18.
Show incident discipline: confirm primary or standby with \conninfo before acting, watch a focused pg_stat_activity query with a stop condition, and never mistake client-side timing for server execution time.
Turn these habits into runbooks: shared .psqlrc defaults, standard watch queries for common incidents, and a rule that every manual fix starts with confirming the connection.
## \watch: a live view from a terminal **`\watch`** re-executes the current query buffer (as `\g` would), or the most recently sent query when the buffer is empty, then waits and repeats. Each result is printed under a header with the `\pset title` (if any), the time the query started and the interval. It keeps going until one of these happens: - you interrupt it with Ctrl+C; - the query fails; - the run count given with `c=` (`count=`) is reached; - the query returns fewer rows than `m=` (`min_rows=`), an option added in psql 17. The wait defaults to **2 seconds**. `\watch i=5` (or the older bare `\watch 5`) overrides it for one run, and psql 18's **`WATCH_INTERVAL`** variable changes the default, for example from `~/.psqlrc`. The `min_rows` stop is the useful trick: a query that lists sessions waiting on a lock, watched with `m=1`, ends by itself when the jam clears. Nothing runs on the server between executions. If psql exits, the watching stops; it is not a scheduled job. A watched query is still a query on a production server, so a little care pays off: - keep it cheap, filtering `pg_stat_activity` or a small table rather than scanning a large one; - keep the interval reasonable, since a heavy query every second on a struggling primary adds to the problem; - give it a `c=` cap, so a forgotten terminal does not poll for days; - remember that it stops on the first failure, so a `statement_timeout` or a dropped connection ends the watch. ## \timing: a client-side stopwatch **`\timing on`** prints `Time: 840.112 ms` after every statement; values over a second are also shown as minutes and seconds. psql starts the clock before sending the statement and stops it after receiving the whole result, so the figure includes: - the network round trip between psql and the server; - the server's planning and execution; - sending the entire result set to the client. That is why a query returning a few hundred thousand rows over a VPN can show seconds in `\timing` while the server-side measurement from `EXPLAIN ANALYZE` shows milliseconds. Reading that plan output is the `EXPLAIN` topic; for psql, the point is that `\timing` measures what the client experienced. ## \conninfo: know where you are before you act During an incident the most dangerous mistake is running a fix on the wrong server. **`\conninfo`** prints the current connection's details, and in psql 18 it changed from a single sentence to a **table** with more rows: | Row | Why it matters at 3 a.m. | |---|---| | Database, Client User, Host, Server Port | confirms the target | | Backend PID | the process to look for in `pg_stat_activity` and the server log | | SSL Connection (plus library, protocol and cipher when on) | whether the session is encrypted | | Superuser | whether the current role, after any `SET ROLE`, has superuser rights | | Hot Standby | whether you are on a read-only standby rather than the primary | `Client User` is the user you connected as, while `Superuser` reflects the current execution context, so the two can disagree after `SET ROLE`. ## Putting them together on call 1. `\conninfo` first, to confirm host, database and whether this is the primary. 2. `\x auto` so wide rows stay readable. 3. A query over `pg_stat_activity` for non-idle sessions ordered by transaction age, followed by `\watch i=5 c=60`, to follow the incident for five minutes. 4. `\timing on` before trying a candidate query, to see what the client actually waits. ## What changed recently | Feature | Release | |---|---| | `\watch m=` / `min_rows` | psql 17 | | `WATCH_INTERVAL` variable | psql 18 | | Tabular `\conninfo` with Superuser and Hot Standby rows | psql 18 | The answer above assumes psql 18; an older psql prints `\conninfo` as a one-line sentence and has no `WATCH_INTERVAL`.
- How do you make \watch stop by itself once a blocking problem has cleared?Give it a row floor. `\watch m=1` stops as soon as the query returns fewer than one row, so a query listing sessions stuck behind a lock ends the watch when nobody is waiting any more. `c=` caps the number of runs instead. The `min_rows` option arrived in psql 17.
- Why can \timing report far more than EXPLAIN ANALYZE's execution time for the same query?psql measures \timing around the whole request, so it adds network latency and the time to transfer the full result set to the client; EXPLAIN ANALYZE measures on the server and discards the rows. A large result over a slow link can take seconds in \timing and milliseconds in EXPLAIN ANALYZE.
- Treating \timing as pure server-side execution time
- Thinking \watch keeps running on the server after psql exits
- Expecting \conninfo to report replication lag
- Setting WATCH_INTERVAL with ALTER SYSTEM as if it were a server setting
- Skipping \conninfo and assuming the session is on the primary
Assumes psql 18: WATCH_INTERVAL and the tabular \conninfo with Superuser and Hot Standby rows are new in 18, and \watch min_rows arrived in 17.