Perform Database Maintenance to Reclaim Disk Space
The C3 Agentic AI Platform provides APIs that reclaim disk space in PostgreSQL and Cassandra. Run these APIs when deleted or updated data still uses disk space, for example after a large data load or cleanup. You run each API in the C3 AI Console.
Choose the operation that fits your database and goal:
| Operation | Database | When to choose it |
|---|---|---|
| Vacuum | PostgreSQL | Reclaim space from updated or deleted rows so that the table can reuse it. Start here. |
| Vacuum with analyze | PostgreSQL | Reclaim space and also update the statistics that the query planner uses. |
| Full vacuum | PostgreSQL | Shrink a table on disk when a large amount of space must be reclaimed from it. Locks the table while it runs. |
| Repair | Cassandra | Make data consistent across replicas. |
| Compaction | Cassandra | Rewrite on-disk data and remove deleted data that Cassandra can purge. |
Prerequisites
Before you start, make sure that you have the following:
- A user with the
C3.ClusterAdminrole can run every API on this page. The Cassandra repair and compaction APIs require this role. To learn more about platform roles, see C3 Agentic AI Platform Built-in Roles. - A PostgreSQL or Cassandra service configured for your application, environment, or cluster. To configure a database service for your cluster, see the "Ensure the database services" section in Final Cluster Configuration Steps. For Cassandra characteristics, see Cassandra Database.
Reclaim disk space in PostgreSQL
When you update or delete a row, PostgreSQL keeps the old version of the row, called a dead tuple, until a VACUUM operation reclaims its space. PostgreSQL runs VACUUM and ANALYZE automatically. ANALYZE updates the statistics that the query planner uses. To learn more, see the PostgreSQL documentation VACUUM SQL Commands.
Reclaim disk space manually after high-volume data operations, when queries slow down, or when automatic vacuuming falls behind. The APIs in this section run on PostgreSQL only; they do not run on H2, the default SQL database of a single-node environment.
Check whether a table needs a vacuum
Run the following code in the C3 AI Console:
DbAdmin.inst().getVacuumStats()The result lists statistics for the tables that have dead tuples, ordered by the number of dead tuples, highest first. Each entry includes a fragmentationPct value: the dead tuples of the table as a percentage of all its tuples. To limit the number of results or check one table, pass the limit and table arguments of DbAdmin#getVacuumStats.
Run a vacuum
Use one of the following APIs:
- DbAdmin#vacuum: Runs the PostgreSQL vacuum operation on the current datastore, the database that stores your application's data.
- DbAdmin#vacuumAll: Runs the same operation on every SQL datastore.
vacuumAllskips datastores that do not have the table or column that you name.
Both APIs take four arguments, in this order:
table: The database table name. Withvacuum, passnullfor all tables. WithvacuumAll, name a table. To get the table name of an entity Type, call<TypeName>.meta().rootC3TableName().column: The column name, ornullfor all columns. Set a column only together with a table.full:truefor a full vacuum.analyze:trueto also update the query planner statistics.
The following examples use DbAdmin.inst().vacuum. The examples that name a table work the same way with DbAdmin.inst().vacuumAll.
| Description | Command |
|---|---|
| Vacuum all tables in the current datastore | DbAdmin.inst().vacuum(null, null, false, false); |
| Vacuum and analyze all tables in the current datastore | DbAdmin.inst().vacuum(null, null, false, true); |
| Vacuum and analyze a specific table | DbAdmin.inst().vacuum("MyTable", null, false, true); |
| Vacuum and analyze a specific column in a table | DbAdmin.inst().vacuum("MyTable", "my_column", false, true); |
| Run a full vacuum and analyze on a specific table | DbAdmin.inst().vacuum("MyTable", null, true, true); |
To update the query planner statistics without a vacuum, run DbAdmin.inst().analyze(null, null).
A full vacuum can reclaim more space than a normal vacuum, but it takes much longer and locks the table exclusively while it runs. A full vacuum also needs extra disk space, because it writes a new copy of the table and releases the old copy only when the operation completes. Use a full vacuum only when a significant amount of space must be reclaimed from a table.
Reclaim disk space in Cassandra
Cassandra uses two maintenance operations. A repair makes data consistent across replicas. A compaction rewrites on-disk data and removes tombstones that Cassandra can purge. A tombstone is a marker that Cassandra writes for deleted data. To learn more, see the Cassandra documentation Repair and Compaction.
Run a repair or a compaction when your Cassandra database has a high tombstone count, for example after you remove a large amount of data. To check the tombstone count, open your server's Grafana instance at https://<your-server-domain>/grafana and go to Cassandra cluster overview. Contact C3 AI Support for credentials to access Grafana.
Use the following APIs of CassandraDB:
- CassandraDB#repair: Runs a Cassandra repair. The method takes a CassandraDB.RepairSpec.
- CassandraDB#compact: Runs a Cassandra compaction. The method takes a CassandraDB.CompactionSpec.
Each operation runs only when the specification sets confirm to true. A new repair or compaction replaces the status record of the previous operation of the same kind. If a repair or a compaction is already running, a new request of the same kind returns a message that says so.
Find your keyspace
A keyspace is the Cassandra container for your tables. To list the keyspaces, run the following code:
Cassandra.Cluster.inst().keyspaces()Keyspace names take one of the following formats, where <hostPrefix> is the host prefix of your Cassandra configuration:
<hostPrefix>_dbif DatastoreConfig#perTenant isfalse.<hostPrefix>_<env>_dbifperTenantistrue. The environments namedc3andreferencehave no<env>segment, so their keyspace is<hostPrefix>_db.
Keyspace names are lowercase. In the <env> segment, _ replaces -, and the platform shortens the segment so that the keyspace name has at most 48 characters. The <hostPrefix>_db form is never shortened.
Run a repair
To run a repair on your application's Cassandra service:
Define the repair specification:
JavaScriptvar spec = {confirm: true, keyspace: "<keyspace>"};Replace
<keyspace>with your keyspace name. The specification sets two required fields:confirm: true: Confirms that you want to run the repair.keyspace: The Cassandra keyspace to repair.
CassandraDB.RepairSpecalso has optional fields, such astables,repairParallelism,intensity, anddatacenters.Run the repair on your application's Cassandra service:
JavaScriptCassandraDB.listForApp()[0].repair(spec)CassandraDB.listForApp()returns the Cassandra services for your application, and[0]selects the first one..repair(spec)runs the repair withspecfrom step 1.To run the repair on another service, select it with a different call. These methods come from CloudService:
- Environment:
CassandraDB.listForEnv()[0].repair(spec) - Cluster:
CassandraDB.listForApp(C3.cluster().c3App().id)[0].repair(spec)
- Environment:
Check the repair status:
JavaScriptCassandraDB.listForApp()[0].repairStatus()The status is
PENDINGuntil the repair job reports its status. If no repair job exists, the status isUNKNOWNwith an error message.
Run a compaction
To run a compaction on your application's Cassandra service:
Define the compaction specification:
JavaScriptvar spec = {confirm: true, keyspace: "<keyspace>"};Replace
<keyspace>with your keyspace name. Theconfirmfield is required. Always setkeyspace. Withoutkeyspace, the call runs a full native compaction of every keyspace in the Cassandra datacenter.CassandraDB.CompactionSpecalso has optional fields, such astables,splitOutput,startToken, andendToken.A compaction adds load to the Cassandra nodes while it runs. A compaction also needs free disk space, because Cassandra writes the compacted data to new files before it deletes the old ones. Run a compaction when the load on the database is low, and check the free disk space first.
Run the compaction on your application's Cassandra service:
JavaScriptCassandraDB.listForApp()[0].compact(spec)To run the compaction on the environment or cluster service, use the calls in Run a repair with
.compact(spec).Check the compaction status:
JavaScriptCassandraDB.listForApp()[0].compactionStatus()The status values are the same as for a repair:
PENDINGuntil the compaction job reports its status, andUNKNOWNwith an error message if no compaction job exists.