Performance diagnostics in Managed Service for PostgreSQL
Managed Service for PostgreSQL provides a built-in tool for diagnosing your database cluster performance. This tool helps you analyze PostgreSQL performance metrics for sessions and queries.
To learn how to identify and resolve cluster performance issues, see the Performance analysis and optimization and Troubleshooting cluster performance sections.
Enabling statistics collection
Enable the Statistics sampling option when creating a cluster or updating its settings. This option is disabled by default.
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.
To enable statistics collection, add the --performance-diagnostics option to the cluster update command:
yc managed-postgresql cluster update <cluster_name_or_ID> \
...
--performance-diagnostics enabled=true,`
`sessions-sampling-interval=<session_sampling_interval>,`
`statements-sampling-interval=<statement_sampling_interval> \
...
Allowed values:
sessions-sampling-interval: From1to86400seconds.statements-sampling-interval: From60to86400seconds.
-
Open the current Terraform configuration file describing your infrastructure.
To learn how to create this file, see Creating a cluster.
For a complete list of configurable Managed Service for PostgreSQL cluster fields, refer to the Terraform provider guides.
-
To set up statistics collection, to the
configsection, add theperformance_diagnosticssection:resource "yandex_mdb_postgresql_cluster" "<cluster_name>" { ... config { ... performance_diagnostics { enabled = <enables statistics collection: true or false> sessions_sampling_interval = <sessions sampling interval> statements_sampling_interval = <statements sampling interval> } ... } ... }Where:
enabled: Enable statistics collection,trueorfalse.sessions_sampling_interval: Session sampling interval, from1to86400seconds.statements_sampling_interval: Statement sampling interval, from60to86400seconds.
-
Check if the settings are correct.
-
In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
-
Run this command:
terraform validateTerraform will show any errors found in your configuration files.
-
-
Confirm updating the resources.
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
Time limits
A Terraform provider sets the timeout for Managed Service for PostgreSQL cluster operations:
- Creating a cluster, including restoring from a backup: 30 minutes.
- Editing a cluster: 60 minutes.
- Deleting a cluster: 15 minutes.
Operations exceeding the set timeout are interrupted.
How do I change these limits?
Add the
timeoutsblock to the cluster description, for example:resource "yandex_mdb_postgresql_cluster" "<cluster_name>" { ... timeouts { create = "1h30m" # 1 hour 30 minutes update = "2h" # 2 hours delete = "30m" # 30 minutes } } -
-
Get an IAM token for API authentication and put it into an environment variable:
export IAM_TOKEN="<IAM_token>" -
To enable statistics collection when creating a cluster:
-
Include
configSpec.performanceDiagnosticsoption to the cURL command implementing the Cluster.Create method:curl \ --request POST \ --header "Authorization: Bearer $IAM_TOKEN" \ --header "Content-Type: application/json" \ --url 'https://mdb.api.cloud.yandex.net/managed-postgresql/v1/clusters' \ --data '{ "configSpec": { "performanceDiagnostics": { "enabled": <enable_statistics_collection>, "sessionsSamplingInterval": "<session_sampling_interval>", "statementsSamplingInterval": "<statement_sampling_interval>" }, ... }, ... }'Where
configSpec.performanceDiagnosticsrepresents the statistics collection settings:enabled: Enables statistics collection,trueorfalse.sessionsSamplingInterval: Session sampling interval. Allowed values range from1to86400.statementsSamplingInterval: Statement sampling interval. Allowed values range from60to86400.
-
Check the server response to make sure your request was successful.
-
-
To enable statistics collection when updating an existing cluster:
-
Call the Cluster.Update method, e.g., via the following cURL
request:Warning
The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the
updateMaskparameter as a single comma-separated string.curl \ --request PATCH \ --header "Authorization: Bearer $IAM_TOKEN" \ --header "Content-Type: application/json" \ --url 'https://mdb.api.cloud.yandex.net/managed-postgresql/v1/clusters/<cluster_ID>' \ --data '{ "updateMask": "configSpec.performanceDiagnostics", "configSpec": { "performanceDiagnostics": { "enabled": <enable_statistics_collection>, "sessionsSamplingInterval": "<session_sampling_interval>", "statementsSamplingInterval": "<statement_sampling_interval>" } } }'Where
configSpec.performanceDiagnosticsrepresents the statistics collection settings:enabled: Enables statistics collection,trueorfalse.sessionsSamplingInterval: Session sampling interval. Allowed values range from1to86400.statementsSamplingInterval: Statement sampling interval. Allowed values range from60to86400.
-
Check the server response to make sure your request was successful.
-
-
Get an IAM token for API authentication and put it into an environment variable:
export IAM_TOKEN="<IAM_token>" -
Clone the cloudapi
repository:cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapiBelow, we assume the repository contents are stored in the
~/cloudapi/directory. -
To enable statistics collection when creating a cluster:
-
Include the
config_spec.performance_diagnosticsoption to the grpcurl command implementing the ClusterService.Create method:grpcurl \ -format json \ -import-path ~/cloudapi/ \ -import-path ~/cloudapi/third_party/googleapis/ \ -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/cluster_service.proto \ -rpc-header "Authorization: Bearer $IAM_TOKEN" \ -d '{ "config_spec": { "performance_diagnostics": { "enabled": <enable_statistics_collection>, "sessions_sampling_interval": "<session_sampling_interval>", "statements_sampling_interval": "<statement_sampling_interval>" }, ... }, ... }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.ClusterService.CreateWhere
config_spec.performance_diagnosticsrepresents the statistics collection settings:enabled: Enables statistics collection,trueorfalse.sessions_sampling_interval: Session sampling interval. Allowed values range from1to86400.statements_sampling_interval: Statement sampling interval. Allowed values range from60to86400.
-
Check the server response to make sure your request was successful.
-
-
To enable statistics collection when updating an existing cluster:
-
Call the ClusterService.Update method, e.g., via the following gRPCurl
request:Warning
The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the
update_maskparameter as an array ofpaths[]strings.Format for listing settings
"update_mask": { "paths": [ "<setting_1>", "<setting_2>", ... "<setting_N>" ] }grpcurl \ -format json \ -import-path ~/cloudapi/ \ -import-path ~/cloudapi/third_party/googleapis/ \ -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/cluster_service.proto \ -rpc-header "Authorization: Bearer $IAM_TOKEN" \ -d '{ "cluster_id": "<cluster_ID>", "update_mask": { "paths": [ "config_spec.performance_diagnostics" ] }, "config_spec": { "performance_diagnostics": { "enabled": <enable_statistics_collection>, "sessions_sampling_interval": "<session_sampling_interval>", "statements_sampling_interval": "<statement_sampling_interval>" } } }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.ClusterService.UpdateWhere
config_spec.performance_diagnosticsrepresents the statistics collection settings:enabled: Enables statistics collection,trueorfalse.sessions_sampling_interval: Session sampling interval. Allowed values range from1to86400.statements_sampling_interval: Statement sampling interval. Allowed values range from60to86400.
-
Check the server response to make sure your request was successful.
-
Getting session statistics
- Go to Managed Service for PostgreSQL.
- Click the name of your cluster and select the Performance diagnostics → Sessions tab.
To view session statistics:
- Specify the time interval.
- Optionally, configure filters.
- Select a data segment.
To show or hide individual categories, click the category name in the chart legend.
To see the query history within a session:
- Specify the required time interval.
- Optionally, configure filters.
-
Get an IAM token for API authentication and put it into an environment variable:
export IAM_TOKEN="<IAM_token>" -
Clone the cloudapi
repository:cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapiBelow, we assume the repository contents are stored in the
~/cloudapi/directory. -
Call the PerformanceDiagnosticsService.ListRawSessionStates method, e.g., via the following gRPCurl
request:grpcurl \ -format json \ -import-path ~/cloudapi/ \ -import-path ~/cloudapi/third_party/googleapis/ \ -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/perf_diag_service.proto \ -rpc-header "Authorization: Bearer $IAM_TOKEN" \ -d '{ "cluster_id": "<cluster_ID>", "from_time": "<time_range_left_boundary>", "to_time": "<time_range_right_boundary>" }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.PerformanceDiagnosticsService.ListRawSessionStatesWhere:
from_time: Left boundary of a time range in RFC-3339 format, e.g.,2024-09-18T15:04:05Z.to_time: End of the time range in the same format asfrom_time.
You can request the cluster ID with the list of clusters in the folder.
-
Check the server response to make sure your request was successful.
To learn what statistics you can get, see the PostgreSQL guides
Getting query statistics
- Go to Managed Service for PostgreSQL.
- Click the name of your cluster and select the Performance diagnostics → Queries tab.
To view query statistics for a specific time interval:
- Select the time interval.
- Optionally, configure filters.
To get details on relative changes in query statistics:
- In the Interval 1 field, select the time interval to use as the baseline for calculations.
- In the Interval 2 field, select the time interval to compare against the baseline interval.
- Optionally, configure filters.
Suppose, 10 SELECT * FROM cities queries were executed in the first interval and 20 in the second. A comparison of the statistics will show a +100% difference for the query count
metric (the Calls table column).
-
Get an IAM token for API authentication and put it into an environment variable:
export IAM_TOKEN="<IAM_token>" -
Clone the cloudapi
repository:cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapiBelow, we assume the repository contents are stored in the
~/cloudapi/directory. -
Call the PerformanceDiagnosticsService.ListRawStatements method, e.g., via the following gRPCurl
request:grpcurl \ -format json \ -import-path ~/cloudapi/ \ -import-path ~/cloudapi/third_party/googleapis/ \ -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/perf_diag_service.proto \ -rpc-header "Authorization: Bearer $IAM_TOKEN" \ -d '{ "cluster_id": "<cluster_ID>", "from_time": "<time_range_left_boundary>", "to_time": "<time_range_right_boundary>" }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.PerformanceDiagnosticsService.ListRawStatementsWhere:
from_time: Left boundary of a time range in RFC-3339 format, e.g.,2024-09-18T15:04:05Z.to_time: End of the time range in the same format asfrom_time.
You can request the cluster ID with the list of clusters in the folder.
-
Check the server response to make sure your request was successful.
To learn more about available statistics, refer to the pg_stat_statements
Getting query execution plan details
The auto_explain moduleEXPLAIN command
You can enable query logging in the DBMS settings:
-
In the Shared preload libraries field, select
auto_explain. -
Enable Auto explain log analyze.
-
Configure the
auto_explainmodule settings:Warning
Selecting
0for Auto explain log min duration or enabling Auto explain log timing can significantly reduce cluster performance.