How to Configure Spanner Slow Query Log

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up a spanner slow query log by reading built-in system stats tables. You can also turn on cloud audit logs and export them to a log sink. Teams often grant broad database roles so engineers can check these performance tables. Still, wide roles let users read private app tables while they hunt for slow queries. How the spanner slow query log works in practice Google Cloud Spanner does not use a single text file for slow queries. Instead, the database engine tracks query speed inside bu

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 a spanner slow query log by reading built-in system stats tables. You can also turn on cloud audit logs and export them to a log sink. Teams often grant broad database roles so engineers can check these performance tables. Still, wide roles let users read private app tables while they hunt for slow queries.

How the spanner slow query log works in practice

Google Cloud Spanner does not use a single text file for slow queries. Instead, the database engine tracks query speed inside built-in system tables. These tables store stats over short time windows like one minute or ten minutes. They record latency, CPU time, and how many rows each statement scans.

To read these system tables, an engineer needs a database role with read rights. You can set up fine-grained roles so users only read system stats tables. Many teams also turn on data read audit logs in the cloud console. The cloud platform then sends every query record to the central log router.

From the log router, you can filter records by latency or statement type. You can route slow records to a log bucket or an analytics warehouse. Your team can then set up alerts when query times cross a set threshold. This workflow gives you a clear view of database bottlenecks over time.

What native query logging solves and what it misses

Native system tables and cloud audit logs help teams find bad query patterns. They show which statements lock rows or scan full tables without an index. Cloud roles also let you control who can log in to the database instance. This setup works well for tracking Automated app traffic and scheduled batch jobs.

Still, native logging and cloud roles leave a large operational gap open. Cloud roles decide who can log in, not what individual statements do in flight. When an engineer logs in to fix a slow query, they run live statements. A careless test query can scan millions of rows and slow down production.

End-to-End Encryption + Log Aggregation & Correlation: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Also, native logs capture events after the query has already run. They do not block a heavy statement before it hits the database engine. If a developer runs a broad select statement, the result may include private data. Native query stats strip out parameters, so you cannot see who read sensitive records. Teams need a way to check each query in flight and mask private fields.

How to keep query checks safe for your team

Start by separating performance stats from raw production tables in your role design. Grant your engineers read rights on the system stats schema only. Never share a single admin service account across the whole engineering team. Shared keys make it impossible to tie a slow ad-hoc query to one person.

When someone needs to test an execution plan, require a peer review first. You should also mask sensitive fields like emails, phone numbers, and card numbers. Masking rewrites matched fields in results before they reach the user screen. This lets engineers test query speed on real data shapes without exposing private records.

Check database and API queries on the wire

Identity tools, roles, and network rules decide who opens a session. Setup alone cannot check what each query does or mask sensitive values in a response once a session starts. When workflows touch Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, or internal HTTP APIs, hoop.dev sits inline in the wire protocol beside those services as an open-source proxy, a sidecar for your Postgres or API. On every query an AI agent or engineer runs against production, hoop.dev decides to pass, mask, block, or hold it for human review.

Your login details and client tools stay as they are while hoop.dev guardrails refuse bad deletes or drops before they run, and hoop.dev data masking rewrites matched sensitive values in responses in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and writes every query and decision to an audit trail that generates evidence for HIPAA, GDPR, and PCI DSS. See the source on GitHub.

Frequently asked questions

Does Cloud Spanner have a single slow query log switch?

No, Spanner does not use a single toggle for slow query files. Instead, it writes latency metrics to built-in query stats tables by default. You can query these tables directly or export metrics to cloud monitoring tools.

Can system stats tables expose private customer data in logs?

System stats tables replace literal values with placeholders to hide raw query inputs. Still, if a user has broad read roles, they can query raw tables directly. You must restrict table roles or mask fields like national IDs in query results.

How do we trace a slow ad-hoc query back to one engineer?

If your team shares a database role or service key, native logs show one identity. You must map each database session to an individual user identity from your provider. This ensures every statement in your audit log shows the exact person who ran it.

The query runs. The emails stay hidden.