What is the pg_stat_activity view?
Answer
pg_stat_activity is a system view showing one row per server process, displaying information about current database connections and queries. Key columns: pid (process ID), usename (user), datname (database), state (active/idle/idle in transaction), query (current or last query), query_start (when query started), wait_event_type/wait_event (what it's waiting for), application_name, client_addr. Common uses: find long-running queries: SELECT pid, now() - query_start AS duration, query FROM pg_stat_activity WHERE state = 'active' ORDER BY duration DESC;. Terminate a query: SELECT pg_cancel_backend(pid); (graceful) or pg_terminate_backend(pid); (forceful).
Previous
What are stored procedures and functions in PostgreSQL?
Next
How do you find and kill long-running queries in PostgreSQL?