SELECT pid, usename AS user_name, application_name, client_addr, state, EXTRACT(EPOCH FROM (now() - xact_start))::int AS open_seconds, LEFT(query, 200) AS queryFROM pg_stat_activityWHERE xact_start IS NOT NULL AND now() - xact_start > interval '5 minutes' AND pid <> pg_backend_pid()ORDER BY xact_startThis query is part of the "PostgreSQL health" pack: connect the database in DataRunner and install the whole pack at once, with queries and dashboard.
What this query shows
A transaction that stays open holds locks and, more quietly, stops VACUUM from removing any row that was deleted after the transaction began. One forgotten session in the state idle in transaction can bloat tables across the whole database.
The query reads pg_stat_activity and lists sessions whose xact_start is more than 5 minutes ago, oldest first. It leaves out its own connection. Run it when a table keeps bloating even though autovacuum runs, when locks pile up, or when the application sits waiting on the database for no clear reason.
How to read each column
- pid: the backend process id, the handle you pass to pg_terminate_backend.
- user_name, application_name, client_addr: who opened the session and from where. These tell you which service to look at.
- state: active means it is running a statement. idle in transaction means it opened a transaction and is doing nothing.
- open_seconds: how long the transaction has been open, not how long the current statement has run.
- query: the first 200 characters of the current statement, or the last one for an idle session.
Healthy and worrying numbers
An empty result is healthy. A long transaction in state active is often legitimate: a report, a migration, a backup with pg_dump. The worrying rows are idle in transaction with a large open_seconds, because nothing is happening on them and they still hold everything they took.
Five minutes is the threshold used by this template. For a pure OLTP application, even 30 seconds of idle in transaction is suspicious. Treat that as a rule of thumb and adjust it to your workload.
What to do about it
First find the cause in the application: a transaction opened and never committed, an HTTP call made inside a transaction, or a client that crashed. Fixing that is the real cure. To free the database now, ask the session to cancel its statement, and if it is idle, end it.
-- cancel the running statement, keep the connectionSELECT pg_cancel_backend(12345); -- close the connection and roll the transaction backSELECT pg_terminate_backend(12345);To stop the problem from coming back, set a timeout for idle transactions.
ALTER DATABASE mydb SET idle_in_transaction_session_timeout = '5min';The setting applies to new sessions. The server ends any session that stays idle in a transaction longer than that, so make sure your application can retry.
Limits of this query
- Other users' query text and details are hidden unless your role is a superuser or a member of pg_read_all_stats (part of pg_monitor). Without it you will see sessions but not their statements.
- The query looks at one server. Replicas have their own sessions.
- Only sessions with a transaction in progress have xact_start. A plain idle session has none and does not appear here.
- Terminating a backend rolls back its work. Check what the session was doing first.
Run it on a schedule in DataRunner
Connect the database to DataRunner, paste this query or start from the template, approve the SQL, then pick a schedule and a channel. Every 15 or 30 minutes works for most teams. Each run executes the query on your database and delivers the result as CSV, Excel, PDF or inline HTML by e-mail, Slack, Telegram, Discord, WhatsApp (beta), a signed webhook or Google Sheets.
This query returns no rows when everything is fine. A run with no rows can be skipped, so a message only arrives when there is something to look at.
If the database sits in a private network, the bridge agent opens the connection from the inside out, so you do not need to open an inbound port.