Learning center

A schema change during a Cloud Run traffic split: which revision must accept it?

While a Cloud Run service splits traffic, every revision in the split, and every tagged revision, queries the same Cloud SQL schema. A migration that lands during the split must work for the oldest of them. A change that removes something the old revision uses waits until that revision holds no sessions and you have given it up as a rollback target. Read the service's traffic block for who can receive requests, and pg_stat_activity for who still holds connections.

How the mismatch shows up#

A traffic split makes two releases serve at once, for as long as the split lasts. A schema change applied in that window meets both. The errors come from whichever revision the change did not plan for:

  • The old revision fails after the migration. A dropped or renamed column gives SQLSTATE 42703 (undefined_column) on the old revision's queries. An added column can fail it too: a connection that prepared SELECT * before the migration gets cached plan must not change result type until the statement is prepared again. PostgreSQL error codes · The cached-plan error after adding a column
  • The new revision fails before the migration. If the new revision takes its share of traffic before its migration has run, its queries name a column that does not exist yet.

Errors from one revision only, starting at a migration's timestamp, point to this mismatch rather than to the release's logic. The four stages of a connection all succeed here; the failure is in the queries.

Which revision meets which schema#

Each change in a schema migration is either additive, meaning the old revision can ignore it, or it removes or changes something the old revision reads or writes. The order of the deploy and the migration decides which revision has to tolerate what:

OrderThe old revision meetsThe new revision meetsRequirement
Migration, then deploy and splitThe new schemaThe new schemaThe migration must be additive for the old revision
Deploy and split, then migrationThe new schema, from the migration onThe old schema, until the migrationThe new revision must also work with the old schema
Contract: drop, rename, NOT NULL on a column the old release does not writeThe new schemaThe new schemaNo session from the old revision, and no plan to roll back to it

The first row is the usual expand step. The second row is what happens when a migration runs from the application's startup or from a job that finishes after the new revision already serves. The third row is the decision this guide is about: when the old revision is really gone. A move between two shapes of data takes several such steps, which moving data while two releases share one database follows end to end.

Check 1: which revisions can still receive requests#

The service's status lists every traffic target, each with revisionName, percent and an optional tag. A tag gives the revision its own URL that reaches it regardless of its percentage, so a revision at 0% with a tag can still receive requests from anything that calls that URL. TrafficTarget fields · Traffic tags

gcloud run services describe orders-api \
  --project shop-prod --region europe-west1 \
  --format='yaml(status.traffic)'

Session affinity changes how the percentages apply. With affinity on, a client's later requests return to the revision it first reached, and affinity takes precedence over the split; when the split changes, Cloud Run moves as few of those clients as it can. A revision at 10% can serve more than 10% of requests. Session affinity and traffic splitting

Cloud Run also starts instances for a revision that has a tag when that revision has a revision-level minimum instances setting, even with no traffic. Minimum instances and revisions

Check 2: which revision still holds connections#

A revision that receives no new requests can still hold database sessions. Cloud Run may keep an instance idle for up to 15 minutes after its last request, and an idle instance might keep its open database connections. The run.googleapis.com/container/instance_count metric, grouped by revision_name and its state label (active or idle), shows those instances. Idle instances · Cloud Run metrics

The database's view is more direct, if each connection names its revision. Cloud Run sets K_REVISION in every container; pass it as the connection's application_name, and group the sessions:

SELECT application_name, count(*) AS sessions,
       max(backend_start) AS newest, max(state_change) AS last_activity
FROM pg_stat_activity
WHERE datname = 'orders'
GROUP BY application_name
ORDER BY application_name;

backend_start is when the client connected, and state_change when the session last changed state, such as finishing a query. Container runtime contract · pg_stat_activity · Tag connections with their release

Old revision in the traffic blockOld revision's sessionsReading
Has a percentageAnyIt serves. Only additive changes
0% with a tagSessions with recent last_activitySomething calls the tag URL. Find the caller, or remove the tag, before a contract step
Not listedSessions, no recent activityIdle instances that have not stopped. Wait, then query again
Not listedNone, over a full cycle of the jobs that share the databaseNo connection from the old revision. Whether to contract is now a rollback decision

A worked fixture: the drop that waited#

The fixture is illustrative. orders-api-00017 reads and writes orders.status. orders-api-00018 reads state and writes both. At 14:00 UTC an expand migration adds state with a trigger that fills it for old writers. At 14:05 the team splits traffic 90/10 to orders-api-00018, and at 14:30 moves it to 100%, keeping the tag prev on orders-api-00017. The contract migration, dropping status, is ready.

Outcome A. At 14:52 the traffic block shows orders-api-00018 at 100 and orders-api-00017 at 0 with tag prev. The session query:

application_namesessionsnewestlast_activity
orders-api-00017214:31:1014:51:58
orders-api-000182114:50:4414:52:03

orders-api-00017 had no percentage for 22 minutes, longer than the 15 minutes an instance may stay idle, and its sessions were busy a few seconds ago. The request log for the revision shows a synthetic check calling the prev URL every minute. Dropping status now would turn those requests into 42703 errors, and would leave orders-api-00017 unable to serve if traffic moved back. The team keeps the column and moves the check to the main URL.

Outcome B. The team removes the tag at 14:55. At 15:40 instance_count for orders-api-00017 has been zero since 15:07, and the session query returns only orders-api-00018. The nightly export already reads state. The remaining question is whether orders-api-00017 must stay a rollback target. Once status is gone, a rollback needs a revision that does not read it. The team records orders-api-00018 as the earliest rollback target and runs the contract migration at 15:45.

Where the checks mislead#

An empty application_name hides which revision a session belongs to. Count such sessions as unknown, never as the new revision's.

Connections that go through a pooler outside Cloud Run report what the pooler sends, and a pool shared by several revisions cannot tell them apart.

The traffic block covers requests. A Cloud Run job, a scheduler target or another service that runs the old code does not appear in it, and only the database's sessions show it.

Questions#

Can I run a migration while Cloud Run splits traffic between two revisions?

Yes, when the change is additive for the older revision. Both revisions run their queries against the schema from the moment it changes, so a drop or rename fails whichever revision still uses the old name.

How long after a traffic move does the old revision keep database connections?

Its instances may stay idle for up to 15 minutes and keep their connections during that time. A tag, session affinity or a revision-level minimum instances setting can keep it serving or running longer.

When can I drop a column the previous revision used?

When no session comes from that revision over a full cycle of the jobs that share the database, and you no longer need it as a rollback target.

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