This topic describes how to diagnose and manage connections to your instance.
Overview
Hologres is compatible with PostgreSQL. You can query the pg_stat_activity view to view connection information for your instance. This helps you analyze connection states and diagnose active SQL queries. Key management operations include:
Query the default maximum number of connections for an instance: The default connection limit varies by instance type. You can run a command to query this limit for your specific instance type.
Manage connections in HoloWeb: View and manage active connections, including terminating them, in the HoloWeb console.
Query connection information using SQL: Better manage your instance by querying connection counts, checking the state of each connection, and terminating idle connections.
Terminate a connection: Use SQL functions to terminate specified connections and release their resources.
Reserve connections for superusers: Use reserved connections for management operations when the number of connections reaches the maximum limit.
Limit the number of connections for a single user: Set a connection limit for a specific user to prevent them from consuming excessive connection resources.
Automatically terminate idle connections: Enable a feature that automatically terminates connections that have been idle for a specified duration.
Best practices: Review recommendations for using Hologres connections effectively.
Query the pg_stat_activity view
The pg_stat_activity view is useful for analyzing and troubleshooting active SQL tasks and other related issues. Execute the following command to view runtime information about instance connections and queries.
select * from pg_stat_activity ;The following table describes the fields in the pg_stat_activity view.
Field | Description |
datid | The object ID (OID) of the database to which the Hologres backend is connected. |
datname | The name of the database to which the Hologres backend is connected. |
pid | The process ID of the Hologres backend. |
usesysid | The OID of the user logged in to the Hologres backend. |
usename | The username for the current connection. |
application_name | The type of client application. Common application types:
|
client_addr | The IP address of the client. The displayed IP address might be resolved and is not guaranteed to be the source IP. |
client_hostname | The hostname of the client. |
client_port | The port number of the client. |
backend_start | The start time of the backend process. |
xact_start | The start time of the current transaction of the process.
|
query_start | The time when the currently active query started. If the connection state is not |
state_change | The time when the connection state ( |
wait_event_type | The type of event for which the backend is waiting, or NULL if none. Valid values:
|
wait_event | The name of the wait event if the backend is currently waiting; otherwise NULL. |
state | The status of the connection. Valid values:
|
backend_xid | The top-level transaction identifier of this backend, if any. |
backend_xmin | The current backend's |
query | The text of the backend's most recent query. If the value of the state field is |
backend_type | The type of the current backend. Possible types include Note Note the |
Usage notes
Only a Superuser can view all connection information, whereas a standard user can view only their own.
Query the default maximum number of connections for an instance
The default connection limit varies by instance type. You can run the following commands to query this limit for your instance. Each command returns the connection limit for a single frontend (FE) node. The total connection limit for the instance is this value multiplied by the number of FE nodes. For more information about the number of FE nodes for each instance type, see Instance management.
Query the maximum number of connections for the entire instance. This is supported in Hologres v1.3.23 and later.
select instance_max_connections();Query the maximum number of connections for a single FE node. The total number of connections for the instance is the number of FE nodes multiplied by this value.
show max_connections;
Manage connections in HoloWeb
You can view and manage active connections visually in the HoloWeb console.
Log on to the HoloWeb console. For more information, see Connect to HoloWeb.
In the top navigation bar, click Diagnostics and Optimization.
In the left navigation pane, click Active Connection Management.
On the Active Connection Management page, you can configure the parameters to view instance connections and related information.
NoteOnly a superuser account can view all connection information. A regular user can view only their own connection information.
The following table describes the parameters and fields.
Parameter
Description
Instance name
The name of the Hologres instance.
Database
The name of the Hologres database. You can select the database whose connections you want to view. If you do not specify this parameter, the connections to all databases are displayed by default.
Database
The connected database name.
NoteThe connections to the database named Postgres are O&M connections that run at the backend. These connections can be ignored.
User Name
The user account that is used to create the connection.
Client Address
The IP address of the client. The displayed IP address may not be the actual IP address of the client but the outbound IP address of a router.
Application Name
The name of the application that is connected to the instance.
State
The status of the connection, which can be one of the following.
active: The connection is active.
idle: Indicates that the resource is not in use.
idle in transaction: The connection is idle in a long-running transaction.
idle in transaction (Aborted): The connection is idle in a failed transaction.
Query Start
The start time of the query.
Query
The query that is executed.
NoteIf the query statement is long, the statement may be truncated.
PID
The process ID (PID) of the query.
Operation
Kill: You can directly close one or more connections that do not meet your expectations.
Details: Displays the connection details.
Query connection information using SQL
If you prefer to use SQL to query connection information, you can use the following methods.
Query the number of connections to the current database.
You can run the following command to view the number of connections to the current database. For more information about the parameters, see Metric description.
For Hologres V1.1 and later:
SELECT datname::TEXT ,COUNT(1) AS COUNT FROM pg_stat_activity WHERE backend_type = 'client backend' AND application_name != 'hologres' GROUP BY datname::TEXT;For Hologres V0.10 and earlier:
SELECT datname ,COUNT(1) AS COUNT FROM pg_stat_activity WHERE backend_type = 'client backend' AND application_name != 'hologres' GROUP BY datname;
View the state of each connection.
You can view the connection state for each instance in the Hologres console, or you can query the
pg_stat_activityview to get the state of all JDBC or PSQL connections by executing the following statement.select * from pg_stat_activity where backend_type = 'client backend' and state = '<statename>';In this command,
statenameis the state you want to query. Valid states include:idle: The connection is idle, and the process is waiting for a new client command.
active: The connection is active, and the process is executing a query.
idle in transaction: The process is within a transaction but is not currently executing a query.
idle in transaction (aborted): The process is in a transaction that has encountered an error, and the process is not currently executing a query.
fastpath function call: The process is executing a
fast-pathfunction.disabled: Tracking is disabled for this process because the
track_activitiesparameter is disabled.
For example, you can run the following command to query the idle connections of the current instance.
select * from pg_stat_activity where backend_type = 'client backend' and state = 'idle';Hologres-related components such as HoloWeb use a certain number of JDBC connections. If your total connection count is within the expected range, you can ignore these connections. However, if the number of SQL connections consistently approaches or reaches the
max_connectionslimit, you should check your application for connection leaks. To avoid hitting the connection limit, configure a reasonable connection pool size in your application or terminate idle connections. For more information, see Terminate a connection.View the number of connections for each access node.
Query the current number of connections for each FE node of the instance. This is supported in Hologres v1.3.23 and later. FE nodes with no active connections do not appear in the query results.
select * from hologres.hg_connections;The fields in the query result are described as follows:
fe_id: The ID of the FE node.
used_connections: The number of connections currently used on the FE node.
max_connections: The maximum number of connections for the FE node, which is the same as the value returned by the
show max_connectionscommand.
Terminate a connection
The following situations indicate that the connection limit for the system or a specific access node has been reached:
The number of connections reaches or exceeds the value of
max_connections. You can view the number of connections on the Monitoring and Alerts page in the Hologres Console.An error message is returned:
FATAL: sorry, too many clients already connection limit exceeded for superusers.An error message is returned:
FATAL: remaining connection slots are reserved for non-replication superuser connections.
When you encounter the preceding situations, you can connect to the instance with a Superuser account and execute the following statement to check for an excessive number of idle connections.
select * from pg_stat_activity where backend_type = 'client backend' and state = 'idle';If the query returns too many idle processes that are no longer needed, find their pid and execute the following statements to terminate them.
select pg_cancel_backend(<pid>); -- cancel the query on the connection
select pg_terminate_backend(<pid>); -- kill the corresponding backend connection process
-- Batch terminate backend IDLE connection processes to release connections
SELECT pg_terminate_backend(pid)
,query
,datname
,usename
,application_name
,client_addr
,client_port
,backend_start
,state
FROM pg_stat_activity
WHERE length(query) > 0
AND pid != pg_backend_pid()
AND backend_type = 'client backend'
AND state = 'idle'
AND application_name != 'hologres'
AND query not like '%pg_cancel_backend%';Reserved connections for superusers
Hologres reserves a number of connections for Superusers. The number of reserved connections varies by instance type. For details, see Instance management. These reserved connections are intended for management operations, such as terminating idle connections, especially when the maximum number of connections has been reached. The maximum number of connections for a standard user is max_connections minus the number of reserved connections. We recommend that you do not use a Superuser account for regular database operations, as this could occupy all available connections, making it impossible to perform administrative tasks, such as releasing connections.
Limit the number of connections for a single user
Set a connection limit for a user
Because Hologres is compatible with PostgreSQL, you can set a connection limit for a specific user to prevent that user from occupying too many connections and wasting resources.
Execute the following statement to limit the maximum number of connections for a single user on a single access node. If the instance has multiple nodes, the total connection limit for the user is (connection limit per node) * (number of nodes).
Syntax
ALTER ROLE "Alibaba Cloud account ID" CONNECTION LIMIT <number>;Parameters
Parameter
Description
Account ID
The ID of the account to be limited. For a RAM user, prefix the account UID with
p4_.For more information, see Account overview.
number
The connection limit.
The value must be in the range of [-1, N]. A value of -1 removes the connection limit for the user.
Examples
The following example limits the RAM user
283813xxxxto a maximum of one connection per node.ALTER ROLE "p4_283813xxxx" CONNECTION LIMIT 1;
View the connection limit per node for each user
Execute the following statement to view the per-node connection limits that are currently configured for instance users.
SELECT rolname, rolconnlimit
FROM pg_roles
WHERE rolconnlimit <> -1;The following is a sample query result:
rolname | rolconnlimit
---------------+--------------
p4_283813xxxx | 1
(1 row)Automatically terminate idle connections
If the number of connections to an instance is consistently near the upper limit, your application might have a connection leak. You can enable the automatic termination of idle connections to release resources. When a connection remains idle (no queries are executed) for longer than the specified time, it is automatically disconnected.
Prerequisites
The automatic termination of idle connections is supported only in Hologres v0.10.25 and later. If your instance runs an earlier version, upgrade it.
Syntax
Session Level
-- Automatically disconnect a connection if it is idle for 10 minutes. The unit is milliseconds. SET idle_session_timeout = 600000;Database level
-- Automatically disconnect connections to this database if they are idle for 10 minutes. The unit is milliseconds. ALTER DATABASE <db_name> SET idle_session_timeout = 600000;Replace
db_namewith the name of the database for which you want to enable automatic termination of idle connections.
Best practices for using connections
The following are best practices for using Hologres connections.
Use the superuser account wisely
Do not use a Superuser account to connect applications or for regular instance operations. If the number of connections exceeds the instance's limit, even the Superuser account might be unable to connect.
Create a dedicated Superuser account for operations so you can use it to log on to HoloWeb and manage connections and queries during emergencies, such as when the connection limit is reached or a query hangs.
Configure a proper connection pool.
By default, Hologres does not automatically terminate idle connections to ensure business stability. We recommend implementing a robust connection pooling mechanism in your application to release idle connections in a timely manner.
Periodically clean up idle connections to prevent them from occupying too many resources and affecting online business.
Common errors
Why do I get the following error when my SQL connection is idle?
terminating connection due to idle state timeout.Cause: This error occurs because the connection was idle for a period longer than the configured
idle_session_timeout. To conserve resources, the server automatically terminates connections that exceed this limit.Solution: To resolve this, you can either reconnect to the instance or increase the
idle_session_timeoutvalue. For instructions on how to modify this setting, refer to Automatically terminate idle connections.