How to Configure Cloud SQL Slow Query Log

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up the cloud sql slow query log using managed database flags. These flags record database statements that run longer than your chosen time limit. Teams use these logs to find slow code and fix database bottlenecks. Still, teams often grant broad admin roles to developers who read these logs. Broad roles create security risks when logs contain sensitive customer text. How the cloud sql slow query log works Cloud SQL is a managed service, so you do not edit server files. Instead, you tu

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You set up the cloud sql slow query log using managed database flags. These flags record database statements that run longer than your chosen time limit. Teams use these logs to find slow code and fix database bottlenecks. Still, teams often grant broad admin roles to developers who read these logs. Broad roles create security risks when logs contain sensitive customer text.

How the cloud sql slow query log works

Cloud SQL is a managed service, so you do not edit server files. Instead, you turn on logging through the cloud provider settings for your instance. Once you set a time threshold, the engine watches every active database statement. If a statement takes too long, the engine writes it to the log. The log entry includes the run time, the user, and the query text.

Cloud SQL restricts who can change database settings and view system data. Customers cannot create or use accounts with true database superuser attributes. A provider system account holds true superuser rights on the database instance. Customers cannot delete or change these built-in system roles or users. Instead, you manage the database using the default admin role provided.

This default admin role has some superuser privileges, like creating extensions or event triggers. It can also create replication users, publications, and subscriptions for your data. The default user belongs to this admin group and can create roles and databases. You must set a password for this default user before logging in. Still, this default user still lacks true superuser or replication attributes.

What native logging solves and what it misses

Native slow query logging helps your team spot bad queries fast. You can see which app or user ran a heavy report. It solves the problem of finding hidden performance bottlenecks in production. Database admins can check the log history and add missing indexes. This keeps the database fast for all your other users and apps.

Still, native logging settings only control who logs in and views cloud logs. They do not control what individual queries do while they run in flight. When a slow statement runs, the log often records the full query text. That text might include raw emails, phone numbers, card numbers, or national IDs. Anyone with log reading rights can see those private values in plain text.

SQL Query Filtering + CloudTrail Log Analysis: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Also, engineers often ask for database login rights to test slow queries directly. Native roles let an engineer log in and run test statements all day. Those roles cannot pause a dangerous query or require approval for sensitive tables. They also cannot mask private fields in query results during live testing. Teams need a way to control queries and mask data in real time.

Managing database roles during performance tuning

When teams fix slow queries, they often share admin passwords to save time. Sharing the default admin account makes it hard to track individual actions. If two engineers use the same login, the logs show one user name. You cannot tell which person ran a heavy query that locked a table. You should give each engineer a separate user account with limited rights.

Even with separate accounts, static database roles stay active all day long. An engineer who needs ten minutes to check a query keeps those rights forever. Over time, too many people hold rights to read production tables and logs. A better approach ties database sessions to your central identity provider. You should also mask sensitive fields like card numbers or emails in results.

Add runtime guardrails and masking beside your databases and APIs

Login rules, role grants, and CI checks govern who connects to your stack. Yet any allowed script, engineer, or AI agent still runs commands with the full scope of its role. For workloads on Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes, SSH, or internal HTTP services, hoop.dev sits inline in the protocol beside the service as an open-source proxy, a sidecar for your database or API. On every production query an agent or human runs, hoop.dev decides whether to pass, mask, block, or escalate it for review.

Before a query runs, hoop.dev guardrails check the statement against your rules, an AI Analyzer you configure can score intent or hold risky calls for human approval, and hoop.dev data masking rewrites sensitive fields in responses in memory. Explore the MIT-licensed project on GitHub.

Frequently asked questions

Can I use a true superuser account to set up the slow query log?

No, Cloud SQL is a managed service and does not give customers superuser accounts. You set up logging through cloud settings rather than superuser database commands.

Does the cloud sql slow query log capture sensitive customer data?

Yes, if a slow statement contains raw values in its filter, the log records them. This means emails, phone numbers, or card numbers in queries can appear in logs.

How should engineers test slow queries without broad database roles?

Give engineers least-privilege roles and require peer approval before they run heavy test queries. Use data masking so query results rewrite matched fields like national IDs or emails.

The query runs. The emails stay hidden.