Perform Database Maintenance to Reclaim Disk Space
You can run APIs to perform database maintenance for PostgreSQL and Cassandra. The C3 Agentic AI Platform provides APIs for PostgreSQL and Cassandra databases to run disk space reclaimation operations.
Prerequisites
Complete the following requirements:
- You must have the
C3.ClusterAdminrole to run the APIs mentioned in this topic. See C3 Agentic AI Platform Built-in Roles to learn more about platform roles. - A PostgreSQL or Cassandra service has been configured and set for your application, environment, or cluster.
- See the "Ensure database services" section in Final Cluster Configuration Steps to learn how to configure and ensure a PostgreSQL or Cassandra database for your cluster.
- See Configure a dedicated Cloud Service for a Datastore Cassandra to learn how to configure and ensure a dedicated PostgreSQL or Cassandra database for your application or environment.
Reclaim disk space in PostgreSQL
PostgreSQL automatically and regularly performs VACUUM and ANALYZE operations to effectively reclaim storage occupied by dead tuples. To learn more about VACUUM in PostgreSQL, see the PostgreSQL documentation VACUUM SQL Commands.
You might want to manually reclaim disk space after running high-volume data operations, when you notice performance degradation, or when automatic vacuuming has not kept up with storage needs. To check if you need to manually run a vacuum operation, run the following code in C3 AI Console:
DbAdmin.inst().getVacuumStats()This code returns statistics for tables, ordered descending by number of dead tuples. See the Type documentation DbAdmin#getVacuumStats to learn how to specify a table or column.
You can manually reclaim disk space for your PostgreSQL database with the following APIs:
- DbAdmin#vacuum: Runs native PostgreSQL vacuum and analyze operations for the current datastore. You can specify a specific table and column, or run it on all tables. The method also supports full vacuum mode and analyzes operations to update query planner statistics.
- DbAdmin#vacuumAll: Runs native PostgreSQL vacuum and analyze operations for all datastores. You can specify tables and columns, and whether to perform full vacuum and analyze operations.
See the Type documentation for DbAdmin to learn about the parameters these APIs take.
Example PostgreSQL vacuum commands
The following are examples of how to run the DbAdmin.vacuum command in C3 AI Console:
| Description | Command |
|---|---|
| Analyze on all tables in current datastore | DbAdmin.inst().vacuum(null, null, false, true); |
Vacuum a specific table with analyze set to true | DbAdmin.inst().vacuum("MyTable", null, false, true); |
| Vacuum a specific column in a table | DbAdmin.inst().vacuum("MyTable", "my_column", false, true); |
| Perform a "full" vacuum on a specific table | DbAdmin.inst().vacuum("MyTable", null, true, true); |
| Vacuum without analyze on all tables | DbAdmin.inst().vacuum(null, null, false, false); |
The parameters that these APIs take match the native operations in PostgreSQL. See the Type documentation for DbAdmin.vacuum and DbAdmin.vacuumAll, and the PostgreSQL documentation VACUUM SQL Commands to learn more about these parameters.
You can also apply these example parameters to the DbAdmin.vacuumAll() API.
Reclaim disk space in Cassandra
Cassandra performs repair and compaction operations to maintain a healthy system. The repair operation ensures data consistency across replicas, and the compaction operation cleans up and optimizes local on-disk data.
To learn more about repair in Cassandra, see the Cassandra documentation Repair.
To learn more about compaction in Cassandra, see the Cassandra documentation Compaction.
You might want to run repair and compaction operations if your Cassandra database has a high tombstone count. You can check tombstone count in your server's Grafana instance at https://<yourserverdomain>/grafana. Navigate to Cassandra cluster overview to check tombstone count. Contact your C3 AI representative for credentials to access Grafana.
Because incremental repair and minor compactions are not sufficient to maintain a healthy system, you can run full repair and full compaction for Cassandra with the following APIs:
- CassandraDB#repair: Runs a native Cassandra repair operation to ensure data consistency across replicas. It uses a CassandraDB.RepairSpec to define repair parameters.
- CassandraDB#compact: Runs a native Cassandra compaction operation to clean up and optimize local on-disk data. Uses a CassandraDB.CompactionSpec to define compaction parameters.
See the Type documentation for CassandraDB to learn more about these APIs.
Example Cassandra repair command
Here is an example of how to run the CassandraDB.repair() API in the C3 AI Console:
Define the CassandraDB.RepairSpec:
JavaScriptspec = {confirm: true, keyspace: "platform_myenv_db"}This code creates a repair specification with two required parameters:
withConfirm(true): Required confirmation to run the repair operationwithKeyspace("platform_myenv_db"): Specifies the Cassandra keyspace to repair (in this example, theplatform_myenv_dbkeyspace)
To search for keyspaces, run the following command:
JavaScriptCassandra.Cluster.inst().keyspaceKeyspaces take the following format:
<cluster>_dbif DatastoreConfig#perTenant is false<cluster>_<env>_dbif DatastoreConfig#perTenant is true
See the Type documentation for CassandraDB.RepairSpec to learn about additional optional parameters such as specific tables, repair parallelism, intensity settings, and datacenter targeting.
Run the
CassandraDB.repairAPI:JavaScriptCassandraDB.listForApp(C3.app()).repair(spec)This code references a Cassandra service ID for your application and runs the repair operation using the previously defined specification.
To reference the Cassandra service ID for your environment or cluster, use the
C3.env()orCluster.c3App()as values for the CloudService#listForEnv and CloudService#listForApp methods. For example, uselistForEnv(C3.env()to refer to your environment Cassandra service ID, or uselistForApp(Cluster.c3App())to refer to the cluster Cassandra service ID.Check the repair status:
JavaScriptCassandraDB.listForApp(C3.app()).repairStatus()This code fetches the current status of the repair operation for the instance of Cassandra specified by the service ID.
Example Cassandra compaction command
Here is an example of how to run the CassandraDB.compact() API in the C3 AI Console:
Define the CassandraDB.CompactionSpec:
JavaScriptspec = {confirm: true}This code creates a compaction specification with the required confirmation parameter.
See the Type documentation for CassandraDB.CompactionSpec to learn about additional optional parameters for configuring compaction operations.
Run the
CassandraDB.compact()API:JavaScriptCassandraDB.listForApp(C3.app()).compact(spec)This code references the Cassandra service ID of your application and runs compaction using the previously defined specification.
Check the compaction status:
JavaScriptCassandraDB.listForApp(C3.app()).compactionStatus()This code fetches the current status of the compaction operation.