Integrate a PostgreSQL instance

Updated at:

This topic describes how to integrate a self-managed PostgreSQL database with Cloud Monitor 2.0. The integration component uses the PostgreSQL adapter for aio-exporter to collect metrics such as connections, transactions, buffer hits, database size, locks, and replication delay. This feature supports PostgreSQL 9.1 or later (version 13 or later is recommended) and can be deployed in a Container Service for Kubernetes (ACK) cluster or on an Elastic Compute Service (ECS) instance in a virtual private cloud (VPC). After the integration is complete, Cloud Monitor automatically generates PostgreSQL instance entities and a built-in dashboard.

Requirements

Ensure that the target instance meets the following requirements before you connect. Otherwise, data collection may fail or metrics may be missing.

Condition

Requirement

Version

PostgreSQL 9.1 or later. Version 13 or later is recommended, and version 16 is end-to-end validated. The collector provides features progressively based on the version. Fewer metrics are available for earlier versions.

Deployment type

Self-managed PostgreSQL. Data can also be collected from managed instances, such as Alibaba Cloud RDS for PostgreSQL, if the account, non-mandatory SSL, and whitelist requirements are met.

Monitoring account

For PostgreSQL 10 or later, grant the pg_monitor or pg_read_all_stats role. For versions earlier than 10, superuser privileges are required, or a superuser must manually create a secure view.

Connection method

Connect to the postgres database with sslmode=disable. The account requires CONNECT ON DATABASE postgres permission. The instance cannot have mandatory SSL enabled.

Extension dependencies

Metrics related to pg_stat_statements require this extension to be loaded and created.

Network connectivity

In a Container Service for Kubernetes (ACK) environment, the target PostgreSQL instance must be accessible from the network of the selected ACK cluster. In an Elastic Compute Service (ECS) environment that uses a Virtual Private Cloud (VPC), the target ECS instance must be accessible from the vSwitch CIDR block of the managed agent. Allow access in the pg_hba.conf file and security groups.

Prerequisites

  • Managed Service for Prometheus is activated.

  • For a CS environment: The target ACK cluster is connected to Cloud Monitor 2.0 and is running. The target PostgreSQL service is accessible from within the cluster network.

  • For an ECS (VPC) environment: The target PostgreSQL service is hosted on an ECS instance. This instance must be accessible from the vSwitch CIDR block of the managed agent.

Billing

  • Billing is based solely on the volume of metric data generated in Billing overview.

Integration methods

Step 1: Create a monitoring account

Use a dedicated, read-only account. Do not use the superuser account. The following example applies to PostgreSQL 10 and later versions:

CREATE USER exporter WITH PASSWORD '<password>';
GRANT CONNECT ON DATABASE postgres TO exporter;
GRANT pg_monitor TO exporter;

Permission

Purpose

CONNECT ON DATABASE postgres

Connect to the default database.

pg_monitor

Read monitoring views such as pg_stat_* and pg_settings.

If the userName parameter is empty, the connection is established without authentication.

Step 2: Choose an integration environment

This plugin supports two integration environments: CS and ECS (VPC). Select the appropriate environment based on where your PostgreSQL service is deployed:

CS

  • The Exporter is installed on a specified ACK cluster. It reuses existing data collectors in that cluster to collect metrics.

  • The monitored object is the ACK cluster.

  • Network requirements: The target PostgreSQL service must be accessible from the cluster's network, such as a service within the cluster or a service that has an internal network address in a VPC accessible by the cluster.

ECS (VPC)

  • Select the ECS instance where the target PostgreSQL service is deployed. The system deploys a fully managed data collector based on the VPC information of that ECS instance.

  • No processes are installed on the ECS instance.

  • The monitored object is the ECS instance.

  • Network requirements: You must configure security groups and ACLs to allow the vSwitch CIDR block of the managed agent to access the target ECS instance.

Environment

Exporter deployment location

Installs processes on service machine?

Monitored object

Network accessibility requirements

CS

A user-specified ACK cluster

No. The Exporter is deployed in the cluster and does not use the host resources of business pods.

