Kill a stuck backend

Force-kill a backend by pid; note: try pg_cancel_backend first to cancel the query only

SELECT pg_terminate_backend(<pid>); -- graceful first: SELECT pg_cancel_backend(<pid>);

tool
postgres29 commands
tags
postgrestroubleshootingperformance

More postgres commands

  • SELECT query, calls, total_exec_time, mean_exec_time, rows FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 20;

    pg_stat_statements: rank queries by cumulative execution time

  • EXPLAIN (ANALYZE, BUFFERS, VERBOSE) SELECT ... ; -- shared hit/read shows cache vs disk I/O

    Run query and show real timings plus buffer hits/reads to spot disk I/O

  • SELECT pid, pg_blocking_pids(pid) AS blocked_by, wait_event_type, query FROM pg_stat_activity WHERE cardinality(pg_blocking_pids(pid)) > 0;

    Show backends waiting on locks and which pids block them

  • SELECT client_addr, state, pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn) AS replay_lag_bytes, write_lag, flush_lag, replay_lag FROM pg_stat_replication;

    Measure replica lag in bytes plus write/flush/replay lag intervals

  • SELECT datname, pg_size_pretty(pg_database_size(datname)) AS size FROM pg_database ORDER BY pg_database_size(datname) DESC;

    Human-readable size of every database, largest first

all 29 postgres commands