Introduction Cloud SQL enforces connection limits based on instance tier. When the limit is reached, new connections are rejected, causing cascading application failures.

Symptoms - PostgreSQL: `FATAL: too many connections for role "xxx"` - MySQL: `ERROR 1040 (HY000): Too many connections` - Cloud Console shows Connections metric at maximum - Cloud SQL Proxy logs show connection refused errors

Common Causes - Connection limit reached (varies by machine type) - Connection leaks in application code - Cloud SQL Proxy not used (each app instance opens separate connections) - Long-running queries holding connections

Step-by-Step Fix 1. **Check current connections**: ```bash gcloud sql instances describe <instance-name> --format="value(settings.databaseFlags)" psql "host=/cloudsql/<project>:<region>:<instance> user=postgres" -c "SELECT count(*) FROM pg_stat_activity;" ```

  1. 1.Use Cloud SQL Auth Proxy:
  2. 2.```bash
  3. 3.cloud-sql-proxy <project>:<region>:<instance> --port 5432 --max-connections 100
  4. 4.`
  5. 5.Kill idle connections:
  6. 6.```sql
  7. 7.-- PostgreSQL
  8. 8.SELECT pg_terminate_backend(pid) FROM pg_stat_activity
  9. 9.WHERE state = 'idle' AND state_change < now() - interval '10 minutes';
  10. 10.`
  11. 11.Upgrade instance tier:
  12. 12.```bash
  13. 13.gcloud sql instances patch <instance-name> --database-flags max_connections=500 --tier db-n1-standard-4
  14. 14.`

Prevention - Always use Cloud SQL Auth Proxy with connection pooling - Set connection pool max to 80% of database max_connections - Monitor Cloud SQL connections metric with alert at 80% - Use Cloud SQL connection insights for usage analysis