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
When creating a cluster or updating its settings:
-
Enable Statistics sampling (disabled by default).
-
Set the Sessions sampling interval and Statements sampling interval. Valid values:
- For sessions: From
5to86400seconds. - For queries: From
60to86400seconds.
- For sessions: From
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder used by default is the one specified when creating the CLI 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 options.
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: From5to86400seconds.statements-sampling-interval: From60to86400seconds.
-
Open the current Terraform configuration file describing your infrastructure.
For more on how to create this file, see Creating a cluster.
For a complete list of configurable Managed Service for PostgreSQL cluster fields, see this Terraform provider guide.
-
To set up statistics collection, add the
performance_diagnosticsblock to theconfigsection:resource "yandex_mdb_postgresql_cluster" "<cluster_name>" { ... config { ... performance_diagnostics { enabled = <activate_statistics_collection> sessions_sampling_interval = <session_sampling_interval> statements_sampling_interval = <statement_sampling_interval> } ... } ... }Where:
enabled: Enables statistics collection,trueorfalse.sessions_sampling_interval: Session sampling interval, from5to86400seconds.statements_sampling_interval: Statement sampling interval, from60to86400seconds.
-
Make sure 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.
-
Timeouts
The Terraform provider sets the following timeouts for Managed Service for PostgreSQL cluster operations:
- Creating a cluster, including restoration from a backup: 30 minutes.
- Updating a cluster: 60 minutes.
- Deleting a cluster: 15 minutes.
Operations exceeding the timeout are aborted.
How can I change these timeouts?
Add a
timeoutssection to the cluster description, e.g.: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 the collection of statistics 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 from5to86400.statementsSamplingInterval: Statement sampling interval. Allowed values range from60to86400.
-
Check the server response to make sure your request was successful.
-
-
To enable the collection of statistics 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 from5to86400.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 that the repository contents reside in the
~/cloudapi/directory. -
To enable the collection of statistics 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 from5to86400.statements_sampling_interval: Statement sampling interval. Allowed values range from60to86400.
-
Check the server response to make sure your request was successful.
-
-
To enable the collection of statistics 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 from5to86400.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 Performance diagnostics → Sessions.
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 that the repository contents reside 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: Start of the 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 get 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
- Navigate to Managed Service for PostgreSQL.
- Click the name of your cluster and select Performance diagnostics → Queries.
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 that the repository contents reside 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: Start of the 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 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.