---
title: "Which Cloud Run revision holds these Cloud SQL connections? | Proofline"
description: "Set application_name from Cloud Run's K_REVISION variable, then group pg_stat_activity by it to count each revision's sessions."
url: https://proofline.sh/learn/which-cloud-run-revision-holds-cloud-sql-connections/
---

[Learning center](/learn/)

# Which Cloud Run revision holds these Cloud SQL connections?

Set application\_name from Cloud Run's K\_REVISION variable, then group pg\_stat\_activity by it to count each revision's sessions. Cloud Run sets K\_REVISION in every container to the name of its revision. Without that name, the database usually cannot tell one revision's sessions from another's on Cloud Run's socket path.

Broken dependencies The Proofline team November 23, 2026 6 min read

## Why the question comes up

A connection table near its limit after a rollout raises one question before any other: which revision's instances hold the sessions? The answer decides the next step. Sessions from the revision taking traffic point at its pool and instance count; sessions from a revision with no traffic point at instances that should have gone away; sessions from no Cloud Run revision at all point at another client. The [connection budget](/learn/cloud-run-cloud-sql-connection-budget/) and the [traffic split guide](/learn/cloud-run-traffic-split-cloud-sql-connections/) both depend on this count, and so does any `53300` refusal at the admission stage of [the four stages](/learn/cloud-run-cloud-sql-connection-errors/#stage).

The general practice of naming connections after the release, with its limits (names cut at 63 characters, empty names that hide a writer), is in [tag PostgreSQL connections with the release](/learn/postgres-which-release-is-connected/). This guide covers what Cloud Run adds to it.

## Step 1: name connections after the revision

Cloud Run sets `K_REVISION` in every running container of a service to the name of the revision being run, alongside `K_SERVICE` for the service. PostgreSQL shows each session's `application_name` in `pg_stat_activity`. Clients set it at connect time as a connection parameter, and libpq-based drivers also read it from the `PGAPPNAME` environment variable. [Container runtime contract](https://cloud.google.com/run/docs/container-contract) · [`application_name`](https://www.postgresql.org/docs/current/runtime-config-logging.html) · [libpq parameters](https://www.postgresql.org/docs/current/libpq-connect.html) · [`PGAPPNAME`](https://www.postgresql.org/docs/current/libpq-envars.html)

Read the value at runtime. `K_REVISION` always names the revision the instance runs; a literal revision name written into the deploy configuration is right only until the next deploy. Two ways to do it:

