Monitor active queries

This page describes how to monitor and troubleshoot the queries that are active in your database. Monitoring these queries can help identify causes of system latency and high CPU usage.

On the Query insights dashboard, you can view a summary of active transactions and a list of transactions with associated query and statistics. The transactions are displayed by the transaction start time. If there is a large number of queries running, then the results might be limited to a subset of total queries.

Before you begin

To monitor active queries, you can use either Cloud SQL Enterprise edition or Cloud SQL Enterprise Plus edition.

To terminate a session or a long-running transaction in active queries, you must use Cloud SQL Enterprise Plus edition for your Cloud SQL for PostgreSQL instance.

For more information about Cloud SQL editions, see Introduction to Cloud SQL editions.

Required roles and permissions

To get the permissions that you need to view active queries, ask your administrator to grant you the following IAM roles on the project that hosts the Cloud SQL instance:

  • View instance activity summary: Cloud SQL Viewer (roles/cloudsql.viewer)
  • View database activity summary and long-running transactions: Database Insights Viewer (roles/databaseinsights.viewer)
  • Terminate a session or long running transaction:
    • Cloud SQL Editor (roles/cloudsql.viewer)
    • Database Insights Operations Admin (databaseinsights.operationsAdmin)

For more information about granting roles, see Manage access to projects, folders, and organizations.

These predefined roles contain the permissions required to view active queries. To see the exact permissions that are required, expand the Required permissions section:

Required permissions

The following permissions are required to view active queries:

  • View database activity summary and long-running transactions:
    • databaseinsights.activeQueries.fetch
    • databaseinsights.activitySummary.fetch

You might also be able to get these permissions with custom roles or other predefined roles.

Enable active queries

To enable active queries for a Cloud SQL Enterprise edition instance, do the following:

  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. To open the Overview page of an instance, click the instance name.
  3. On the Configuration tile, click Edit configuration.
  4. In the Customize your instance section, expand Query insights.
  5. Make sure Enable Query insights is enabled.
  6. Select Active query analysis.
  7. Click Save.

To enable active queries for a Cloud SQL Enterprise Plus edition instance, do the following:

  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. To open the Overview page of an instance, click the instance name.
  3. On the Configuration tile, click Edit configuration.
  4. In the Customize your instance section, expand Query insights.
  5. Make sure Enable Query insights is enabled.
  6. Select Enable Enterprise Plus features.
  7. Select Active query analysis.
  8. Click Save.

To be able to terminate a session or long-running transaction in active queries, you must use query insights for Cloud SQL Enterprise Plus edition.

Disable active queries

To disable active queries on a Cloud SQL Enterprise edition or Cloud SQL Enterprise Plus edition instance, do the following:

  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. To open the Overview page of an instance, click the instance name.
  3. On the Configuration tile, click Edit configuration.
  4. In the Customize your instance section, expand Query insights.
  5. Clear the Active query analysis checkbox.
  6. Click Save.

View active queries

To view your active queries, complete the following steps:

  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. To open the Overview page of an instance, click the instance name.

  3. Do one of the following to display the Query insights dashboard:

    • Select the Query insights tab.
    • Click Go to Query insights for more in-depth info on queries and performance. The Query insights dashboard opens. It shows details about the instance at the top.
  4. Click the Active queries tab.

    From here, you can modify the query information that appears in the Longest running transactions table:

    • Database: filters query load on a specific database or all databases.
    • User: filters query load from a specific user account.
    • Summary scorecard of all active queries: provides an overview of all the active queries by displaying the total connections based on the following parameters:

      1. Distribution of active connections categorized by connection state.
      2. Distribution of active transactions based on transaction state.
      3. Distribution of query durations.
    • Longest running transactions: provides an overview of the top 50 running queries (in the active and idle in transaction state) based on descending execution time. You can filter and sort active queries in the table.

View normalized active queries

You can view a list of top long-running transactions with normalized active queries on the Query insights dashboard. A normalized active query removes sensitive data and returns a digest. The digest is the same for different values used in the following example:

  • Regular queries

    • select * from test_table where id=1;
    • select * from test_table_where id=2;
  • Digest or normalized query

    • select * from test_table where id=?;

A query that runs in different sessions is displayed as different entries on the dashboard.

View top longest running transactions

The Longest running transactions table on the Query insights dashboard contains the following columns:

Column name Description
Process ID Unique identifier of the connection or thread ID.
Query SQL query text.
Status Status of the connection.
Session duration Duration of the last session running.
Transaction duration Duration of the active transaction running.
Query duration Duration of the last query running in that transaction.
Wait event type Wait event type of the occurring wait event.
Wait event Occurring wait event.
Database Database name on which this connection is running.
Application name Application name on which this connection is running.
User name Name of the user connected to the database.
Client address Specific IP address of the client that sent a query.
Action Contains a link for terminating a transaction.

The display is refreshed every 60 seconds automatically.

Terminate a process

To terminate a process or long-running transaction in active queries, you must use Cloud SQL Enterprise Plus edition and enable both Active query analysis and query insights for Cloud SQL Enterprise Plus edition.

Long running operations can take longer to terminate.

To terminate a query or transaction, complete the following steps:

  1. In the Longest running transaction table, select a query.
  2. In the Action column, click Terminate connection.
  3. In the Terminate connection window, click Confirm.

If the system successfully terminates the query or transaction, then a success message appears. The system also performs a rollback if needed.

What's next