Show currently running queries
Read-only
What do risk levels mean?
- Read-only
- Inspects state without changing anything.
- Low risk
- Reversible with a routine follow-up command.
- Medium risk
- Changes state; undo path documented.
- Destructive
- Deletes or overwrites; confirmation required.
Displays every query currently executing on the server with its duration, so you can spot long-running or blocked queries that may need attention.
postgresqlqueriesactivitymonitor
Commands
List active queries ordered by longest-running first
psql -c "SELECT pid, usename, datname, now() - query_start AS duration, state, query FROM pg_stat_activity WHERE state = 'active' AND query NOT LIKE '%pg_stat_activity%' ORDER BY duration DESC;"Verification
psql -c "SELECT count(*) AS active_queries FROM pg_stat_activity WHERE state = 'active';"A count of currently active queries is printed; zero means no queries are executing.
Undo
Not undoable
This recipe only reads server activity metadata and changes nothing.
Pitfalls
- Very short queries may complete before the listing renders; the output is a point-in-time snapshot.
- Only superusers see queries run by other users.
- The duration column can be NULL for queries that just started.