How the question comes up#
A deploy goes out, database CPU rises, and Query Insights shows a query near the top of the load table. The release seems to explain it, but the window also holds a backfill, a traffic peak and a nightly job. Rolling back costs a release, and rolling back for a query the old revision also sends gains nothing. The general version of this split is in bad release or unhealthy dependency?; this guide does it with the database's own load data.
Nothing fails at any of the four connection stages. The connections work and the queries are slow.
What Query Insights can group by#
Query Insights shows database load by normalized query, with literals removed, and lets you filter by database, user and client address. It also groups load by tags, which the application adds to its SQL as comments in the sqlcommenter format. Only six keys are kept, action, controller, framework, route, application and db driver; Query Insights drops all others. Recording tags is an instance setting, --insights-config-record-application-tags. Query Insights · gcloud sql instances patch
None of those dimensions is the Cloud Run revision. Every revision usually connects as the same user, and Cloud Run's outbound traffic leaves from a shared pool of dynamic addresses unless you configure a static one, so the client address does not name a revision either. Outbound IP addresses Two ways put the revision into the data:
- A tag. Set the
applicationkey from theK_REVISIONenvironment variable, which Cloud Run sets to the revision's name. The tags table then has one row per revision, with load, average execution time and call count. Container runtime contract application_name. Set the connection'sapplication_namefromK_REVISION. Query Insights does not group by it, butpg_stat_activityshows it beside each running query. Tag connections with their release
Retention limits how far back you can compare: query metrics are kept for 7 days on Cloud SQL Enterprise edition and 30 days on Enterprise Plus. A comparison with last week's release needs the earlier window to still be there. Query Insights retention
Check 1: place the release in time#
gcloud run revisions describe NEW_REVISION \
--project shop-prod --region europe-west1 \
--format='value(metadata.name, metadata.creationTimestamp)'
gcloud run services describe orders-api \
--project shop-prod --region europe-west1 \
--format='yaml(status.traffic)'
The creation time says when the revision existed, and the traffic block what serves now. The revision's first request log entries say when it began serving; use that time as the boundary for the comparison. ObjectMeta fields · TrafficTarget fields
Check 2: compare the query by revision#
With revision tags, open the Tags view for the window in which both revisions served, select the query, and compare the rows. Without tags, compare the Queries view for equal windows before and after the boundary, and sample pg_stat_activity during the slow period:
SELECT application_name, count(*) AS running,
max(now() - query_start) AS longest,
left(query, 80) AS query
FROM pg_stat_activity
WHERE state = 'active' AND backend_type = 'client backend'
GROUP BY application_name, left(query, 80)
ORDER BY running DESC;
query_start is when the active query started, and query its text. A sample shows only what runs at that moment, so repeat it a few times during the slow period. pg_stat_activity
| What the comparison shows | Reading |
|---|---|
| The query appears only under the new revision, or only after the boundary | The release added it. Look at the code path and its plan |
| Both revisions send it; average time rose for both at the same moment | Not the release. Something shared changed: data volume, a plan, a lock, another workload |
| Both send it; only the new revision's call count rose | The release calls an existing query more often, for example in a loop. Its average time may not change |
| Both send it; only the new revision's average time rose | Different parameters or a different statement shape from the new code; read the sampled plans for each |
Query Insights samples query plans, and the Query details page splits load into CPU, IO wait and lock wait. A query that rose in lock wait for both revisions points at a lock, not at the release. Query details
A worked fixture: one query, two answers#
The fixture is illustrative. orders-api tags its queries with application set to K_REVISION and route set to the request path. orders-api-00018 takes 10% of traffic at 14:05 UTC and 50% at 14:30. Database load on orders-db rises in the 14:30 hour, and the top query in Query Insights is a SELECT on order_events filtered by order_id.
Outcome A. The Tags view for 14:30 to 14:50, all queries:
| application | route | Avg execution time (ms) | Times called |
|---|---|---|---|
orders-api-00018 | /orders/{id}/history | 412 | 18,240 |
orders-api-00017 | /orders/{id} | 6 | 21,050 |
Filtered to the order_events query, only the orders-api-00018 row remains; orders-api-00017 has no history route. The release added the query, and its sampled plan shows a sequential scan of order_events. The team moves traffic back to orders-api-00017 at 14:52, and CPU returns to its earlier level by 14:55. The fix is an index, rehearsed before the next rollout.
Outcome B. Same deploy, a different picture:
| application | route | Avg execution time (ms) before 14:20 | after 14:20 |
|---|---|---|---|
orders-api-00017 | /orders/{id} | 7 | 380 |
orders-api-00018 | /orders/{id} | 7 | 395 |
Both revisions send the same query, and its average rose for both at 14:20, ten minutes before the second traffic step. The load is mostly IO wait. A backfill job began writing to order_events at 14:19. The release is not the cause, and a rollback would leave the query just as slow. The team pauses the backfill.
Where the checks mislead#
A tag is only as good as the code that sets it. A path that skips the ORM, or a connection pool shared by several services, sends untagged queries, and those are missing from the Tags view. Check that the tagged load roughly accounts for the total.
Query Insights stores normalized queries. Two calls that differ only in their literals are one row, so a slow parameter value hides inside a fast average.
During a traffic split, each revision's load scales with its share. Compare average execution time, and call counts weighed against each revision's traffic share: at 10% traffic, the new revision's total load looks small even when every call is slow.
The traffic split guide covers the related case of a schema change that only one revision tolerates.
Questions#
Can Query Insights show query load per Cloud Run revision?
Not by itself: it groups by query, database, user, client address and application tags. Put the revision name into a tag, such as the application key set from K_REVISION, and the Tags view shows load per revision.
How do I know if a slow query started with a deploy?
Compare the query's load and average execution time per revision while both serve, or before and after the moment the new revision took traffic. A query only the new revision sends came with the release.
Does Query Insights use PostgreSQL's application_name?
Its documented filters are database, user, client address and tags, and its tags come from sqlcommenter comments in the SQL. application_name is visible in pg_stat_activity, which shows the queries running at the moment you look.