How to list active connections on PostgreSQL?

2 min read 07-10-2024
How to list active connections on PostgreSQL?


Unmasking the Active Connections in Your PostgreSQL Database

Ever wondered who's accessing your PostgreSQL database and what they're up to? Knowing the active connections provides valuable insights into your database's health and performance. This information can help you identify potential security risks, troubleshoot performance bottlenecks, and monitor user activity.

This article guides you through the process of listing active connections in PostgreSQL, providing clear explanations and helpful examples.

The PostgreSQL Connection Landscape

PostgreSQL manages connections using a dedicated process for each client. When a client connects to the database, PostgreSQL spawns a backend process to handle the interaction. This backend process is responsible for executing queries, managing data, and returning results. To understand active connections, we need to examine these backend processes.

Unveiling the Connections: The pg_stat_activity View

The pg_stat_activity system view is your go-to tool for monitoring active connections. It provides a wealth of information about each backend process, including:

  • pid: The process ID (PID) of the backend process.
  • datname: The name of the database the connection is using.
  • usename: The username of the connected user.
  • client_addr: The IP address of the client connecting to the database.
  • query: The currently executing query (if any).
  • query_start: The timestamp when the current query started.
  • state: The current state of the backend process (e.g., "idle", "active").

Querying for Active Connections

The pg_stat_activity view provides various options for retrieving information about active connections. Here are some examples:

1. Listing all active connections:

SELECT pid, datname, usename, client_addr, query_start, state
FROM pg_stat_activity;

This query returns a list of all active connections with their respective process IDs, database names, usernames, client addresses, query start times, and current states.

2. Filtering by specific criteria:

SELECT pid, datname, usename, client_addr, query_start, state
FROM pg_stat_activity
WHERE datname = 'your_database_name';

This query filters the results to only display connections to a specific database named "your_database_name." You can also filter by username, client address, or any other column in the pg_stat_activity view.

3. Identifying long-running queries:

SELECT pid, datname, usename, query_start, query, state
FROM pg_stat_activity
WHERE state = 'active'
ORDER BY query_start ASC;

This query identifies active backend processes and sorts them by their query start times, allowing you to spot long-running queries.

4. Spotting idle connections:

SELECT pid, datname, usename, query_start, state
FROM pg_stat_activity
WHERE state = 'idle';

This query displays information about all idle connections.

5. Using psql for interactive exploration:

You can also use the psql interactive shell to query pg_stat_activity:

psql -d your_database_name -c "SELECT * FROM pg_stat_activity;"

This command will connect to the database "your_database_name" and execute the SELECT * FROM pg_stat_activity; command, displaying the results in the psql shell.

Understanding and Utilizing the Information

The information provided by pg_stat_activity can be incredibly valuable for database administration:

  • Security: Analyze client addresses and user activity to identify potential security threats.
  • Performance: Monitor query durations to spot bottlenecks and identify inefficient queries.
  • Monitoring: Track database usage and identify potential overload situations.
  • Troubleshooting: Identify the source of issues by analyzing the queries executed by different connections.

By using the techniques outlined in this article, you gain valuable insights into the activity happening within your PostgreSQL database. This information can be used to enhance security, optimize performance, and proactively manage your database resources.