Manually trigger major compaction in a Spanner database

This document explains how to manually trigger a major compaction in a Spanner database.

Several storage-related features in Spanner, such as tier storage or columnar engine, rely on a database-wide major compaction before they are fully enabled. By default, major compactions occur automatically across all tables over seven day periods. This means you might wait up to seven days for a new feature to be fully available. To make new features available immediately, you can manually trigger a major compaction.

The compaction process is a long-running operation (LRO).

Pricing

Manually triggering a major compaction temporarily increases CPU utilization on your Spanner instance. If additional compute capacity is required to offset this increase in CPU utilization, this will increase costs.

Performance

Major compactions run as background operations. However, if your instance has consistently heavy CPU usage, the compaction workload might interfere with other critical operations. In such cases, you can temporarily scale up the instance to ensure stable performance during compaction.

Manually trigger a major compaction

Google Cloud console

  1. Open the Google Cloud console and select your instance.

    Go to Spanner instance

  2. Select a database.

  3. In the navigation menu, click Spanner Studio.

  4. Open a new tab by clicking New SQL editor tab or New tab.

  5. Run one of the following commands to start compaction, based on your database dialect:

    GoogleSQL

    CALLcompact_all();
    

    PostgreSQL

    CALLspanner.compact_all();
    

    This operation returns a long-running operation (LRO) ID that you can use to find the operation in the Operations list.

  6. To monitor the progress of the compaction operation, in the navigation menu, click Operations.

C++

To trigger compactions programmatically using the C++ client library:

voidCompact(google::cloud::spanner::Clientclient){
namespacespanner=::google::cloud::spanner;
spanner::SqlStatementselect("CALL compact_all()");
usingRowType=std::tuple<std::string>;
autorows=client.ExecuteQuery(std::move(select));
for(auto&row:spanner::StreamOf<RowType>(rows)){
if(!row)throwstd::move(row).status();
std::cout << "Long-running operation ID: " << std::get<0>(*row) << "\n";
}
}

You can check the progress of a long-running database operation. You can also cancel the ongoing major compaction request using the LRO ID. For more information, see Cancel a long-running database operation.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026年08月26日 UTC.