Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Blog
  • Pricing
  • Documentation
© 2025 Direct Cursus Technology L.L.C.
Yandex Managed Service for PostgreSQL
  • Getting started
    • All guides
      • Viewing cluster logs
      • Performance diagnostics
      • Monitoring the state of clusters and hosts
      • Connecting to DataLens
  • Access management
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Public materials
  • Release notes

In this article:

  • Enabling statistics collection
  • Getting session statistics
  • Getting query statistics
  • Getting information about query execution plans
  1. Step-by-step guides
  2. Logs and monitoring
  3. Performance diagnostics

Performance diagnostics in Managed Service for PostgreSQL

Written by
Yandex Cloud
Updated at May 5, 2025
  • Enabling statistics collection
  • Getting session statistics
  • Getting query statistics
  • Getting information about query execution plans

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 collectionEnabling statistics collection

Management console
CLI
Terraform
REST API
gRPC API

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 CLI yet, install and initialize it.

The folder specified when creating the CLI profile is used by default. To change the default folder, use the yc config set folder-id <folder_ID> command. 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: Between 1 and 86400 seconds.
  • statements-sampling-interval: Between 60 and 86400 seconds.
  1. 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.

  2. To set up statistics collection, to the config section, add the performance_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 or false.
    • sessions_sampling_interval: Session sampling interval, from 1 to 86400 seconds.
    • statements_sampling_interval: Statement sampling interval, from 60 to 86400 seconds.
  3. Make sure the settings are correct.

    1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.

    2. Run this command:

      terraform validate
      

      Terraform will show any errors found in your configuration files.

  4. Confirm updating the resources.

    1. Run this command to view the planned changes:

      terraform plan
      

      If 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.

    2. If everything looks correct, apply the changes:

      1. Run this command:

        terraform apply
        
      2. Confirm updating the resources.

      3. 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
      }
    }
    
  1. Get an IAM token for API authentication and put it into the environment variable:

    export IAM_TOKEN="<IAM_token>"
    
  2. To enable statistics collection when creating a cluster:

    1. 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 from 1 to 86400.
      • statementsSamplingInterval: Statement sampling interval. The possible values range from 60 to 86400.
    2. View the server response to make sure the request was successful.

  3. To enable statistics collection when updating an existing cluster:

    1. Use the Cluster.Update method and send the following request, e.g., via cURL:

      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 updateMask parameter 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": <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 from 1 to 86400.
      • statementsSamplingInterval: Statement sampling interval. The possible values range from 60 to 86400.
    2. View the server response to make sure the request was successful.

  1. Get an IAM token for API authentication and put it into the environment variable:

    export IAM_TOKEN="<IAM_token>"
    
  2. 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.

  3. To enable statistics collection when creating a cluster:

    1. 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 from 1 to 86400.
      • statements_sampling_interval: Statement sampling interval. The possible values range from 60 to 86400.
    2. View the server response to make sure the request was successful.

  4. To enable statistics collection when updating an existing cluster:

    1. Use the ClusterService.Update call and send the following request, e.g., via gRPCurl:

      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_mask parameter as an array of paths[] 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 from 1 to 86400.
      • statements_sampling_interval: Statement sampling interval. The possible values range from 60 to 86400.
    2. View the server response to make sure the request was successful.

Getting session statisticsGetting session statistics

Management console
gRPC API
  1. In the management console, go to the folder page and select Managed Service for PostgreSQL.
  2. Click the cluster name and select the Performance diagnostics → Sessions tab.

To view session statistics:

  1. Specify the required time interval.
  2. (Optional) Set filters.
  3. 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:

  1. Specify the required time interval.
  2. (Optional) Set filters.
  1. Get an IAM token for API authentication and put it into the environment variable:

    export IAM_TOKEN="<IAM_token>"
    
  2. 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.

  3. Use the PerformanceDiagnosticsService.ListRawSessionStates call and send the following 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 for from_time.

    You can get the cluster ID with a list of clusters in the folder.

  4. 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 statisticsGetting query statistics

Management console
gRPC API
  1. In the management console, go to the folder page and select Managed Service for PostgreSQL.
  2. Click the cluster name and select the Performance diagnostics → Queries tab.

To view the query statistics for a specific time interval:

  1. Select the time interval you need.
  2. (Optional) Set filters.

To get information about the relative change in the query statistics:

  1. In the Interval 1 field, select the time interval to be used as a calculation basis for statistics.
  2. In the Interval 2 field, select the time interval to compare the statistics for interval 1 with.
  3. (Optional) Set filters.

Let’s assume, 10 SELECT * FROM cities queries were made in the first interval and 20 in the second. When comparing statistics, the difference by the number of queries metric (the Calls column in the table) will be +100%.

  1. Get an IAM token for API authentication and put it into the environment variable:

    export IAM_TOKEN="<IAM_token>"
    
  2. 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.

  3. Use the PerformanceDiagnosticsService.ListRawStatements call and send the following 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 for from_time.

    You can get the cluster ID with a list of clusters in the folder.

  4. 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 and pg_stat_kcache extensions documentation.

Getting information about query execution plansGetting information about query execution plans

The auto_explain module allows you to log execution plans for slow queries automatically, without the EXPLAIN command. This is useful for tracking non-optimized queries. This logging feature uses the general PostgreSQL log.

To enable query logging, change the DBMS settings:

  1. Use the Shared preload libraries field to select the auto_explain option.

  2. Enable the Auto explain log analyze setting.

  3. 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.

    • Auto explain log buffers
    • Auto explain log min duration
    • Auto explain log nested statements
    • Auto explain log timing
    • Auto explain log triggers
    • Auto explain log verbose
    • Auto explain sample rate

Was the article helpful?

Previous
Viewing cluster logs
Next
Monitoring the state of clusters and hosts
© 2025 Direct Cursus Technology L.L.C.