Performance diagnostics in Managed Service for PostgreSQL
Managed Service for PostgreSQL provides a built-in tool for DBMS cluster performance diagnostics. Use it to analyze PostgreSQL performance metrics for sessions and queries.
For information on how to identify and troubleshoot cluster performance problems, see the Performance analysis and optimization and Searching for cluster performance issues sections.
Enabling statistics collection
Enable the Statistics sampling option when creating a cluster or updating its settings (the option is disabled by default).
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
To enable statistics collection, provide the --performance-diagnostics
parameter in the update cluster 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> \
...
Acceptable parameter values include:
sessions-sampling-interval
: Between1
and86400
seconds.statements-sampling-interval
: Between60
and86400
seconds.
-
Open the current Terraform configuration file with an infrastructure plan.
For more information about creating this file, see Creating clusters.
For a complete list of available Managed Service for PostgreSQL cluster configuration fields, see the Terraform provider documentation
. -
To set up statistics collection, to the
config
section, add theperformance_diagnostics
section: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,true
orfalse
.sessions_sampling_interval
: Session sampling interval, from1
to86400
seconds.statements_sampling_interval
: Statement sampling interval, from60
to86400
seconds.
-
Make sure the settings are correct.
-
Using the command line, navigate to the folder that contains the up-to-date Terraform configuration files with an infrastructure plan.
-
Run the command:
terraform validate
If there are errors in the configuration files, Terraform will point to them.
-
-
Confirm updating the resources.
-
Run the command to view planned changes:
terraform plan
If the resource configuration descriptions are correct, the terminal will display a list of the resources to modify and their parameters. This is a test step. No resources are updated.
-
If you are happy with the planned changes, apply them:
-
Run the command:
terraform apply
-
Confirm the update of 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
timeouts
block 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 place it in the environment variable:
export IAM_TOKEN="<IAM_token>"
-
To enable statistics collection when creating a cluster:
-
Use the Cluster.create method and add the
configSpec.performanceDiagnostics
parameter to the cURL command for cluster creation: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": <activate_statistics_collection:_true_or_false>, "sessionsSamplingInterval": "<session_sampling_interval>", "statementsSamplingInterval": "<statement_sampling_interval>" }, ... }, ... }'
Where
configSpec.performanceDiagnostics
represents the statistics settings:enabled
: Enables statistics collection.sessionsSamplingInterval
: Session sampling interval. The possible values range from1
to86400
.statementsSamplingInterval
: Statement sampling interval. The possible values range from60
to86400
.
-
View the server response to make sure the request was successful.
-
-
To enable statistics collection when updating an existing cluster:
-
Use the Cluster.update method and make a request, e.g., via cURL
:Warning
This API method overrides all parameters of the object being modified that were not explicitly passed in the request to the default values. To avoid this, list the settings you want to change in the
updateMask
parameter (one line separated by commas).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": <activate_statistics_collection:_true_or_false>, "sessionsSamplingInterval": "<session_sampling_interval>", "statementsSamplingInterval": "<statement_sampling_interval>" } } }'
Where
configSpec.performanceDiagnostics
represents the statistics settings:enabled
: Enables statistics collection.sessionsSamplingInterval
: Session sampling interval. The possible values range from1
to86400
.statementsSamplingInterval
: Statement sampling interval. The possible values range from60
to86400
.
-
View the server response to make sure the request was successful.
-
-
Get an IAM token for API authentication and place it in the environment variable:
export IAM_TOKEN="<IAM_token>"
-
Clone the cloudapi
repository:cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
Below, we assume the repository contents are stored in the
~/cloudapi/
directory. -
To enable statistics collection when creating a cluster:
-
Use the ClusterService/Create method and add the
config_spec.performance_diagnostics
parameter to the grpcurl cluster creation command: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": <activate_statistics_collection:_true_or_false>, "sessions_sampling_interval": "<session_sampling_interval>", "statements_sampling_interval": "<statement_sampling_interval>" }, ... }, ... }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.ClusterService.Create
Where
config_spec.performance_diagnostics
represents the statistics collection settings:enabled
: Enables statistics collection.sessions_sampling_interval
: Session sampling interval. The possible values range from1
to86400
.statements_sampling_interval
: Statement sampling interval. The possible values range from60
to86400
.
-
View the server response to make sure the request was successful.
-
-
To enable statistics collection when updating an existing cluster:
-
Use the ClusterService/Update call and make a request, e.g., via gRPCurl
:Warning
This API method will assign default values to all object parameters not explicitly set in the request. To avoid this, list the settings you want to change in the
update_mask
parameter 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": <activate_statistics_collection:_true_or_false>, "sessions_sampling_interval": "<session_sampling_interval>", "statements_sampling_interval": "<statement_sampling_interval>" } } }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.ClusterService.Update
Where
config_spec.performance_diagnostics
represents the statistics collection settings:enabled
: Enables statistics collection.sessions_sampling_interval
: Session sampling interval. The possible values range from1
to86400
.statements_sampling_interval
: Statement sampling interval. The possible values range from60
to86400
.
-
View the server response to make sure the request was successful.
-
Getting session statistics
- In the management console
, go to the folder page and select Managed Service for PostgreSQL. - Click the cluster name and select the Performance diagnostics → Sessions tab.
To view session statistics:
- Specify the required time interval.
- (Optional) Set filters.
- Select the required data segment.
To show or hide individual categories, click the category name in the chart legend.
To view the history of queries run during a session:
- Specify the required time interval.
- (Optional) Set filters.
-
Get an IAM token for API authentication and place it in the environment variable:
export IAM_TOKEN="<IAM_token>"
-
Clone the cloudapi
repository:cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
Below, we assume the repository contents are stored in the
~/cloudapi/
directory. -
Use the PerformanceDiagnosticsService/ListRawSessionStates call and make a request, e.g., via gRPCurl
: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.ListRawSessionStates
Where:
from_time
: Left boundary of a time range in RFC-3339 format, e.g.:2024-09-18T15:04:05Z
.to_time
: Right boundary of a time range, the format is the same as forfrom_time
.
You can get the cluster ID with a list of clusters in the folder.
-
View the server response to make sure the request was successful.
For more information about what statistics you can get, see the PostgreSQL documentation
Getting query statistics
- In the management console
, go to the folder page and select Managed Service for PostgreSQL. - Click the cluster name and select the Performance diagnostics → Queries tab.
To view the query statistics for a specific time interval:
- Select the time interval you need.
- (Optional) Set filters.
To get information about the relative change in the query statistics:
- In the Interval 1 field, select the time interval to be used as a calculation basis for statistics.
- In the Interval 2 field, select the time interval to compare the statistics for interval 1 with.
- (Optional) Set filters.
Let’s assume, 10 SELECT * FROM cities
queries were made in the first interval and 20 in the second. If we compare the statistics, the difference according to the number of queries
metric (the Calls
column in the table) will be +100%
.
-
Get an IAM token for API authentication and place it in the environment variable:
export IAM_TOKEN="<IAM_token>"
-
Clone the cloudapi
repository:cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
Below, we assume the repository contents are stored in the
~/cloudapi/
directory. -
Use the PerformanceDiagnosticsService/ListRawSessionStates call and make a request, e.g., via gRPCurl
: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.ListRawStatements
Where:
from_time
: Left boundary of a time range in RFC-3339 format, e.g.:2024-09-18T15:04:05Z
.to_time
: Right boundary of a time range, the format is the same as forfrom_time
.
You can get the cluster ID with a list of clusters in the folder.
-
View the server response to make sure the request was successful.
You can learn more about what statistics you can get in the pg_stat_statements
Getting information about query execution plans
The auto_explain
moduleEXPLAIN
command
To enable query logging, change the DBMS settings:
-
Use the Shared preload libraries field to select the
auto_explain
option. -
Enable the Auto explain log analyze setting.
-
Specify the
auto_explain
module settings:Warning
Selecting
0
for Auto explain log min duration or enabling Auto explain log timing can significantly reduce cluster performance.