Learning center

After a Cloud SQL restore or clone, which instance does each Cloud Run revision use?

After a point-in-time recovery, a clone or an instance replacement, a Cloud Run revision can keep writing to the old Cloud SQL instance without a single error. The clone has the same users and passwords, so both instances accept the login. Compare what each revision and job is configured to reach with where the sessions actually arrive, on both instances, before you retire the old one.

Why nothing fails#

A clone is a new, independent copy of the source instance, with its own name and its own IP addresses. A point-in-time recovery always creates a new instance as well. Restoring a backup to a different, existing instance overwrites that instance's data instead. In all three cases, the instance the service should use after the operation is not the one it used before, unless the restore overwrote the original. Clone instances · Point-in-time recovery · Restore to a different instance

Because the clone copies the database, including its roles, a revision that still points at the old instance connects, authenticates and runs its queries. None of the four stages fails. The symptoms are in the data: a record a customer saved is missing on the new instance, a report built on the new instance shows fewer orders than the service accepted, or the old instance's connection count does not fall after the cutover.

Check 1: what each revision and job targets#

A revision can name its instance in up to three places, and a cutover has to change all of the ones it uses:

WhereWhat it holdsRead it with
run.googleapis.com/cloudsql-instances annotationConnection names whose sockets Cloud Run mounts under /cloudsql/metadata.annotations
A plain environment variableA host, socket path or connection namespec.containers[].env
A secretA whole connection string, read through a versionspec.containers[].env[].valueFrom.secretKeyRef
for r in $(gcloud run revisions list --service orders-api \
    --project shop-prod --region europe-west1 --format='value(metadata.name)'); do
  gcloud run revisions describe "$r" \
    --project shop-prod --region europe-west1 \
    --format='yaml(metadata.name, metadata.annotations, spec.containers[0].env)'
done

gcloud run jobs describe JOB --project shop-prod --region europe-west1 --format=yaml

The annotation applies to revisions and to job executions. --add-cloudsql-instances appends to the list and --set-cloudsql-instances replaces it, so a cutover made with --add leaves the old socket mounted and reachable. Annotations by resource · gcloud run deploy

A secret reference names a version, latest or an alias, and the value behind it can still carry the old connection name. Read which version each revision references, then compare the versions' creation times with the cutover; a pinned version created before it can only hold the old target. Secret references

Then list the instances themselves, with their connection names and IP addresses, to match against what the revisions hold:

gcloud sql instances describe INSTANCE --project shop-prod \
  --format='yaml(name, connectionName, ipAddresses, createTime, state)'

Instance fields

Check 2: where the sessions arrive#

Configuration tells you intent. The databases tell you what happened. Cloud SQL's database/postgresql/num_backends metric counts connections per instance; after the cutover, the old instance's count should fall to your own administrative sessions. Cloud SQL metrics

To see which code holds them, connect to each instance and group its sessions. This works when the applications set application_name from K_REVISION and jobs from CLOUD_RUN_EXECUTION: Container runtime contract · Tag connections with their release

SELECT usename, application_name, count(*) AS sessions,
       max(backend_start) AS newest
FROM pg_stat_activity
WHERE backend_type = 'client backend'
GROUP BY usename, application_name
ORDER BY newest DESC;

pg_stat_activity

From inside the application, inet_server_addr() returns the address on which the server accepted the connection, which you can match against the instances' ipAddresses. It returns NULL for a Unix-socket connection, which is what Cloud Run's built-in connection uses, so on that path it cannot tell the instances apart; use the session query on both instances instead. System information functions

Old instance after the cutoverNew instanceReading
Only your own sessionsThe service's current revisionThe cutover reached everything that connects
Sessions from the current revisionSome sessions from it tooThe revision reaches both: one code path uses another variable or secret
Sessions from a job or another serviceThe serviceThe service moved; the job's own configuration did not
Sessions with an empty application_nameAnyUnknown clients. Match usename and backend_start with the jobs' schedules before deciding

A worked fixture: a restore that split the writes#

The fixture is illustrative. A bad data migration corrupted orders.state at 05:40 UTC on 2026-11-23. At 06:10 the team runs a point-in-time recovery of orders-db to 05:39, creating orders-db-pitr (shop-prod:europe-west1:orders-db-pitr). At 06:55 they deploy orders-api-00019 with --add-cloudsql-instances shop-prod:europe-west1:orders-db-pitr and DB_SOCKET=/cloudsql/shop-prod:europe-west1:orders-db-pitr. The plan is to retire orders-db at 12:00.

At 11:30, num_backends on orders-db is still above the single administrative session the team expected.

Outcome A. num_backends reads 9. The session query on orders-db:

usenameapplication_namesessionsnewest
orders_apporders-api-00019611:27:41
orders_apporders-export-k2v8d211:00:03
postgrespsql111:29:50

orders-api-00019 should not be here. Its description shows cloudsql-instances with both connection names, because the deploy used --add, and a second variable, DB_URL_REFUNDS, from the secret orders-db-refunds-url at version 2, created on 2026-10-02 and naming orders-db. The refunds path has written to the old instance since 06:55. The team stops the retirement, deploys orders-api-00020 with the refunds secret at a new version naming orders-db-pitr and --set-cloudsql-instances listing only the new instance, and reconciles the refunds written to orders-db between 06:55 and the new deploy.

Outcome B. num_backends reads 3, and the same query shows only the two orders-export-k2v8d sessions and psql. The service moved cleanly. gcloud run jobs describe orders-export shows the job's own cloudsql-instances annotation still naming orders-db, so the hourly export has read the old instance, whose orders stopped at 06:55. The team updates the job and checks the reports it produced since 07:00.

Where the checks mislead#

A session query is a snapshot. A job that runs daily, or a code path used once an hour, may hold no session at the moment you look. Read num_backends on the old instance over a full day, or keep the old instance and watch it before you delete it.

Cloud SQL's sampled metrics take up to a few minutes to appear, so a count read right after a deploy may still show the previous minute.

A pooler between Cloud Run and Cloud SQL hides the client's application_name unless it passes it through, and the database sees the pooler's own address.

If the restore overwrote the original instance in place, both checks look normal and the question becomes whether the data is right, not where it goes. The traffic side of a cutover, two revisions serving at once, is in a schema change during a traffic split.

Questions#

Does a Cloud SQL clone keep the same connection name and IP address?

No. A clone is a new instance with its own name, so its connection name differs, and Cloud SQL gives it new IP addresses. Every revision, job and secret that named the old instance still reaches the old one.

Why do no errors appear when Cloud Run connects to the old Cloud SQL instance?

The old instance is still running and holds the same users and passwords as the copy, so the login and the queries succeed there. Only the data differs.

How do I find which Cloud SQL instance a Cloud Run revision uses?

Read the revision's Cloud SQL connections annotation, its environment variables and the secret versions it references, then check each instance's sessions for that revision's application_name. The configuration shows what it can reach; the sessions show what it actually uses.

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.

Start free with GitHub

Related reading