ACK cluster

PostgreSQL is accessible from the cluster's network.

ECS(VPC)

A fully managed data collector deployed by the system based on the VPC.

No. No processes are installed on the ECS instance.

ECS instance

The managed agent's vSwitch CIDR block must be able to access the target ECS instance.

Step 3: Configure integration

  1. Log on to the Cloud Monitor 2.0 console. After you select the target workspace, click Integration Center in the navigation pane on the left. Then, click PostgreSQL.

  2. Select the integration environment (CS or ECS (VPC)) and then choose the Monitored Object from the drop-down list.

  3. Configure the integration settings. When you are finished, click OK.

    Configuration item

    Required

    Description

    PostgreSQL service endpoint

    Yes

    For a CS environment, enter the in-cluster endpoint. For an ECS(VPC) environment, enter the internal or public endpoint.

    PostgreSQL service port

    Yes

    Default: 5432

    Cloud vendor / Deployment source

    No

    This value is written to the provider tag. For self-hosted deployments, the value is self-hosted/postgresql.

    PostgreSQL account

    No

    Default: exporter. Grant the pg_monitor permission.

    PostgreSQL password

    No

    Not required if the account name is empty.

    Metric collection interval (seconds)

    Yes

    Default: 15

    Custom Tags

    No

    Additional tags to inject into metrics.

    Use public registry address

    No

    Disabled by default. Enable this option if the internal image repository is inaccessible.

  4. After the integration is successful, you can view the integrated entities and related observable data in Entity Explorer.

Data usage

Entities

After you integrate PostgreSQL, Cloud Monitor periodically discovers PostgreSQL instances using the labels of metrics such as pg_up and generates middleware entities:

Dimension

Description

Domain

middleware

Entity type

middleware.postgresql

Entity filter condition

.entity with(domain='middleware', type='middleware.postgresql')

Observed object (attached entity)

For CS environments, the object is acs.ack.cluster. For ECS (VPC) environments, the object is acs.ecs.instance.

Entity fields are derived from collected labels and are independent of the number of installations:

Field

Description

instance_id

The instance identity. It is obtained from the target_uri label in the host:port format.

provider

The deployment source, such as self-hosted/postgresql or alibaba-cloud/rds-pg.

port

The service port.

region_id

The region.

env_type

The deployment environment type (CS or ECS).

cluster_id

The ID of the cluster to which the instance belongs.

You can use these fields to retrieve instances in the Entity Explorer or Middleware Insight applications.

Built-in dashboards

Dashboard name

Access point

Description

PostgreSQL Overview (Instance Overview)

The Instance Overview tab on the entity details page.

A UModel dashboard. Variables are automatically bound based on the instance context.

PostgreSQL overview

Grafana dashboard list

A Grafana dashboard. Available in both Chinese and English.

Core metrics

The following is a complete list of 31 metrics for the middleware.metric.postgresql entity dataset. You can use these metrics directly in entity details, dashboards, and alerts.

Metric name

Type

Unit

Display name

Description

pg_up

gauge

-

PostgreSQL Active

Indicates whether the PostgreSQL instance is reachable and data collection is successful. 1 means active, and 0 means unavailable.

pg_numbackends

gauge

-

Backend Connections

The current number of backend connections, aggregated by database.

pg_max_connections

gauge

-

Maximum Connections

The maximum number of concurrent client connections allowed (the max_connections configuration).

pg_connection_usage

gauge

%

Connection Usage

The percentage of current backend connections relative to max_connections.

pg_xact_commit

counter

count/s

Transactions Committed per Second

The number of transactions committed per second, aggregated by database.

pg_xact_rollback

counter

count/s

Transactions Rolled Back per Second

The number of transactions rolled back per second, aggregated by database.

pg_blks_read

counter

count/s

Disk Blocks Read per Second

The number of blocks read from the disk per second (buffer miss).

pg_blks_hit

counter

count/s

Buffer Hits per Second

The number of buffer hits per second.

pg_cache_hit_ratio

gauge

%

Buffer Hit Rate