-   For a libpq-based driver, export it in the container's entrypoint before the application starts: `export PGAPPNAME="$K_REVISION"`.
-   For any driver with an `application_name` connection parameter, including those that sit behind a Cloud SQL language connector, pass `os.environ["K_REVISION"]` (or your language's equivalent) when the pool creates connections.

Only printable ASCII survives, and names must stay under 64 characters in a standard build. Cloud Run revision names fit, but a prefix such as `orders-api@` adds to the length. [`application_name` limits](https://www.postgresql.org/docs/current/runtime-config-logging.html)

## Step 2: group the sessions by revision

Run this as a role that can see every session; ordinary users see full detail only for their own: [pg\_stat\_activity](https://www.postgresql.org/docs/current/monitoring-stats.html)

```
SELECT application_name,
       count(*)                                AS sessions,
       count(*) FILTER (WHERE state = 'idle')  AS idle,
       min(backend_start)                      AS first_opened,
       max(backend_start)                      AS last_opened
FROM pg_stat_activity
WHERE backend_type = 'client backend'
GROUP BY application_name
ORDER BY sessions DESC;
```

`backend_start` is the time the client connected, and `state` is `idle` while the session waits for a command. Then list the revisions and where traffic goes:

```
gcloud run services describe SERVICE \
  --project PROJECT --region REGION \
  --format='yaml(status.traffic)'
```

For the past, Cloud SQL's `database/postgresql/num_backends_by_application` metric counts connections grouped by application, sampled every 60 seconds, and Cloud Run's `container/instance_count` gives each revision's instances by `active` and `idle`. [Cloud SQL metrics](https://cloud.google.com/monitoring/api/metrics_gcp_c) · [Cloud Run metrics](https://cloud.google.com/monitoring/api/metrics_gcp_p_z)

| A revision's group | Its traffic and instances | Reading |
| --- | --- | --- |
| Sessions, revision takes traffic | Instances running | Expected. Divide sessions by instances and compare with the pool limit |
| Sessions, revision takes no traffic | Idle instances, last request under 15 minutes ago | Idle instances that have not been shut down yet; they can stay up to 15 minutes |
| Sessions, revision takes no traffic | Instances running for longer than that | Something keeps them: a revision-level minimum on a revision that is tagged or still referenced in the split |
| Sessions, revision has no instances | Zero | Sessions that outlived their instance. See [sessions left after shutdown](/learn/cloud-run-shutdown-cloud-sql-connections/) |
| Empty name | Not applicable | A client that sets no name: another service, a job, a tool, or a revision built before the change |

[Idle instances](https://cloud.google.com/run/docs/about-instance-autoscaling) · [Minimum instances and tags](https://cloud.google.com/run/docs/configuring/min-instances)

## When the application sets no name

Without a name, `pg_stat_activity` still has `usename`, `backend_start` and `client_addr`, and each has a limit.

`usename` separates revisions only if they log in as different database users, which is rare.

`backend_start` can be lined up with instance starts. Cloud Run logs each start in `varlog/system`, with the instance ID and a reason, and the revision in `resource.labels.revision_name`. Sessions opened in a minute when only one revision started instances probably belong to it. Once both revisions scale in the same minutes, the method stops working. [Instance scaling logs](https://cloud.google.com/run/docs/logging)

`client_addr` is the IP address of the client connected to the backend. On Cloud Run's built-in connection, the instance reaches the database through the Cloud SQL Auth Proxy, and Cloud SQL's documentation does not say which address a proxied session reports, so do not read it as an instance's address on that path. On any path, an address identifies a revision only if no other revision shares that source address. [pg\_stat\_activity](https://www.postgresql.org/docs/current/monitoring-stats.html) · [Connect from Cloud Run](https://cloud.google.com/sql/docs/postgres/connect-run)

If none of these separates the revisions, deploy the naming change first and count again on the next rollout.

## A worked fixture: 120 sessions from a revision with no traffic

The fixture is illustrative. `orders-api` names its connections from `K_REVISION`. `orders-api-00018` took all traffic at 09:12:30 UTC on 2026-11-23. Each instance runs one pool of 10. At 09:40 the database holds 302 sessions, and someone asks whether the new revision is leaking.

| application\_name | sessions | idle | first\_opened (UTC) | last\_opened (UTC) |
| --- | --- | --- | --- | --- |
| `orders-api-00018` | 160 | 98 | 09:12:31 | 09:38:02 |
| `orders-api-00017` | 120 | 120 | 2026-11-21 08:00:14 | 09:11:50 |
| (empty) | 22 | 19 | 2026-11-20 06:00:03 | 09:30:11 |

`instance_count` shows 16 instances for `orders-api-00018`: 160 sessions is 10 each, the pool limit, so no leak. `orders-api-00017` gets no traffic and still runs 12 idle instances 28 minutes after the rollout, past the 15-minute idle window. `status.traffic` shows why: `orders-api-00017` carries the tag `prev`, added during an earlier incident, and the revision has `autoscaling.knative.dev/minScale: '12'`. A tagged revision with a revision-level minimum starts its minimum instances and keeps them active with no requests, and each keeps its pool. The following command changes the service's traffic configuration by removing the tag, after which those instances are no longer kept:

```
gcloud run services update-traffic orders-api \
  --project shop-prod --region europe-west1 \
  --remove-tags prev
```

[`update-traffic`](https://cloud.google.com/sdk/gcloud/reference/run/services/update-traffic)

On the next rollout, `orders-api-00019` replaces `orders-api-00018` with no tags involved. At 10:20, five minutes after the step, `orders-api-00018` still shows 70 idle sessions; at 10:31 the group is gone and `instance_count` for that revision reads zero. That is the idle window ending, and it needs no action.

The empty group belongs to `orders-worker` and the analysts' tools, which set no name. Its 22 sessions match the "other clients" figure in the budget, so it is not the cause here.

## Where the checks mislead

A name is only as reliable as the code that sets it. A job built from the same image, or a local script run with the same environment, reports the same revision name, so read `usename` and the session's timing alongside it.

A connection pooler between Cloud Run and PostgreSQL may report its own name or pass the client's through. Check what yours shows before relying on the grouping.

`pg_stat_activity` is a snapshot. A count taken after an incident shows who holds connections now; the metrics hold the minutes you need when the incident is over.

## Questions

### How do I see which Cloud Run revision opened a PostgreSQL connection?

Set `application_name` to the value of `K_REVISION` when the application connects, then group `pg_stat_activity` by `application_name`. Cloud Run sets `K_REVISION` to the running revision's name in every container of a service.

### Can I set PGAPPNAME to K\_REVISION in the Cloud Run service configuration?

Set it in the container's entrypoint, where `K_REVISION` already has its value, for example `export PGAPPNAME="$K_REVISION"`. `PGAPPNAME` affects only libpq-based drivers; other drivers need the `application_name` connection parameter.

### Why does an old Cloud Run revision with no traffic still hold database connections?

Its instances may still be idle, which Cloud Run allows for up to 15 minutes after their last request, or a revision-level minimum keeps them up because the revision is tagged or still referenced in the split.

## One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. [Eligibility and fair-use terms](/free-code-review/).

[Start free with GitHub](https://proofline.sh/orgs/plan?offer=free_month)

## Related reading

-   ### [Tag PostgreSQL connections with the release that opened them](/learn/postgres-which-release-is-connected/)
    
    Breaking migrations The Proofline team November 22, 2026 3 min read
    
-   ### [Too many Cloud SQL connections during a Cloud Run traffic split](/learn/cloud-run-traffic-split-cloud-sql-connections/)
    
    Capacity overruns The Proofline team November 23, 2026 7 min read
