Learning center

Turning on Cloud SQL logical decoding: what the restart costs Cloud Run

Enabling logical replication on Cloud SQL for PostgreSQL means setting the cloudsql.logical_decoding flag, which restarts the instance. Measure what the last restart or maintenance cost your Cloud Run service, set every restart-requiring flag in one change, and schedule it where that cost is acceptable. If the service took minutes to recover last time, fix its reconnects first.

What the flag change does#

Cloud SQL enables logical decoding through its own flag, cloudsql.logical_decoding, rather than PostgreSQL's wal_level. Its default is off, and changing it requires a restart. The logical replication guide adds that the other flags it discusses need a restart too, and suggests reading them all before choosing settings so you restart once. Among them, max_replication_slots and max_wal_senders also require a restart, and max_wal_senders must be at least max_replication_slots. If you use pglogical, cloudsql.enable_pglogical is one more. Configure logical replication · Cloud SQL flags

Three consequences reach beyond the flag:

  • Replicas restart as well. When a primary restarts, its replicas restart to match its configuration.
  • The restart drops connections. Restarting drains the instance's connections and stops it, then starts it to accept new ones; it also empties the instance's cache, so queries run slower for a while after. Restart an instance
  • WAL grows. PostgreSQL implements logical decoding by writing additional information to its write-ahead log, and a replication slot that nobody reads holds WAL indefinitely. That is the topic of an inactive replication slot fills Cloud SQL storage.

For the Cloud Run service, a restart is a stage-4 event in the four stages: sessions that were working end, and new connections fail until the instance accepts them again. The same restart, made without warning, is the incident in a flag change mistaken for a bad deploy.

Check 1: what did the last restart cost the service?#

Your own history is better evidence than any estimate. Cloud SQL records each RESTART, UPDATE and MAINTENANCE operation with its start and end times: Cloud SQL operations · gcloud sql operations list

gcloud sql operations list --instance orders-db --project shop-prod \
  --limit 50 \
  --format='table(operationType, status, startTime, endTime)'

Pick the most recent one that restarted the database, and read the service's errors from a minute before its start until the errors stop:

gcloud logging read \
  'resource.type="cloud_run_revision"
   AND resource.labels.service_name="orders-api"
   AND severity>=ERROR
   AND timestamp>="OP_START_MINUS_1M" AND timestamp<="OP_END_PLUS_30M"' \
  --project shop-prod --order=asc --limit=500 \
  --format='value(timestamp, resource.labels.revision_name, labels.instanceId, textPayload, jsonPayload.message)'

Errors from PostgreSQL ending a session during shutdown carry SQLSTATE 57P01 (admin_shutdown); attempts during the restart fail to connect. PostgreSQL error codes

What the service's logs showReading
Errors from the operation's start until shortly after its end, then noneThe service reconnects on its own. The restart costs about the operation's length in failed requests
Errors that continue long after the operation ended, from instances that started before itThe application's pool kept broken connections. The restart costs until those instances stop. Fix the pool's validation or reconnect first
Errors after the end that follow a surge of new connectionsThe service reconnected all at once, possibly against max_connections. Spread the reconnects before the next restart

Google's maintenance guidance points the same way: dropped connections and failed in-flight transactions are the expected transient errors, and connection pools plus retries with exponential back-off are the recommended defence. Minimize the impact of maintenance

Check 2: what the change will set#

gcloud sql instances patch --database-flags replaces every flag set on the instance; a flag missing from the list returns to its default. Read the current flags first, then pass them all with the new ones:

gcloud sql instances describe orders-db --project shop-prod \
  --format='yaml(settings.databaseFlags)'

Set a database flag · gcloud sql instances patch

The patch is the state change. It restarts orders-db and its replicas, and it sets the listed flags and resets any you leave out:

gcloud sql instances patch orders-db --project shop-prod \
  --database-flags=EXISTING_FLAGS,cloudsql.logical_decoding=on,max_replication_slots=12,max_wal_senders=14

The values above are an example; size slots and senders for every consumer, counting read replicas, which use a slot and a WAL sender each. The --diff flag shows what changed as a result.

Decide: when to restart#

Last restart costChoose
Errors for roughly the operation's lengthA window with the fewest requests, and announce it to callers that can retry
Errors that lasted until instances recycledFix reconnects first, then test them with a restart of a non-production instance under load
A connection surge after the restartLower pool sizes or stagger reconnects, then schedule

Also check what runs in the window. A transaction still open when its connection drops does not commit, so a batch job or backfill caught by the restart has to start that transaction again. A Cloud Run job's task that fails is retried, by default up to 3 times, which can retry straight into the restart. Job retries

A worked fixture: one flag, two schedules#

The fixture is illustrative. The team wants to stream orders changes out of shop-prod:europe-west1:orders-db, which has one read replica, orders-db-replica. The last database restart was maintenance on 2026-11-10.

Outcome A. gcloud sql operations list shows MAINTENANCE from 03:02:11 to 03:03:05 UTC. The service's errors:

Time (UTC)RevisionError
03:02:12orders-api-00017SQLSTATE 57P01 on 6 requests
03:02:14 to 03:03:05orders-api-00017The driver cannot connect to the socket's server, 41 requests
03:03:21orders-api-00017Last error, a pool timeout

Errors stopped 16 seconds after the operation ended. The service reconnects. The team schedules the patch for 03:00 on a weekday, with cloudsql.logical_decoding, max_replication_slots and max_wal_senders in the same change, and the existing log_lock_waits flag included. The nightly export job runs at 01:00, so nothing long runs at 03:00.

Outcome B. Same maintenance window, but orders-worker, a second service, logged errors until 03:31:40. Its errors came from four instances that started before 03:02 and none from instances started after. Its pool kept the dropped connections and handed them out until each instance stopped. A restart would cost orders-worker half an hour. The team adds connection validation to its pool, restarts a staging instance at 14:00 with the worker under load, sees errors end 20 seconds after the restart, and schedules production for the following week.

Where the checks mislead#

A maintenance event is not identical to a flag restart. It switches to an updated VM and may last a different time. Use it to judge how the service recovers, not how long the restart will take.

The logs show errors that the service logged. A client that retries silently, or a request that waits and succeeds slowly, does not appear at ERROR severity; read latency for the same window too.

Replicas restart after the primary. A service that reads from a replica sees its own outage, at a slightly different time.

After the flag is on, the work is the replication itself. Cutting over to a logically replicated database has its own trap with sequences: set sequences before the cutover.

Questions#

Does enabling cloudsql.logical_decoding restart the Cloud SQL instance?

Yes. Cloud SQL's flag reference lists it as requiring a restart, and a restart of a primary also restarts its replicas.

Can I enable logical decoding without downtime for Cloud Run?

The restart drops every connection and refuses new ones until the instance is back. The service avoids failed user requests only if it retries across that window. The last restart in the operations list shows how the service handled one.

Why did my Cloud Run service keep failing after Cloud SQL came back?

Its connection pool likely still held connections that the restart closed, and handed them to requests. Instances started after the restart connect fresh, which is why errors come only from older instances.

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