Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
PostgreSQL:
SELECT pid, usename, datname, client_addr, state, now() - query_start AS runtime, left(query, 60) AS query
FROM pg_stat_activity WHERE backend_type = 'client backend' ORDER BY runtime DESC NULLS LAST;MySQL / MariaDB:
SHOW FULL PROCESSLIST;Step by step
- PostgreSQL: count by user and state — the overview first:
SELECT usename, state, count(*) FROM pg_stat_activity
WHERE backend_type = 'client backend' GROUP BY 1, 2 ORDER BY 3 DESC;
SHOW max_connections;| State | Means |
|---|---|
active | Running a query now. |
idle | Connected, doing nothing (normal for a connection pool). |
idle in transaction | Opened a transaction and waits — holds locks. Many of these = an app bug. |
- Who blocks whom (PostgreSQL):
SELECT pid, usename, pg_blocking_pids(pid) AS blocked_by, state, left(query, 60) AS query
FROM pg_stat_activity WHERE cardinality(pg_blocking_pids(pid)) > 0;- Stop a session — first only the query, then (if needed) the whole connection:
SELECT pg_cancel_backend(12345);
SELECT pg_terminate_backend(12345);- MySQL / MariaDB: the same questions:
SELECT user, host, db, command, time, state, LEFT(info, 60) AS query
FROM information_schema.processlist WHERE command <> 'Sleep' ORDER BY time DESC;
SHOW STATUS LIKE 'Threads_connected';
SHOW VARIABLES LIKE 'max_connections';
KILL QUERY 4711;
KILL 4711;KILL QUERY stops the statement, KILL closes the connection.
- From the operating system — which hosts connect to the port:
ss -tn state established '( sport = :5432 )' | awk 'NR>1 {split($4,a,":"); print a[1]}' | sort | uniq -c | sort -rn- Prevent the problems: a connection pool (PgBouncer for PostgreSQL, ProxySQL for MySQL), per-user limits, and timeouts:
ALTER ROLE shop CONNECTION LIMIT 80;
ALTER DATABASE shop SET idle_in_transaction_session_timeout = '5min';
ALTER ROLE report SET statement_timeout = '60s';Check that it worked
After the fix, the counts are below max_connections, there are no long idle in transaction sessions, nothing is blocked, and the app's error rate is normal.
Common problems
| Problem | Cause | Fix |
|---|---|---|
FATAL: sorry, too many clients already | All connection slots used. | Find the user with the most connections; use a pooler; fix the app's pool size. |
Many idle in transaction sessions | The app opens transactions and does not commit. | Fix the app; idle_in_transaction_session_timeout. |
| A migration waits forever | It needs a lock that a long session holds. | pg_blocking_pids; finish or cancel the blocker; set lock_timeout for migrations. |
pg_terminate_backend returns false / no right | You are not superuser or the same role. | Run as an admin, or give the role pg_signal_backend. |
MySQL Too many connections | max_connections reached; sleeping connections from apps. | Lower wait_timeout for apps; pool; raise the limit only if RAM allows. |
| You killed the wrong session | Similar queries from many PIDs. | Check user, client address and query before you kill. |
Go further
- Command pages: psql, mysql, ss.
- Next how-tos: Create a PostgreSQL user and database, List open network connections, Kill a process that hangs.
- Full guides: PostgreSQL, MariaDB / MySQL. At work: PostgreSQL in daily use.
1What does the state idle in transaction mean, and why is it dangerous?
The session opened a transaction and waits; it can hold locks that block other queries.
2What is the difference between pg_cancel_backend and pg_terminate_backend?
Cancel stops the running query; terminate closes the whole connection.