Jump to Content
DevOps & SRE

Terraform support for enabling Cloud SQL Query Insights

April 7, 2023
Karan Thapar

Product Manager, Databases, Google Cloud

Aman Tayal

Software Engineer, Databases, Google Cloud

Query Insights helps our users to detect, diagnose, and troubleshoot query performance related issues for Cloud SQL databases. Query Insights provides detailed query-level telemetry and monitoring data to empower our users to investigate performance issues, root-cause latency in the system, and fix problematic queries, all in a single user journey.

With our latest release, Query Insights can now be enabled for Cloud SQL - both Postgres and MySQL - using terraform, which is an infrastructure as code tool. Infrastructure as code allows users to build, change, and manage infrastructure in a safe, consistent, and repeatable way by defining resource configurations that users can version, reuse, and share. With this, our customers can now enable Query Insights and the supported configurations for their Cloud SQL instances through Google terraform provider (version 4.44.0 onward) in addition to the traditional way of enabling via the UI.

The various configurations that are supported via terraform include:

a. Query_insights_enabled: Default is false and can be enabled as true.

b. Query_string_length: Default is 1024 and can be configured in between 256 and 4500 in bytes. Changing this configuration will restart the instance.

c. Record_application_tags: true if Query Insights should record application tags from query when enabled.

d. Record_client_address: true if Query Insights should record client IP address when Query Insights is enabled

e. Query_plans_per_minute: Default is 5 and can be configured in between 5 and 20. 

For Query_plans_per_minute configuration, value in terraform is “known after apply”, as the default value is not defined in terraform provider.

Complete documentation for using terraform for Cloud SQL including the specific attributes and their usage can be found here.

Example tf

An example enable_insights.tf file which shows enabling the insights as well as the various configurations is as given below:

Loading...

Steps to apply the changes

To apply your Terraform configuration in a Google Cloud project, complete the following steps:

1. Launch Cloud Shell.

2. Set the Google Cloud project where you want to apply the Terraform configuration: 
export GOOGLE_CLOUD_PROJECT=PROJECT_ID

3. Create a directory and a new file within that directory. The filename must have the .tf extension—for example main.tf. In this tutorial, the file is referred to as main.tf.
mkdir DIRECTORY && cd DIRECTORY && touch enable_insights.tf

4. Copy the sample code into the newly created enable_insights.tf. <is there a github link instead>. Review and modify the sample parameters to apply to your environment. Save your changes.

5. Initialize Terraform:
terraform init

6. Review the configuration and verify that the resources that Terraform is going to create or update match your expectations:
terraform validate && terraform plan

7. Apply the Terraform configuration by running the following command and entering yes at the prompt:
terraform apply

8. Wait until Terraform displays the "Apply complete!" message. Open your Google Cloud project to view the results. In the Google Cloud console, navigate to your resources in the UI to make sure that Terraform has created or updated them.

What’s next:

Visit our docs to learn more about how Query Insights for MySQL and Postgres can help you monitor and optimize your database performance. You can quickly set up your own MySQL database with this tutorial.

Posted in