Compaction policy
A compaction policy, policy type system_iceberg_compaction, sets the thresholds at which
table maintenance compacts an Iceberg table and the options
the rewrite runs with. Attach it to a catalog, schema, or table; a policy on a catalog or schema covers
every table beneath it.
Quick start
-
Create the policy. The UI creates custom policies only, so create a compaction policy over REST. An empty
contenttakes every default in Policy content.Create a compaction policycurl -sS -X POST "https://{gravitino_host}/api/metalakes/{metalake}/policies" \
-H "Accept: application/vnd.gravitino.v1+json" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{
"name": "iceberg_compaction",
"comment": "Compact Iceberg tables",
"policyType": "system_iceberg_compaction",
"enabled": true,
"content": {"rewriteOptions": {"min-input-files": "5"}}
}' -
Attach it.
- UI
- REST
On the catalog, schema, or table page in Explore, click Policies in the header, choose
iceberg_compactionunder Policies, and click Update policies.Attach the policy to a schemacurl -sS -X POST "https://{gravitino_host}/api/metalakes/{metalake}/objects/schema/{catalog}.{schema}/policies" \
-H "Accept: application/vnd.gravitino.v1+json" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"policiesToAdd": ["iceberg_compaction"]}'Use
objects/catalog/{catalog}orobjects/table/{catalog}.{schema}.{table}for the other levels.
Creating a policy takes CREATE_POLICY on the metalake, and attaching it takes APPLY_POLICY; see
Privileges.
Policy content
Every field is optional.
| Field | Default | Meaning |
|---|---|---|
minDataFileMse | 405323966463344 | Fires when custom-data-file-mse reaches this value, in bytes². Must be >= 0 |
minDeleteFileNumber | 1 | Fires when custom-delete-file-number reaches this value. Must be >= 0 |
dataFileMseWeight | 1 | Weight of custom-data-file-mse in the score. Must be >= 0 |
deleteFileNumberWeight | 100 | Weight of custom-delete-file-number in the score. Must be >= 0 |
maxPartitionNum | 50 | Most partitions one evaluation selects for rewrite. Must be > 0 |
rewriteOptions | {} | Iceberg rewrite_data_files options passed to the rewrite job, such as min-input-files |
Every compaction policy also carries two fixed properties that cannot be set: strategy.type is
iceberg-data-compaction and job.template-name is builtin-iceberg-rewrite-data-files.
How the policy decides
The two values the policy reads are table statistics written by the builtin-iceberg-update-stats
job:
custom-data-file-mseis the mean squared shortfall of each data file below 128 MiB. A file at or above 128 MiB contributes nothing, so many small files push it up quickly.custom-delete-file-numbercounts position and equality delete files.
The policy fires when either reaches its threshold:
custom-data-file-mse >= minDataFileMse || custom-delete-file-number >= minDeleteFileNumber
When several tables or partitions qualify, the score orders them:
custom-data-file-mse × dataFileMseWeight + custom-delete-file-number × deleteFileNumberWeight
Tune the policy
Set minDataFileMse from a target file size and the shortfall you tolerate:
minDataFileMse = (target file size in bytes × ratio)²
A ratio between 0.1 and 0.2 suits most tables. The default uses 128 MiB (134217728 bytes) and
0.15, giving 405323966463344.
minDeleteFileNumber of 1 compacts a table as soon as it has a delete file, which keeps
merge-on-read tables fast to read at the cost of frequent rewrites. Raise it to compact less often.
Raising deleteFileNumberWeight puts tables with delete files ahead of tables that only have small
files.
A starting point for production is the defaults plus these rewriteOptions:
| Option | Value | Effect |
|---|---|---|
target-file-size-bytes | 134217728 | Writes 128 MiB files, matching the size the statistics measure |
min-input-files | 5 | Skips a file group with fewer than five files |
delete-file-threshold | 1 | Rewrites any data file that has a delete file against it |