The buffer hit rate (blks_hit / (blks_hit + blks_read)).

pg_tup_returned

counter

count/s

Rows Returned per Second

The rate at which rows are returned by queries per second.

pg_tup_fetched

counter

count/s

Rows Fetched per Second

The rate at which rows are fetched by queries per second.

pg_tup_inserted

counter

count/s

Rows Inserted per Second

The rate at which rows are inserted per second.

pg_tup_updated

counter

count/s

Rows Updated per Second

The rate at which rows are updated per second.

pg_tup_deleted

counter

count/s

Rows Deleted per Second

The rate at which rows are deleted per second.

pg_deadlocks

counter

count/s

Deadlocks per Second

The rate of deadlocks detected per second, aggregated by database.

pg_conflicts

counter

count/s

Conflicts per Second

The rate of queries canceled due to conflicts with the recovery process (on replicas).

pg_temp_files

counter

count/s

Temporary Files per Second

The rate at which temporary files are created by queries per second, aggregated by database.

pg_temp_bytes

counter

bytes

Temporary File Bytes per Second

The rate in bytes at which queries write to temporary files per second.

pg_blk_read_time

counter

ms

Block Read Time

The rate of time spent reading data file blocks per second (block read time).

pg_blk_write_time

counter

ms

Block Write Time

The rate of time spent writing data file blocks per second (block write time).

pg_database_size

gauge

bytes

Database Size

The disk space used by each database in bytes.

pg_activity_count

gauge

-

Connections by State

The number of connections for each backend state, such as active, idle, and idle in transaction.

pg_max_tx_duration

gauge

s

Longest Transaction Duration

The longest duration in seconds among all currently running transactions.

pg_locks_count

gauge

-

Locks by Mode

The number of currently held locks, aggregated by lock mode.

pg_replication_lag

gauge

s

Replication Lag in Seconds

The replication delay in seconds relative to the primary database (replicas only).

pg_replication_wal_lag_bytes

gauge

bytes

Replication WAL Lag in Bytes

The difference in Write-Ahead Logging (WAL) bytes between the primary database and each replica (pg_wal_lsn_diff).

pg_checkpoints_req

counter

count/s

Checkpoints Requested per Second

The rate of requested checkpoints per second.

pg_checkpoints_timed

counter

count/s

Timed Checkpoints per Second

The rate of checkpoints triggered by the timer per second.

pg_buffers_alloc

counter

count/s

Buffers Allocated per Second

The rate at which buffers are allocated by the background writer process per second.

pg_collector_success

gauge

-

Collection successful

Indicates whether data collection by each postgres_exporter collector was successful. 1 means success, and 0 means failure.

pg_collector_duration

gauge

s

Collector Duration

The duration in seconds of the last data collection by each postgres_exporter collector.

In addition to the aggregated metrics from the MetricSet, the raw metrics collected by the exporter (pg_* / postgres_*) are written to Prometheus storage. You can query them directly using Prometheus Query Language (PromQL).

Example queries

# Active
pg_up

# Connection usage
100 * pg_stat_database_numbackends / clamp_min(pg_settings_max_connections, 1)

# Buffer hit rate
pg_stat_database_blks_hit
  / clamp_min(pg_stat_database_blks_hit + pg_stat_database_blks_read, 1)

Alert templates

This integration component does not have built-in alert rules. You can configure alerts in the Alert Center based on the metrics described above. For example, you can set an alert for scenarios where the pg_up active metric is not equal to 1, or when the number of connections or message accumulation exceeds a threshold.

Storage policies

After you integrate PostgreSQL, Cloud Monitor creates a corresponding storage instance to store observable data.

Data type

Default storage location

Notes

Metrics

Prometheus instance: RegionShare:{{workspaceName}}:{{regionId}}

Associated dataset: middleware.metric.postgresql

Manage or remove the integration

To modify or cancel the integration, go to the Integration Management page of Integration Center, find the corresponding integration policy, and then click Edit or Delete. When you delete the integration policy, the Exporter and the collection configuration are removed together. Historical metrics that have already been written are retained based on the lifecycle policy of the storage.