// SQL snippet

Find long-running transactions in PostgreSQL

A transaction open for hours never shows up in a query plan, but it holds its locks and stops vacuum cleaning up. Find them in one query.

Main statement
PostgreSQL
다음도 지원
MySQL

The task

The damage from a long transaction is indirect. While it stays open, VACUUM cannot remove row versions that its snapshot might still need, so dead rows pile up and tables bloat. It keeps every lock it has taken, so other sessions queue behind it. On a standby with hot_standby_feedback = on, a long query there holds back cleanup on the primary in the same way. pg_stat_activity shows when each transaction started in xact_start, and backend_xmin shows how far back its snapshot reaches.

The snippet

Written for PostgreSQL. Versions for MySQL are under Variations.

PostgreSQL
SELECT pid, usename, datname, application_name, state,
       now() - xact_start AS xact_age,
       now() - state_change AS state_age,
       age(backend_xmin) AS xmin_age_xids,
       LEFT(query, 120) AS last_or_current_query
FROM pg_stat_activity
WHERE xact_start IS NOT NULL
  AND pid <> pg_backend_pid()
ORDER BY xact_start
LIMIT 25;

작동 방식

Every session inside a transaction, active or idle, has a non-null xact_start; state_change tells you how long it has sat in its current state, which separates an actively working long transaction from an idle one that forgot to commit. xmin_age_xids is how many transaction ids old the session's snapshot is; the session with the largest value is the one holding back vacuum. state = 'idle in transaction' rows with a large xact_age are the ones to raise with the application team. An application that opens a transaction, runs a fast query, and then waits for a user or a remote API before committing looks exactly like this.

Variations

Tables with the most dead rows (what a long transaction leaves behind)

SELECT relname, n_dead_tup,
       pg_size_pretty(pg_total_relation_size(relid)) AS table_size
FROM pg_stat_user_tables
WHERE n_dead_tup > 10000
ORDER BY n_dead_tup DESC
LIMIT 10;

MySQL: long InnoDB transactions

MySQL
SELECT trx_id, trx_state, trx_started,
       TIMESTAMPDIFF(SECOND, trx_started, NOW()) AS age_seconds,
       trx_rows_locked, trx_rows_modified,
       trx_mysql_thread_id AS processlist_id
FROM information_schema.INNODB_TRX
ORDER BY trx_started
LIMIT 25;

Questions that come up

How long is too long for a transaction?

For OLTP work, transactions should take milliseconds to seconds. Anything open for more than a few minutes deserves a look, and anything over an hour is costing you: for that whole time vacuum cannot remove dead rows newer than its snapshot in any table it could see. Long read-only reports belong on a replica, or should be split into shorter transactions.

How do I set a hard cap on idle transactions?

Set idle_in_transaction_session_timeout (per role or globally), which terminates sessions sitting idle inside a transaction past the limit. statement_timeout does not cover this case: it only bounds statement execution, not the gaps between statements inside a transaction.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.