Learning center

What max_connections does this Cloud SQL for PostgreSQL instance have?

Running SHOW max_connections gives the limit in force; the instance's flags show whether it is a flag or Cloud SQL's memory-based default. Subtract the reserved slots to get what ordinary clients can use, and plan any increase as a restart.

Why the number is often not what people expect#

The limit decides when PostgreSQL starts refusing new sessions with SQLSTATE 53300, the admission failure at the last of the four stages. Before you size pools or instance counts against it, you need the exact figure, and three things make a guess unreliable:

  • Without a flag, Cloud SQL derives the default from memory, and the memory that counts is that of the largest instance in the chain of primaries. A read replica takes its default from its primary, or from the primary's primary, up to the root of the replication tree.
  • A flag set at any time since the instance was created overrides the default, and the console's machine shape says nothing about it.
  • Not every slot is available to the application. PostgreSQL keeps superuser_reserved_connections slots for superusers and reserved_connections slots for roles with pg_use_reserved_connections.

Cloud SQL flags: max_connections · Connection settings

Check 1: read the values in force#

From any session on the instance:

SELECT name, setting
FROM pg_settings
WHERE name IN ('max_connections', 'superuser_reserved_connections', 'reserved_connections');

Cloud SQL's quotas page gives the same check, SELECT * FROM pg_settings WHERE name = 'max_connections';. The slots open to ordinary roles are max_connections minus the two reservations. PostgreSQL's defaults are 3 for superuser_reserved_connections and 0 for reserved_connections; the second exists from PostgreSQL 16. Cloud SQL's supported-flag list includes reserved_connections, with a restart, and does not list superuser_reserved_connections, so you can read that one but not set it. Maximum concurrent connections · Supported flags

On Cloud SQL the superuser reserve is out of your reach. Customers cannot create or use users with the PostgreSQL superuser attribute; the postgres user belongs to cloudsqlsuperuser and does not have SUPERUSER. When the table is full, your administrative session is refused like the application's, so count the reserved slots as gone. Superuser restrictions

Check 2: flag or default?#

Read the instance's configuration without connecting:

gcloud sql instances describe INSTANCE \
  --project PROJECT \
  --format='yaml(settings.tier, settings.databaseFlags, masterInstanceName)'

If settings.databaseFlags lists max_connections, that value applies. If it does not, Cloud SQL uses its default, which depends on the memory of the largest instance in the chain of primaries. masterInstanceName names the primary of a read replica; follow it up and read each primary's tier too. A custom tier's name carries its CPUs and memory, as in db-custom-{CPUs}-{Memory}. Instance resource · Machine series · gcloud sql instances describe

Cloud SQL's flag table gives these defaults:

Memory of the largest instance in the chainDefault max_connections
tiny (about 0.5 GB)25
small (about 1.7 GB)50
from 3.75 GB100
from 6 GB200
from 7.5 GB400
from 15 GB500
from 30 GB600
from 60 GB800
120 GB or more1,000

When the value from Check 1 matches neither the flag nor the table, check which instance the session reached: a replica and its primary can carry different values, and the connection name in the session's connector or socket path is the reliable reference.

Check 3: what raising it involves#

QuestionWhat the documentation saysConsequence
Does it need a restart?The flag table marks max_connections as requiring a restart; PostgreSQL sets it only at server startEvery session drops. Schedule it, and expect clients to reconnect
What about replicas?A replica's value must be at least the primary's. Changes on the primary propagate to replicas set lower or left at the default, and restart themOne change can restart the primary and each affected replica
How high can it go?max_connections, autovacuum_max_workers and max_worker_processes together cannot exceed 262142A hard ceiling, rarely the binding one
What does it cost?PostgreSQL sizes some resources, including shared memory, directly from max_connectionsMore slots take memory before any session opens; more active sessions take CPU

Supported flags · Connection settings

The following command changes the instance: it sets the flag list and restarts the database. --database-flags replaces every flag previously set, and any flag you leave out returns to its default, so include the existing flags from Check 2:

gcloud sql instances patch INSTANCE \
  --project PROJECT \
  --database-flags=max_connections=NEW_VALUE,EXISTING_FLAG=EXISTING_VALUE

Configure database flags · gcloud sql instances patch

If more slots are not the answer, the client side has the other levers: the Cloud Run connection budget compares capping instances, shrinking pools and raising the limit.

A worked fixture: two instances, two sources#

The fixture is illustrative. orders-db runs db-custom-2-7680, 7.5 GB of memory. Its read replica orders-db-replica runs db-custom-1-3840, 3.75 GB, and serves the reporting queries of orders-api.

The replica. An engineer sizing the reporting pool reads the replica's tier and the table, expects 100 slots, and plans for that. On the replica:

namesetting
max_connections400
superuser_reserved_connections3
reserved_connections0

gcloud sql instances describe orders-db-replica shows no max_connections among its flags and masterInstanceName: orders-db. The default follows the largest instance in the chain, the 7.5 GB primary, so the replica has 400 slots and 397 for ordinary roles. The pool plan was four times too small, and nothing needs to change on the database.

The primary, a month later. At 14:20 UTC one afternoon, orders-api reports 53300 with about 195 sessions open, far below the 397 everyone assumed. On the primary, max_connections reads 200. settings.databaseFlags lists max_connections: '200', and gcloud sql operations list --instance orders-db shows an UPDATE at 02:10 UTC the same night, during a cost review that had also set two logging flags. Usable slots are now 197. The decision is whether 200 was intended. If not, restore 400 with a planned restart, keeping the logging flags in the same --database-flags list. Cloud SQL operations

Where the checks mislead#

The default follows memory, so a figure read before a tier change describes the old machine. Read max_connections again after any resize of the instance or of a primary above it.

The slot count is not the number the application can use. Other roles, jobs and administrative tools share the table, and pg_stat_activity grouped by usename shows who holds the rest. Ordinary users see full detail only for their own sessions, so run that query as a role that can see all of them. pg_stat_activity

With Managed Connection Pooling enabled, max_connections also bounds the server connections its poolers can open, and Cloud SQL recommends reserving at least 15 per vCPU for them with the default pool size. Managed Connection Pooling and Cloud Run

Questions#

What is the default max_connections on Cloud SQL for PostgreSQL?

It depends on the memory of the largest instance in the chain of primaries: 25 for about 0.5 GB up to 1,000 for 120 GB or more, with 100 from 3.75 GB and 400 from 7.5 GB. A max_connections flag overrides it.

Does changing max_connections on Cloud SQL restart the instance?

Yes. Cloud SQL marks the flag as requiring a restart, and PostgreSQL reads it only at server start. Replicas whose value is lower than the new primary value, or left at the default, are restarted as well.

Why does my small read replica allow as many connections as its primary?

Without a flag, the default comes from the memory of the largest instance in the chain of primaries, so a replica inherits the default its primary's memory gives. A replica's value must also be at least the primary's.

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