The WT_SESSION::compact method can be used to compact data sources. Parallel activity can happen on the data sources while compaction is running but it might affect the efficiency of compaction. Furthermore, it is not possible to run compaction from multiple sessions on the same data source at the same time.
Note: Because checkpoints named by the application are not discarded until explicitly removed or replaced, they may prevent WT_SESSION::compact from accomplishing anything.
Foreground compaction is executed when a specific URI is given to the WT_SESSION::compact method. The call returns once compaction has been executed on the specified URI.
Example:
The "background" configuration item can be used to interact with background compaction when calling the WT_SESSION::compact method. The call immediately returns to the application but does not guarantee compaction will be enabled/disabled at the same time. A successful call simply means the request has been submitted to WiredTiger. For instance, if background compaction is disabled while it is compacting a table, compaction will be stopped as soon as it is safe to do so.
When background compaction is enabled, WiredTiger autonomously compacts the eligible files in the database. You can read more about eligible files on the following page: Compaction.
Note: When interacting with background compaction, NULL should be assigned to the argument corresponding to the URI.
Example:
More information about compaction can be read here: Compaction.