pg_top - PostgreSQL Process Monitor

Monitor and analyze top CPU PostgreSQL processes with pg_top. View query plans, locks, and user index statistics for efficient database management.

pg_top - PostgreSQL Process Monitor

The pg_top utility is a powerful command-line tool designed to display and update information about the top CPU-consuming PostgreSQL processes. It provides real-time insights into database activity, helping administrators identify performance bottlenecks and optimize query execution.

Monitor PostgreSQL Processes

pg_top allows you to connect to your PostgreSQL server and monitor active processes. You can specify connection details such as the port, username, database name, and whether to prompt for a password. This enables granular control over which database instance you are observing.

# Example: Connect to port 5432 using user 'postgres', database 'mydatabase', and prompt for password
pg_top -p 5432 -U postgres -d mydatabase -W

Interactive Mode Features

pg_top offers an interactive mode with several keys to access detailed process information:

  • A - Display Actual Query Plan

    Pressing 'A' shows the actual query plan (EXPLAIN ANALYZE) for a selected backend process. This is invaluable for understanding how PostgreSQL is executing a query and identifying performance issues within the execution path.

  • E - Display Re-determined Execution Plan

    Pressing 'E' displays the re-determined execution plan (EXPLAIN) of the SQL statement being run by a backend process. This helps in analyzing the planner's choices without the overhead of actual execution.

  • L - Display Held Locks

    Pressing 'L' reveals the currently held locks by a backend process. This is crucial for diagnosing deadlocks and understanding lock contention issues that can impact application performance.

  • Q - Display Current Query

    Pressing 'Q' shows the currently running query of a backend process. This is a quick way to see what a specific process is actively working on.

  • X - Display User Index Statistics

    Pressing 'X' displays user index statistics. This can help in identifying underutilized or overutilized indexes, aiding in index optimization strategies.

Optimizing Database Performance

By leveraging pg_top, database administrators can gain deep insights into their PostgreSQL server's performance. Understanding CPU usage, query execution plans, and lock situations allows for proactive tuning and maintenance, ensuring a responsive and efficient database environment.

Further Resources