This page describes how to shrink the disk size of a Cloud SQL instance.
Before you begin ΒΆ
-
The documentation refers to checking
max_wal_sizeβ at the time of writing, it must be below 5 128 MB for the operation to proceed:sqlSHOW max_wal_size; -
The documentation refers to checking installed extensions - you can use this command:
sqlSELECT * FROM pg_extension; -
Find the minimum target size and estimated duration:
bashgcloud sql instances get-storage-shrink-config <instance-name>Example output:
PlaintextThe estimated operation time is 124 minutes. minimalTargetSizeGb: '722'Choose a target size slightly above the minimum (e.g. 750 GB if the minimum is 722 GB).
Procedure ΒΆ
Warning
Shrinking disk requires taking the application offline. Schedule this operation during a maintenance window or outside office hours. Expect a downtime of 2 hours or more depending on instance size.
-
Stop the application:
bashnais app stop <app-name> -
Create a manual backup in the Cloud SQL Console (
https://console.cloud.google.com/sql/instances/<instance-name>/backups) before proceeding. -
Start the shrink operation:
bashgcloud sql instances perform-storage-shrink <instance-name> \ --storage-size=<target-size-gb> \ --no-async--no-asynckeeps the command running and shows a spinner for the duration of the operation. Completely optional. -
Optional monitoring of progress (in a separate terminal):
There is (currently) no way to monitor the progress of the operation. You can, however, check if the operation is ongoing:
List recent operations and find the one without an end time:
bashgcloud sql operations list --instance <instance-name> --limit 10Inspect the running operation, using the UUID from the previous command:
bashgcloud sql operations describe <operation-uuid>Optionally, watch for the application to scale back up (it should not during the operation):
bashwatch -n5 nais app status <app-name> -
Start the application once the operation completes:
Either:
bashnais app start <app-name>or:
bashnais app set replicas <app-name> --min 4 --max 4 -
Verify that the application is running and that disk usage is as expected.
-
Optionally delete the manual backup after the application has been verified stable β typically the next business day.
Rollback ΒΆ
If something goes wrong, restore from the manual backup you created in step 2. See the Google Cloud Console for restore options.