Google uses AI technology to translate content into your preferred language. AI translations can contain errors.
缩减实例存储空间容量
本页面介绍了如何使用存储空间缩减功能管理 Cloud SQL 实例的存储容量。
准备工作
查看功能限制。
如果您使用的是 gcloud CLI,请确保您使用的是最新版本的 Google Cloud SDK 和 gcloud CLI。
- 安装最新版本的 gcloud CLI(gcloud CLI 563.0.0 版或更高版本)。
- 如需获取当前的 gcloud CLI 版本,请运行
gcloud --version。
- 如需更新 gcloud CLI 版本,请运行
gcloud components update。
获取以下 IAM 权限:
cloudsql.instances.getDiskShrinkConfig
cloudsql.instances.performDiskShrink
以下任一角色都具有这些权限。在分配角色时,我们建议您应用最小权限原则:
- Cloud SQL 管理员 (
roles/cloudsql.admin)
- Cloud SQL Editor (
roles/cloudsql.editor)
如需了解详情,请参阅以下资源:
准备实例
在开始之前,请完成以下要求。
这些步骤对于主实例和只读副本都是相同的。
如果更新的是只读副本,请务必先缩小主实例的容量,然后再缩小副本的大小。
获取要管理的实例所属项目的项目 ID。
检查实例的当前状态,确保其正在运行。一种检查方法是使用 describe 命令:
gcloud sql instances describe
返回以下状态:
RUNNABLE
如果未运行,请启动实例。
创建当前实例的备份。这样,如果您在配置过程中遇到问题,便可以在以后恢复实例。
如果您运行的是 PostgreSQL 实例,请将 max_wal_size 标志值设置为小于 5128 MB。如果 max_wal_size 值过大,实例可能启动缓慢,并且操作可能会失败。
停用所有可实现出站连接(或需要出站数据传输)的扩展程序或功能。
例如,出站连接可能使用以下扩展程序或功能之一建立,应在执行存储空间缩减操作之前将其停用:
dblink 扩展程序
PL/proxy 扩展程序
pglogical 扩展程序
- 外部数据封装容器(例如
postgres_fdw、oracle_fdw 和 tds_fdw)
- PostgreSQL 10 及更高版本中的逻辑复制
缩减实例的存储容量
以下步骤对于主实例和只读副本都是相同的。
检查实例的目标最小存储空间容量和估计的操作时间。此信息可帮助您规划存储空间缩减操作。
gcloud
运行 gcloud sql instances get-storage-shrink-config 命令:
gcloud sql instances get-storage-shrink-config INSTANCE_NAME
替换以下内容:
响应包含 minimalTargetSizeGb 的值。您可以使用此值来确定在运行存储空间缩减操作时要用于 --storage-size 参数的值。
响应还会返回一条包含估计操作时间的消息,类似于以下内容:
...
"The estimated operation time is 20 minutes".
REST v1
使用 GET 方法查看实例的目标最小存储容量。
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:主实例所在项目的名称。
- INSTANCE_ID:主实例的 ID。
HTTP 方法和网址:
GET https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/getDiskShrinkConfig
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/getDiskShrinkConfig"
PowerShell (Windows)
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/getDiskShrinkConfig" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
响应
{
"kind": "sql#instance",
"min_target_size_gb": "1234"
"message": "The size of your instance's storage impacts IOPS, and therefore shrinking your storage
may have a negative impact on IOPS. Before shrinking your storage, please refer to our
documentation on Cloud SQL limits to understand what impact shrinking your storage might have:
https://cloud.google.com/sql/docs/quotas#fixed-limits. Additionally, we recommend leaving an
appropriate buffer for operational purposes, like reindexing, etc. We recommend a buffer of at
least 100 GB or 20% more than current usage, whichever is larger. For more details, please refer
to: https://cloud.google.com/sql/docs/shared-responsibility. The estimated operation time is
20 minutes."
}
响应包含 min_target_size_gb 的值。您可以使用此值来确定在运行存储空间缩减操作时要用于 --storage-size 参数的值。
响应还会返回一条包含估计操作时间的消息,类似于以下内容:
...
"The estimated operation time is 20 minutes".
REST v1beta4
使用 GET 方法查看实例的目标最小存储容量。
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:主实例所在项目的名称。
- INSTANCE_ID:主实例的 ID。
HTTP 方法和网址:
GET https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances/INSTANCE_ID/getDiskShrinkConfig
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances/INSTANCE_ID/getDiskShrinkConfig"
PowerShell (Windows)
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances/INSTANCE_ID/getDiskShrinkConfig" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
响应
{
"kind": "sql#instance",
"min_target_size_gb": "1234"
"message": "The size of your instance's storage impacts IOPS, and therefore shrinking your storage
may have a negative impact on IOPS. Before shrinking your storage, please refer to our
documentation on Cloud SQL limits to understand what impact shrinking your storage might have:
https://cloud.google.com/sql/docs/quotas#fixed-limits. Additionally, we recommend leaving an
appropriate buffer for operational purposes, like reindexing, etc. We recommend a buffer of at
least 100 GB or 20% more than current usage, whichever is larger. For more details, please refer
to: https://cloud.google.com/sql/docs/shared-responsibility. The estimated operation time is
20 minutes."
}
响应包含 min_target_size_gb 的值。您可以使用此值来确定在运行存储空间缩减操作时要用于 --storage-size 参数的值。
响应还会返回一条包含估计操作时间的消息,类似于以下内容:
...
"The estimated operation time is 20 minutes".
缩减实例的存储容量。
gcloud
运行以下命令以缩小实例上的存储空间:
gcloud sql instances perform-storage-shrink INSTANCE_NAME \
--storage-size=TARGET_STORAGE_SIZE
替换以下内容:
- INSTANCE_NAME:实例的名称。
- TARGET_STORAGE_SIZE:目标存储容量(以 GB 为单位)。例如,100 GB。
REST v1
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:实例所在项目的 ID。
- INSTANCE_ID:实例的 ID。
- TARGET_STORAGE_SIZE:目标存储空间大小(以 GB 为单位)。
HTTP 方法和网址:
POST https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/performDiskShrink
请求 JSON 正文:
{
"targetSizeGb": TARGET_STORAGE_SIZE
}
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
将请求正文保存在名为 request.json 的文件中,然后执行以下命令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/performDiskShrink"
PowerShell (Windows)
将请求正文保存在名为 request.json 的文件中,然后执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/performDiskShrink" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
响应
{
"kind": "sql#operation",
"targetLink": "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID",
"status": "PENDING",
"user": "user@example.com",
"insertTime": "2019-09-25T22:19:33.735Z",
"operationType": "UPDATE",
"name": "operation-id",
"targetId": "INSTANCE_ID",
"selfLink": "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/operations/operation-id",
"targetProject": "PROJECT_ID"
}
REST v1beta4
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:实例所在项目的 ID。
- INSTANCE_ID:实例的 ID。
- TARGET_STORAGE_SIZE:实例的目标存储空间大小。
HTTP 方法和网址:
POST https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances/INSTANCE_ID/performDiskShrink
请求 JSON 正文:
{
"targetSizeGb": TARGET_STORAGE_SIZE
}
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
将请求正文保存在名为 request.json 的文件中,然后执行以下命令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances/INSTANCE_ID/performDiskShrink"
PowerShell (Windows)
将请求正文保存在名为 request.json 的文件中,然后执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances/INSTANCE_ID/performDiskShrink" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
响应
{
"kind": "sql#operation",
"targetLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances/INSTANCE_ID",
"status": "PENDING",
"user": "user@example.com",
"insertTime": "2019-09-25T22:19:33.735Z",
"operationType": "UPDATE",
"name": "operation-id",
"targetId": "INSTANCE_ID",
"selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/operations/operation-id",
"targetProject": "PROJECT_ID"
}
如需检查存储空间缩减操作的状态,请参阅检查操作的状态。
取消存储空间缩减操作
获取存储空间缩减操作 ID。
您可以运行以下 list 命令来检索操作 ID:
gcloud sql operations list
发送取消请求。
gcloud
运行以下命令可取消请求:
gcloud sql operations cancel OPERATION_ID
替换以下内容:
OPERATION_ID:要取消的存储空间缩减操作的 ID。
REST v1
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:实例所在项目的 ID。
- OPERATION_ID:要取消的存储空间缩减操作的 ID。
HTTP 方法和网址:
POST https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/operations/OPERATION_ID/cancel
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
执行以下命令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d "" \
"https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/operations/OPERATION_ID/cancel"
PowerShell (Windows)
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-Uri "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/operations/OPERATION_ID/cancel" | Select-Object -Expand Content
您应该会收到一个成功的状态代码 (2xx) 和一个空响应。
REST v1beta4
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:实例所在项目的 ID。
- OPERATION_ID:要取消的存储空间缩减操作的 ID。
HTTP 方法和网址:
POST https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/operations/OPERATION_ID/cancel
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
执行以下命令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d "" \
"https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/operations/OPERATION_ID/cancel"
PowerShell (Windows)
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-Uri "https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/operations/OPERATION_ID/cancel" | Select-Object -Expand Content
您应该会收到一个成功的状态代码 (2xx) 和一个空响应。
查看取消请求或其他操作的状态
如需检查请求的状态,请运行以下命令:
gcloud
获取取消请求的操作 ID:
gcloud sql operations list
检查操作的状态:
gcloud sql operations describe OPERATION_ID
替换以下内容:
OPERATION_ID:要检查的操作的 ID。
REST v1
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:实例所在项目的名称。
- OPERATION_ID:存储空间缩减操作的 ID。
HTTP 方法和网址:
GET https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/operations/OPERATION_ID
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/operations/OPERATION_ID"
PowerShell (Windows)
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/operations/OPERATION_ID" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
响应
{
"kind": "sql#operation",
"targetLink": "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID",
"status": "RUNNING",
"user": "user@example.com",
"insertTime": "2023-12-18T22:43:09.849Z",
"startTime": "2023-12-18T22:43:10.373Z",
"operationType": "UPDATE",
"name": "OPERATION_ID",
"targetId": "example_instance",
"selfLink": "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/operations/OPERATION_ID",
"targetProject": "PROJECT_ID"
}
REST v1beta4
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:实例所在项目的名称。
- OPERATION_ID:存储空间缩减操作的 ID。
HTTP 方法和网址:
GET https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/operations/OPERATION_ID
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/operations/OPERATION_ID"
PowerShell (Windows)
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/operations/OPERATION_ID" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
响应
{
"kind": "sql#operation",
"targetLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances/INSTANCE_ID",
"status": "RUNNING",
"user": "user@example.com",
"insertTime": "2023-12-18T22:43:09.849Z",
"startTime": "2023-12-18T22:43:10.373Z",
"operationType": "UPDATE",
"name": "OPERATION_ID",
"targetId": "example_instance",
"selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/operations/OPERATION_ID",
"targetProject": "PROJECT_ID"
}
问题排查
如需了解问题排查,请参阅更新存储容量时出现的问题。
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2026-05-16。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2026-05-16。"],[],[]]