Session monitoring on an ApsaraDB RDS for PostgreSQL instance helps you understand the status of the RDS instance and troubleshoot performance issues. This topic describes how to use the pg_stat_activity system view, Simple Log Service, and CloudMonitor to monitor the sessions of an RDS instance. You can use the monitoring result to understand the status and performance of the RDS instance over a specific period of time.
Background information
In most cases, a database system is a relatively large application. If the load of the database system is heavy, a large number of memory, CPU, I/O, and network resources are consumed. ApsaraDB RDS for PostgreSQL uses process models, and each session corresponds to a background process. Session monitoring helps you understand the status of an RDS instance and troubleshoot the performance bottlenecks of the RDS instance.
pg_stat_activity
The pg_stat_activity view provides information about all sessions that are running in a database system. You can query sessions in a database system at regular intervals to retain session information. This process is equivalent to regular snapshot creation for the database system. This helps you understand the status of a database system and troubleshoot performance issues of the database system.
Session-related parameters
The following table describes the parameters in a pg_stat_activity view.
Parameter | Data type | Description |
datid | oid | The object identifier (OID) of the database to which the process is connected. |
datname | name | The name of the database to which the process is connected. |
pid | integer | The process ID (PID). |
leader_pid | integer | The leader process for parallel processing or application jobs. If no leader or worker process exists, the value of this parameter is NULL. |
usesysid | oid | The OID of the user who interacts with the process. |
usename | name | The username of the user who interacts with the process. |
application_name | text | The name of the application to which the process is connected. |
client_addr | inet | The IP address of the client to which the process is connected. |
client_hostname | text | The hostname of the client. You can perform a reverse Domain Name System (DNS) lookup of client_addr to obtain the value of this parameter. |
client_port | integer | The TCP port used by the client. If a UNIX socket is used, the value of this parameter is -1. |
backend_start | timestamp with time zone | The start time of the process. |
xact_start | timestamp with time zone | The start time of the current transaction of the process. If no active transaction exists, the value of this parameter is NULL. |
query_start | timestamp with time zone | The start time of the ongoing query. |
state_change | timestamp with time zone | The most recent time when the status was changed. |
wait_event_type | text | The type of the event that is waiting in the process. |
wait_event | text | The name of the event that is waiting in the process. If no event is waiting, the value of this parameter is NULL. |
state | text | The status of the current process. |
backend_xid | xid | The top-level transaction ID of the current process. |
backend_xmin | xid | The minimum transaction ID of the current process. |
query_id | bigint | The most recent query ID of the process. |
query | text | The text of the most recent query of the process. |
backend_type | text | The process type. |
SQL statements for session collection
This section provides a sample SQL statement for session collection. The leader_id parameter is available only for RDS instances that run PostgreSQL13 or later. You can adjust the SQL statement for session collection based on the database engine version of your RDS instance.
Prerequisites
A RAM user is created and the following permissions are granted to the RAM user. For more information, see Create a RAM user and grant permissions to the RAM user.
AliyunRDSFullAccess: the permissions to manage all ApsaraDB RDS resources.
AliyunLogFullAccess: the permissions to manage all Simple Log Service resources.
AliyunCloudMonitorFullAccess: the permissions to manage CloudMonitor resources.
A project and a Logstore are created. For more information, see Activate Simple Log Service.
The username and password of the required database on your RDS instance are obtained. For more information, see Accounts and permissions.
Procedure
Collect session information in the
pg_stat_activitysystem view and send the collected information to the Logstore of Simple Log Service at regular intervals.This section provides an example on how to use Simple Log Service SDK for Java to collect and send data at regular intervals.
Install Simple Log Service SDK for Java. For more information, see Install Simple Log Service SDK for Java.
Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables. For more information, see Configure environment variables.
Import the required dependencies into the pom.xml file. In this example, a Maven project is used, and Maven dependencies are required.
Run the following sample program to collect session information about the RDS instance and send the collected information to the Logstore of Simple Log Service:
The following table describes specific parameters. You can modify the parameters based on your business requirements.
Parameter
Description
Example
PG_URL
The URL that is used to connect to the RDS instance. The value must contain the name of the database that you want to monitor.
jdbc:postgresql://pgm-bp1c82mky1avip****.pg.rds.aliyuncs.com:5432/testdb01
PG_USER
The username of the account that is used to log on to the database.
testdbuser
PG_PASSWORD
The password of the account that is used to log on to the database.
****
LOG_ENDPOINT
The endpoint of Simple Log Service. For more information, see Endpoints.
cn-hangzhou.log.aliyuncs.com
LOG_PROJECT
The name of the project of Simple Log Service.
rdspg-test
LOG_STORE
The name of the Logstore of Simple Log Service.
rdspg-sls
After the preceding operations are complete, you can query the session logs of the RDS instance in the Logstore of Simple Log Service. For more information, see Guide to log query and analysis.
ImportantYou can query and analyze the session logs only after you create indexes in the Logstore of Simple Log Service. For more information, see Create indexes.
Import the session logs in Simple Log Service to CloudMonitor.
Log on to the CloudMonitor console, create a metric, and then import logs from Simple Log Service to CloudMonitor. After logs are imported from Simple Log Service to CloudMonitor, you can create a dashboard to view the monitoring chart of the metric. For more information, see Manage metrics for logs imported from Simple Log Service.
Optional. Use the ApsaraDB RDS API to import slow query logs and error logs to Simple Log Service.
You can analyze the status and performance of the RDS instance and troubleshoot performance issues based on the monitoring information about instance sessions, slow query logs, and error logs.
This section describes how to use Simple Log Service SDK for Java together with the DescribeSlowLogRecords and DescribeErrorLogs operations of ApsaraDB RDS to obtain the slow query logs and error logs of an RDS instance and send the obtained logs to Simple Log Service at regular intervals. The following information provides the sample code.
NoteYou can click Debug in API documentation of an operation to go to the Debugging page in OpenAPI Explorer and view how to install and use the operation.
The following table describes specific parameters. You can modify the parameters based on your business requirements.
Parameter
Description
Example
LOG_ENDPOINT
The endpoint of Simple Log Service. For more information, see Endpoints.
cn-hangzhou.log.aliyuncs.com
LOG_PROJECT
The name of the project of Simple Log Service.
rdspg-test
LOG_STORE
The name of the Logstore of Simple Log Service.
rdspg-sls
instanceId
The instance ID.
pgm-bp1c82mky1av****
Create an alert rule in CloudMonitor based on the created metric. If the metric value of a resource meets the alert conditions, CloudMonitor automatically sends alert notifications. For more information, see Create an alert rule.